PDFRasterizer.NET 4.0 is now available. Supports .NET Core. Renders pixel-perfectly.

Relevant products

C# Print PDF Document

How to print PDF form c# or WPF Print PDF? In this sample you can get an impression of how to print a PDF document using C#. We won’t be using a graphical user interface as the purpose of this sample is to show the mechanic of printing PDFs. This document will open the specified PDF and will print each page on the standard printer and paper specified by the users operating system. If you want to see how you can use this in a GUI, please refer to the code sample in which you obtain when downloading PDF Rasterizer.

C# Print document by using PDF Rasterizer.

The main point of printing using PDF Rasterizer is the drawing of each page. During C# PDF printing, an event will be thrown for each page. Each page is then drawn onto the graphical surface provided by the PrintPageEventHandler function.

Tall Components - .NET libraries for PDF development. Check out our Code Samples. Check out the Code Sample C# Print PDF and get an impression of how to print a PDF in C#.

static int currentPage = 0;
static Document d;
static PrintDocument printDocument;

static void Main(string[] args)
{
    using (FileStream fs = new FileStream(@"..\..\input.pdf", FileMode.Open, FileAccess.Read))
    {
        d = new Document(fs);
        printDocument = new PrintDocument();
        printDocument.DocumentName = d.DocumentInfo.Title;
        printDocument.DefaultPageSettings.Margins.Left = 0;
        printDocument.DefaultPageSettings.Margins.Right = 0;
        printDocument.DefaultPageSettings.Margins.Bottom = 0;
        printDocument.DefaultPageSettings.Margins.Top = 0;
        printDocument.OriginAtMargins = true;

        // uncomment to avoid status dialog
        //printDocument.PrintController = new StandardPrintController();

        printDocument.PrintPage += new PrintPageEventHandler(printPage);
        printDocument.Print();
    }
}

private static void printPage(object sender, PrintPageEventArgs e)
{
    e.HasMorePages = false;
    if (currentPage <= d.Pages.Count)
    {
        e.Graphics.PageUnit = GraphicsUnit.Point;
        Page page = d.Pages[currentPage];

        e.Graphics.MultiplyTransform(GetPrintTransformation(page, e.Graphics.VisibleClipBounds));
        page.Draw(e.Graphics);
        if (currentPage < d.Pages.Count - 1)
        {
            currentPage++;
            e.HasMorePages = true;
        }
    }
}

private static Matrix GetPrintTransformation(Page page, RectangleF printableArea)
{
    Matrix transformation = new Matrix();

    float pdfWidth = (float)page.Width;
    float pdfHeight = (float)page.Height;
    float pdfRatio = pdfWidth / pdfHeight;

    PaperSize paperSize = printDocument.PrinterSettings.PaperSizes[0];
    float paperWidth = ((float)paperSize.Width * 72f) / 100f;
    float paperHeight = ((float)paperSize.Height * 72f) / 100f;

    // It is possible that a driver returns bogus data for the printable area. If that is
    // obviously the case, we are going to ignore the usePrintableArea settings (otherwise
    // we get invalid transforms).
    paperWidth = printableArea.Width;
    paperHeight = printableArea.Height;
    transformation.Translate(printableArea.Left, printableArea.Top);

    float paperRatio = paperWidth / paperHeight;

    float scaledPdfWidth = pdfWidth;
    float scaledPdfHeight = pdfHeight;

    bool rotate = false;
    if ((pdfRatio > 1 && paperRatio < 1) || (pdfRatio < 1 && paperRatio > 1))
    {
        // need to rotate
        rotate = true;
        float temp = scaledPdfWidth;
        scaledPdfWidth = scaledPdfHeight;
        scaledPdfHeight = temp;
        pdfRatio = 1 / pdfRatio;
    }

    float scale = 1;
    if (pdfRatio > paperRatio)
        scale = paperWidth / scaledPdfWidth;
    else
        scale = paperHeight / scaledPdfHeight;

    scaledPdfWidth *= scale;
    scaledPdfHeight *= scale;

    // auto-rotate
    if (rotate)
    {
        transformation.Rotate(-90);
        transformation.Translate(-scaledPdfHeight, 0);
    }

    // scale
    transformation.Scale(scale, scale);

    return transformation;
}

More C# Print PDF code samples?

Check out other C# Print PDF code samples:

No results

Add Long Term Validation (LTV) data to an existing signature
Render PDF to multipage color TIFF
Render PDF page to Skia surface
Render PDF page as PNG
How to downscale all images in a PDF
This code sample shows how to downscale images in a PDF file. Read this Tall Components code sample or search our code sample base.
How to generate and export certificates
In this Tall Components code sample we will show you how you can create an RSA 2048-bit PEM certificate and how you can export it.
How to downscale all images in a PDF
This code sample shows how to downscale images in a PDF file. Read this Tall Components code sample or search our code sample base.
Add Stamp to PDF
This code sample shows how to stamp a PDF in C#. You can stamp images and watermarks to your PDF for example.
How to use a system font for rendering text
This code sample shows how you can render text using a system font. Read this Tall Components code sample or search our code sample base.
Customize the GUI interaction of a radio button
This sample shows how one can create a custom radio button interactor that responds to other key strokes than just the space bar.
Customize the UI interaction of a check box
This sample shows how one can create a custom check box interactor that responds to other key strokes than just the space bar.
PDF to grayscale TIFF
In the .NET framework the support for grayscale bitmaps is a bit puzzling. There is a way to convert PDF to grayscale Tiff in C#.
How to reduce PDF file size
Adobe Acrobat has an option to save a document with a reduced size. This article shows how to reduce pdf file size with PDFKit.NET.
How do I create graphics with Icc based colors
This article explains how one creates graphics that use Icc based colors. Read this code sample or search our code sample base.
Highlight fields in PDF
This code sample shows how to mark all fields in a PDF document. Read this Tall Components code sample or search our code sample base.
Add a note to PDF
This code sample shows how to add a note to a PDF document. Read this Tall Components code sample or search our code sample base.
Display PDF in a WPF app and stay responsive – the code
This article helps how to stay responsive while rendering a a PDF document in a WPF application. Read this or search our code sample base.
Draw interactively on a PDF page
This article allows users to interactively draw lines on PDF pages and save the resulting document. Read this or search our code sample base.
Resize PDF pages
This sample demonstrates how to change the size of pages in the PDF file. Read this code sample or search our code sample base.
Verify a custom digital PDF signature
In the article we explain how to verify a custom digital PDF signature. Read this Tall Components code sample or search our code sample base.
C# Print PDF documents from a WPF application
This code sample shows how to print PDF documents from a WPF application using C#. Read this code sample or search our code sample base.
Extract glyph boxes from PDF
This sample creates a bitmap for each page and draws boxes for each glyph. Read this code sample or search our code sample base.
Use TrueType font collections
This article describes how to select a font from a font collection and how this is embedded in a PDF document. Read or search our base.
Layout text with MultilineTextShape
This sample demonstrates how to format and layout text with MultilineTextShape. Read this code sample or search our code sample base.
Calculate the height of a paragraph in PDF
This code sample demonstrates how to use the event mechanism to calculate the height of a paragraph in a PDF.
Merge PDF files in C# .NET
In the following code sample you can see how you can easily merge PDF files into one. Read this code sample or search our code sample base.
How do I extract page destinations from bookmarks?
This article explains how one can determine which page a particular bookmark refers to. Read this code sample or search our code sample base.
Clip PDF page content in C#
This sample demonstrates how to mask out a rectangular area on a PDF page using a clip shape in C#. Read this or search our code sample base.
How do I use PDFControls.NET in a WPF application
This code sample shows you how PDFControls.NET can be used in a WPF application. Read this code sample or search our code sample base.
Fill PDF form
This code sample shows how to assign values to various types of PDF form fields. Read this code sample or search our code sample base.
Extract glyphs and sort by reading order
This code sample shows how to extract glyphs from PDF and sort. Read this Tall Components code sample or search our code sample base.
Add bookmarks to PDF
This code sample shows how to create bookmarks in an existing PDF document. Read this code sample or search our code sample base.
How to scale content of PDF
This code sample creates a copy of a PDF document but the content in the output PDF is scaled. Read this or search our code sample base.
Create rectangles with rounded corners
A Rectangle with rounded corners is not a standard shape. However it is easy to create these. Read this or search our code sample base.
Create text with decorations
Create a document with text with decorations like gradient color, shadow, emboss and glow. Read this or search our code sample base.
Create layers in PDF and draw on each layer
This code sample shows how to create a document and adds two new layers. Read this code sample or search our code sample base.
Multipage TIFF to PDF
This article shows you how to convert a multipage TIFF to PDF using TallPDF.NET. Read this code sample or search our code sample base.
TIFF to PDF C#
This article shows you how to convert a multipage TIFF to PDF using PDFKit.NET. Read this code sample or search our code sample base.
Crop content on a PDF page
This code sample shows how to remove all content that is outside a crop rectangle. Read this code sample or search our code sample base.
How to embed files in a PDF document
This code sample shows how to add embedded files (attachments) to a PDF document. Read this code sample or search our code sample base.
Remove graphics from PDF
This code sample shows how to partly erase images from a PDF under an explicitly specified rectangle. Read or search our code sample base.
Change the color inside a PDF
This code sample shows how to change color in a PDF using C# and VB.NET. Read this code sample or search our code sample base.
Create PDF in C#
This simple code sample shows you how to create a PDF using C# and print "Hello World" to it. Read or search our code sample base.
Text formatting
This code sample shows how to format your text. Read this Tall Components code sample or search our code sample base.
Import FDF into PDF
In this code sample we will show how you can easily import an FDF file and populate the form fields in a PDF.
Flatten PDF form
This code sample shows how to fill out a form and then flatten it. Read this code sample or search our code sample base.
Digitally sign a PDF form in C# or VB.NET
This code sample shows you how you can import a digital signature and use it to sign a PDF document in C# or VB.NET.
Vector graphics in PDF
This code sample shows how to draw different vector graphics such as line, ellipses and bezier curves. Read code sample or search our base.
Translate PDF page content
This code sample shows how to apply a single translation to all the content on a PDF page. Read or search our code sample base.
Extract graphics from PDF
This c# code sample shows how to extract text, images and curves as shapes from a PDF document. Read or search our code sample base.
Determine the content bounding box
This article shows how to determine the content bounding of a page. Read this Tall Components code sample or search our code sample base.
How to add page numbers to your PDF
This article shows how to add page numbers to PDF using TallPDF built-in features. Read or search our code sample base.
Create / impose PDF 2-up
This code sample shows how to do 2-up imposition using the PageShape class. Read this code sample or search our code sample base.
Search text in PDF
This article shows how to search a PDF for text in C# using the Document. Read this code sample or search our code sample base.
Append multiple PDF documents
This code sample illustrates to loop through a given folder and append each PDF documents found into a new generated PDF document.
Convert PDF to plain text
The following code sample shows how to convert the collection of glyphs on a PDF page to a text string. Read or search our code sample base.
Flatten Markup Annotation
This article illustrate how to turn markups to images by flatten markups. Read this code sample or search our code sample base.
Add text field to PDF
This code sample illustrates how you can add TextFields to your PDF. Read this Tall Components code sample or search our code sample base.
Extract embedded files from PDF
This code sample illustrates how to loop through embedded files in an existing PDF document and extract those attachments. Read more.
Extract images from PDF
This code sample illustrates how to iterate through existing content in a PDF document and save each images found as a new Image file.
Add a Diagonal Watermark to PDF in C#
This code sample shows how to add a diagonal watermark to an existing PDF in C#. Read this code sample or search our code sample base.
Fit image to PDF page
This code sample shows how to stamp an image on a PDF page and have the image fit to the size of the page. Read this code sample.
Add simple html text to PDF
The SimpleXhtmlShape lets you add text with simple HTML mark-up to a PDF document. Read or search our code sample base.
Add multiline text to a PDF document
The MultilineTextShape lets you add multiline text to PDF with mixed formatting and line-breaking. Read this code sample or search our code sample base.
Add single-line text to PDF
TextShape allows you to add single-line text to a PDF document. Read this Tall Components code sample or search our code sample base.
Create a new digitally signed PDF document
This code sample creates a new PDF document, adds a signature fields, and then digitally signs it. Read or search our code sample base.
PDF Viewer Preferences
The following code sample shows how to set the viewer preferences of a PDF document. Read or search our code sample base.
Change page orientation PDF
Code sample to change the page orientation PDF. Read this Tall Components code sample or search our code sample base.
Split PDF pages in C# and VB.NET
The following code sample shows how to split PDF pages in C# and VB.NET. Read this code sample or search our code sample base.
Append two or more existing PDF files
The following code sample shows how to append PDF files. Read this Tall Components code sample or search our code sample base.
Change colors of black-and-white TIFF after converting from PDF
In this article, we will demonstrate how to change the color palette after converting a PDF document to 1bpp TIFF in C#.
Determine if a PDF only contains images
The following sample code determines whether a PDF document only contains images (and not other shapes such as text fragments).
Add footer to PDF
This code sample shows how to add a footer in a PDF document. Read this Tall Components code sample or search our code sample base.
Convert SVG to PDF
This article explains how to use these text and fonts in SVG documents. Read this code sample or search our code sample base.
Fill in a PDF form using MVC
In this sample we will create a small ASP.NET application to fill in pdf forms, using a webform. Read or search our code sample base.
C# render pdf in browser using MVC
In this code sample we will be using the PDF Rasterizer package in a MVC context to render specific PDF pages in a browser.
Convert XHTML to PDF
This code sample shows how to add a note to a PDF document C#. Read this code sample or search our code sample base.
Add hyperlink to PDF
This code samples draws a hyperlink on an existing PDF page. Read this code sample or search our code sample base.
Rotate a PDF page
This code sample creates a copy of a PDF document with each page rotated 90 degrees. Read or search our code sample base.
Change the formatting of a numeric field
This code sample changes the format by changing the JavaScript of the action. Read or search our code sample base.
How to mirror PDF pages and other shapes
In this code sample we will mirror the pages in an existing PDF document. Read this code sample or search our code sample base.
Fill in a template PDF document
This code sample shows you how you can import a template document and fill it in with data from a database.
How to add autosized text to PDF
This code sample shows you how you can add a piece of auto-sized text to your PDF document. Read or search our code sample base.
Create formfields in PDF documents
This codesample shows how you can create different types of formfields in PDF. Read this code sample or search our code sample base.
Export FDF from PDF form
This code sample helps to exports FDF from PDF. Read this Tall Components code sample or search our code sample base.
Add a link with an internal destination to PDF
This code sample helps with internal links in a PDF. Read this Tall Components code sample or search our code sample base.
Remove PDF security settings
This code sample shows how you can remove the security setting of a PDF quite easily; just set the security attribute to null.
Add a link to PDF with an external destination
This code sample helps adding an external link to your PDF. Read this code sample or search our code sample base.
How to sign and verify updates to a PDF document
This code sample shows how you can sign PDF documents and how to handle updates to form data. Read or search our code sample base.
Convert PDF to PNG using WPF
In this code sample we take a look how you can create PNG images from a PDF using the Windows Presentation Foundation.
Embed TrueType font
This code sample shows how to draw text on a page using a TrueType font and embed this font in the PDF. Read or search our code sample base.
Override MouseWheel event
This code sample helps to override MouseWheel event. Read this Tall Components code sample or search our code sample base.
Convert PDF to an image using a dither matrix
This code sample shows how to convert a PDF page to a bitmap image, while applying a dither matrix to the generated image.
Font mapping
If you have a PDF document which uses non-embedded fonts, you can specify your font substitution map to specify which fonts need to be used.
Convert PDF with layers to image
In the code sample below you see how you can incorporate multiple layers into your rendering process. Read or search our code sample base.
C# Print PDF Document
C# Print PDF? With this sample you can get an impression of how to print a PDF in C#. Read this code sample or search our code sample base!
Render PDF with ResolveFont event handler
When you try to render a PDF which uses non-system fonts, you can add your own specified fonts to make sure it is rendered correctly.
Render PDF to EMF
This code sample helps render PDF to EMF. Read this Tall Components code sample or search our code sample base.
Convert PDF to XPS
In this sample you can see how you convert your PDF document to an XPS format (which is Microsoft Windows' version of PDF).
How to create a thumbnail viewer
This sample shows how you can create a thumbnail viewer for your PDF application and it builds on the SimpleViewer code sample.
How to create a tiling for shapes in PDF
In this code sample we will show you how you can create a rectangle and fill it using a TilingBrush containing dots.
Add footer with left and right aligned text on same line
Add a footer to a PDF document that has left aligned text with a page number and right aligned text on the same line.
Convert PDF to JPG in C#
This code sample shows how to convert PDF to JPG or JPEG in C#. Read this Tall Components code sample or search our code sample base.
EMF to PDF as vector image
EMF is the Windows Meta file file format for images. Read this Tall Components code sample or search our code sample base.
EMF to PDF as raster image
This code sample helps to create a Bitmap (PDF) from an EMF file in C#. Read this Tall Components code sample or search our code sample base.
Replace field with image
This code sample opens a PDF with a textfield and replaces this textfield with an image. Read or search our code sample base.
Add a rubber stamp annotation with a custom icon
This article shows how to create a customized rubber stamp annotation. Read this code sample or search our code sample base.
Create a text annotation in PDF with rich text
This sample shows how to create annotations in PDF that use a rich text for their appearance. Read or search our code sample base.
XhtmlParagraph and TrueType fonts
The following code converts XHTML to PDF using the XhtmlParagraph. Read this code sample or search our code sample base.
What is the resulting fontsize in PDF for rich text used in a SimpleXhtmlShape
TallPDF supports the SimpleXhtmlShape. This shape accepts PDF rich text as its Text attribute. Read or search our code sample base.
Read and write meta data from PDF
This article shows a sample for reading and writing meta data from PDF using C#. Read this code sample or search our code sample base.
Create a custom signature handler to sign and verify PDF documents
This code sample shows how to create your own signature handler. Read this code sample or search our code sample base.
Merge PDF
In this code sample we will create a new PDF by stitching two existing PDF documents. Read or search our code sample base.
Stitch PDF documents
This code sample shows how to stich PDF pages from an existing PDF document to a newly generated PDF using TallPDF.NET.
Download and convert image to PDF
This article shows how to download an image on a web page as a PDF document. Read this code sample or search our code sample base.
Convert TXT to PDF
This code sample helpt converting TXT to PDF. Read this Tall Components code sample or search our code sample base.
Add barcodes to PDF
This article demonstrates how to add barcodes to a PDF document. Read this code sample or search our code sample base.
Convert PDF to multipage TIFF in C# .NET
This article shows how to convert PDF to multipage TIFF in C# using PDFRasterizer.NET 3.0. Read or search our code sample base.
Convert multiple PDF pages to bitmap
In this article, we will convert multple pages of a PDF to a single bitmap. Read this code sample or search our code sample base.
Render a PDF to bitmap
This article explains the most basic way to convert PDF to bitmap. Read this code sample or search our code sample base.
Bulleted list from XML and XSL
This code sample helps getting a bulleted list from XML and XSL. Read this code sample or search our code sample base.
Tagged PDF
This code sample helps create and consume tagged PDF documents. Read this code sample or search our code sample base.
PDFKit.NET 5.0 – detailed changes to the API
This article compares the builds that 4.0 and 5.0 have in common: net20, net40, net40-client and net40-wpf.
Create tagged PDF
This code sample demonstrate how to create a new tagged PDF. Read this Tall Components code sample or search our code sample base.
PDFKit.NET 5.0 and .NET Core
PDFKit.NET 5.0 includes a build targeting .NET Standard 1.5. According to the following table, it supports .NET Core 1.0 and higher.
PDFKit.NET 5.0 and Xamarin
This code samples helps with PDFKit.NET 5.0 and Xamarin. Read this Tall Components code sample or search our code sample base.
Dynamic XFA
The new dynamic XFA API of PDFKit.NET 5.0 allows populating and consuming dynamic XFA documents. Read or search our code sample base.
PDFKit.NET 5.0 .NET Standard API
This code sample helps with PDFKit.NET 5.0 .NET Standard API. Read this Tall Components code sample or search our code sample base.
.NET Core console app on MacOS
In this article we create step by step a simple .NET Core console application on MacOS. Read or search our code sample base.
Add tags to existing PDF
This article demonstrates how to open existing PDF document, read visual content and tag the visual content based on its type and content.
Read PDF tags
This code sample enumerates the tags in an existing PDF document. Read this code sample or search our code sample base.
Merge XDP data with dynamic XFA form
The following code imports XDP data into a dynamic XFA form. Read this Tall Components code sample or search our code sample base.
Fill XFA form and export XDP data
The following code opens an XFA form and programmatically fills text field and selects items from drop-down lists and exports data as XDP.
Fill and save dynamic XFA form
The following code opens a XFA form and programmatically fills text fields, selects an item from a drop-down list and 'clicks' a button.
Use PDFKit.NET 5.0 with a Xamarin.Forms app
This article demonstrates a simple Xamrin.Forms app that runs on Android, iOS and UWP. Read or search our code sample base.
Use multiple licenses
This code sample explains how to register multiple license keys with a single application. Read or search our code sample base.
Licensing and .NET Standard
This code sample helps with licensing and .NET Standard. Read this Tall Components code sample or search our code sample base.
Reduce PDF size
PDFKit5 includes support for non-desctructive PDF compresssion. Read this code sample or search our code sample base.
Generate PDF form from XML
The following code sample shows how to generate a PDF form from XML. Read this code sample or search our code sample base.
Generate PDF with local images from XML with Xamarin.iOS
This code sample generates a PDF from XML with an image that is included as resource. Read or search our code sample base.
Disable submit button after submitting
The following code adds a JavaScript action to a submit button that will disable the button after submitting.
Write Document to HttpResponse
In PDFKit.NET 5.0 we decided to remove the Document.Write(HttpResponse) because it required the client to add a reference to System.