2016-07-20 99 views
1

有没有办法打印PDF并选择以编程方式使用的纸盒?使用不同的打印机托盘打印PDF c#

我愿意接受诸如将PDF转换为其他格式并从那里打印的建议。

我可以使用PaperSource()和PrintDocument()打印到正确的纸盒,是否可以将PDF转换为这些函数可以理解的格式?

感谢。

+0

你试过产生不同的纸张大小的PDF文档?你现在用什么代码生成PDF文件?您的打印机可以理解哪些脚本? – zipzit

+0

我正在使用PDF表格并使用iTextSharp进行编辑,然后保存为PDF,不知道打印机将理解哪些脚本 –

+0

我认为您需要[PaperCut](http://www.papercut.com/tour/advanced-scripting /)或类似的东西。我怀疑用正确的打印机脚本工具,您可以通过文件名提取来确定使用哪个纸盒。 E. G.'Filename_big.pdf'与'Filename_little.pdf' – zipzit

回答

1

基于从类似here on MSDN越来越纸盒PaperSource,如果你不介意使用Ghostscript.NET,这应该为你工作:

public void PrintPdf(string filePath, string printQueueName, PaperSource paperTray) 
{ 
    using (ManualResetEvent done = new ManualResetEvent(false)) 
    using (PrintDocument document = new PrintDocument()) 
    { 
     document.DocumentName = "My PDF"; 
     document.PrinterSettings.PrinterName = printQueueName; 
     document.DefaultPageSettings.PaperSize = new PaperSize("Letter", 850, 1100); 
     document.DefaultPageSettings.PaperSource = paperTray; 
     document.OriginAtMargins = false; 

     using (var rasterizer = new GhostscriptRasterizer()) 
     { 
      var lastInstalledVersion = 
       GhostscriptVersionInfo.GetLastInstalledVersion(
         GhostscriptLicense.GPL | GhostscriptLicense.AFPL, 
         GhostscriptLicense.GPL); 

      rasterizer.Open(filePath, lastInstalledVersion, false); 

      int xDpi = 96, yDpi = 96, pageNumber = 0; 

      document.PrintPage += (o, p) => 
      { 
       pageNumber++; 
       p.Graphics.DrawImageUnscaledAndClipped(
        rasterizer.GetPage(xDpi, yDpi, pageNumber), 
        new Rectangle(0, 0, 850, 1100)); 
       p.HasMorePages = pageNumber < rasterizer.PageCount; 
      }; 

      document.EndPrint += (o, p) => 
      { 
       done.Set(); 
      }; 

      document.Print(); 
      done.WaitOne(); 
     } 
    } 
}