bezzad / PdfiumViewer

PDF viewer based on Google's PDFium port to .Net Core.
Apache License 2.0
130 stars 46 forks source link

Print button #5

Open arsoftinformatica opened 3 years ago

arsoftinformatica commented 3 years ago

Hello, I'm sending a code that I found in the stackoverflow to print the document in pdf, if you can implement this option, thank you.

public bool PrintPDF(PdfDocument document , string printer, string paperName, string filename, int copies) { try { // Create the printer settings for our printer var printerSettings = new PrinterSettings { PrinterName = printer, Copies = (short)copies, };

            // Create our page settings for the paper size selected
            var pageSettings = new PageSettings(printerSettings)
            {
                Margins = new Margins(0, 0, 0, 0),
            };
            foreach (PaperSize paperSize in printerSettings.PaperSizes)
            {
                if (paperSize.PaperName == paperName)
                {
                    pageSettings.PaperSize = paperSize;
                    break;
                }
            }

            // Now print the PDF document

                using (var printDocument = document.CreatePrintDocument())
                {
                    printDocument.PrinterSettings = printerSettings;
                    printDocument.DefaultPageSettings = pageSettings;
                    printDocument.PrintController = new StandardPrintController();
                    printDocument.Print();
                }
            return true;
        }
        catch (Exception)
        {
            return false;
        }
    }

var ret = PrintPDF(Renderer.Document,"Microsoft Print to PDF", "A4", dialog.FileName, 1);

q2a3z commented 4 months ago

Hi arsoftinformatica. I have fixed some bug and added the print function based on the origal PdfiumViewer project. If you want to ues it in your project, that will be help.

20