Q:
How do I convert multipage TIFFs to PDF?
---
A:
You can use PDFNet to convert multipage tiffs to PDF.
pdftron.PDF.Image.Create(filename) method will embed only the first
image in the TIFF file (see AddImage sample project
http://www.pdftron.com/net/samplecode.html#AddImage), however you can
use pdftron.PDF.Image.Create(System.Drawing.Bitmap) to import TIFF
images frame by frame.
In this case, Image.SelectActiveFrame() method from DotNet Framework
is useful to select the 'current' image. See
http://msdn2.microsoft.com/en-us/library/ms533838.aspx for more
information and some sample code on how to use this API.
GDI+ does have some limitations when it comes to TIFF support (e.g.
CMYK images etc). As a workaround, you can use any third party TIFF or
image library (such as libtiff - http:// www.libtiff.org/ or freeimage
- http://freeimage.sourceforge.net) that can load TIFF without relying
on GDI+. In this case you would decompress the Tiff file, frame by
frame, in memory and would pass the RAW image data to
pdftron.PDF.Image.Create() method).
The code used to load TIFF using freeimage might be along the
following lines:
// Assuming you are developing in C#:
string img1 = @"C:\image.tif";
Int32 fi =
FreeImageAPI.FreeImage.Load( FreeImageAPI.FREE_IMAGE_FORMAT.FIF_TIFF,
img1,
(int)FreeImageAPI.LoadSaveFlags.TIFF_RGB |
(int)FreeImageAPI.LoadSaveFlags.TIFF_NONE );
UInt32 hmem = FreeImageAPI.FreeImage.OpenMemory(null,0);
//
FreeImageAPI.FreeImage.SaveToMemory(FreeImageAPI.FREE_IMAGE_FORMAT.FIF_TIFF,
fi,hmem,(int)FreeImageAPI.LoadSaveFlags.TIFF_CMYK |
(int)FreeImageAPI.LoadSaveFlags.TIFF_NONE);
System.IntPtr ptr = IntPtr.Zero;
int size_in_bytes = 0;
FreeImage.AcquireMemory(hmem,ref ptr,ref size_in_bytes);
byte[] content = new byte[size_in_bytes];
Marshal.Copy(ptr ,content,0,(content.Length));
FreeImage.CloseMemory(hmem);
FreeImage.Unload(fi);
// And then embed the raw (and compress) image data using PDFNet API.
pdftron.PDF.Image img = Image.Create(doc.GetSDFDoc(),content,
600,900,8,ColorSpace.CreateDeviceRGB() ,0);
Element element_new = f.CreateImage(img, new Matrix2D(143, 0, 0, 215,
215, 387)); writer.WritePlacedElement(element_new);
writer.End();
reader.End();