Developer Tools
|
Office Productivity Applications
|
Platform-Agnostic APIs
|
Home | Online Demos | Downloads | Buy Now | Support | About Us | News | Working Together | Contact Us
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 com.gnostice.net.pdfone.examples { class PDF_to_EMF { [DllImport("gdi32.dll")] static extern IntPtr CopyEnhMetaFile( // Copy EMF to file IntPtr hemfSrc, // Handle to EMF String lpszFile // File ); [DllImport("gdi32.dll")] static extern bool DeleteEnhMetaFile( IntPtr hemf ); static void Main(string[] args) { // Create a metafile Metafile metafile; // Create a handle to a metafile IntPtr iptrMetafileHandle, iptrMetafileHandleCopy; // 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 iptrMetafileHandleCopy = CopyEnhMetaFile( iptrMetafileHandle, "image_of_page_#" + i.ToString() + ".emf"); // Delete source and copied metafiles from memory DeleteEnhMetaFile(iptrMetafileHandle); DeleteEnhMetaFile(iptrMetafileHandleCopy); /* // To save metafile as a raster image, // comment four 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(); } // Clean up doc.Close(); Console.WriteLine("\nDocument closed."); doc.Dispose(); 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.
This article uses a console application. While you could adapt it for an ASP.NET application to do that, we wouldn't recommend that.
For these reasons, we suggest that you use an ASP.NET application only to receive the requests for conversion and perform the actual conversion asynchronously using a "Windows Service" application, which runs as a background process on the same server or another computer. A Windows Service application can call Win32 API without restriction and does not timeout like a web application. We have published another article where this ASP.NET front end with a Windows service file conversion backend is demonstrated. Unfortunately, that article is for a Delphi product but you will get the idea of how to do it. You just need to write the Windows Service application in C#.
---o0O0o---
Our .NET Developer Tools | |
---|---|
Gnostice Document Studio .NETMulti-format document-processing component suite for .NET developers. |
PDFOne .NETA .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 DelphiMulti-format document-processing component suite for Delphi/C++Builder developers, covering both VCL and FireMonkey platforms. |
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. |
Our Java developer tools | |
---|---|
Gnostice Document Studio JavaMulti-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 | |
---|---|
StarDocsCloud-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. |