List of usage examples for org.apache.pdfbox.multipdf LayerUtility appendFormAsLayer
public PDOptionalContentGroup appendFormAsLayer(PDPage targetPage, PDFormXObject form, AffineTransform transform, String layerName) throws IOException
From source file:com.devnexus.ting.web.controller.PdfUtils.java
License:Apache License
public PdfUtils(float margin, String title) throws IOException { this.margin = margin; doc = new PDDocument(); baseFont = PDType0Font.load(doc, PdfUtils.class.getResourceAsStream("/fonts/Arial.ttf")); headerFont = PDType1Font.HELVETICA_BOLD; subHeaderFont = PDType1Font.HELVETICA_BOLD; devnexusLogo = PDDocument.load(PdfUtils.class.getResourceAsStream("/fonts/devnexus-logo.pdf")); this.currentPage = new PDPage(); this.pages.add(currentPage); this.doc.addPage(currentPage); final PDRectangle mediabox = currentPage.getMediaBox(); this.width = mediabox.getWidth() - 2 * margin; float startX = mediabox.getLowerLeftX() + margin; float startY = mediabox.getUpperRightY() - margin; this.initialHeightCounter = startY; this.heightCounter = startY; LOGGER.info(String.format(//from w w w . j a v a2 s .co m "Margin: %s, width: %s, startX: %s, " + "startY: %s, heightCounter: %s, baseFontSize: %s, headerFontSize: %s", margin, width, startX, startY, heightCounter, baseFont, headerFont)); contents = new PDPageContentStream(doc, currentPage); // Add Logo final LayerUtility layerUtility = new LayerUtility(doc); final PDFormXObject logo = layerUtility.importPageAsForm(devnexusLogo, 0); final AffineTransform affineTransform = AffineTransform.getTranslateInstance(100, startY - 50); affineTransform.scale(2d, 2d); layerUtility.appendFormAsLayer(currentPage, logo, affineTransform, "devnexus-logo"); this.heightCounter -= 100; this.contents.beginText(); this.contents.setFont(headerFont, headerFontSize); this.currentLeading = this.lineSpacing * baseFontSize; this.contents.setLeading(this.currentLeading); contents.newLineAtOffset(50, heightCounter); println(title); this.contents.setFont(baseFont, baseFontSize); this.currentLeading = this.lineSpacing * baseFontSize; this.contents.setLeading(this.currentLeading); println(); }