簡體   English   中英

使用 PDFSharp 打印 PDF

[英]Printing PDFs with PDFSharp

我有以下代碼:

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);
      }
    }
  }
}

對於我的生活,我無法讓它工作並打印出一個 PDF。 每當我去打印時,我都會收到錯誤“找不到指定的文件”。 有人知道我的代碼是否有問題嗎? 我在這里使用 PDFSharp ...

一個觀察,在以下行中:

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

您正在使用“@”來轉義字符串並轉義反斜杠。 刪除“@”或使用單個反斜杠。

還要確保這是您的 EXE 的正確路徑。

更新:如果您已確認您的 Acrobat Reader EXE 路徑正確,那么接下來要看的是您傳遞給 PdfFilePrinter 構造函數的“打印機名稱”參數。

您正在傳遞" \\\\ny-dc-03\\\\IT-01"作為打印機名稱。 這需要與出現在 Windows 打印機列表中的打印機名稱完全匹配,而不僅僅是任意 IP 打印機。

如果這是正確的,請務必刪除前導空格: "\\\\ny-dc-03\\\\IT-01"

您正在通過" \\\\ny-dc-03\\\\IT-01"

我認為這應該是"\\\\\\\\ny-dc-03\\\\IT-01"@"\\\\ny-dc-03\\IT-01"

不確定@"\\\\ny-dc-03\\\\IT-01"是否可以工作,但"\\\\ny-dc-03\\\\IT-01"不能工作,因為 UNC 名稱以雙反斜杠開頭。

這可能是顯而易見的,但卻是雜技演員:

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

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM