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

How To Convert PDF To Image Formats In .NET

Learn to export PDF pages as raster images.
By V. Subhash
Important

In Version 5 of PDFOne .NET, a new SaveAsImage() method was introduced, which internally use this code. You do not have to go through the metafile-creation process.

In an earlier article, we learned how to export PDF documents to Windows metafiles. This was a bit complicated because .NET does not have an encoder for EMF or WMF, and we had to manage with some unmanaged calls. EMF files are useful only Windows. For wider use, we may need BMP, JPEG, PNG or other raster image formats.

Exporting PDF page content to a raster image format is much more simpler. (In the older article, there were code comments that hinted as much.) The example source code in this article shows us how.

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

namespace PDFOne_dotNET_Examples {

  class Convert_PDF_To_Images {

    static void Main(string[] args) {

      // Create a metafile
      Metafile metafile;

      // Load a PDF document
      PDFDocument doc =
         new PDFDocument("Your license key");
      // Load the input document and get its page count
      doc.Load("input_doc.pdf");
      Console.WriteLine("Document loaded.");


      // Iterate through all pages in the document
      for (int i = 1; i <= doc.GetPageCount(); i++) {

          // Export current page as a metafile
          metafile = doc.GetPageMetafile(i);

          // Save metafile to PNG and JPEG formats
          metafile.Save(
              "image_of_page_#" + i.ToString() + ".png",
              ImageFormat.Png);
          metafile.Save(
              "image_of_page_#" + i.ToString() + ".jpg",
              ImageFormat.Jpeg);


          Console.Write("\rPage #1-" + i + " exported.");

          // Release any resources used by the metafile
          metafile.Dispose();

      }

      // Clean up
      doc.Close();
      Console.WriteLine("\nDocument closed.");
      doc.Dispose();

      Console.WriteLine("Press Enter to exit.");
      Console.In.ReadLine();

    }
  }
}

What we do here is iterate through all the pages, export page contents as a metafile, and save the metafile as a PNG and JPEG image. That's it. (The program would have been smaller if it were not for the obsession with console output. ☺)

---o0O0o---

Our .NET Developer Tools
XtremeDocumentStudio .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
XtremeDocumentStudio 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
XtremeDocumentStudio (for 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-2017 Gnostice Information Technologies Private Limited. All rights reserved.