2009-05-19 199 views
5

我有以下代碼:打印PDF文件與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這裏...

+0

其中是PDFSharp.PDF.Printing命名空間? – nuander 2016-03-16 23:20:12

+0

ahhh在當前版本的PDFSharp中,整個印刷類被註釋掉 – nuander 2016-03-17 19:48:25

回答

7

一個觀察,在下面一行:

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

您使用的是「@」,以逃避串並逃離反斜槓。刪除「@」或使用單個反斜槓。

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

UPDATE:如果您確認您有正確的路徑到你的Acrobat Reader軟件EXE,看是要傳遞到PdfF​​ilePrinter構造函數中的「打印機名稱」參數,接下來的事情。

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

如果這是正確的,一定要刪除尾隨空格:"\\ny-dc-03\\IT-01"

1

這可能在以下說明明顯,但雜技演員:

C:\ Documents和 設置\ mike.smith \桌面\的Adobe 閱讀器9.0.exe

這只是你的用戶名暗示你的名字不是邁克史密斯。

+0

哦,不,哈哈我只是放在一個虛擬的名字,因爲我不想在這裏發佈我的真實姓名......但是,那是它駐留的地方我的電腦 – yeahumok 2009-05-19 16:34:59

0

你傳入" \\ny-dc-03\\IT-01"

我想這應該是因爲UNC名稱以雙反斜線開始"\\\\ny-dc-03\\IT-01"@"\\ny-dc-03\IT-01"

不知道是否@"\\ny-dc-03\\IT-01"會的工作,但"\\ny-dc-03\\IT-01"不能工作。