Skip to main content Skip to footer

PDF to Image Conversion using GcPdf in .NET Core

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.

PDF to Image Conversion

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.

3 Ways to Convert PDFs to Images

  1. Render PDF method This method involves the usage of Grapecity.Documents.Drawing.GcGraphics reference in the application. Hence, the user would need to have a license for GrapeCity Documents for Imaging API. It allows page-by-page PDF conversion to images.

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: Image1

Page view as an image: Image2

  1. GcPdfDocument.SaveAsXXX methods 

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.

Image3

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");

  1. Page.SaveAsXXX methods This method involves saving a specific page from PDF as an image. It is called using the Grapecity.Documents.Pdf.Page class instance. As the previous method, this one also saves the page to five different formats mentioned above. For jpg, this is done as follows:
 var page1 = doc.Pages.Last;  
 page1.SaveAsJpeg("Page.jpg");

Page view from the PDF: Image4

Page view as an image: Image5

PDF rendering on GcGraphics can be carried out with certain limitations:

  • Gradients are not supported
  • Soft masks are not supported
  • Type1 fonts are not supported

Happy coding! Be sure to leave your thoughts and comments below.

Try this tutorial yourself!

Download the sample

Esha Dhir

Associate Software Engineer
comments powered by Disqus