List of usage examples for com.itextpdf.text.pdf PdfStamper setFullCompression
public void setFullCompression() throws DocumentException
From source file:watermark.java
public static void main(String[] args) { try {//from ww w . j av a2s . co m PdfReader reader = new PdfReader(args[0]); PdfStamper stamp = new PdfStamper(reader, new FileOutputStream(args[1])); stamp.getWriter().setCompressionLevel(9); // without this code it'll 3 times bigger int bottom_margin = Integer.parseInt(args[2]); String lic = String.format(LIC, args[3]); BaseFont bf = BaseFont.createFont(FONTFILE, BaseFont.IDENTITY_H, BaseFont.EMBEDDED); Chunk c = new Chunk(lic, new Font(bf, 7, Font.NORMAL, TCOLOR)); c.setTextRenderMode(PdfContentByte.TEXT_RENDER_MODE_FILL_STROKE, 1 / 800f, null); Phrase phrase = new Phrase(c); int pages = reader.getNumberOfPages(); int xpos = 90; for (int i = 1; i <= pages; i++) { if (i % 2 == 0) xpos = 160; else xpos = 90; PdfContentByte under = stamp.getUnderContent(i); ColumnText.showTextAligned(under, Element.ALIGN_LEFT, phrase, xpos, bottom_margin, -0); } stamp.setFullCompression(); // without this code it'll 3 times bigger stamp.close(); } catch (Exception e) { e.printStackTrace(); } }
From source file:org.h819.commons.file.MyPDFUtils.java
/** * ? pdf ??// w w w . j av a 2 s. c o m * * @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 ? pdf//from w w w .j av a2 s . c o m * * @param srcPdf the original PDF * @param destPdf the resulting PDF * @param imageFactor The multiplication factor for the image (? imageFacto =0.5f) * @throws IOException * @throws DocumentException */ public static void compressPdf(File srcPdf, File destPdf, float imageFactor) throws IOException, DocumentException { PdfReader reader = new PdfReader(srcPdf.getAbsolutePath()); 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() * imageFactor); int height = (int) (bi.getHeight() * imageFactor); if (width <= 0 || height <= 0) continue; BufferedImage img = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB); AffineTransform at = AffineTransform.getScaleInstance(imageFactor, imageFactor); 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(destPdf.getAbsolutePath())); stamper.setFullCompression(); stamper.close(); reader.close(); }