java 使用 iText 从 TIFF 图像创建 PDF

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/7721447/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-30 21:07:07  来源:igfitidea点击:

Creating PDF from TIFF image using iText

javaitext

提问by romeroqj

I'm currently generating PDF files from TIFF images using iText.

我目前正在使用 iText 从 TIFF 图像生成 PDF 文件。

Basically the procedure is as follows: 1. Read the TIFF file. 2. For each "page" of the TIFF, instantiate an Imageobject and write that to a Documentinstance, which is the PDF file.

基本过程如下: 1. 读取 TIFF 文件。2. 对于 TIFF 的每个“页面”,实例化一个Image对象并将其写入Document实例,即 PDF 文件。

I'm having a hard time understanding how to add those images to the PDF keeping the original resolution.

我很难理解如何将这些图像添加到 PDF 并保持原始分辨率。

I've tried to scale the Imageto the dimensions in pixels of the original image of the TIFF, for instance:

我试图将图像缩放到 TIFF 原始图像的像素尺寸,例如:

// Pixel Dimensions 1728 × 2156 pixels
// Resolution 204 × 196 ppi
RandomAccessFileOrArray tiff = new RandomAccessFileOrArray("/path/to/tiff/file");
Document pdf = new Document(PageSize.LETTER);
Image temp = TiffImage.getTiffImage(tiff, page);
temp.scaleAbsolute(1728f, 2156f);
pdf.add(temp);

I would really appreciate if someone can shed some light on this. Perhaps I'm missing the functionality of the Imageclass methods...

如果有人能对此有所了解,我将不胜感激。也许我缺少Image类方法的功能......

Thanks in advance!

提前致谢!

回答by Saurabh

I think if you scale the image then you can not retain the original resolution (please correct me if I am wrong :)). What you can try doing is to creat a PDF document with different sized pages (if images are of different resolution in the tif image).

我认为如果缩放图像,则无法保留原始分辨率(如果我错了,请纠正我:))。您可以尝试做的是创建一个具有不同大小页面的 PDF 文档(如果图像在 tif 图像中具有不同的分辨率)。

Try the following code. It sets the size of PDF page equal to that of image file and then create that PDF page. the PDF page size varies according to the image size so the resolution is maintained :)

试试下面的代码。它将 PDF 页面的大小设置为与图像文件的大小相等,然后创建该 PDF 页面。PDF 页面大小因图像大小而异,因此分辨率保持不变:)

import java.io.FileOutputStream;
import java.io.IOException;

import com.itextpdf.text.Document;
import com.itextpdf.text.DocumentException;
import com.itextpdf.text.Image;
import com.itextpdf.text.PageSize;
import com.itextpdf.text.Paragraph;
import com.itextpdf.text.Rectangle;
import com.itextpdf.text.pdf.PdfWriter;
import com.itextpdf.text.pdf.RandomAccessFileOrArray;
import com.itextpdf.text.pdf.codec.TiffImage;

public class Tiff2Pdf {

    /**
     * @param args
     * @throws DocumentException
     * @throws IOException
     */
    public static void main(String[] args) throws DocumentException,
            IOException {

        String imgeFilename = "/home/saurabh/Downloads/image.tif";

        Document document = new Document();
        PdfWriter writer = PdfWriter.getInstance(
                document,
                new FileOutputStream("/home/saurabh/Desktop/out"
                        + Math.random() + ".pdf"));
        writer.setStrictImageSequence(true);
        document.open();

        document.add(new Paragraph("Multipages tiff file"));
        Image image;
        RandomAccessFileOrArray ra = new RandomAccessFileOrArray(imgeFilename);
        int pages = TiffImage.getNumberOfPages(ra);
        for (int i = 1; i <= pages; i++) {
            image = TiffImage.getTiffImage(ra, i);
            Rectangle pageSize = new Rectangle(image.getWidth(),
                    image.getHeight());
            document.setPageSize(pageSize);
            document.add(image);
            document.newPage();
        }

        document.close();

    }

}

回答by try67

I've found that this line doesn't work well:

我发现这条线不能很好地工作:

document.setPageSize(pageSize);

If your TIFF files only contain one image then you're better off using this instead:

如果您的 TIFF 文件只包含一张图片,那么您最好使用它:

RandomAccessFileOrArray ra = new RandomAccessFileOrArray(imageFilePath);
Image image = TiffImage.getTiffImage(ra, 1);
Rectangle pageSize = new Rectangle(image.getWidth(), image.getHeight());

Document document = new Document(pageSize);
PdfWriter writer = PdfWriter.getInstance(document,  new FileOutputStream(outputFileName));
writer.setStrictImageSequence(true);
document.open();
document.add(image);
document.newPage();

document.close();

This will result in a page size that fits the image size exactly, so no scaling is required.

这将导致页面大小完全适合图像大小,因此不需要缩放。

回答by PbxMan

Another example non-deprecatedup to iText 5.5 with the first page issue fixed. I'm using 5.5.11 Itext.

另一个示例在 iText 5.5 之前未弃用,第一页问题已修复。我正在使用 5.5.11 Itext。

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