Convert PDF to JPEG Using C#, VB, and Java Code

Our support department recently responded to a request for a code sample to convert PDF to JPEG. The customer needed to leverage LEADTOOLS to split and convert thousands of PDF files into single page JPEGs.

JPEG images are universal and can be loaded by practically any client on any operating system. If the PDF files are photos, JPEG is the perfect universal image format for conversion. JPEG is designed specifically for photo images, supporting 24 bits per pixel color and 8 bits per pixel grayscale data.

If the PDF files are text based and need to be converted to a universal image format, PNG and GIF are more appropriate. PNG and GIF support fewer bits per pixel, including 1- and 4-bits and leverage lossless compressions. These factors result in less data to be compressed and clearer images of text and clip art. Typically, PNG results in smaller file sizes when compared to GIF.

Below are some C#, VB, and Java code snippets that can be used to convert PDF files to JPGs. The snippets can be easily modified to save the images as GIF or PNG by changing the constant on the codecs.Save line.

Convert PDF to JPEG C# Code


var codecs = new RasterCodecs()
// https://www.leadtools.com/help/sdk/v21/dh/co/rastercodecs-options.html
codecs.Options.Load.AllPages = true; 
// https://www.leadtools.com/help/sdk/v21/dh/co/rastercodecs-load.html
var image = codecs.Load(pathToInputFile);  
for (var i = 1; i < image.PageCount; i++)   
{  
  var outputFile = Path.Combine(pathToOutputFolder, string.Format("page{0}.jpg", i));  
  image.Page = i; 
  // https://www.leadtools.com/help/sdk/v21/dh/co/rastercodecs-save.html
  codecs.Save(image, outputFile, RasterImageFormat.Jpeg, 0);  
}  
image.Dispose();
codecs.Dispose();

For more information on converting PDF to JPEG images in .NET, check out the LEADTOOLS Load and Save Images Tutorial for C# programmers.

Convert PDF to JPEG VB Code


Dim codecs As New Leadtools.Codecs.RasterCodecs() 
codecs.Options.Load.AllPages = True  
Dim image As Leadtools.RasterImage = codecs.Load(pathToInputFile) 
For i As Integer = 1 To image.PageCount Step 1
    Dim outputFile As String = Path.Combine(pathToOutputFolder, String.Format("page{0}.jpg", i)) 
    image.Page = i;
    codecs.Save(image, outputFile, RasterImageFormat.Jpeg, 0)  
Next
image.Dispose() 
codecs.Dispose() 

Convert PDF to JPEG Java Code


leadtools.codecs.RasterCodecs codecs = new leadtools.codecs.RasterCodecs(); 
leadtools.RasterImage image = codecs.Load(pathToInputFile);  
for (int i = 1; i < image.PageCount; i++) {
    String outputFile = pathToOutputFolder + "page" + String.valueOf(i) + ".jpg";
    image.Page = i; 
    codecs.Save(image, outputFile, RasterImageFormat.Jpeg, 0);   
}
image.dispose(); 
codecs.dispose();

For more information on converting PDF to JPEG images in Java, check out the LEADTOOLS Load and Save Images Tutorial for Java programmers.

See For Yourself – Free Evaluation

Download the LEADTOOLS SDK for free. It’s fully-functional for 60 days and comes with free chat and email support.

Stay Tuned For More Conversion Samples

Did you see our previous post, “Convert Multi-page TIFF to Searchable PDF in C#, Java, and C”? Stay tuned for more conversion examples to see how LEADTOOLS easily fits into any workflow converting PDF files into other document files or images and back again. Need help in the meantime? Contact our support team for free technical support! For pricing or licensing questions, you can contact our sales team via email or call us at +1-704-332-5532.

About 

Developer Advocate

    Find more about me on:
  • linkedin
  • twitter
  • youtube
This entry was posted in PDF and tagged , , . Bookmark the permalink.

Leave a Reply

Your email address will not be published. Required fields are marked *