List of usage examples for org.apache.pdfbox.pdmodel PDPage PDPage
public PDPage()
From source file:org.kuali.coeus.common.impl.person.signature.PersonSignatureServiceImpl.java
License:Open Source License
/** * This method is to scan for signature tag in each page and apply the signature * at desired location./* ww w. java2 s . c o m*/ * @param imageData * @param originalByteArrayOutputStream */ @SuppressWarnings("unchecked") protected ByteArrayOutputStream scanAndApplyAutographInEachPage(byte[] imageData, ByteArrayOutputStream originalByteArrayOutputStream) throws Exception { ByteArrayOutputStream outputStream = originalByteArrayOutputStream; byte[] pdfFileData = originalByteArrayOutputStream.toByteArray(); PDDocument originalDocument = getPdfDocument(pdfFileData); //PDDocument.load(is); PDDocument signatureDocument = new PDDocument(); List<PDPage> originalDocumentPages = originalDocument.getDocumentCatalog().getAllPages(); for (PDPage page : originalDocumentPages) { List<String> signatureTags = new ArrayList<String>(getSignatureTagParameter()); PersonSignatureLocationHelper printer = new PersonSignatureLocationHelper(signatureTags); PDStream contents = page.getContents(); if (contents != null) { printer.processStream(page, page.findResources(), page.getContents().getStream()); } PDPage signaturePage = new PDPage(); if (printer.isSignatureTagExists()) { PDJpeg signatureImage = new PDJpeg(signatureDocument, getBufferedImage(imageData)); PDPageContentStream stream = new PDPageContentStream(signatureDocument, signaturePage, true, true); for (PersonSignaturePrintHelper signatureHelper : printer.getPersonSignatureLocations()) { float coordinateX = signatureHelper.getCoordinateX(); float coordinateY = signatureHelper.getCoordinateY() - signatureImage.getHeight() - ADDITIONAL_SPACE_BETWEEN_TAG_AND_IMAGE; stream.drawImage(signatureImage, coordinateX, coordinateY); stream.close(); } } else { signaturePage = page; } signatureDocument.addPage(signaturePage); } Overlay overlay = new Overlay(); overlay.overlay(signatureDocument, originalDocument); originalDocument.save(outputStream); originalDocument.close(); signatureDocument.close(); return outputStream; }
From source file:org.nuxeo.typeDocPkg.TestPdfBoxN.java
License:Apache License
public void create(String message, String outfile) throws IOException, COSVisitorException { PDDocument doc = null;//from w ww. j a v a 2 s .c om try { doc = new PDDocument(); PDPage page = new PDPage(); doc.addPage(page); PDPageContentStream contentStream = new PDPageContentStream(doc, page); PDFont font = PDType1Font.HELVETICA; contentStream.beginText(); contentStream.setFont(font, 12); contentStream.moveTextPositionByAmount(100, 700); contentStream.drawString(message); contentStream.endText(); contentStream.close(); doc.save(outfile); doc.close(); } catch (IOException e) { e.printStackTrace(); } }
From source file:org.pdfmetamodifier.OutlineHelperTest.java
License:Apache License
/** * Test for {@link OutlineHelper#lineListToOutlines(org.apache.pdfbox.pdmodel.PDPageTree, java.util.List)} and * {@link OutlineHelper#outlinesToLineList(org.apache.pdfbox.pdmodel.interactive.documentnavigation.outline.PDDocumentOutline, org.apache.pdfbox.pdmodel.PDPageTree, org.apache.pdfbox.pdmodel.PDDestinationNameTreeNode)}. * /*from ww w.ja v a2s . co m*/ * @throws IOException */ @Test public void lineListToOutlinesAndBack() throws IOException { final List<String> lineList = new ArrayList<>(); lineList.add("Bookmarks"); lineList.add(" Title 1|1"); lineList.add(" Title 1.1|2"); lineList.add(" Title 1.2|3"); lineList.add(""); lineList.add(" Title 2|5"); lineList.add(" Title 2.1|6"); lineList.add(" Title 2.2|7"); lineList.add(""); lineList.add(" Title 3|9"); lineList.add(" Title 3.1|10"); lineList.add(" Title 3.2|11"); lineList.add(""); final List<String> cleanLineList = new ArrayList<>(); for (String line : lineList) { if (!line.isEmpty()) { cleanLineList.add(line); } } final PDPageTree pageTree = mock(PDPageTree.class); final List<PDPage> mockPages = new ArrayList<>(); when(pageTree.get(anyInt())).then(new Answer<PDPage>() { @Override public PDPage answer(final InvocationOnMock invocation) throws Throwable { final int idx = (int) invocation.getArguments()[0]; for (int i = mockPages.size(); i <= idx; ++i) { mockPages.add(new PDPage()); } return mockPages.get(idx); } }); when(pageTree.indexOf(any(PDPage.class))).then(new Answer<Integer>() { @Override public Integer answer(final InvocationOnMock invocation) throws Throwable { final PDPage page = (PDPage) invocation.getArguments()[0]; return mockPages.indexOf(page); } }); final PDDocumentOutline documentOutline = OutlineHelper.lineListToOutlines(pageTree, lineList); final List<String> resultLineList = OutlineHelper.outlinesToLineList(documentOutline, pageTree, null); assertEquals(cleanLineList.size(), resultLineList.size()); for (int i = 0; i < cleanLineList.size(); ++i) { assertEquals(cleanLineList.get(i), resultLineList.get(i)); } }
From source file:org.pensco.CreateStatementsOp.java
License:Open Source License
protected Blob buildPDF(String inCustomer, Calendar inStart, Calendar inEnd) throws IOException, COSVisitorException { Blob result = null;/*www .j a va2s. c o m*/ PDDocument pdfDoc = new PDDocument(); PDPage page = new PDPage(); pdfDoc.addPage(page); PDRectangle rect = page.getMediaBox(); float rectH = rect.getHeight(); PDFont font = PDType1Font.HELVETICA; PDFont fontBold = PDType1Font.HELVETICA_BOLD; PDFont fontOblique = PDType1Font.HELVETICA_OBLIQUE; PDPageContentStream contentStream = new PDPageContentStream(pdfDoc, page); int line = 0; contentStream.beginText(); contentStream.setFont(fontOblique, 10); contentStream.moveTextPositionByAmount(230, 20); contentStream.drawString("(Statement randomly generated)"); contentStream.endText(); line += 3; contentStream.beginText(); contentStream.setFont(fontBold, 12); contentStream.moveTextPositionByAmount(300, rectH - 20 * (++line)); contentStream.drawString(inCustomer); contentStream.endText(); contentStream.beginText(); contentStream.setFont(fontBold, 12); contentStream.moveTextPositionByAmount(300, rectH - 20 * (++line)); contentStream.drawString( "Statement from " + yyyyMMdd.format(inStart.getTime()) + " to " + yyyyMMdd.format(inEnd.getTime())); contentStream.endText(); line += 3; statementLines = ToolsMisc.randomInt(3, 9); boolean isDebit = false; for (int i = 1; i <= statementLines; ++i) { contentStream.beginText(); contentStream.setFont(font, 12); contentStream.moveTextPositionByAmount(100, rectH - 20 * line); contentStream.drawString("" + i); contentStream.endText(); contentStream.beginText(); contentStream.setFont(font, 12); contentStream.moveTextPositionByAmount(120, rectH - 20 * line); isDebit = ToolsMisc.randomInt(0, 10) > 7; if (isDebit) { contentStream.drawString("Withdraw Funds to account " + MiscUtils.getSomeUID(6)); } else { contentStream.drawString("Add Funds to account " + MiscUtils.getSomeUID(6)); } contentStream.endText(); contentStream.beginText(); if (isDebit) { contentStream.setFont(fontOblique, 12); contentStream.moveTextPositionByAmount(350, rectH - 20 * line); } else { contentStream.setFont(font, 12); contentStream.moveTextPositionByAmount(450, rectH - 20 * line); } contentStream.drawString("" + ToolsMisc.randomInt(1000, 9000) + "." + ToolsMisc.randomInt(10, 90)); contentStream.endText(); line += 1; } contentStream.close(); contentStream = null; if (logoImage != null) { PDXObjectImage ximage = new PDPixelMap(pdfDoc, logoImage); contentStream = new PDPageContentStream(pdfDoc, page, true, true); contentStream.endMarkedContentSequence(); contentStream.drawXObject(ximage, 10, rectH - 20 - ximage.getHeight(), ximage.getWidth(), ximage.getHeight()); contentStream.close(); contentStream = null; } result = MiscUtils.saveInTempFile(pdfDoc); pdfDoc.close(); return result; }
From source file:org.qi4j.envisage.print.PDFWriter.java
License:Apache License
private void writeGraphPage(GraphDisplay graphDisplay) throws IOException { File tFile = File.createTempFile("envisage", "png"); graphDisplay.saveImage(new FileOutputStream(tFile), "png", 1d); BufferedImage img = ImageIO.read(tFile); int w = img.getWidth(); int h = img.getHeight(); int inset = 40; PDRectangle pdRect = new PDRectangle(w + inset, h + inset); PDPage page = new PDPage(); page.setMediaBox(pdRect);//from w ww.j a v a2 s. c o m doc.addPage(page); PDJpeg xImage = new PDJpeg(doc, img); PDPageContentStream contentStream = new PDPageContentStream(doc, page); contentStream.drawImage(xImage, (pdRect.getWidth() - w) / 2, (pdRect.getHeight() - h) / 2); contentStream.close(); }
From source file:org.qi4j.envisage.print.PDFWriter.java
License:Apache License
private void createNewPage() { try {//w ww . ja va 2s.co m if (curContentStream != null) { curContentStream.endText(); curContentStream.close(); } PDPage page = new PDPage(); doc.addPage(page); curContentStream = new PDPageContentStream(doc, page); curPageSize = page.getArtBox(); //System.out.println("pSize: " + pdRect.getWidth() + "," + pdRect.getHeight()); curContentStream.beginText(); curY = curPageSize.getHeight() - startY; curContentStream.moveTextPositionByAmount(startX, curY); if (curFont != null) { setFont(curFont, curFontSize); } } catch (IOException e) { throw new PrintingException("Unable to create page.", e); } }
From source file:org.socialbiz.cog.util.PDFUtil.java
License:Apache License
private void setPage() throws Exception { if (document == null) { //Initialize document = new PDDocument(); page = new PDPage(); page.setMediaBox(PDPage.PAGE_SIZE_A4); document.addPage(page);/*from ww w .j av a 2s . c o m*/ contentStream = new PDPageContentStream(document, page, false, false); contentStream.beginText(); contentStream.moveTextPositionByAmount(xPos, yPos); isNewPage = true; } else { if (yPos <= 40) { //Close the old page create new contentStream.endText(); contentStream.close(); page = new PDPage(); page.setMediaBox(PDPage.PAGE_SIZE_A4); document.addPage(page); contentStream = new PDPageContentStream(document, page, false, false); yPos = 800; xShift = 0; yShift = 0; contentStream.beginText(); contentStream.setFont(font, font_size); contentStream.moveTextPositionByAmount(xPos, yPos); isNewPage = true; } else { contentStream.moveTextPositionByAmount(xShift, yShift); } } }
From source file:org.socialbiz.cog.util.PDFUtil.java
License:Apache License
public static void main(String[] args) { //For test try {/*from w ww.j av a 2 s. co m*/ String path = args[0]; PDDocument document = new PDDocument(); PDPage page = new PDPage(); page.setMediaBox(PDPage.PAGE_SIZE_A4); document.addPage(page); PDFont font = PDType1Font.HELVETICA; PDPageContentStream contentStream = new PDPageContentStream(document, page, false, false); contentStream.beginText(); contentStream.setFont(font, 12); contentStream.moveTextPositionByAmount(100, 800); String x = "hello world"; contentStream.drawString(x); contentStream.moveTextPositionByAmount(-90, -15); contentStream.setFont(font, 12); contentStream.drawString("Hello World3"); contentStream.endText(); contentStream.close(); document.save(path); document.close(); System.out.println("DONE.."); } catch (Exception e) { e.printStackTrace(); } }
From source file:org.socialbiz.cog.WikiToPDF.java
License:Apache License
private void startPage() throws Exception { pdpage = new PDPage(); pdpage.setMediaBox(PDPage.PAGE_SIZE_LETTER); pddoc.addPage(pdpage);/*from w w w. j ava2 s . com*/ xPos = LEFT_MARGIN; yPos = TOP_MARGIN; //but don't write here, do a newline first! contentStream = new PDPageContentStream(pddoc, pdpage, true, false); /* draw a boundary box at margins box(LEFT_MARGIN, TOP_MARGIN, RIGHT_MARGIN, BOTTOM_MARGIN); */ contentStream.beginText(); //really small font for the header and footer contentStream.setFont(helvetica.getFont(false, false), 8); contentStream.moveTextPositionByAmount(xPos, yPos + 20); contentStream.drawString(headerText); contentStream.moveTextPositionByAmount(0, BOTTOM_MARGIN - TOP_MARGIN - 40); contentStream.drawString(footerText); contentStream.moveTextPositionByAmount(0, TOP_MARGIN + 20 - BOTTOM_MARGIN); currentFont = currentFamily.getFont(isBold, isItalic); contentStream.setFont(currentFont, currentLineSize); isNewPage = true; lineRemainder = 0f; pageNum++; }
From source file:org.wangwei.pdf.hexpdf.HexPDF.java
License:Apache License
/** * Close the current page (if any), and open a new one. Cursor position is reset to top-left corner of writable area * (within margins)/*from w w w . j a v a 2s . c o m*/ * * @see #closePage() */ public void newPage() { numPages++; if (currentPage != null) { closePage(); } currentPage = new PDPage(); float x1 = pageSize.getLowerLeftX(); float y1 = pageSize.getLowerLeftY(); float x2 = pageSize.getUpperRightX(); float y2 = pageSize.getUpperRightY(); float w = pageSize.getWidth(); float h = pageSize.getHeight(); if (orientation == HexPDF.PORTRAIT) { pageWidth = w; pageHeight = h; currentPage.setMediaBox(new PDRectangle(new BoundingBox(x1, y1, x2, y2))); } else { pageWidth = h; pageHeight = w; currentPage.setMediaBox(new PDRectangle(new BoundingBox(y1, x1, y2, x2))); } setDimensions(); cursorX = contentStartX; cursorY = contentStartY; try { cs = new PDPageContentStream(this, currentPage); cs.setFont(font, fontSize); } catch (IOException ex) { Logger.getLogger(HexPDF.class.getName()).log(Level.SEVERE, null, ex); } }