Printing PDFs with PDFSharp
Asked Answered
T

3

7

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...

Tarweed answered 19/5, 2009 at 15:27 Comment(2)
where is the PDFSharp.PDF.Printing namespace???Mousseline
ahhh in the current version of PDFSharp the entire Printing class is commented outMousseline
A
9

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".

Antecedent answered 19/5, 2009 at 15:32 Comment(0)
S
1

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.

Schlicher answered 19/5, 2009 at 15:31 Comment(2)
oh no lol i just put in a dummy name since i didn't want to post my real name up here...but yes that is where it resides on my computerTarweed
Registry.ClassesRoot.OpenSubKey(@"Software\Adobe\Acrobat\Exe"), the default value is what holds the path to the currently installed version of Acrobat, whether reader or pro.Vaccination
S
1

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.

Sedulous answered 25/8, 2009 at 11:17 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.