Andrew
Andrew

Reputation: 41

Print functionality in Pdfium .NET SDK

I'm currently working on a C# application that uses PDF render features for my employer. Pdfium.Net does an excellent job of displaying the PDFs but the app must be able to print them as well. Anybody know a way to print the current PDF document through this API? I've checked in the likely places and I haven't found anything.

Upvotes: 2

Views: 7386

Answers (3)

Ben Hartmann
Ben Hartmann

Reputation: 31

PDFium has some built in functionality for printing if you're using the PDFRenderer. For example:

    private void cmdPrint_Click(object sender, EventArgs e)
    {
        PrintDialog p = new PrintDialog();
        p.Document = pdfRenderer.Document.CreatePrintDocument();
        DialogResult printResult = p.ShowDialog();
        if (printResult  == DialogResult.OK)
        {
            p.Document.Print();
        }
    }

Upvotes: 0

user5776642
user5776642

Reputation: 1

One of the .Render() variants allows you draw directly onto the Graphics context cutting out the need for the intermediate bitmap in the example above.

Upvotes: 0

Paul
Paul

Reputation: 46

To print a PDF document, you can use standard .Net Framework, such as shown in the code below:

//.Net Framework class from System.Drawing.Printing namespace
PrintDocument pd = new PrintDocument();
int pageForPrint = 0;

pd.PrintPage += (s, e) =>
{
    using (PdfBitmap bmp = new PdfBitmap((int)e.PageSettings.PrintableArea.Width, (int)e.PageSettings.PrintableArea.Height, true))
    {
        //Render to PdfBitmap using page's Render method with FPDF_PRINTING flag
        pdfView1.Document.Pages[pageForPrint].Render
            (bmp,
            0,
            0,
            (int)e.PageSettings.PrintableArea.Width,
            (int)e.PageBounds.Height,
            Patagames.Pdf.Enums.PageRotate.Normal, Patagames.Pdf.Enums.RenderFlags.FPDF_PRINTING);

        //Draw rendered image to printer's graphics surface
        e.Graphics.DrawImageUnscaled(bmp.Image,
            (int)e.PageSettings.PrintableArea.X,
            (int)e.PageSettings.PrintableArea.Y);

        //Print next page
        if(pageForPrint< pdfView1.Document.Pages.Count)
        {
            pageForPrint++;
            e.HasMorePages = true;
        }
    }
};

//start printing routine
pd.Print();

Upvotes: 3

Related Questions