简体   繁体   English

PrintDocument 打印空白页?

[英]PrintDocument printing blank pages?

I am trying to print a pdf document and save it to a file when I receive the prompt to save the document.当我收到保存文档的提示时,我正在尝试打印 pdf 文档并将其保存到文件中。 The file is generated with the right number of pages but all pages are blank.生成的文件页数正确,但所有页面都是空白的。 What am I missing in the PrintPage handler below?我在下面的 PrintPage 处理程序中缺少什么? Thanks for any advice.感谢您的任何建议。

var ctrl = new StandardPrintController();

using (PrintDocument doc = new PrintDocument())
    {
        doc.PrintController = ctrl;
        doc.PrinterSettings.PrinterName = "CutePDF Writer";
        doc.PrinterSettings.PrintFileName = fileName;

        doc.PrintPage += (s, e) =>
        {
            pageNo++;
            if (pageNo < frameCount)
            {
                e.HasMorePages = true;
            }
            else
            {
                e.HasMorePages = false;
            }
        };

        doc.Print();
    }

If you want to print the pdf, you can use PdfiumViewer an open source library.如果要打印 pdf,可以使用开源库 PdfiumViewer。

Link to Nuget package:链接到 Nuget 包:

https://www.nuget.org/packages/PdfiumViewer/ https://www.nuget.org/packages/PdfiumViewer/

Then modify your code to load the document you want to print:然后修改您的代码以加载您要打印的文档:

        var ctrl = new StandardPrintController();

        using (var document = PdfDocument.Load(filename))
        {
            using (PrintDocument doc = document.CreatePrintDocument())
            {
                doc.PrintController = ctrl;
                doc.PrinterSettings.PrinterName = "CutePDF Writer";
                doc.PrinterSettings.PrintFileName = fileName;

                doc.PrintPage += (s, e) =>
                {
                    pageNo++;
                    if (pageNo < frameCount)
                    {
                        e.HasMorePages = true;
                    }
                    else
                    {
                        e.HasMorePages = false;
                    }
                };

                doc.Print();
            }
        }
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM