【问题标题】:c# printing through PDF drivers, print to file option will output PS instead of PDFc#通过PDF驱动打印,打印到文件选项将输出PS而不是PDF
【发布时间】:2017-06-21 14:04:48
【问题描述】:

在挣扎了一整天后,我发现了问题,但这并没有解决我的问题。

简而言之:

我需要打开一个 PDF,转换为 BW(灰度),搜索一些单词并在找到的单词附近插入一些注释。乍一看,这似乎很容易,但我发现 PDF 文件的处理难度很大(没有“单词”概念等等)。

现在第一个任务,转换为灰度让我发疯了。我没有找到商业或免费的可行解决方案。我想出了这个解决方案:

  • 打开 PDF
  • 使用 Windows 驱动程序、一些免费的 PDF 打印机进行打印

这很丑陋,因为我会强迫 C# 用户安装这样的 3'rd 方软件,但是.. 那是当下。我测试了 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)文件,这显然不会被 Adob​​e Reader 识别。

现在的问题(我很肯定可能会遇到其他可能想要从 C# 打印 PDF 的人)例如如何打印到 CutePDF 并且仍然抑制任何文件名对话框?

换句话说,只需使用从 C# 应用程序中以编程方式选择的文件名静默打印。或者以某种方式说服“打印到文件”通过 PDF 驱动程序,而不是 Windows 默认的 PS 驱动程序。

非常感谢任何提示。

【问题讨论】:

    标签: c#


    【解决方案1】:

    我在这篇文章中通过商业组件解决了灰度转换问题,并且我还在那里发布了我的完整解决方案,以防任何人都会像我一样挣扎。

    Converting PDF to Grayscale pdf using ABC PDF

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2014-08-25
      • 1970-01-01
      • 2014-07-22
      • 2013-07-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-10-19
      相关资源
      最近更新 更多