2017-06-21 187 views
0

掙扎了整整一天後,我發現了這個問題,但這並沒有解決我的問題。c#打印通過PDF驅動程序,打印到文件選項將輸出PS而不是PDF

短:

我需要打開一個PDF,轉換爲BW(灰度),搜索一些詞,並插入一些筆記附近找到的單詞。首先看起來很簡單,但我發現PDF文件的處理非常困難(沒有「單詞」概念等)。

現在第一個任務,轉換爲灰度只是讓我發瘋。我沒有找到工作解決方案,無論是商業還是免費的。我想出了這個解決方案:

  • 打開PDF
  • 使用Windows進行打印驅動程序,一些免費的PDF打印機

這是相當難看,因爲我將強制C#用戶安裝這種3'第三方SW但是..那是fpr的時刻。我測試了FreePDF,CutePDF和PDFCreator。他們都在按預期「獨立」工作。

現在,當我試圖從C#打印,很明顯,我不希望打印對話框,只需選擇BW選項並打印(亦稱轉換)

下面的代碼只是使用PDF庫,出只有清晰度。

   Aspose.Pdf.Facades.PdfViewer viewer = new Aspose.Pdf.Facades.PdfViewer(); 
       viewer.BindPdf(txtPDF.Text); 

       viewer.PrintAsGrayscale = true; 
       //viewer.RenderingOptions = new RenderingOptions { UseNewImagingEngine = true }; 

       //Set attributes for printing 
       //viewer.AutoResize = true;   //Print the file with adjusted size 
       //viewer.AutoRotate = true;   //Print the file with adjusted rotation 
       viewer.PrintPageDialog = true; //Do not produce the page number dialog when printing 

       ////PrinterJob printJob = PrinterJob.getPrinterJob(); 

       //Create objects for printer and page settings and PrintDocument 
       System.Drawing.Printing.PrinterSettings ps = new System.Drawing.Printing.PrinterSettings(); 
       System.Drawing.Printing.PageSettings pgs = new System.Drawing.Printing.PageSettings(); 
       //System.Drawing.Printing.PrintDocument prtdoc = new System.Drawing.Printing.PrintDocument(); 


       //prtdoc.PrinterSettings = ps; 

       //Set printer name 
       //ps.PrinterName = prtdoc.PrinterSettings.PrinterName; 


       ps.PrinterName = "CutePDF Writer"; 
       ps.PrintToFile = true; 

       ps.PrintFileName = @"test.pdf"; 
       // 
       //ps. 

       //Set PageSize (if required) 
       //pgs.PaperSize = new System.Drawing.Printing.PaperSize("A4", 827, 1169); 

       //Set PageMargins (if required) 
       //pgs.Margins = new System.Drawing.Printing.Margins(0, 0, 0, 0); 

       //Print document using printer and page settings 
       viewer.PrintDocumentWithSettings(ps); 
       //viewer.PrintDocument(); 

       //Close the PDF file after priting 

我發現,似乎是有點解釋是,如果你選擇

ps.PrintToFile = TRUE;

無論是C#PDF庫還是PDF打印機驅動程序,Windows都會跳過PDF驅動程序,而不是PDF文件將輸出PS(postscript)顯然不會被Adobe Reader識別的文件。

現在的問題(我確信其他人可能會想要從C#打印PDF文件)是如何打印到CutePDF例如仍然禁止任何文件名對話框?

換句話說,只需用C#應用程序中編程選擇的文件名靜靜地打印。或者不知何故說服「打印到文件」來通過PDF驅動程序,而不是Windows默認的PS驅動程序。

非常感謝任何提示。

回答