How to reduce PDF file size programmatically in Ja

2020-08-18 05:51发布

Document document = new Document(reader.getPageSizeWithRotation(1));
PdfCopy writer = new PdfCopy(document, new FileOutputStream(outFile));
document.open();
PdfImportedPage page = writer.getImportedPage(reader, ++i);
writer.setFullCompression();
writer.addPage(page);
document.close();
writer.close();

I am using iText to split and merger the PDF, I need your help to reduce (compress) the output PDF size programmatically. Please let me know the steps to achieve the same.

标签: java pdf itext
4条回答
兄弟一词,经得起流年.
2楼-- · 2020-08-18 06:16

use iText

PdfReader reader = new PdfReader(new FileInputStream("input.pdf"));
PdfStamper stamper = new PdfStamper(reader, new FileOutputStream("output.pdf"));
int total = reader.getNumberOfPages() + 1;
for ( int i=1; i<total; i++) {
   reader.setPageContent(i + 1, reader.getPageContent(i + 1));
}
stamper.setFullCompression();
stamper.close();
查看更多
Luminary・发光体
3楼-- · 2020-08-18 06:19

With writer.setFullCompression() you already compressed file as much as possible. With iText you can't do anything more.

查看更多
走好不送
4楼-- · 2020-08-18 06:25

Also change the PdfCopy to PdfSmartCopy. It will eliminate duplicate streams which have the same hash (md5).

查看更多
迷人小祖宗
5楼-- · 2020-08-18 06:25

You can use ghostscript, invoking the exe with specific parameters for print your pdf with the ghostscript's pdfwriter (example: sDEVICE=pdfwrite -sOutputFile=myfile.pdf). There are several accepted parameters, for compression or quality levels, etc. It may result and optimized and smaller file.

查看更多
登录 后发表回答