如何使用java将多个多页tiff文件合并为单个pdf?

时间:2017-07-18 06:30:20

标签: java pdf itext

我使用以下代码将多个多页面的tif文件转换为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());
        }
    }
}

" output.pdf"包含合并的所有tif文件。但只有每个tif文件的第一页合并为pdf。其余页面将被忽略。例如,如果" / input /"包含1.tif,2.tif,3.tif和1.tif包含3页,2.tif包含2页,3.tif包含1页,然后只有所有这些tif文件的第一页合并为pdf。 我不想使用" jai"罐。请告诉我这是什么问题。

即使我尝试了以下内容,

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);
                     }

但我无法遍历图像实例。

1 个答案:

答案 0 :(得分:2)

TIFF图像由页面组成,您只能查看“第一页”。您需要遍历所有页面。请参阅为“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);
    }
}

我看到你还在使用iText 5.你可能想切换到iText 7,因为iText 5已进入“维护模式”(这意味着:只修复错误,没有新的开发)。对于iText 7,代码是不同的。见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();