PDFOne .NET
Powerful all-in-one PDF library for .NET
Compatibility
VS 2005/2008/2010/2012/2013

A Render-On-Graphics Demo For PDFOne .NET

Learn how to render PDF pages on a Graphics object.
By L. Santhanam

If your application generates PDF documents in real time, then why not show snapshots of the document dynamically? In the ProPlus edition of PDFOne .NET, the PDFDocument component has certain methods that allow you to export images of PDF pages. They are RenderOnGraphics() and GetPageMetafile(). In the following code example, you will see how to use the RenderOnGraphics() method. This code snippet uses a bitmap, which will then be used to render the Graphics item. The bitmap is saved to a file only to demonstrate the results.

using System;
using System.Collections.Generic;
using System.Text;
using System.Drawing;
using System.Drawing.Imaging;
using System.Drawing.Drawing2D;
using Gnostice.PDFOne;

class RenderOnGraphicsDemo
{

    static void Main(string[] args)
    {
        // Determine current DPI
        Graphics Graphics1 = Graphics.FromHwndInternal(IntPtr.Zero);
        int DPI = (int) Graphics1.DpiX;
        Graphics1.Dispose();

        // Load a PDF document
        PDFDocument PDFDocument1 = new PDFDocument("your-license-key");
        PDFDocument1.Load("sample_doc.pdf");

        // Get page 3 from the document
        PDFPage PDFPage1 = PDFDocument1.GetPage(3);

        // Get dimensions of page 3 in pixels
        int PageWidth = DPI * ((int)PDFPage1.GetWidth(PDFMeasurementUnit.Points) / 72);
        int PageHeight = DPI * ((int)PDFPage1.GetHeight(PDFMeasurementUnit.Points) / 72);

        // Create a bitmap with the page's dimensions
        Bitmap Bitmap1 = new Bitmap(PageWidth, PageHeight);

        // Set the Graphics instance to an image
        Graphics1 = Graphics.FromImage(Bitmap1);

        // Make the Graphics instance suitable for rendering PDF content
        Graphics1.SmoothingMode = SmoothingMode.AntiAlias;
        Graphics1.PageUnit = GraphicsUnit.Point;
        Graphics1.PixelOffsetMode = PixelOffsetMode.HighQuality;
        Graphics1.TextRenderingHint = System.Drawing.Text.TextRenderingHint.AntiAlias;
        Graphics1.InterpolationMode = InterpolationMode.High;

        // Render page 3 on Graphics instance
        PDFDocument1.RenderOnGraphics(Graphics1, 3);
        PDFDocument1.Close();

        // Save rendered content to a bitmap image file
        Bitmap1.Save("sample_doc_page3.bmp", ImageFormat.Bmp);

        // Clean up
        Bitmap1.Dispose();
        Graphics1.Dispose();
        PDFDocument1.Dispose();

    }
}

Original PDF Document

Rendered Contents Saved To An Image File

RenderOnGraphics() used in the above code snippet will render content as is displayed on the screen by a viewer application. A PDF page may look different when it is printed on paper. If you wish to render on graphics to show a "Print Preview," then you need to use the other overload of the method that has a flag for rendering print output. Here is the method definition.

// In C#
public void RenderOnGraphics(
   Graphics g,
   int pageNum,
   bool printing
)
// In VB
Public Overloads Sub RenderOnGraphics( _
   ByVal g As Graphics, _
   ByVal pageNum As Integer, _
   ByVal printing As Boolean _
)

If you set the printing parameter to true and then call RenderOnGraphics(), then PDFDocument will not render form fields, annotations and other PDF page elements that are not meant to be printed.

---o0O0o---

Our .NET Developer Tools
Gnostice Document Studio .NET

Multi-format document-processing component suite for .NET developers.

PDFOne .NET

A .NET PDF component suite to create, edit, view, print, reorganize, encrypt, annotate, and bookmark PDF documents in .NET applications.

Our Delphi/C++Builder developer tools
Gnostice Document Studio Delphi

Multi-format document-processing component suite for Delphi/C++Builder developers, covering both VCL and FireMonkey platforms.

eDocEngine VCL

A Delphi/C++Builder component suite for creating documents in over 20 formats and also export reports from popular Delphi reporting tools.

PDFtoolkit VCL

A Delphi/C++Builder component suite to edit, enhance, view, print, merge, split, encrypt, annotate, and bookmark PDF documents.

Our Java developer tools
Gnostice Document Studio Java

Multi-format document-processing component suite for Java developers.

PDFOne (for Java)

A Java PDF component suite to create, edit, view, print, reorganize, encrypt, annotate, bookmark PDF documents in Java applications.

Our Platform-Agnostic Cloud and On-Premises APIs
StarDocs

Cloud-hosted and On-Premises REST-based document-processing and document-viewing APIs

Privacy | Legal | Feedback | Newsletter | Blog | Resellers © 2002-2024 Gnostice Information Technologies Private Limited. All rights reserved.