C# 使用 PDFSharp 打印 PDF

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/883456/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-05 05:17:54  来源:igfitidea点击:

Printing PDFs with PDFSharp

c#pdfprintingpdfsharp

提问by yeahumok

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

对于我的生活,我无法让它工作并打印出一个 PDF。每当我去打印时,我都会收到错误“找不到指定的文件”。有人知道我的代码是否有问题吗?我在这里使用 PDFSharp ...

采纳答案by ichiban

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.

还要确保这是您的 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.

更新:如果您已确认您的 Acrobat Reader EXE 的路径正确,那么接下来要看的是您传递给 PdfFilePrinter 构造函数的“打印机名称”参数。

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.

" \\ny-dc-03\\IT-01"作为打印机名称传递。这需要与出现在 Windows 打印机列表中的打印机名称完全匹配,而不仅仅是任意 IP 打印机。

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

如果这是正确的,请务必删除尾随空格:"\\ny-dc-03\\IT-01"

回答by Omar Kooheji

This may be stating the obvious but is acrobat at:

这可能是显而易见的,但却是杂技演员:

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

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.

只是您的用户名暗示您的名字不是 Mike smith。

回答by I liked the old Stack Overflow

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

你路过 " \\ny-dc-03\\IT-01"

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

我认为这应该是"\\\\ny-dc-03\\IT-01"@"\\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.

不确定是否@"\\ny-dc-03\\IT-01"会起作用,但"\\ny-dc-03\\IT-01"不能起作用,因为 UNC 名称以双反斜杠开头。