Convert very large JPEG images to PDF

XFINIUM.PDF 6.7 brings support for JPEG to PDF conversion with very low memory consumption for very large images (hundreds of megabytes).

Very large JPEG images (hundreds of megabytes) can be converted to PDF using only a few megabytes of memory, assuming that both the input JPEG image and the output PDF file are stored on disk.
The new class for handling these images is called PdfXLJpegImage and it is used just like its sibling class PdfJpegImage.
The constructor of PdfXLJpegImage class takes a Stream as parameter and the only requirement is that this Stream is available till the PDF file is generated.
The conversion of very large JPEG images to PDF using the PdfXLJpegImage class is shown below:

public void ConvertJpegToPdf(Stream jpegStream, Stream pdfStream)
    PdfFixedDocument document = new PdfFixedDocument();
    PdfPage page = document.Pages.Add();
    PdfXLJpegImage jpeg = new PdfXLJpegImage(imageStream);
    // Convert the JPEG to PDF and save the PDF
    page.Graphics.DrawImage(jpeg, 0, 0, page.Width, page.Height);

Public Sub ConvertJpegToPdf(jpegStream As Stream, pdfStream As Stream)
    Dim document As New PdfFixedDocument()
    Dim page As PdfPage = document.Pages.Add()
    Dim jpeg As New PdfXLJpegImage(imageStream)
    ' Convert the JPEG to PDF and save the PDF
    page.Graphics.DrawImage(jpeg, 0, 0, page.Width, page.Height)
End Sub

If you want the library to dispose the jpeg stream after the PDF file was created, so that you don’t have to keep track of it, you can set the second parameter of the PdfXLJpegImage constructor to true.

PdfXLJpegImage jpeg = new PdfXLJpegImage(imageStream, true);
Dim jpeg As New PdfXLJpegImage(imageStream, True)

This class can be used to convert any JPEG images to PDF, not just very large ones. In fact, if you have to convert many JPEG images to PDF (for example you create a photo album), this class can help lower the memory usage.

Download the new version of XFINIUM.PDF library and give it a try. If you have questions, you can always contact us.

10 thoughts on “Convert very large JPEG images to PDF”

    1. We tested with a 700MB JPEG image, it is the largest one we could find. If you have a larger image, feel free to share it with us and we’ll test it.

  1. Hi,
    I’m doing an app to UWP. I want to create a pdf with a PNG image but I’m having problems in drawing it in the PDF file.
    Here is my code:

    byte[] bytes = File.ReadAllBytes ( Application.dataPath + Path.DirectorySeparatorChar + "Streaming" + Path.DirectorySeparatorChar + "sel.png");
    Stream prueb = new MemoryStream();
    await prueb.WriteAsync(bytes, 0, bytes.Length);

    PdfPngImage png = new PdfPngImage(prueba);
    page.Graphics.DrawImage(png, pos_x, pos_y - 5, 15, 15);

    When executing it I get a ‘PNG Decode Exception’
    What is the problem that I can’t see?
    Thanks in advance.

  2. Is not working…
    The exact displayed exception is:
    ‘PngDecodeException: Invalid PNG image, unknown PNG signature’
    But the PNG image is correct and the path too…

  3. If I take a picture with my android phone and try to convert this to a PDF, I get a blank PDF document.
    When I Use the picture from the samples, it works.
    Any suggestions?

    1. Can you send us ( a sample project that we can run and reproduce the problem? Also please include a sample image taken with your phone.

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.

%d bloggers like this: