当前位置:编程学习 > JAVA >>

PDF转tiff图片格式为什么那么大,有没有解决方法

        File file = new File("e://1.pdf");  
        RandomAccessFile raf = new RandomAccessFile(file, "r");  
        FileChannel channel = raf.getChannel();  
        ByteBuffer buf = channel.map(FileChannel.MapMode.READ_ONLY, 0, channel  
                .size());  
        PDFFile pdffile = new PDFFile(buf);  
  
        System.out.println("页数: " + pdffile.getNumPages());  
  
        for (int i = 1; i <= pdffile.getNumPages(); i++) {  
            // draw the first page to an image   
            PDFPage page = pdffile.getPage(i);  
  
            // get the width and height for the doc at the default zoom   
            Rectangle rect = new Rectangle(0, 0, (int) page.getBBox()  
                    .getWidth(), (int) page.getBBox().getHeight());  
  
            // generate the image   
            Image img = page.getImage(rect.width*6, rect.height*6, // width &   
             // height   
                    rect, // clip rect   
                    null, // null for the ImageObserver   
                    true, // fill background with white   
                    true // block until drawing is done   
                    );  
            RenderedOp src = JAI.create("AWTImage", img);
OutputStream os = new FileOutputStream("e://1333333.tiff");
TIFFEncodeParam param = new TIFFEncodeParam();
//param.setCompression(TIFFEncodeParam.COMPRESSION_GROUP4); 
ImageEncoder encoder1 = ImageCodec.createImageEncoder("TIFF", os, param);
encoder1.encode(src);
os.close();
img.flush();
os.flush();


以上为PDF转tiff的源代码 原始PDF只有300多k 转成tiff后会变成60M+ 造成打印机打印图片效率低 
注:客户打印机需打印tiff格式文件

PDF是根据word生成的 tiff是根据PDF生成的 

求大神提供解决方案,不胜感激~~~ tiff PDF转tiff --------------------编程问答-------------------- 帖子,你怎么了帖子,你不能沉了,等着你救命呢..... --------------------编程问答-------------------- 尝试压缩一下图片。。
我这有一个压缩图片的程序,是那种质量基本不会变,但是大小就变小很多的。。
但是不知道是否适用于tiff的原图图片。。 --------------------编程问答--------------------
引用 2 楼 AA5279AA 的回复:
尝试压缩一下图片。。
我这有一个压缩图片的程序,是那种质量基本不会变,但是大小就变小很多的。。
但是不知道是否适用于tiff的原图图片。。
求压缩程序...我这个是酱紫的,不能压缩tiff文件

public static String zipImageFile(String oldFile, int width, int height,   
            float quality, String smallIcon) {   
             if (oldFile == null) {   
            return null;   
        }   
        String newImage = null;   
        try {   
            /** 对服务器上的临时文件进行处理 */  
         File file = new File(oldFile);
            Image srcFile = ImageIO.read(file);
            int w = srcFile.getWidth(null);
            int h = srcFile.getHeight(null);
            //width = w/4;
            //height = h/4;
            
            /** 宽,高设定 */  
            BufferedImage tag = new BufferedImage(w, h,   
                    BufferedImage.TYPE_INT_RGB);   
            tag.getGraphics().drawImage(srcFile, 0, 0, w, h, null);   
            String filePrex = oldFile.substring(0, oldFile.indexOf('.'));   
            /** 压缩后的文件名 */  
            newImage = filePrex + smallIcon   
                    + oldFile.substring(filePrex.length());   
  
            /** 压缩之后临时存放位置 */  
            FileOutputStream out = new FileOutputStream(newImage);   
  
            JPEGImageEncoder encoder = JPEGCodec.createJPEGEncoder(out);   
            JPEGEncodeParam jep = JPEGCodec.getDefaultJPEGEncodeParam(tag);   
            /** 压缩质量 */  
            jep.setQuality(quality, true);   
            encoder.encode(tag, jep);   
            out.close();   
            } catch (FileNotFoundException e) {   
            e.printStackTrace();   
        } catch (IOException e) {   
            e.printStackTrace();   
        }   
        return newImage;   
    }   
--------------------编程问答--------------------
引用 2 楼 AA5279AA 的回复:
尝试压缩一下图片。。
我这有一个压缩图片的程序,是那种质量基本不会变,但是大小就变小很多的。。
但是不知道是否适用于tiff的原图图片。。

再问一下,有木有做过PDF无损转换成jpg文件.. 求指导....
以下是我现有的PDF转tiff代码 其中包括PDF转jpg代码(打算将PDF无损转成Jpg之后再由jpg转成tiff 但是转换后jpg图片失真) 所以求大神指教无损转jpg 或是PDF直接转tiff压缩大小的方法。。。
public static void setup() throws IOException {   
        // load a pdf from a byte buffer   
        File file = new File("e://1.pdf");  
        RandomAccessFile raf = new RandomAccessFile(file, "r");  
        FileChannel channel = raf.getChannel();  
        ByteBuffer buf = channel.map(FileChannel.MapMode.READ_ONLY, 0, channel  
                .size());  
        PDFFile pdffile = new PDFFile(buf);  
        System.out.println("页数: " + pdffile.getNumPages());   
        for (int i = 1; i <= pdffile.getNumPages(); i++) {  
            // draw the first page to an image
            PDFPage page = pdffile.getPage(i);  
            /**
            BufferedImage image = page.convertToImage();
    ImageIO.write(image, "bmp", new File("e:/333.bmp"));
    */
            // get the width and height for the doc at the default zoom   
            Rectangle rect = new Rectangle(0, 0, (int) page.getBBox()  
                    .getWidth(), (int) page.getBBox().getHeight());  
  
            // generate the image   
            Image img = page.getImage(rect.width*6, rect.height*6, // width &   
             // height   
                    rect, // clip rect   
                    null, // null for the ImageObserver   
                    true, // fill background with white   
                    true // block until drawing is done   
                    ); 
            /**PDF直接转tiff文件 开始*/
            RenderedOp src = JAI.create("AWTImage", img);
OutputStream os = new FileOutputStream("e://1333333.tiff");
TIFFEncodeParam param = new TIFFEncodeParam();
//param.setCompression(TIFFEncodeParam.COMPRESSION_GROUP4); 
ImageEncoder encoder1 = ImageCodec.createImageEncoder("TIFF", os, param);
encoder1.encode(src);
os.close();
img.flush();
os.flush();
/**PDF直接转tiff文件 结束*/

/**PDF转jpg文件 开始**/
            BufferedImage tag = new BufferedImage(rect.width, rect.height,  
                    BufferedImage.TYPE_INT_RGB);  
            tag.getGraphics().drawImage(img, 0, 0, rect.width, rect.height,  
                    null);  
            FileOutputStream out = new FileOutputStream(  
                    "e://"  
                       + i+System.currentTimeMillis() + ".jpg"); // 输出到文件流   
            JPEGImageEncoder encoder = JPEGCodec.createJPEGEncoder(out); 
            JPEGEncodeParam jep = JPEGCodec.getDefaultJPEGEncodeParam(tag);   
            /** 压缩质量 */  
            jep.setQuality((float)(100 / 100.0),true);   
            encoder.encode(tag, jep);
            //encoder.encode(tag); // JPEG编码   
            out.close();  
            /**PDF转tiff文件 结束**/
        }   
    }  
--------------------编程问答-------------------- 这是我压缩图的程序,jpg的没问题,原图格式的没试过。。
tiff转换成jpg或者pdf直接转换成jpg的话可以用直接用压图小工具,那些工具都是可以批量压图的,但是缺点好像是一次只能压缩一个文件夹下的。。


// 图片处理
public boolean compressPic(String inputDir, String outputDir,
String inputFileName, String outputFileName, 
boolean gp,long filesize) {
/*
 * System.out.println("输入路径为:"+inputDir);
 * System.out.println("输入名为:"+inputFileName);
 * System.out.println("输出路径为:"+outputDir);
 * System.out.println("输出名为:"+outputFileName);
 * System.out.println("宽为:"+width+",高为"+height);
 */
boolean flag=false;
try {
// 获得源文件
file = new File(inputDir + inputFileName);
if (!file.exists()) {
System.out.println("图片不存在");
return false;
}
//System.out.println("图片地址:" + inputDir + inputFileName);
Image img = ImageIO.read(file);
//如果700*700的,那么修改width和height
//返回true的话进行
if(img.getWidth(null) == -1){
return false;
}else if (ifsize700(img)&&filesize>500) {
// 设置图片长宽
setWidthAndHeight(779, 779);
}else if(ifsize800(img)){
setWidthAndHeight(890, 890);
}else{
System.out.println("尺寸不正确");
return false;
}

// 是否是等比缩放 标记
this.proportion = gp;


// file = new File("D://test/1/1.jpg");
// D://test/1/1.jpg
// d:\test\11 (2)-300k.jpg
int newWidth;
int newHeight;
// 判断是否是等比缩放
if (this.proportion == true) {
// 为等比缩放计算输出的图片宽度及高度
double rate1 = ((double) img.getWidth(null))
/ (double) outputWidth + 0.1;
double rate2 = ((double) img.getHeight(null))
/ (double) outputHeight + 0.1;
// 根据缩放比率大的进行缩放控制
double rate = rate1 > rate2 ? rate1 : rate2;
newWidth = (int) (((double) img.getWidth(null)) / rate);
newHeight = (int) (((double) img.getHeight(null)) / rate);
} else {
newWidth = outputWidth; // 输出的图片宽度
newHeight = outputHeight; // 输出的图片高度
}
BufferedImage tag = new BufferedImage((int) newWidth,
(int) newHeight, BufferedImage.TYPE_INT_RGB);
/*
 * Image.SCALE_SMOOTH 的缩略算法 生成缩略图片的平滑度的 优先级比速度高 生成的图片质量比较好 但速度慢
 */

tag.getGraphics().drawImage(
img.getScaledInstance(newWidth, newHeight,
Image.SCALE_SMOOTH), 0, 0, null);
FileOutputStream out = new FileOutputStream(outputDir
+ outputFileName);
// JPEGImageEncoder可适用于其他图片类型的转换
JPEGImageEncoder encoder = JPEGCodec.createJPEGEncoder(out);
encoder.encode(tag);
out.close();
} catch (IOException ex) {
ex.printStackTrace();
}
return true;
}
--------------------编程问答-------------------- pdf转成jpg的,这段不是我写的。借用别人的。楼主可以试试

  public   static   void  setup()  throws  IOException {  
  
        // load a pdf from a byte buffer   
        File file = new  File(  
                "c://xxxxx.pdf" );  
        RandomAccessFile raf = new  RandomAccessFile(file,  "r" );  
        FileChannel channel = raf.getChannel();  
        ByteBuffer buf = channel.map(FileChannel.MapMode.READ_ONLY, 0 , channel  
                .size());  
        PDFFile pdffile = new  PDFFile(buf);  
  
        System.out.println("页数: "  + pdffile.getNumPages());  
  
        for  ( int  i =  1 ; i <= pdffile.getNumPages(); i++) {  
            // draw the first page to an image   
            PDFPage page = pdffile.getPage(i);  
  
            // get the width and height for the doc at the default zoom   
            Rectangle rect = new  Rectangle( 0 ,  0 , ( int ) page.getBBox()  
                    .getWidth(), (int ) page.getBBox().getHeight());  
  
            // generate the image   
            Image img = page.getImage(rect.width, rect.height, // width &   
                                                                // height   
                    rect, // clip rect   
                    null ,  // null for the ImageObserver   
                    true ,  // fill background with white   
                    true   // block until drawing is done   
                    );  
  
            BufferedImage tag = new  BufferedImage(rect.width, rect.height,  
                    BufferedImage.TYPE_INT_RGB);  
            tag.getGraphics().drawImage(img, 0 ,  0 , rect.width, rect.height,  
                    null );  
            FileOutputStream out = new  FileOutputStream(  
                    "c://picture//"   
                            + i + ".jpg" );  // 输出到文件流   
            JPEGImageEncoder encoder = JPEGCodec.createJPEGEncoder(out);  
            encoder.encode(tag); // JPEG编码   
  
            out.close();  
        }  
补充:Java ,  Java EE
CopyRight © 2012 站长网 编程知识问答 www.zzzyk.com All Rights Reserved
部份技术文章来自网络,