JAVA使用pdfbox-tools-2.0.3转换为tif转换

时间:2017-07-04 13:57:53

标签: java pdf pdfbox tiff file-conversion

我正在将pdf文件转换为tif格式。为此,我使用pdfbox-tools
 org.apache.pdfbox的版本为2.0.3。但它需要将内置类TIFFUtil.java和MetaUtil.java修改为public。我们有没有其他方式可以做到而不是触及jar中的类?

static void saveAsMultipageTIFF1(ArrayList<BufferedImage> bimTab, String filename, int dpi) throws IOException {
    Iterator<ImageWriter> writers = ImageIO.getImageWritersByFormatName("tiff");
    ImageWriter imageWriter = writers.next();

    ImageOutputStream ios = ImageIO.createImageOutputStream(new File(filename));
    imageWriter.setOutput(ios);
    imageWriter.prepareWriteSequence(null);
    for (BufferedImage image : bimTab) {
        ImageWriteParam param = imageWriter.getDefaultWriteParam();
        IIOMetadata metadata = imageWriter.getDefaultImageMetadata(new ImageTypeSpecifier(image), param);
        param.setCompressionMode(ImageWriteParam.MODE_EXPLICIT);
        TIFFUtil.setCompressionType(param, image);
        TIFFUtil.updateMetadata(metadata, image, dpi);
        imageWriter.writeToSequence(new IIOImage(image, null, metadata), param);
    }
    imageWriter.endWriteSequence();
    imageWriter.dispose();
    ios.flush();
    ios.close();
}

1 个答案:

答案 0 :(得分:0)

您可以使用以下内容将pdf转换为tiff文件。

import java.awt.image.*;
import java.io.*;
import javax.imageio.ImageIO;
import org.icepdf.core.exceptions.*;
import org.icepdf.core.pobjects.*;
import org.icepdf.core.util.GraphicsRenderingHints;
public class PDFToTIFF {
private static final String FILEPATH = "<Your PDF File path>/yourPDF.pdf";
public static void main(String[] args) {
      Document document = new Document();
      try {
         document.setFile(FILEPATH);
      } catch (PDFException ex) {
         System.out.println("Error parsing PDF document " + ex);
      } catch (PDFSecurityException ex) {
         System.out.println("Error encryption not supported " + ex);
      } catch (FileNotFoundException ex) {
         System.out.println("Error file not found " + ex);
      } catch (IOException ex) {
         System.out.println("Error IOException " + ex);
      }
      float scale = 3.5f;
      float rotation = 0f;
      for (int i = 0; i < document.getNumberOfPages(); i++) {
         BufferedImage image = (BufferedImage) document.getPageImage(
             i, GraphicsRenderingHints.PRINT, Page.BOUNDARY_CROPBOX, rotation, scale);
         RenderedImage rendImage = image;
         try {
            System.out.println(" capturing page " + i);
            File file = new File("Image_" + i + ".tif");
            ImageIO.write(rendImage, "tiff", file);
         } catch (IOException e) {
            e.printStackTrace();
         }
         image.flush();
      }
      document.dispose();
}
}

如果你想设置压缩和更改字节序,那么你可以使用ImageIO扩展插件之一(TwelveMonkeys ImageIO)来做到这一点。