使用PDFbox将pdf转换为tiff时,Tiff图像文件为空白

时间:2016-01-07 19:56:05

标签: java pdfbox javax.imageio

当我运行下面显示的代码并用于ImageIO.getImageWritersBySuffix("tiff")而不是jpg时,会创建tiff文件,但它们是空白的0字节。

import java.io.*;
import org.apache.pdfbox.pdmodel.*;
import org.apache.pdfbox.pdmodel.PDDocument;
import java.awt.image.BufferedImage;
import java.io.File;
import java.util.Iterator;
import java.util.List;
import javax.imageio.IIOImage;
import javax.imageio.ImageIO;
import javax.imageio.ImageWriteParam;
import javax.imageio.ImageWriter;
import javax.imageio.stream.ImageOutputStream;

public class Converter {

    /**
     * @param args
     */
    public static void main(String[] args) {
        PDDocument pdf;
        BufferedWriter wr;
        try {
            File input = new File("C:\\test.pdf");
            pdf = PDDocument.load(input);
            List<PDPage> pages = pdf.getDocumentCatalog().getAllPages();

            for (int i = 0; i < pdf.getNumberOfPages(); i++) {
                ImageOutputStream imgOutStream = null;
                ImageWriter imageWriter = null;

                try {
                    PDPage page = (PDPage) pages.get(i);
                    BufferedImage image = page.convertToImage();
                    String fileName = "C:\\TIFF" + (i + 1) + ".tif";
                    imgOutStream = ImageIO.createImageOutputStream(new File(fileName));

                    boolean foundWriter = false;
                    // Iterator<ImageWriter> writerIter =
                    ImageIO.getImageWritersByFormatName("jpg");
                    Iterator<ImageWriter> writerIter = ImageIO.getImageWritersBySuffix("tiff");

                    while (writerIter.hasNext() && writerIter != null) {
                        try {
                            imageWriter = (ImageWriter) writerIter.next();

                            ImageWriteParam writerParams = imageWriter.getDefaultWriteParam();
                            if (writerParams.canWriteCompressed()) {
                                writerParams.setCompressionMode(ImageWriteParam.MODE_EXPLICIT);
                                writerParams.setCompressionQuality(1.0f);
                            }

                            imageWriter.setOutput(imgOutStream);
                            imageWriter.write(null, new IIOImage(image, null, null), writerParams);
                            foundWriter = true;
                        } catch (Exception ex) {
                            System.out.println("1. exception occured...");
                            ex.printStackTrace();
                        } finally {
                            if (imageWriter != null) {
                                imageWriter.dispose();
                            }
                        }

                        if (!foundWriter) {
                            System.out.println("Image writer not found");
                        }

                    }

                } catch (Exception ex) {
                    System.out.println("2. exception occured...");
                    ex.printStackTrace();
                }

            }

            if (pdf != null) {
                pdf.close();
            }

            // close to flush the stream.
            // wr.close();

        } catch (Exception e) {
            e.printStackTrace();
        }
    }

}

1 个答案:

答案 0 :(得分:0)

为什么要烦恼复杂的代码和棘手的问题?请改用:

ImageIOUtil.writeImage(image, fileName, 72);

这将创建一个72 dpi的TIFF文件(这是调用convertToImage()时获得的分辨率)。 fileName必须以“.tif”或“.tiff”结尾。

重要提示:您需要使用jai_imageio.jar库。