简体   繁体   中英

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. The file is generated with the right number of pages but all pages are blank. What am I missing in the PrintPage handler below? 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.

Link to Nuget package:

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();
            }
        }
    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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