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); document.Save(pdfStream); }
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) document.Save(pdfStream) 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.
Hi,
Do you have plans to support this for Tiff version as well?
Yes, support for large TIFF images will follow in the near future.
What is the maximum tested size of the jpeg file?
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.
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.
You have to set the stream position to 0 before creating the PdfPngImage object.
prueba.Position = 0;
PdfPngImage png = new PdfPngImage(prueba);
I was wrong. It works! Thank you
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…
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?
Can you send us (support@xfiniumpdf.com) a sample project that we can run and reproduce the problem? Also please include a sample image taken with your phone.