List of usage examples for com.itextpdf.text.pdf PdfWriter setFullCompression
public void setFullCompression() throws DocumentException
From source file:com.github.albfernandez.joinpdf.JoinPdf.java
License:Open Source License
private void setParametersAndHeaders(final PdfWriter writer, final Document document) throws DocumentException { if (this.extraCompression) { writer.setFullCompression(); }/*www.j a v a2 s .c o m*/ if (this.isCrypt() && bouncyCastleLoaded) { int permisos = PdfWriter.ALLOW_PRINTING; writer.setEncryption(null, null, permisos, PdfWriter.ENCRYPTION_AES_128); } if (!StringUtils.isBlank(this.getMetadataAuthor())) { document.addAuthor(this.getMetadataAuthor()); } if (!StringUtils.isBlank(this.getMetadataKeywords())) { document.addKeywords(this.getMetadataKeywords()); } if (!StringUtils.isBlank(this.getMetadataTitle())) { document.addTitle(this.getMetadataTitle()); } if (!StringUtils.isBlank(this.getMetadataSubject())) { document.addSubject(this.getMetadataSubject()); } }
From source file:com.tommontom.pdfsplitter.PdfMerge.java
public static void doMerge(java.util.List<InputStream> list, String[] imageList, String[] listWordExcels, OutputStream outputStream) throws DocumentException, IOException { Document document = new Document(PageSize.LETTER, 0, 0, 0, 0); PdfWriter writer = PdfWriter.getInstance(document, outputStream); writer.setFullCompression(); document.open();/* www .j ava2 s . com*/ PdfContentByte cb = writer.getDirectContent(); Image img; for (InputStream in : list) { PdfReader reader = new PdfReader(in); for (int i = 1; i <= reader.getNumberOfPages(); i++) { document.newPage(); //import the page from source pdf PdfImportedPage page = writer.getImportedPage(reader, i); //add the page to the destination pdf cb.addTemplate(page, 0, 0); } } for (int i = 0; i < imageList.length; i++) { document.newPage(); if (imageList[i] != null) { img = Image.getInstance(String.format("%s", imageList[i])); Rectangle one = new Rectangle(img.getPlainWidth(), img.getPlainHeight()); document.setPageSize(one); if (img.getScaledWidth() > img.getScaledHeight()) { img.rotate(); } if (img.getScaledWidth() > 792 || img.getScaledHeight() > 792) { img.scaleToFit(792, 792); } img.setDpi(150, 150); document.add(img); } } for (int i = 0; i < listWordExcels.length; i++) { if (imageList[i] != null) { File input = new File(listWordExcels[i]); File output = new File(listWordExcels[i] + ".pdf"); String outputS = listWordExcels[i] + ".pdf"; OpenOfficeConnection connection = new SocketOpenOfficeConnection(8100); connection.connect(); DocumentConverter converter = new OpenOfficeDocumentConverter(connection); converter.convert(input, output); PdfReader readerWord = new PdfReader(outputS); PdfImportedPage page = writer.getImportedPage(readerWord, readerWord.getNumberOfPages()); cb.addTemplate(page, 0, 0); } } outputStream.flush(); document.close(); outputStream.close(); }
From source file:net.FilterLogic.imaging.ToPDF.java
License:Apache License
private void writeMultiPagePDF(String fileName) throws Exception { float STD_WIDTH = 620; float STD_HEIGHT = 775; float newWidth = 0; float newHeight = 0; float xPos = 0; float yPos = 0; boolean scaleImage = true; boolean pdfAutoOrientation = true; boolean autoCenter = false; boolean portrait = true; BufferedImage img = null;/* w w w .j av a 2 s .c o m*/ Document pdf; PdfWriter writer; try { // scale image String si = this.documentProperties.getProperty(KEY_SCALE_TO_FIT, KEY_SCALE_TO_FIT_DEFAULT); scaleImage = Boolean.parseBoolean(si); // auto-orientation String ao = this.documentProperties.getProperty(KEY_AUTO_ORIENTATION, KEY_AUTO_ORIENTATION_DEFAULT); pdfAutoOrientation = Boolean.parseBoolean(ao); // auto-center String ac = this.documentProperties.getProperty(KEY_AUTO_CENTER, KEY_AUTO_CENTER_DEFAULT); autoCenter = Boolean.parseBoolean(ac); if (document.size() > 0) img = document.get(0); if (img != null) { // if dpi set, calculate new width/height if (horizontalDPI > 0 && verticalDPI > 0) { float xd = (float) horizontalDPI / 100; float yd = (float) verticalDPI / 100; newWidth = img.getWidth() / xd; newHeight = img.getHeight() / yd; } else { newWidth = img.getWidth(); newHeight = img.getHeight(); } // if image width or height changed, scale if (newWidth != img.getWidth() || newHeight != img.getHeight()) scaleImage = true; // if auto orientation, set portrait or landscape if (pdfAutoOrientation) { if (newWidth >= newHeight) { pdf = new Document(PageSize.LETTER.rotate()); portrait = false; } else { pdf = new Document(PageSize.LETTER); portrait = true; } } else { // else, always portrait pdf = new Document(PageSize.LETTER); portrait = true; } writer = PdfWriter.getInstance(pdf, new FileOutputStream(fileName)); writer.setFullCompression(); pdf.open(); // set document props setDocumentProperties(pdf); int t = 0; float pdfPageWidth = pdf.getPageSize().getWidth(); float pdfPageHeight = pdf.getPageSize().getHeight(); // if new image larger than standard size, override and enable image scaling if (newWidth > pdfPageWidth || newHeight > pdfPageHeight) { scaleImage = true; if (newWidth > pdfPageWidth) newWidth = pdfPageWidth; if (newHeight > pdfPageHeight) newHeight = pdfPageHeight; } // break out each page to single file while (t < totalPages) { PdfContentByte cb = writer.getDirectContent(); com.itextpdf.text.Image pdfImage; if (img != null) { pdfImage = com.itextpdf.text.Image.getInstance(img, null); // calculate center if (autoCenter) { if (portrait) { xPos = (pdfPageWidth - newWidth) / 2; yPos = (pdfPageHeight - newHeight) / 2; } else { //xPos = ((pdfPageHeight * (float)1.60) - newWidth) / 2; xPos = (pdfPageHeight - newWidth) / 2; yPos = (pdfPageWidth - newHeight) / 2; } } else { // if not scaling, set image to top left if (!scaleImage) { xPos = 0; // calculate top left corner yPos = pdfPageWidth - newHeight; } else { xPos = 0; yPos = 0; } } // check if x and y pos >=0 if (xPos < 0) xPos = 0; if (yPos < 0) yPos = 0; if (scaleImage) { if (!portrait) { pdfImage.scaleToFit(newHeight, newWidth); } else { pdfImage.scaleToFit(newWidth, newHeight); } // check is scaled height/width match new width/height // if not, recalculate center if autcenter enabled. if (newWidth != pdfImage.getScaledWidth() || newHeight != pdfImage.getScaledHeight()) { newWidth = pdfImage.getScaledWidth(); newHeight = pdfImage.getScaledHeight(); // calculate center if (autoCenter) { xPos = (pdfPageWidth - newWidth) / 2; yPos = (pdfPageHeight - newHeight) / 2; // check if x and y pos >=0 if (xPos < 0) xPos = 0; if (yPos < 0) yPos = 0; } } pdfImage.setAbsolutePosition(xPos, yPos); } else { pdfImage.setAbsolutePosition(xPos, yPos); } cb.addImage(pdfImage); // inc counter ++t; if (t < totalPages) { img = document.get(t); // if dpi set, calculate new width/height if (horizontalDPI > 0 && verticalDPI > 0) { float xd = (float) horizontalDPI / 100; float yd = (float) verticalDPI / 100; newWidth = img.getWidth() / xd; newHeight = img.getHeight() / yd; } else { newWidth = img.getWidth(); newHeight = img.getHeight(); } // if auto orientation, set portrait or landscape if (pdfAutoOrientation) { if (newWidth >= newHeight) { pdf.setPageSize(PageSize.LETTER.rotate()); portrait = false; } else { pdf.setPageSize(PageSize.LETTER); portrait = true; } } else { // else, always portrait pdf.setPageSize(PageSize.LETTER); portrait = true; } // create new page. must happen after setting page orientation pdf.newPage(); writer.newPage(); // get new pages width/height pdfPageWidth = pdf.getPageSize().getWidth(); pdfPageHeight = pdf.getPageSize().getHeight(); // set width/height to something normal if (newWidth > pdfPageWidth) newWidth = pdfPageWidth; if (newHeight > pdfPageHeight) newHeight = pdfPageHeight; } } } pdf.close(); // add file name to list fileNames = new ArrayList<String>(); fileNames.add(fileName); } } catch (Exception e) { throw new Exception(e); } }
From source file:org.gephi.io.exporter.preview.PDFExporter.java
License:Open Source License
public boolean execute() { Progress.start(progress);// w ww . j a v a2 s.c o m PreviewController controller = Lookup.getDefault().lookup(PreviewController.class); controller.getModel(workspace).getProperties().putValue(PreviewProperty.VISIBILITY_RATIO, 1.0); controller.refreshPreview(workspace); PreviewProperties props = controller.getModel(workspace).getProperties(); Rectangle size = new Rectangle(pageSize); if (landscape) { size = new Rectangle(pageSize.rotate()); } Color col = props.getColorValue(PreviewProperty.BACKGROUND_COLOR); size.setBackgroundColor(new BaseColor(col.getRed(), col.getGreen(), col.getBlue())); Document document = new Document(size); PdfWriter pdfWriter = null; try { pdfWriter = PdfWriter.getInstance(document, stream); pdfWriter.setPdfVersion(PdfWriter.PDF_VERSION_1_5); pdfWriter.setFullCompression(); } catch (DocumentException ex) { Exceptions.printStackTrace(ex); } document.open(); PdfContentByte cb = pdfWriter.getDirectContent(); cb.saveState(); props.putValue(PDFTarget.LANDSCAPE, landscape); props.putValue(PDFTarget.PAGESIZE, size); props.putValue(PDFTarget.MARGIN_TOP, new Float((float) marginTop)); props.putValue(PDFTarget.MARGIN_LEFT, new Float((float) marginLeft)); props.putValue(PDFTarget.MARGIN_BOTTOM, new Float((float) marginBottom)); props.putValue(PDFTarget.MARGIN_RIGHT, new Float((float) marginRight)); props.putValue(PDFTarget.PDF_CONTENT_BYTE, cb); target = (PDFTarget) controller.getRenderTarget(RenderTarget.PDF_TARGET, workspace); if (target instanceof LongTask) { ((LongTask) target).setProgressTicket(progress); } try { controller.render(target, workspace); } catch (Exception e) { throw new RuntimeException(e); } cb.restoreState(); document.close(); Progress.finish(progress); props.putValue(PDFTarget.PDF_CONTENT_BYTE, null); props.putValue(PDFTarget.PAGESIZE, null); return !cancel; }
From source file:org.patientview.radar.service.impl.PdfDocumentDataBuilder.java
License:Open Source License
public byte[] build(DocumentData documentData) { ByteArrayOutputStream outputStream = new ByteArrayOutputStream(); try {//from w w w .j av a 2 s. c om Document document = new Document(PageSize.A4, 0, 0, 20, 20); PdfWriter writer = PdfWriter.getInstance(document, outputStream); writer.setFullCompression(); writer.setLinearPageMode(); document.open(); PdfPTable table = new PdfPTable(documentData.getHeaders().size()); table.setWidthPercentage(90); // create the header cells for (String s : documentData.getHeaders()) { table.addCell(createHeadingCell(s)); } // create the data row cells for (List<String> l : documentData.getRows()) { for (String s : l) { table.addCell(createRowCell(s)); } } document.add(table); document.close(); } catch (Exception e) { LOGGER.error("Could not build pdf " + e.getMessage()); } return outputStream.toByteArray(); }
From source file:pdfcompressor.PDFCompressor.java
public void outputPDF(String pathToOutput) throws FileNotFoundException, DocumentException, BadElementException, IOException { com.itextpdf.text.Image itextImg; Document outDocument = new Document(); outDocument.setMargins(0f, 0f, 0f, 0f); PdfWriter writer = PdfWriter.getInstance(outDocument, new FileOutputStream(pathToOutput)); writer.setFullCompression(); writer.open();//ww w.java2 s . c o m outDocument.open(); int progress = 0; for (java.awt.Image img : getBuffedImg()) { itextImg = Image.getInstance(getImageByteArray(img, compressRate)); itextImg.scaleToFit(595f, 842f); outDocument.add(itextImg); for (ProgressListener listener : saveListener) { listener.haveProgress(++progress, numOfPages); } } outDocument.close(); writer.close(); for (ProgressListener listener : saveListener) { listener.finished(); } }
From source file:pdfcompressor.PDFCompressor.java
public void getFileSize() throws DocumentException, IOException { com.itextpdf.text.Image itextImg; Document outDocument = new Document(); outDocument.setMargins(0f, 0f, 0f, 0f); ByteArrayOutputStream memoryOutput = new ByteArrayOutputStream(); PdfWriter writer = PdfWriter.getInstance(outDocument, memoryOutput); writer.setFullCompression(); writer.open();/*from w w w . j a v a 2 s .co m*/ outDocument.open(); for (java.awt.Image img : getBuffedImg()) { itextImg = Image.getInstance(getImageByteArray(img, compressRate)); itextImg.scaleToFit(595f, 842f); outDocument.add(itextImg); } outDocument.close(); writer.close(); for (ProgressListener listener : sizeEstimateListener) { listener.finished(memoryOutput.toByteArray().length); } }