How to render pdfs using C#

Google has open sourced its excellent PDF rendering engine – PDFium – that it wrote with Foxit Software.

There is a C# nuget package called PdfiumViewer which gives a C# wrapper around PDFium and allows PDFs to be displayed and printed.

I have used it and was very impressed with the quality of the rendering.

PDFium works directly with streams so it doesn’t require any data to be written to disk.

This is my example from a WinForms app

    public void LoadPdf(byte[] pdfBytes)
        var stream = new MemoryStream(pdfBytes);

    public void LoadPdf(Stream stream)
        // Create PDF Document
        var pdfDocument = PdfDocument.Load(stream);

        // Load PDF Document into WinForms Control

Edit: To get the pdfRenderer control in WinForm: Add the PdfiumViewer NuGet package to the project; open the projects packages folder in Windows Explorer and drag the PdfiumViewer.dll file onto the Toolbox window; A control called PdfRenderer will be available to add:

Adding PdfRenderer control to WinForms

Leave a Comment