Print document

Printing
8/24/2015

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.

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

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