2015-02-06 275 views
0

我正在使用iText來壓縮現有的pdf。 我使用的示例從iText in Action book(第二版):如何在不調整圖像大小的情況下壓縮PDF?

package part4.chapter16; 

import java.awt.Graphics2D; 
import java.awt.geom.AffineTransform; 
import java.awt.image.BufferedImage; 
import java.io.ByteArrayOutputStream; 
import java.io.FileOutputStream; 
import java.io.IOException; 

import javax.imageio.ImageIO; 

import com.itextpdf.text.DocumentException; 
import com.itextpdf.text.pdf.PRStream; 
import com.itextpdf.text.pdf.PdfName; 
import com.itextpdf.text.pdf.PdfNumber; 
import com.itextpdf.text.pdf.PdfObject; 
import com.itextpdf.text.pdf.PdfReader; 
import com.itextpdf.text.pdf.PdfStamper; 
import com.itextpdf.text.pdf.parser.PdfImageObject; 

public class ResizeImage { 

    /** The resulting PDF file. */ 
    public static String RESULT = "results/part4/chapter16/resized_image.pdf"; 
    /** The multiplication factor for the image. */ 
    public static float FACTOR = 0.5f; 

    /** 
    * Manipulates a PDF file src with the file dest as result 
    * @param src the original PDF 
    * @param dest the resulting PDF 
    * @throws IOException 
    * @throws DocumentException 
    */ 
    public void manipulatePdf(String src, String dest) throws IOException, DocumentException { 
     PdfName key = new PdfName("ITXT_SpecialId"); 
     PdfName value = new PdfName("123456789"); 
     // Read the file 
     PdfReader reader = new PdfReader(SpecialId.RESULT); 
     int n = reader.getXrefSize(); 
     PdfObject object; 
     PRStream stream; 
     // Look for image and manipulate image stream 
     for (int i = 0; i < n; i++) { 
      object = reader.getPdfObject(i); 
      if (object == null || !object.isStream()) 
       continue; 
      stream = (PRStream)object; 
      if (value.equals(stream.get(key))) { 
       PdfImageObject image = new PdfImageObject(stream); 
       BufferedImage bi = image.getBufferedImage(); 
       if (bi == null) continue; 
       int width = (int)(bi.getWidth() * FACTOR); 
       int height = (int)(bi.getHeight() * FACTOR); 
       BufferedImage img = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB); 
       AffineTransform at = AffineTransform.getScaleInstance(FACTOR, FACTOR); 
       Graphics2D g = img.createGraphics(); 
       g.drawRenderedImage(bi, at); 
       ByteArrayOutputStream imgBytes = new ByteArrayOutputStream(); 
       ImageIO.write(img, "JPG", imgBytes); 
       stream.clear(); 
       stream.setData(imgBytes.toByteArray(), false, PRStream.NO_COMPRESSION); 
       stream.put(PdfName.TYPE, PdfName.XOBJECT); 
       stream.put(PdfName.SUBTYPE, PdfName.IMAGE); 
       stream.put(key, value); 
       stream.put(PdfName.FILTER, PdfName.DCTDECODE); 
       stream.put(PdfName.WIDTH, new PdfNumber(width)); 
       stream.put(PdfName.HEIGHT, new PdfNumber(height)); 
       stream.put(PdfName.BITSPERCOMPONENT, new PdfNumber(8)); 
       stream.put(PdfName.COLORSPACE, PdfName.DEVICERGB); 
      } 
     } 
     // Save altered PDF 
     PdfStamper stamper = new PdfStamper(reader, new FileOutputStream(RESULT)); 
     stamper.close(); 
     reader.close(); 
    } 

    /** 
    * Main method. 
    * 
    * @param args no arguments needed 
    * @throws DocumentException 
    * @throws IOException 
    */ 
    public static void main(String[] args) throws IOException, DocumentException { 

     new ResizeImage().manipulatePdf(src, dest); 
    } 
} 

上述代碼減少圖像的基於FACTOR大小。我不想減小尺寸,但改變DPI以減小圖像尺寸。 任何幫助將不勝感激。我是新來的Java。 還有一些其他可用於壓縮PDF的開源工具?

+0

由於圖像中提取我需要一些圖像壓縮技術無需添加回PDF – 2015-02-06 14:30:37

+0

前調整到被應用,這正是你複製的例子/粘貼的是什麼做的。你沒看過你拿這個榜樣的書嗎? – 2015-02-06 14:35:33

回答

3

你從我的書中複製/粘貼了一個例子,但看起來你還沒有讀過這本書,也沒有真正嘗試過這個例子。你說:「我不想減少維度,但改變DPI。」

那麼......這正是我書中的例子!在SpecialID示例中,我創建了一個使用此矩形定義頁面大小的PDF:new Rectangle(400, 300)。這意味着我們有一個頁面可以測量400到300點(請參閱下面的屏幕截圖中的藍色點)。在這個頁面上,我添加了一個500×332像素的JPG(見紅點)。使用以下方法將此圖像縮放到400乘300點:

img.scaleAbsolute(400, 300); 

該圖像需要55332個字節(綠點)。

enter image description here

請注意,我們可以很容易地計算出DPI:所述圖像的寬度爲400分;那是5.555英寸。圖像的高度是300點;那是4.166英寸。寬度的像素數爲500,因此X方向的DPI爲500 x 72/400或90 DPI。高度的像素數爲332,因此DPI爲332 x 72/300或79.68 DPI。

您想通過降低分辨率來縮小JPG的字節數。但是:您希望圖像的大小保持不變:它仍然必須覆蓋400點和300點。

這是究竟是在您的問題中複製/粘貼的ResizeImage示例中做了什麼。 Let't進去看看吧:

enter image description here

的圖像仍然有400 300點(這是你想要的,不是嗎?),但分辨率也大幅度下降:現在的形象是250由166個像素而不是500個×332個像素而不改變其頁面大小!

現在讓我們來計算新的DPI:在X方向上,我們有250 x 72/400.這是45 DPI。在Y方向上,我們有166 x 72/300.這是39.84 DPI。 這就是我們以前的DPI的一半!這是巧合嗎?當然不是!這就是我們使用的FACTOR。 (這都是簡單的數學問題。)

由於降低分辨率,圖像現在只需要5343字節(而不是原始的55332)。您已成功壓縮圖像。

總之:你錯誤地解釋了你正在使用的例子。你說它沒有達到你想要的。我可以證明它確實;-)

從你在這個答案上發表的評論看來,你是混淆的概念,如圖像分辨率,圖像壓縮等。爲了消除混淆,你應該閱讀以下內容問題和答案:

+0

感謝您的解釋。由於圖片的尺寸仍爲400x300,圖片轉換爲250x166,因此圖片看起來像素化。任何方法,如下采樣或改變DPI值,這將避免像素化。 – 2015-02-06 14:41:28

+1

看來你不理解「降低分辨率」的概念。 – 2015-02-06 14:46:37

+0

我明白了你的觀點,但是我需要避免像素化,我沒有在尺寸上大幅度增加。 – 2015-02-06 14:48:33

相關問題