1

我正在尝试使用 java 将 PDF 转换为 TIFF 文件(以下示例)。

我使用 PDF 到 Tiff 进行传真(ICEPDF Apache 示例),但转换后的 tiff 文件只有 1 dpi(我在生成的 TIFF 文件中几乎看不到它)。

我想做的是使用 jrxml 文件来获取包含我的数据的 PDF,然后将其转换为 TIFF。样本输出:

在此处输入图像描述

是否有任何方法可以更改或设置参考示例的 dpi?

我必须采取任何步骤将 PDF 中的图像转换为 TIFF 才能看到吗?

更新 :

import java.awt.Graphics;
import java.awt.Transparency;
import java.awt.image.BufferedImage;
import java.awt.image.DataBuffer;
import java.awt.image.IndexColorModel;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.StringReader;
import java.util.HashMap;
import java.util.Iterator;

import javax.imageio.IIOImage;
import javax.imageio.ImageIO;
import javax.imageio.ImageTypeSpecifier;
import javax.imageio.ImageWriteParam;
import javax.imageio.ImageWriter;
import javax.imageio.metadata.IIOMetadata;
import javax.imageio.stream.ImageOutputStream;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;

import org.icepdf.core.exceptions.PDFException;
import org.icepdf.core.exceptions.PDFSecurityException;
import org.icepdf.core.pobjects.PDimension;
import org.icepdf.core.pobjects.Page;
import org.icepdf.core.util.GraphicsRenderingHints;
import org.w3c.dom.Document;
import org.xml.sax.InputSource;

import net.sf.jasperreports.engine.JasperCompileManager;
import net.sf.jasperreports.engine.JasperExportManager;
import net.sf.jasperreports.engine.JasperFillManager;
import net.sf.jasperreports.engine.JasperPrint;
import net.sf.jasperreports.engine.JasperReport;
import net.sf.jasperreports.engine.data.JRXmlDataSource;


public class JasperTesting2{

    public static void main(String[] args) throws Exception{


        String sourceFileName = "C:\\Users\\abcd\\Desktop\\bill.jrxml";

        HashMap<String, Object> params = new HashMap<String, Object>();

        String result = "<?xml version='1.0' encoding='utf-8'?> with some data";

        Document doc = convertStringToDocument(result);
        JRXmlDataSource xmlDataSource = new JRXmlDataSource(doc, "/result/data/tax_receipt_list");


        JasperReport jasperReport = JasperCompileManager.compileReport(sourceFileName);
        System.out.println(jasperReport);
        JasperPrint jasperPrint = JasperFillManager.fillReport(jasperReport, new HashMap(), xmlDataSource);
        System.out.println(jasperPrint.getPages().size());
        JasperExportManager.exportReportToPdfFile(jasperPrint, "C:\\Users\\abcd\\Desktop\\bill.pdf");

        converToTIF("C:\\Users\\abcd\\Desktop\\bill.pdf", "C:\\Users\\abcd\\Desktop\\bill.tif");






    }

    private static Document convertStringToDocument(String xmlStr) {
        DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
        DocumentBuilder builder;
        try {
            builder = factory.newDocumentBuilder();
            Document doc = builder.parse(new InputSource(new StringReader(xmlStr)));
            return doc;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }





     private static void converToTIF(String pdfFilePath, String tifFilePath){



         Iterator<ImageWriter> iterator = ImageIO.getImageWritersByFormatName("tiff");
         if(!iterator.hasNext()){
             System.out.println("포맷의 문제가 있습니다.");
         }


         boolean foundCompressionType = false;
         for(String type : iterator.next().getDefaultWriteParam().getCompressionTypes()){   // 이미 compression이 되있는지 확인
             if("CCITT T.6".equals(type)){
                 foundCompressionType = true;
                 break;
             }
         }







         if(!foundCompressionType){
             System.out.println("TIFF ImageIo does not support Group 4 Fax");
         }


         org.icepdf.core.pobjects.Document document = new org.icepdf.core.pobjects.Document();
         try{
             document.setFile(pdfFilePath);
             File pdfFile = new File(pdfFilePath);
             pdfFile.delete();
         }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 handling PDF document"+ex);
         }


         try{

             File file = new File(tifFilePath);
             ImageOutputStream ios = ImageIO.createImageOutputStream(file);
             ImageWriter writer = ImageIO.getImageWritersByFormatName("tif").next();
             writer.setOutput(ios);


             for(int i=0; i<document.getNumberOfPages();i++){
                 final double targetDPI = 200.0;
                 float scale = 1.0f;
                 float rotation = 0f;

                 PDimension size = document.getPageDimension(i, rotation, scale);

                 double dpi = Math.sqrt((size.getWidth()*size.getWidth())+
                         (size.getHeight()*size.getHeight()))/
                         Math.sqrt((8.5*8.5)+(11*11));

                 if(dpi<(targetDPI-0.1)){
                     scale = (float)(targetDPI/dpi);
                     size = document.getPageDimension(i, rotation, scale);
                 }



                 int pageWidth = (int) size.getWidth();
                 int pageHeight = (int) size.getHeight();
                 int[] cmap = new int[] {0xFF000000, 0xFFFFFFFF};
                 IndexColorModel cm = new IndexColorModel(1, cmap.length, cmap, 0, false, Transparency.BITMASK,DataBuffer.TYPE_BYTE);

                 BufferedImage image = new BufferedImage(pageWidth, pageHeight, BufferedImage.TYPE_BYTE_BINARY, cm);

                 Graphics g = image.createGraphics();
                 document.paintPage(i, g, GraphicsRenderingHints.PRINT, Page.BOUNDARY_CROPBOX, rotation, scale);
                 g.dispose();

                 IIOImage img = new IIOImage(image, null, null);
                 ImageWriteParam param = writer.getDefaultWriteParam();
                 param.setCompressionMode(param.MODE_EXPLICIT);
                 param.setCompressionType("CCITT T.6");


                 ImageTypeSpecifier typeSpecifier = ImageTypeSpecifier.createFromBufferedImageType(BufferedImage.TYPE_INT_RGB);
                 IIOMetadata metadata = ImageIO.getImageWritersByFormatName("tif").next().getDefaultImageMetadata(typeSpecifier, param);






                 if(i==0){
                     writer.write(null, img, param);
                 }else{
                     writer.writeInsert(-1, img, param);
                 }

                 image.flush();

             }
             ios.flush();
             ios.close();
             writer.dispose();


         }catch (Exception e){
             System.out.println("Error saving file" + e);
             e.printStackTrace();
         }

     }
}
4

0 回答 0