Reputation: 57
I am using the following code to convert multiple multi page tif files into pdf.
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.OutputStream;
import org.apache.log4j.Logger;
import com.itextpdf.text.BaseColor;
import com.itextpdf.text.Document;
import com.itextpdf.text.Element;
import com.itextpdf.text.Image;
import com.itextpdf.text.pdf.PdfPCell;
import com.itextpdf.text.pdf.PdfPTable;
import com.itextpdf.text.pdf.PdfWriter;
public class TifToPdf {
public static void main(String args[])
{
String src = "/input/";//path of the folder containing multiple tif files where each tif has multiple pages"
File folder = new File(src);
try
{
// Creating a new pdf
OutputStream file = new FileOutputStream(new File("output.pdf"));
//Adding images in PDF
Document document = new Document();
PdfWriter writer = PdfWriter.getInstance(document, file);
document.open();
PdfPTable table = new PdfPTable(1);
table.setWidthPercentage(100); //Width 100%
table.setSpacingBefore(10f); //Space before table
table.setSpacingAfter(10f); //Space after table
//Set Column widths
float[] columnWidths = {10f};
table.setWidths(columnWidths);
for (final File fileEntry : folder.listFiles())
{
Image image = Image.getInstance(src + "/" + fileEntry.getName());
PdfPCell cell1 = new PdfPCell(image,true);
cell1.setBorderColor(BaseColor.WHITE);
cell1.setPaddingBottom(100);
cell1.setHorizontalAlignment(Element.ALIGN_CENTER);
cell1.setVerticalAlignment(Element.ALIGN_MIDDLE);
table.addCell(cell1);
}
document.add(table);
document.close();
writer.close();
} catch(FileNotFoundException ex){
System.out.println("Error in locating folder in local to import files "+ex.getMessage());
}catch (Exception e){
System.out.println("Error in merging tiff files to pdfs "+e.getMessage());
}
}
}
The "output.pdf" contains all the tif files merged. But only the first page of the each tif file is merged into pdf. The remaining pages are ignored. For example if the "/input/" contains 1.tif,2.tif,3.tif and 1.tif contains 3 pages, 2.tif contains 2 pages, 3.tif conatins 1 page, then only the first pages of all these tif files are merged into pdf. I dont want to use "jai" jar. Kindly let me know what is the issue.
Even I tried the following,
Image images = Image.getInstance(src + "/" + fileEntry.getName());
for (Image image : images) {
PdfPCell cell1 = new PdfPCell(image,true);
cell1.setBorderColor(BaseColor.WHITE);
cell1.setPaddingBottom(100);
cell1.setHorizontalAlignment(Element.ALIGN_CENTER);
cell1.setVerticalAlignment(Element.ALIGN_MIDDLE);
table.addCell(cell1);
}
But I am not able to traverse the image instance.
Upvotes: 0
Views: 3451
Reputation: 77606
A TIFF image consists of pages, and you only look at the "first page". You need to loop over all the pages. See the example that was written for "iText in Action - Second edition": PagedImages
public static void addTif(Document document, String path)
throws DocumentException, IOException {
RandomAccessFileOrArray ra = new RandomAccessFileOrArray(path);
int n = TiffImage.getNumberOfPages(ra);
Image img;
for (int i = 1; i <= n; i++) {
img = TiffImage.getTiffImage(ra, i);
img.scaleToFit(523, 350);
document.add(img);
}
}
I see that you are still using iText 5. You might want to switch to iText 7 because iText 5 has gone in "maintenance mode" (which means: only bug fixes, no new development). For iText 7, the code is different. See chapter 3 of the tutorial:
IRandomAccessSource ras3 =
new RandomAccessSourceFactory().createSource(url3);
RandomAccessFileOrArray raf3 = new RandomAccessFileOrArray(ras3);
int pages3 = TiffImageData.getNumberOfPages(raf3);
for (int i = 1; i <= pages3; i++) {
img = new Image(
ImageDataFactory.createTiff(url3, true, i, true));
document.add(img);
}
document.close();
Upvotes: 2