如何使用java将多个多页面tiff文件合并为一个pdf文件?
问题描述:
我正在使用以下代码将多个多页面tif文件转换为pdf。如何使用java将多个多页面tiff文件合并为一个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 conatins 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);
}
但我不能遍历图像实例。
答
TIFF图像由页面组成,您只能看“第一页”。您需要遍历全部的页面。看到已书面的例子“的iText在行动 - 第二版”:我看到你还在使用的iText 5.您可能要因为iText的5“维护已经切换到iText的7 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 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();
+0
谢谢布伦。有用 :) –
您只为每个文件添加一个图像。您需要为文件中的每个图像执行此操作。 –
@ThorbjørnRavn Andersen我每次都尝试遍历图像实例。但我无法做到。请看看我更新的代码。它是正确的方式吗? –