通过爪哇用DPI压缩PDF大小

寻找一种方法来压缩PDF质量,并在JAVA中更改DPI

例如,我尝试了PDFBox /itext库,但仍然无法实现它。特别是如果当前的PDF DPI较高,我需要设置DPI(我需要降低扫描文档的质量)

请注意,我只看免费和开源库。


Helenr
浏览 163回答 2
2回答

浮云间

最后,我找到了使用itextpdf库的最佳解决方案。我们可以根据因子减少 DPI。例如:因子 = 新数字像素/当前数字像素(系数 = 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 {&nbsp; &nbsp; public static final String SRC = "/Users/xxxx/Downloads/low/input.pdf";&nbsp; &nbsp; public static final String DEST = "/Users/xxxx/Downloads/low/output.pdf";&nbsp; &nbsp; public static final float FACTOR = 0.5f;&nbsp; &nbsp; public static void main(String[] args) throws DocumentException, IOException {&nbsp; &nbsp; &nbsp; &nbsp; File file = new File(DEST);&nbsp; &nbsp; &nbsp; &nbsp; file.getParentFile().mkdirs();&nbsp; &nbsp; &nbsp; &nbsp; new ReduceSize().manipulatePdf(SRC, DEST);&nbsp; &nbsp; }&nbsp; &nbsp; public void manipulatePdf(String src, String dest) throws DocumentException, IOException {&nbsp; &nbsp; &nbsp; &nbsp; PdfReader reader = new PdfReader(src);&nbsp; &nbsp; &nbsp; &nbsp; int n = reader.getXrefSize();&nbsp; &nbsp; &nbsp; &nbsp; PdfObject object;&nbsp; &nbsp; &nbsp; &nbsp; PRStream stream;&nbsp; &nbsp; &nbsp; &nbsp; // Look for image and manipulate image stream&nbsp; &nbsp; &nbsp; &nbsp; for (int i = 0; i < n; i++) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; object = reader.getPdfObject(i);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if (object == null || !object.isStream())&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; continue;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; stream = (PRStream)object;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if (!PdfName.IMAGE.equals(stream.getAsName(PdfName.SUBTYPE)))&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; continue;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if (!PdfName.DCTDECODE.equals(stream.getAsName(PdfName.FILTER)))&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; continue;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; PdfImageObject image = new PdfImageObject(stream);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; BufferedImage bi = image.getBufferedImage();&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if (bi == null)&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; continue;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; int width = (int)(bi.getWidth() * FACTOR);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; int height = (int)(bi.getHeight() * FACTOR);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if (width <= 0 || height <= 0)&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; continue;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; BufferedImage img = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; AffineTransform at = AffineTransform.getScaleInstance(FACTOR, FACTOR);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Graphics2D g = img.createGraphics();&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; g.drawRenderedImage(bi, at);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; ByteArrayOutputStream imgBytes = new ByteArrayOutputStream();&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; ImageIO.write(img, "JPG", imgBytes);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; stream.clear();&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; stream.setData(imgBytes.toByteArray(), false, PRStream.NO_COMPRESSION);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; stream.put(PdfName.TYPE, PdfName.XOBJECT);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; stream.put(PdfName.SUBTYPE, PdfName.IMAGE);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; stream.put(PdfName.FILTER, PdfName.DCTDECODE);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; stream.put(PdfName.WIDTH, new PdfNumber(width));&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; stream.put(PdfName.HEIGHT, new PdfNumber(height));&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; stream.put(PdfName.BITSPERCOMPONENT, new PdfNumber(8));&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; stream.put(PdfName.COLORSPACE, PdfName.DEVICERGB);&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; reader.removeUnusedObjects();&nbsp; &nbsp; &nbsp; &nbsp; // Save altered PDF&nbsp; &nbsp; &nbsp; &nbsp; PdfStamper stamper = new PdfStamper(reader, new FileOutputStream(dest));&nbsp; &nbsp; &nbsp; &nbsp; stamper.setFullCompression();&nbsp; &nbsp; &nbsp; &nbsp; stamper.close();&nbsp; &nbsp; &nbsp; &nbsp; reader.close();&nbsp; &nbsp; }}

蝴蝶不菲

请尝试完全压缩PdfReader reader = new PdfReader ( src) ;&nbsp;PdfStamper stamper = new PdfStamper( reader, new FileOutputStream(dest) ,&nbsp;Pdfwrlter. VERSION 1_5) ;&nbsp;stamper.getWriter().setCompressionLeveI (9);int total = reader . getNumberOfPages() + 1;&nbsp;for (int i = 1; i < total; i++) {&nbsp; &nbsp; &nbsp; reader . setpagecontent (i, reader . getpagecontent (i) ) ;&nbsp;}stamper. setFuIICompression() ;&nbsp;stamper. close ( ) ;&nbsp;
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Java