iText7:使用iText从TIFF多页图像创建PDF

时间:2018-03-21 19:19:53

标签: tiff itext7 multipage

我正在尝试使用iText 7.1.1将TIFF图像转换为包含多个页面的PDF文件。感谢那些让我开始阅读本文Create PDF from TIFF image using iText的人。但是,它是iText 5.5.x,我无法在iText 7中复制它。

我确实找到TiffImageData.getNumberOfPages(raf)来替换int pages = TiffImage.getNumberOfPages(rafa)

但是,我无法替换iText7中的TiffImage.getTiffImage(rafa, i)。我需要使用new Image(ImageDataFactory.createTiff(...))吗?感谢任何建议。

iText 5.5.x代码

import java.io.FileOutputStream;
import java.io.RandomAccessFile;
import java.nio.channels.FileChannel;
import com.itextpdf.text.Document;
import com.itextpdf.text.Image;
import com.itextpdf.text.Rectangle;
import com.itextpdf.text.io.FileChannelRandomAccessSource;
import com.itextpdf.text.pdf.PdfWriter;
import com.itextpdf.text.pdf.RandomAccessFileOrArray;
import com.itextpdf.text.pdf.codec.TiffImage;

public class Test1 {
    public static void main(String[] args) throws Exception {
    RandomAccessFile aFile = new RandomAccessFile("/myfolder/origin.tif", "r");
    FileChannel inChannel = aFile.getChannel();
    FileChannelRandomAccessSource fcra =  new FileChannelRandomAccessSource(inChannel);
    Document document = new Document();
    PdfWriter.getInstance(document,  new FileOutputStream("/myfolder/destination.pdf"));
    document.open();              
    RandomAccessFileOrArray rafa = new RandomAccessFileOrArray(fcra);
    int pages = TiffImage.getNumberOfPages(rafa);
    Image image;
    for (int i = 1; i <= pages; i++) {            
        image = TiffImage.getTiffImage(rafa, i);
        Rectangle pageSize = new Rectangle(image.getWidth(), image.getHeight());
        document.setPageSize(pageSize);
        document.newPage();
        document.add(image);
    }
    document.close();
    aFile.close();            
}

2 个答案:

答案 0 :(得分:2)

  

我是否需要使用新的Image(ImageDataFactory.createTiff(...))

你想要这个:ImageDataFactory.createTiff(bytes, recoverFromImageError, page, direct)

然后你会打开一个新的PDF,循环浏览TIFF页面并:

  1. 获取TIFF图片大小
  2. 在PDF中创建与TIFF页面大小相匹配的新页面
  3. 将TIFF图像添加到新的PDF页面
  4. 以下是Bruno Lowagie关于在iText 7中使用TIFF的说明:How to avoid an exception when importing a TIFF file?

    我看到你可能想要完全正常工作的代码。你走了:

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

    有些人可能会建议您使用高级API来更干净地实现这一点,但这应该足以满足您的问题。

答案 1 :(得分:1)

与上面相同,但在vb.net中。 它将多页TIFF转换为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