iText7: Creating PDF from TIFF multipage image using iText
Do I need to use new Image( ImageDataFactory.createTiff(...))
Yes.
You want this: ImageDataFactory.createTiff(bytes, recoverFromImageError, page, direct)
Then you would open a new PDF, loop through the TIFF pages and:
- Get the TIFF image size
- Create a new page in the PDF matching the TIFF page size
- Add the TIFF image to the new PDF page
Here is a note from Bruno Lowagie on using TIFF with iText 7: How to avoid an exception when importing a TIFF file?
I see you probably want fully working code. Here you go:
import com.itextpdf.io.image.ImageData;
import com.itextpdf.io.image.ImageDataFactory;
import com.itextpdf.io.image.TiffImageData;
import com.itextpdf.io.source.RandomAccessFileOrArray;
import com.itextpdf.io.source.RandomAccessSourceFactory;
import com.itextpdf.kernel.geom.PageSize;
import com.itextpdf.kernel.geom.Rectangle;
import com.itextpdf.kernel.pdf.PdfDocument;
import com.itextpdf.kernel.pdf.PdfPage;
import com.itextpdf.kernel.pdf.PdfWriter;
import com.itextpdf.kernel.pdf.canvas.PdfCanvas;
public class TiffToPdf {
public static void main(String[] args) throws IOException {
Path tiffFile = Paths.get("/myfolder/origin.tiff");
RandomAccessFileOrArray raf = new RandomAccessFileOrArray(new RandomAccessSourceFactory().createBestSource(tiffFile.toString()));
int tiffPages = TiffImageData.getNumberOfPages(raf);
raf.close();
try (PdfDocument output = new PdfDocument(new PdfWriter("/myfolder/destination.pdf"))) {
for (int page = 1; page <= tiffPages; page++) {
ImageData tiffImage = ImageDataFactory.createTiff(tiffFile.toUri().toURL(), true, page, true);
Rectangle tiffPageSize = new Rectangle(tiffImage.getWidth(), tiffImage.getHeight());
PdfPage newPage = output.addNewPage(new PageSize(tiffPageSize));
PdfCanvas canvas = new PdfCanvas(newPage);
canvas.addImage(tiffImage, tiffPageSize, false);
}
}
}
}
Some might suggest you use the high level API to achieve this a little more cleanly but this should be sufficient for your question.
This is the same above but in vb.net. It converts a multipage TIFF to a PDF.
Imports System.IO
Imports iTextSharp.text
Imports iTextSharp.text.pdf
Sub ConvertTIFF2PDF(ByVal inFile As String, ByVal outFile As String)
Dim pdfDoc As PdfDocument = New PdfDocument(New PdfWriter(outFile))
Dim doc As Document = New Document(pdfDoc)
Dim aFile = New RandomAccessFileOrArray(New RandomAccessSourceFactory().CreateBestSource(inFile.ToString))
Dim tiffPages = TiffImageData.GetNumberOfPages(aFile)
Dim uri As System.Uri = New Uri(inFile)
For i As Integer = 1 To tiffPages
Console.WriteLine("tiffPages: " & (i) & " of " & tiffPages.ToString)
Dim tiffImage = ImageDataFactory.CreateTiff(uri, False, i, False)
Dim tiffPageSize = New Geom.Rectangle(tiffImage.GetWidth(), tiffImage.GetHeight())
Dim newPage = pdfDoc.AddNewPage(New PageSize(tiffPageSize))
Dim canvas As PdfCanvas = New PdfCanvas(newPage)
canvas.AddImage(tiffImage, tiffPageSize, False)
Next
doc.Close()
pdfDoc.Close()
aFile.Close()
End Sub