使用iText从TIFF图像创建PDF

使用iText从TIFF图像创建PDF

问题描述:

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

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

基本上,步骤如下:
1.读取TIFF文件。
2.对于TIFF的每个页面,实例化 图像 对象并将其写入 文档 instance,这是PDF文件。

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

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

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

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

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

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

如果有人能对此有所了解,我真的很感激。也许我错过了 Image 类方法的功能......

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

提前致谢!

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

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页面的大小设置为等于图像文件的大小,然后创建该PDF页面。 PDF页面大小因图像大小而异,因此保持分辨率:)

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 :)

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

    }

}