简体   繁体   中英

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

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

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.

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM