Convert large TIFF images to PDF

XFINIUM.PDF 6.8 brings support for converting very large TIFF to PDF with very low memory consumption.
Large TIFFs (hundreds of megabytes or higher, commonly found in printing industry) can be converted to PDF using only a few megabytes of memory, assuming that both the input TIFF image and the output PDF file are stored on disk.
The new class for handling these images is called PdfXLTiffImage and it is used just like its sibling class PdfTiffImage.
The constructor of PdfXLTiffImage class takes a Stream as parameter and a frame number and the only requirement is that this Stream is available till the PDF file is generated.
The conversion of TIFFs to PDF using the PdfXLTiffImage class is shown below:

public void ConvertLargeTiffToPdf(Stream tiffStream, Stream pdfStream)
{
    PdfFixedDocument document = new PdfFixedDocument();
    PdfPage page = document.Pages.Add();
    PdfXLTiffImage tiff = new PdfXLTiffImage(imageStream, 0);
    // Convert the TIFF to PDF and save the PDF
    page.Graphics.DrawImage(tiff, 0, 0, page.Width, page.Height);
    document.Save(pdfStream);
}
Public Sub ConvertTiffToPdf(tiffStream As Stream, pdfStream As Stream)
    Dim document As New PdfFixedDocument()
    Dim page As PdfPage = document.Pages.Add()
    Dim tiff As New PdfXLTiffImage(imageStream, 0)
    ' Convert the TIFF to PDF and save the PDF
    page.Graphics.DrawImage(tiff, 0, 0, page.Width, page.Height)
    document.Save(pdfStream)
End Sub

If the TIFF image has multiple frames and you want to convert all of them to PDF the code below shows how this can be accomplished:

public void ConvertMultiFrameTiffToPdf(Stream tiffStream, Stream pdfStream)
{
    PdfFixedDocument document = new PdfFixedDocument();
    bool moreFrames = true;
    int frameIndex = 0;
    while (moreFrames)
    {
        tiffStream.Position = 0;
        PdfXLTiffImage xlTiff = new PdfXLTiffImage(tiffStream, frameIndex);
        PdfPage page = document.Pages.Add();
        page.Graphics.DrawImage(xlTiff, 0, 0, page.Width, page.Height);

        frameIndex++;
        moreFrames = frameIndex < xlTiff.FrameCount;
    }
    document.Save(pdfStream);
}
Public Sub ConvertMultiFrameTiffToPdf(tiffStream As Stream, pdfStream As Stream)
    Dim document As New PdfFixedDocument()
    Dim moreFrames As Boolean = True
    Dim frameIndex As Integer = 0
    While moreFrames
        tiffStream.Position = 0
        Dim xlTiff As New PdfXLTiffImage(tiffStream, frameIndex)
        Dim page As PdfPage = document.Pages.Add()
        page.Graphics.DrawImage(xlTiff, 0, 0, page.Width, page.Height)

        frameIndex += 1
        moreFrames = frameIndex < xlTiff.FrameCount
    End While
    document.Save(pdfStream)
End Sub

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

PdfXLTiffImage tiff = new PdfXLTiffImage(imageStream, frameNumber, true);

This class can be used to convert any TIFFs to PDF, not just very large ones. In fact, if you have to convert many TIFFs to PDF (for example you create a photo album), this class can help lower the memory usage.
In our tests, using a console application, a 3.5GB TIFF image was converted to a PDF file using only 30 MB of mmeory.

Leave a Reply

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