Developer Tools
|
Office Productivity Applications
|
Enterprise Solutions
|
|||||||||||||||||||||||







In a previous article, I showed you how to rasterize a PDF document by first exporting all page content as an image file and then rendering the images in a different PDF document. In this article, you will see how to export PDF page content as vector images.
PDFDocument.GetPageMetafile()Windows metafile is Microsoft file format for storing GDI instructions. GDI instructions from a metafile can be "played back" to re-create the same appearance. This also means that the appearance is scalable. (Raster images are not scalable.)
In the above-mentioned article, the method PDFDocument.GetPageMetafile() was used for exporting vector images of PDF pages. However, the System.Drawing.Metafile class of the .NET framework does not support saving to EMF/WMF formats. If you attempt to save to one of those formats, the resultant file will be a PNG file.
In the code example shown below, two native GDI methods are used to ensure that the metafile is saved as a EMF file. A Metafile instance can have a huge memory footprint. So, the code is meant for small documents. The garbage collections instructions can help but will not save your program in all cases.
using System;
using System.Collections.Generic;
using System.Text;
using System.Drawing;
using System.Drawing.Imaging;
using System.Runtime.InteropServices;
using Gnostice.PDFOne;
namespace PDFOne_dotNET_Examples {
class PDFDocument_GetPageMetafile {
[DllImport("gdi32.dll")]
static extern IntPtr CopyEnhMetaFile( // Copy EMF to file
IntPtr hemfSrc, // Handle to EMF
String lpszFile // File
);
[DllImport("gdi32.dll")]
static extern int DeleteEnhMetaFile( // Delete EMF
IntPtr hemf // Handle to EMF
);
static void Main(string[] args) {
// Create a metafile
Metafile metafile;
// Create a handle to a metafile
IntPtr iptrMetafileHandle;
// 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);
// Get a handle to the metafile
iptrMetafileHandle = metafile.GetHenhmetafile();
// Export metafile to an image file
CopyEnhMetaFile(
iptrMetafileHandle,
"image_of_page_#" + i.ToString() + ".emf");
// Delete the metafile from memory
DeleteEnhMetaFile(iptrMetafileHandle);
/*
// To save metafile as a raster image,
// comment three previous statements.
// Metafile.Save does not save to EMF/WMF.
metafile.Save(
"image_of_page_#" + i.ToString() + ".png",
ImageFormat.Png);
*/
Console.Write("\rPage #1-" + i + " exported.");
// Release any resources used by the metafile
metafile.Dispose();
// Use the following statements if you are loading
// huge documents with rich content
GC.Collect();
GC.WaitForPendingFinalizers();
}
// Clean up
doc.Close();
Console.WriteLine("\nDocument closed.");
doc.Dispose();
GC.Collect();
GC.WaitForPendingFinalizers();
Console.WriteLine("Press Enter to exit.");
Console.In.ReadLine();
}
}
}
The image below shows the difference between the EMF and PNG exports of the metafile, contents of both of which have been magnified. The PNG image has lost some detail. The EMF has retained fidelity, as the GDI instructions were rescaled and played back each time the image was magnified.

In Version 5 of PDFOne .NET, we introduced new overloads of the PDFDocument.GetPageMetafile method which allow you to specify a DPI value at which the contents of the PDF pages need to be exported. Using this option, you will be able to export PDF page content at high resolutions.
---o0O0o---
| Our Developer Tools | |
|---|---|
eDocEngine VCLA Delphi/C++Builder component suite for creating documents in over 20 formats and also export reports from popular Delphi reporting tools. |
PDFtoolkit VCLA Delphi/C++Builder component suite to edit, enhance, view, print, merge, split, encrypt, annotate, and bookmark PDF documents. |
XtremePDFConverter VCLA Delphi/C++Builder component to intelligently convert PDF to user-friendly Word RTF documents. |
|
PDFOne .NETA .NET PDF component suite to create, edit, view, print, reorganize, encrypt, annotate, and bookmark PDF documents in .NET applications. |
XtremeDocumentStudio .NETMulti-format document-processing component suite for .NET developers |
PDFOne (for Java™)A Java™ PDF component suite to create, edit, view, print, reorganize, encrypt, annotate, bookmark PDF documents in Java™ applications. |
XtremeFontEngine (for Java)Java font engine to render glyphs from Type 1, Type 2 (CFF), and TrueType fonts |
| Our Office Productivity Applications |
|---|
Free PDF ReaderA free, fast, and portable application for viewing, printing and converting PDF documents. |
| Privacy | Legal | Feedback | Newsletter | Resellers | © 2002-2013 Gnostice Information Technologies Private Limited. All rights reserved. |
This site is best viewed on a screen with minimum resolution of 1152 x 864 pixels. Windows XP users are advised to use Microsoft ClearType Tuning for optimal experience. Also, please use the latest version of a standards-compliant browser such as Firefox, Opera, or Dragon (Chromium).