PDF files make life easier due to their non-editable nature and easy portability. While everyone can make use of PDF files to share data, there might be situations when it is required to use the PDF file content as images in web pages, word processing documents, and PowerPoint presentations. Changes to the PDF might also be needed. For example, pointing out text modifications and marking certain areas can be done if they are converted to images and then opened in image editors.
Let's say a company maintains a PDF to track monthly expenses on a project. It contains various resources and vendor-related expenses. The company does not necessarily wish to share the details concerning all the resources and vendors at once. Thus, in order to share the information for a resource/vendor with management, they might want to generate an image of a page from the PDF. This involves converting pages from the PDF to individual images, and calls for the conversion of PDF content to images using code. To serve the purpose, the GcPdf API has been enhanced so this task is completed with ease.
You can draw PDF pages on the image graphics using page.Draw(..) method. If your PDF pages contain annotations, you can draw the PDF page annotations on the graphics object using page.DrawAnnotations(..) method. Once drawn on the graphics, just save the bitmap image in any image format.
1.1 Load a document in GcPdfDocument:
GcPdfDocument doc = new GcPdfDocument();
var fs = new FileStream(Path.Combine("Resources", "MonthlyProjectExpenseTracking.pdf"),FileMode.Open,FileAccess.Read);
doc.Load(fs,null);
1.2 Render a page's content:
var page = doc.Pages[1];
var sz = page.Bounds;
GcBitmap bmp = new GcBitmap((int)(sz.Width + 0.5f), (int)(sz.Height + 0.5f), true);
using (GcGraphics gr = bmp.CreateGraphics(Color.White ))
{
page.Draw(gr, new RectangleF(0, 0, sz.Width,sz.Height));
}
where "page" is the Page class instance specifying which page should be converted to an image.
1.3 Save the GcBitmap instance to an image:
bmp.SaveAsJpeg("WholePage.jpg");
Page view from the PDF:
Page view as an image:
This method involves specifying the file names for saving the PDF document to images. The methods are supported for various image formats, namely:
Format | Method |
---|---|
JPEG (with specified quality) | SaveAsJpeg |
PNG | SaveAs Png |
BMP | SaveAsBmp |
TIFF | SaveAsTiff |
GIF | SaveAsGif |
FileName is a format string with current page number (1-based) passed to it as the value to be formatted. In case the formatting is not added to the string, all pages will be saved in the same file name with the last page overwriting all the other pages.
This is done as follows:
doc.SaveAsJpeg("WholeDocument_{0}.jpg");
This generates five images for a PDF having 5 pages.
TIFF format uses normal FileName in the method, without the specification of formatted string as in other formats. This is done as follows:
doc.SaveAsTiff("WholeDocument.tiff");
var page1 = doc.Pages.Last;
page1.SaveAsJpeg("Page.jpg");
Page view from the PDF:
Page view as an image:
PDF rendering on GcGraphics can be carried out with certain limitations:
Happy coding! Be sure to leave your thoughts and comments below.
Try this tutorial yourself!