Convert PDF to JPG in .NET
Do you need to convert PDF to JPG in C# or VB.NET? You can use Docotic.Pdf library to generate JPEG images from PDF documents in .NET projects. The library can also help you with converting PDF to PNG or TIFF.
9.5.17573-dev 14,726 passed Total NuGet downloads 4,765,230Docotic.Pdf comes with free and paid licenses. The library is available on NuGet and from our site. Get the library and a free time-limited license key on the Download C# .NET PDF library page.
Save PDF pages as JPG in C#
You need just a few lines of code to convert PDF to JPG in .NET. This C# code snippet saves PDF pages to JPG images with a white background and 72x72 dpi resolution:
using BitMiracle.Docotic.Pdf;
PdfDrawOptions options = PdfDrawOptions.Create();
options.Compression = ImageCompressionOptions.CreateJpeg();
options.BackgroundColor = new PdfRgbColor(255, 255, 255);
using var pdf = new PdfDocument("your-document.pdf");
for (int i = 0; i < pdf.PageCount; ++i)
pdf.Pages[i].Save($"page_{i}.jpg", options);
Try the corresponding sample code for saving PDF page as PNG, JPG, or TIFF image from GitHub.
You can also work with System.IO.Stream instead of files:
PdfDrawOptions options = PdfDrawOptions.Create();
options.Compression = ImageCompressionOptions.CreateJpeg();
options.BackgroundColor = new PdfRgbColor(255, 255, 255);
using Stream input = ..;
using var pdf = new PdfDocument(input);
using Stream output = new MemoryStream();
pdf.Pages[0].Save(output, options);
...
Convert PDF to JPG with a custom resolution
PdfDrawOptions class allows you to customize PDF to JPG conversion process.
For example, you can use HorizontalResolution
and VerticalResolution
properties to set resolution
of resulting JPEG images:
PdfDrawOptions options = PdfDrawOptions.Create();
options.Compression = ImageCompressionOptions.CreateJpeg();
options.BackgroundColor = new PdfRgbColor(255, 255, 255);
options.HorizontalResolution = 600;
options.VerticalResolution = 600;
page.Save("page.jpg", options);
Generate JPEG thumbnails for PDF pages
PdfDrawOptions
class also provides methods for limiting a resulting image size. For example,
you can use it to generate JPEG thumbnails from PDF pages. This C# sample creates a 300x300 pixels JPEG thumbnail
of the first page:
PdfDrawOptions options = PdfDrawOptions.CreateFitSize(new PdfSize(300, 300), false);
options.Compression = ImageCompressionOptions.CreateJpeg();
options.BackgroundColor = new PdfGrayColor(100);
pdf.Pages[0].Save("thumbnail.jpg", options);
Convert PDF to CMYK JPEG in .NET
The ImageCompressionOptions.CreateJpeg()
method creates
JpegImageCompressionOptions objects.
You can use JpegImageCompressionOptions
methods to set a desired JPEG image quality or color space.
This C# code sample shows how to convert a PDF page to CMYK JPEG with 50 quality:
PdfDrawOptions options = PdfDrawOptions.Create();
options.BackgroundColor = new PdfGrayColor(100);
options.Compression = ImageCompressionOptions.CreateJpeg().SetCmyk().SetQuality(50);
page.Save("cmyk.jpg", options);
Is it possible to convert PDF to JPG with transparency?
JPEG images do not support transparency. That's why the code samples above explicitly use white background.
If you want to convert PDF to semi-transparent images, use PNG or TIFF as an output image compression.
Conclusion
Docotic.Pdf library allows you to convert PDF to JPG in .NET easily. You can customize size, resolution, color space for output JPEG images.
You can also use Docotic.Pdf for converting PDF to PNG or TIFF images. Download and try PDF to image code samples from GitHub.
Contact us if you have questions about PDF to JPG conversion.
Related resources
- Convert PDF to image in C# and VB.NET article
- Render and print PDF in C# and VB.NET article
- Code examples for PDF to image conversion