Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Printing PDFs with PDFSharp

I have the following code:

using System;
using System.Diagnostics;
using System.IO;
using PdfSharp.Pdf.Printing;

namespace PrintPdfFile
{

  class Program
  {
    [STAThread]
    static void Main(string[] args)
    {
      // Set Acrobat Reader EXE, e.g.:
        PdfFilePrinter.AdobeReaderPath = @"C:\\Documents and Settings\\mike.smith\\Desktop\\Adobe Reader 9.0.exe";
      // -or-
        //PdfPrinter.AdobeReaderPath = @"C:\Program Files\Adobe\[...]\AcroRd32.exe";

      //// Ony my computer (running a German version of Windows XP) it is here:
        //PdfFilePrinter.AdobeReaderPath = @"C:\\Documents and Settings\\mike.smith\\Desktop\\Adobe Reader 9.0.exe";

      // Set the file to print and the Windows name of the printer.
      // At my home office I have an old Laserjet 6L under my desk.
      PdfFilePrinter printer = new PdfFilePrinter(@"C:\Documents and Settings\mike.smith\Desktop\Stuff\ReleaseNotesAndFolderList.pdf", " \\ny-dc-03\\IT-01");

      try
      {
        printer.Print();
      }
      catch (Exception ex)
      {
        Console.WriteLine("Error: " + ex.Message);
      }
    }
  }
}

For the life of me i cannot get this to work and print out a single PDF. Anytime i go to print, i get the error "Cannot find the file specified". Does anybody have any idea if something is wrong with my code?? I'm using PDFSharp here...

like image 413
yeahumok Avatar asked May 19 '09 15:05

yeahumok


3 Answers

One observation, in the following line:

PdfFilePrinter.AdobeReaderPath 
      = @"C:\\Documents and Settings\\mike.smith\\Desktop\\Adobe Reader 9.0.exe";

You are using the "@" to escape the string and also escaping the backslashes. Either remove the "@" or use a single backslash.

Also make sure that is the correct path to your EXE.

UPDATE: If you have confirmed that you have the correct path to your Acrobat Reader EXE, the next thing to look at is the "Printer Name" parameter that you are passing to the PdfFilePrinter constructor.

You are passing " \\ny-dc-03\\IT-01" as the printer name. This needs to match the name of printer exactly as it appears in the list of Printers in Windows, not just an arbitrary IP printer.

If this is correct, be sure to remove, the leading space: "\\ny-dc-03\\IT-01".

like image 166
ichiban Avatar answered Oct 22 '22 22:10

ichiban


This may be stating the obvious but is acrobat at:

C:\Documents and Settings\mike.smith\Desktop\Adobe Reader 9.0.exe

It's just your user name implies that your name isn't Mike smith.

like image 24
Omar Kooheji Avatar answered Oct 22 '22 21:10

Omar Kooheji


You are passing " \\ny-dc-03\\IT-01"

I think this should be "\\\\ny-dc-03\\IT-01" or @"\\ny-dc-03\IT-01"

Not sure if @"\\ny-dc-03\\IT-01" will work, but "\\ny-dc-03\\IT-01" cannot work as UNC names start with a double backslash.

like image 1
I liked the old Stack Overflow Avatar answered Oct 22 '22 22:10

I liked the old Stack Overflow