通过Java使用大图像压缩pdf

通过Java使用大图像压缩pdf,第1张

通过Java使用大图像压缩pdf

我使用下面的代码进行概念验证…起作用了:)感谢Bruno为我设定了正确的路径:)

package compressPDF;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 {//public static String RESULT = "results/part4/chapter16/resized_image.pdf";public static float FACTOR = 0.5f;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(src);    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))) {        PdfObject pdfsubtype = stream.get(PdfName.SUBTYPE);        System.out.println(stream.type());        if (pdfsubtype != null && pdfsubtype.toString().equals(PdfName.IMAGE.toString())) { 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.BEST_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(dest));    stamper.close();    reader.close();}public static void main(String[] args) throws IOException, documentException {    //createPdf(RESULT);    new ResizeImage().manipulatePdf("C:/_dev_env_/TEMP/compressPDF/TRPT_135002_1470_20131212_121423.PDF", "C:/_dev_env_/TEMP/compressPDF/compressTest.pdf");}}


欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/zaji/5479106.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-12-12
下一篇 2022-12-12

发表评论

登录后才能评论

评论列表(0条)

保存