List of usage examples for com.lowagie.text.pdf PdfStamper getUnderContent
public PdfContentByte getUnderContent(int pageNum)
PdfContentByte
to write under the page of the original document. From source file:org.silverpeas.core.util.PdfUtil.java
License:Open Source License
/** * Add a image under or over content on each page of a PDF file. * @param pdfSource the source pdf file, this content is not modified by this method * @param imageToAdd the image file/*from w w w. j av a2 s . c o m*/ * @param pdfDestination the destination pdf file, with the image under or over content * @param isBackground indicates if image is addes under or over the content of the pdf source * file */ private static void addImageOnEachPage(InputStream pdfSource, File imageToAdd, OutputStream pdfDestination, final boolean isBackground) { // Verify given arguments if (imageToAdd == null || !imageToAdd.isFile()) { throw new SilverpeasRuntimeException("The image file doesn't exist"); } else if (!FileUtil.isImage(imageToAdd.getPath())) { throw new SilverpeasRuntimeException("The picture to add is not an image file"); } PdfReader reader = null; try { // Get a reader of PDF content reader = new PdfReader(pdfSource); // Obtain the total number of pages int pdfNbPages = reader.getNumberOfPages(); PdfStamper stamper = new PdfStamper(reader, pdfDestination); // Load the image Image image = Image.getInstance(imageToAdd.getPath()); float imageWidth = image.getWidth(); float imageHeigth = image.getHeight(); // Adding the image on each page of the PDF for (int i = 1; i <= pdfNbPages; i++) { // Page sizes Rectangle rectangle = reader.getPageSize(i); // Compute the scale of the image float scale = Math.min(100, (rectangle.getWidth() / imageWidth * 100)); image.scalePercent(Math.min(scale, (rectangle.getHeight() / imageHeigth * 100))); // Setting the image position for the current page image.setAbsolutePosition(computeImageCenterPosition(rectangle.getWidth(), image.getScaledWidth()), computeImageCenterPosition(rectangle.getHeight(), image.getScaledHeight())); // Adding image PdfContentByte imageContainer = isBackground ? stamper.getUnderContent(i) : stamper.getOverContent(i); imageContainer.addImage(image); } // End of the treatment : closing the stamper stamper.close(); } catch (Exception e) { SilverLogger.getLogger(PdfUtil.class).error(e); throw new SilverpeasRuntimeException( "A problem has occurred during the adding of an image into a pdf file", e); } finally { if (reader != null) { reader.close(); } } }
From source file:org.silverpeas.util.PdfUtil.java
License:Open Source License
/** * Add a image under or over content on each page of a PDF file. * @param pdfSource the source pdf file, this content is not modified by this method * @param imageToAdd the image file//from ww w .ja v a 2 s. c o m * @param pdfDestination the destination pdf file, with the image under or over content * @param isBackground indicates if image is addes under or over the content of the pdf source * file */ private static void addImageOnEachPage(InputStream pdfSource, File imageToAdd, OutputStream pdfDestination, final boolean isBackground) { // Verify given arguments if (imageToAdd == null || !imageToAdd.isFile()) { throw new RuntimeException("The image file doesn't exist"); } else if (!FileUtil.isImage(imageToAdd.getPath())) { throw new RuntimeException("The picture to add is not an image file"); } PdfReader reader = null; try { // Get a reader of PDF content reader = new PdfReader(pdfSource); // Obtain the total number of pages int pdfNbPages = reader.getNumberOfPages(); PdfStamper stamper = new PdfStamper(reader, pdfDestination); // Load the image Image image = Image.getInstance(imageToAdd.getPath()); float imageWidth = image.getWidth(); float imageHeigth = image.getHeight(); // Adding the image on each page of the PDF for (int i = 1; i <= pdfNbPages; i++) { // Page sizes Rectangle rectangle = reader.getPageSize(i); // Compute the scale of the image float scale = Math.min(100, (rectangle.getWidth() / imageWidth * 100)); image.scalePercent(Math.min(scale, (rectangle.getHeight() / imageHeigth * 100))); // Setting the image position for the current page image.setAbsolutePosition(computeImageCenterPosition(rectangle.getWidth(), image.getScaledWidth()), computeImageCenterPosition(rectangle.getHeight(), image.getScaledHeight())); // Adding image PdfContentByte imageContainer = isBackground ? stamper.getUnderContent(i) : stamper.getOverContent(i); imageContainer.addImage(image); } // End of the treatment : closing the stamper stamper.close(); } catch (Exception e) { SilverTrace.error("util", "PdfUtil.stamp", "EX_ERROR_PDF_ADD_WATERWARK", e); throw new RuntimeException("A problem has occured during the adding of an image into a pdf file", e); } finally { if (reader != null) { reader.close(); } } }
From source file:papertoolkit.render.sheets.PDFSheetRenderer.java
License:BSD License
/** * Uses the iText package to render a PDF file. iText is nice because we can write to a * Graphics2D context. Alternatively, we can use PDF-like commands. * /* ww w. ja v a 2s .c om*/ * @param destPDFFile */ public void renderToPDF(File destPDFFile) { try { final FileOutputStream fileOutputStream = new FileOutputStream(destPDFFile); final PdfReader reader = pdfSheet.getReader(); DebugUtils.println("NumPages in Existing PDF: " + reader.getNumberOfPages()); // allows us to stamp on top of an existing PDF final PdfStamper stamp = new PdfStamper(reader, fileOutputStream); // change the content on top of page 1 // bottom layer for regions final PdfContentByte topLayer = stamp.getOverContent(1 /* page number */); final PdfContentByte bottomLayer = stamp.getUnderContent(1); renderToPDFContentLayers(destPDFFile, topLayer, bottomLayer); stamp.close(); // save the pattern info to the same directory automatically savePatternInformation(); // do this automatically } catch (FileNotFoundException e) { e.printStackTrace(); } catch (DocumentException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } }
From source file:questions.ocg.AddOptionalWatermark.java
public static void main(String[] args) throws DocumentException, IOException { PdfReader reader = new PdfReader(RESOURCE); PdfStamper stamper = new PdfStamper(reader, new FileOutputStream(RESULT)); Image image1 = Image.getInstance(IMAGE_PRINTED); Image image2 = Image.getInstance(IMAGE_NOT_PRINTED); PdfLayer watermark_printed = new PdfLayer("printed", stamper.getWriter()); watermark_printed.setOn(false);//from ww w.j a va 2s . co m watermark_printed.setOnPanel(false); watermark_printed.setPrint("print", true); PdfLayer watermark_not_printed = new PdfLayer("not_printed", stamper.getWriter()); watermark_not_printed.setOn(true); watermark_not_printed.setOnPanel(false); watermark_not_printed.setPrint("print", false); for (int i = 0; i < stamper.getReader().getNumberOfPages();) { PdfContentByte cb = stamper.getUnderContent(++i); Rectangle rectangle = stamper.getReader().getPageSizeWithRotation(i); cb.beginLayer(watermark_printed); float AbsoluteX = rectangle.getLeft() + (rectangle.getWidth() - image1.getPlainWidth()) / 2; float AbsoluteY = rectangle.getBottom() + (rectangle.getHeight() - image1.getPlainHeight()) / 2; image1.setAbsolutePosition(AbsoluteX, AbsoluteY); cb.addImage(image1); cb.endLayer(); cb.beginLayer(watermark_not_printed); AbsoluteX = rectangle.getLeft() + (rectangle.getWidth() - image2.getPlainWidth()) / 2; AbsoluteY = rectangle.getBottom() + (rectangle.getHeight() - image2.getPlainHeight()) / 2; image2.setAbsolutePosition(AbsoluteX, AbsoluteY); cb.addImage(image2); cb.endLayer(); } stamper.close(); }