List of usage examples for com.itextpdf.text.pdf PdfReader setPageContent
public void setPageContent(final int pageNum, final byte content[])
From source file:org.h819.commons.file.MyPDFUtils.java
/** * ? pdf ??//from ww w .j a v a 2 s . com * * @param srcPdfFile the original PDF * @param descPdfFile the resulting PDF * @throws java.io.IOException * @throws DocumentException */ public static void compressPdf(File srcPdfFile, File descPdfFile) throws IOException, DocumentException { if (srcPdfFile == null || !srcPdfFile.exists()) throw new IOException("src pdf file '" + srcPdfFile + "' does not exsit."); PdfReader reader = getPdfReader(srcPdfFile); PdfStamper stamper = new PdfStamper(reader, new FileOutputStream(descPdfFile.getAbsoluteFile()), PdfWriter.VERSION_1_7); stamper.getWriter().setCompressionLevel(9); int total = reader.getNumberOfPages() + 1; for (int i = 1; i < total; i++) { reader.setPageContent(i, reader.getPageContent(i)); } stamper.setFullCompression(); stamper.close(); reader.close(); }
From source file:org.h819.commons.file.MyPDFUtils.java
/** * ?pdf//from ww w .ja va2 s . c om * * @param srcPdfFile the original PDF * @param descPdfFile the resulting PDF * @throws java.io.IOException * @throws DocumentException */ public static void decompressPdf(File srcPdfFile, File descPdfFile) throws IOException, DocumentException { if (srcPdfFile == null || !srcPdfFile.exists()) throw new IOException("src pdf file '" + srcPdfFile + "' does not exsit."); PdfReader reader = getPdfReader(srcPdfFile); PdfStamper stamper = new PdfStamper(reader, new FileOutputStream(descPdfFile.getAbsoluteFile())); stamper.getWriter().setCompressionLevel(PdfStream.NO_COMPRESSION); int total = reader.getNumberOfPages() + 1; for (int i = 1; i < total; i++) { reader.setPageContent(i, reader.getPageContent(i)); } stamper.close(); reader.close(); }
From source file:org.sejda.impl.itext5.component.PdfStamperHandler.java
License:Open Source License
/** * Enables compression if compress is true * /*from w w w .j a va2 s . c o m*/ * @param compress * @throws TaskException */ public void setCompression(boolean compress, PdfReader reader) throws TaskException { if (compress) { try { stamper.getWriter().setCompressionLevel(PdfStream.BEST_COMPRESSION); int total = reader.getNumberOfPages() + 1; for (int i = 1; i < total; i++) { reader.setPageContent(i, reader.getPageContent(i)); } stamper.setFullCompression(); } catch (DocumentException de) { throw new TaskException("Unable to set compression on stamper", de); } catch (IOException e) { throw new TaskException("Unable to set compression on stamper", e); } } }