java pdf压缩_java - 通过Java用DPI压缩pdf大小[关闭] - 堆栈内存溢出

本文介绍了一种使用iTextPDF库来压缩PDF文件中图像DPI的方法,通过调整图像尺寸并重新保存,实现了PDF大小的减小。代码示例展示了如何遍历PDF对象,找到DCTDECODE过滤器的图像流,然后按特定因子缩放图像并更新其在PDF中的尺寸信息。
摘要由CSDN通过智能技术生成

最后,我找到了使用itextpdf库的最佳解决方案。 我们可以根据该因子减少DPI。

例如:系数= NewDPI / CurrentDPI(FACTOR = 0.5f)

import java.awt.Graphics2D;

import java.awt.geom.AffineTransform;

import java.awt.image.BufferedImage;

import java.io.ByteArrayOutputStream;

import java.io.File;

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 ReduceSize {

public static final String SRC = "/Users/xxxx/Downloads/low/input.pdf";

public static final String DEST = "/Users/xxxx/Downloads/low/output.pdf";

public static final float FACTOR = 0.5f;

public static void main(String[] args) throws DocumentException, IOException {

File file = new File(DEST);

file.getParentFile().mkdirs();

new ReduceSize().manipulatePdf(SRC, DEST);

}

public void manipulatePdf(String src, String dest) throws DocumentException, IOException {

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 (!PdfName.IMAGE.equals(stream.getAsName(PdfName.SUBTYPE)))

continue;

if (!PdfName.DCTDECODE.equals(stream.getAsName(PdfName.FILTER)))

continue;

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);

if (width <= 0 || height <= 0)

continue;

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(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);

}

reader.removeUnusedObjects();

// Save altered PDF

PdfStamper stamper = new PdfStamper(reader, new FileOutputStream(dest));

stamper.setFullCompression();

stamper.close();

reader.close();

}

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值