Note: The PdfDecoder class is part of our PdfRasterizer module, which is an add-on to DotImage. To use this class, you must add Atalasoft.dotImage.PdfRasterizer.dll as a reference to your project, and you will need a license file for this module. You can request an evaluation license file for this module using the DotImage Activation Wizard.
A common task that comes accross in the document management world is converting from one file format to another. The two most commonly used formats for digital document images are PDF and TIFF. This task may confuse DotImage developer's because the PDF and TIFF codecs function slightly different from each other. Here I will explain the different approaches to this problem. First, I'll start by explaining the simple case, PDF to TIFF.
There are two main ways to save a TIFF file in DotImage. The first is to have all of the images (pages of the TIFF file) loaded into memory at once, and pass them all to the TiffEncoder to save the file. As you can tell, this is not very memory efficient for large documents. The second way to save a TIFF file is to append the pages, one by one to an existing TIFF file. This way, we only need to keep a single image in memory at any given time.
As with all mutlipage formats, DotImage lets us read a single page from a file. So we can load each page from the PDF file as needed. As you can see from the following example, the first way is much easier to implement, but the second way will conserve a lot of memory.
// First way
TiffEncoder enc = new TiffEncoder();
// Second way
TiffEncoder noAppend = new TiffEncoder(TiffCompression.Default, true);
PdfDecoder pdf = new PdfDecoder();
for(int i=0; i< numPages; i++)
AtalaImage img = pdfDecoder.Read(inStream, i, null);
noAppend.Save(outStream, img, null);
' First way
Private enc As TiffEncoder = New TiffEncoder()
' Second way
Dim noAppend As TiffEncoder = New TiffEncoder(TiffCompression.Default, True)
Dim pdf As PdfDecoder = New PdfDecoder()
Dim i As Integer=0
Do While i< numPages
Dim img As AtalaImage = pdfDecoder.Read(inStream,i,Nothing)
noAppend.Save(outStream, img, Nothing)
i += 1
That’s it! We have just converted a PDF file to a TIFF file. Now to save the TIFF as PDF. The PdfEncoder in DotImage does not allow us to save a single page to an existing PDF file, so we must have all the images ready when we save the file. But the good part is that these images don’t necessarily need to be held in memory. The PdfEncoder has the standard overload to save an AtalaImage or ImageCollection, but it also introduces a new class called PdfImageCollection, which is made up of PdfImage’s. This class gives us the flexibility to point to a file instead of an image in memory. To do this, simply create a PdfImageCollection that contains a PdfImage that points to the TIFF file that we want to convert.
PdfImageCollection col = new PdfImageCollection();
Col.Add(new PdfImage(“TheDoc.tif”, -1, PdfCompressionType.Auto));
PdfEncoder enc = new PdfEncoder();
Private col As PdfImageCollection = New PdfImageCollection()
Col.Add(New PdfImage(“TheDoc.tif”, -1, PdfCompressionType.Auto))
Dim enc As PdfEncoder = New PdfEncoder()
Giving -1 as the frame index will force the entire TIFF image to be loaded. Converting to PDF isn’t really any harder than converting the other way, it just requires a little more knowledge of the PDF namespace. Using these techniques, the ability to convert from TIFF to PDF, and vice versa, can be easily integrated into any document imaging application using DotImage.
Namespaces used in these examples: