List of usage examples for org.apache.pdfbox.pdmodel PDPage PDPage
public PDPage(COSDictionary pageDictionary)
From source file:org.fit.cssbox.render.PDFRenderer.java
License:Open Source License
/** * Creates document witch first page in it using PDFBox *//* w w w. j a v a2s. c o m*/ private int createDocPDFBox() { try { doc = new PDDocument(); page = new PDPage(pageFormat); doc.addPage(page); content = new PDPageContentStream(doc, page); } catch (Exception e) { e.printStackTrace(); return -1; } return 0; }
From source file:org.fit.cssbox.render.PDFRenderer.java
License:Open Source License
/** * Inserts N pages to PDF document using PDFBox *//*from ww w . ja va2 s . co m*/ private int insertNPagesPDFBox(int pageCount) { for (int i = 1; i < pageCount; i++) { page = new PDPage(pageFormat); doc.addPage(page); } return 0; }
From source file:org.nmrfx.processor.gui.graphicsio.PDFWriter.java
License:Open Source License
public void create(boolean landScape, double width, double height, String fileName) throws GraphicsIOException { // the document this.landScape = landScape; this.fileName = fileName; doc = new PDDocument(); try {//from www . j av a 2s. c o m PDPage page = new PDPage(PDRectangle.LETTER); doc.addPage(page); PDRectangle pageSize = page.getMediaBox(); pageWidth = pageSize.getWidth(); pageHeight = pageSize.getHeight(); contentStream = new PDPageContentStream(doc, page, false, false); // add the rotation using the current transformation matrix // including a translation of pageWidth to use the lower left corner as 0,0 reference if (landScape) { page.setRotation(90); contentStream.transform(new Matrix(0, 1, -1, 0, pageWidth, 0)); } } catch (IOException ioE) { throw new GraphicsIOException(ioE.getMessage()); } }
From source file:org.olat.core.util.pdf.PdfDocument.java
License:Apache License
public PDPage addPage() throws IOException { if (currentContentStream != null) { currentContentStream.close();// w ww .ja v a2s. c o m } PDPage page = new PDPage(PDPage.PAGE_SIZE_A4); document.addPage(page); currentPage = page; currentContentStream = new PDPageContentStream(document, currentPage); PDRectangle mediabox = currentPage.findMediaBox(); width = mediabox.getWidth() - 2 * marginLeftRight; currentY = mediabox.getUpperRightY() - marginTopBottom; return page; }
From source file:org.primaresearch.pdf.PageToPdfConverterUsingPdfBox.java
License:Apache License
private void addPage(PDDocument doc, Page page) { try {/*from w w w . j a v a2s. c o m*/ // Create a new blank page and add it to the document //TODO Use image DPI and size //The measurement unit of the PDF is point (1 Point = 0.0352777778 cm) //For now: Set the PDF size to the PAGE size (1px = 1pt) //PDPage pdfPage = new PDPage(PDPage.PAGE_SIZE_A4); PDPage pdfPage = new PDPage(new PDRectangle(page.getLayout().getWidth(), page.getLayout().getHeight())); doc.addPage(pdfPage); if (DEBUG) { System.out.println("Mediabox width: " + pdfPage.getMediaBox().getWidth()); System.out.println("Mediabox height: " + pdfPage.getMediaBox().getHeight()); } // Start a new content stream which will "hold" the to be created content PDPageContentStream contentStream = new PDPageContentStream(doc, pdfPage); try { addText(contentStream, page); // Define a text content stream using the selected font, moving the cursor and drawing the text "Hello World" //contentStream.beginText(); //contentStream.setFont( font, 12 ); //contentStream.moveTextPositionByAmount( 100, 700 ); //contentStream.drawString( "Hello World" ); //contentStream.endText(); } finally { // Make sure that the content stream is closed: contentStream.close(); } } catch (Exception exc) { exc.printStackTrace(); } }
From source file:org.zaproxy.zap.extension.alertReport.AlertReportExportPDF.java
License:Apache License
/** * add a title page to the PDF document//from w w w. java 2s .com * * @param extensionExport * @throws IOException */ private void addTitlePage(ExtensionAlertReportExport extensionExport) throws IOException { page = new PDPage(pageSize); document.addPage(page); // calculate initial positioning on the page (origin = bottom left) textInsertionPoint = new Point2D.Float(page.findMediaBox().getLowerLeftX() + marginPoints, page.findMediaBox().getUpperRightY() - marginPoints); // draw the logo at 40% size. textInsertionPoint = addImage(extensionExport.getParams().getLogoFileName(), 40f, textInsertionPoint); for (int i = 0; i < 4; i++) { textInsertionPoint = addText(textFormatting, " ", textInsertionPoint); } textInsertionPoint = addText(titlePageHeader1Formatting, extensionExport.getParams().getTitleReport(), textInsertionPoint); for (int i = 0; i < 3; i++) { textInsertionPoint = addText(textFormatting, " ", textInsertionPoint); } textInsertionPoint = addText(titlePageHeader2Formatting, extensionExport.getParams().getCustomerName(), textInsertionPoint); for (int i = 0; i < 15; i++) { textInsertionPoint = addText(textFormatting, " ", textInsertionPoint); } textInsertionPoint = addText(smallLabelFormatting, extensionExport.getMessages().getString("alertreport.export.message.export.pdf.confidential"), textInsertionPoint); textInsertionPoint = addText(smallPrintFormatting, extensionExport.getParams().getConfidentialText(), textInsertionPoint); }
From source file:org.zaproxy.zap.extension.alertReport.AlertReportExportPDF.java
License:Apache License
/** * Add the specified text to the PDF document, using the specified formatting, and continuing * from the specified text insertion point. The method will handle all text wrapping and * pagination, in order to keep all of the text within the page body, and off the margin. New * pages will be added by the method, if required. * * @param formatting/* w w w .ja v a 2 s. co m*/ * @param text * @param textInsertionPoint * @return * @throws IOException */ private Point2D.Float addText(Formatting formatting, String text, Point2D.Float textInsertionPoint) throws IOException { // handles the case where an alert category falls off the end of a page (this is not // automatically handled by the pdfbox library) PDPageContentStream contentStream = new PDPageContentStream(document, page, true, true); // contentStream.moveTo (0,0); contentStream.beginText(); contentStream.setFont(formatting.getFont(), formatting.getFontSize()); float pageWidthPoints = page.getMediaBox().getWidth(); float usableWidthPoints = pageWidthPoints - (marginPoints * 2); // all text must be drawn at a y pos > this value to be off the margin, and on the page // (note: the origin is at bottom left of the page) float textYMinThreshold = marginPoints; float previousX = 0, previousY = 0; float xoffset = 0, yoffset = 0; List<String> textByLine = splitTextForWidth(text, formatting, usableWidthPoints); for (String lineOfText : textByLine) { // calculate the x position, depending on the justification // the font size (which is measured in points) needs to feed into the calculation of the // y position. // it isn't known until now, and could be different for each distinct piece of text float textWidthInPoints = formatting.getFontSize() * formatting.getFont().getStringWidth(lineOfText) / 1000; switch (formatting.getTextJustification()) { case LEFT: xoffset = (float) textInsertionPoint.getX() - previousX; yoffset = (float) textInsertionPoint.getY() - formatting.getFontSize() - previousY; previousX = (float) textInsertionPoint.getX(); previousY = (float) textInsertionPoint.getY() - formatting.getFontSize(); break; case RIGHT: xoffset = pageWidthPoints - textWidthInPoints - previousX; yoffset = (float) textInsertionPoint.getY() - formatting.getFontSize() - previousY; previousX = pageWidthPoints - textWidthInPoints; previousY = (float) textInsertionPoint.getY() - formatting.getFontSize(); break; case CENTRE: xoffset = (pageWidthPoints - textWidthInPoints) / 2 - previousX; yoffset = (float) textInsertionPoint.getY() - formatting.getFontSize() - previousY; previousX = (pageWidthPoints - textWidthInPoints) / 2; previousY = (float) textInsertionPoint.getY() - formatting.getFontSize(); break; default: throw new IOException("Unsupported text justification option: " + formatting.textJustification); } float absoluteY = (float) textInsertionPoint.getY() - formatting.getFontSize(); if (absoluteY < textYMinThreshold) { // close off the current page contentStream.endText(); contentStream.saveGraphicsState(); contentStream.close(); // and start a new page.. page = new PDPage(pageSize); document.addPage(page); contentStream = new PDPageContentStream(document, page, true, true); contentStream.beginText(); contentStream.setFont(formatting.getFont(), formatting.getFontSize()); // calculate initial positioning on the page (origin = bottom left) textInsertionPoint = getPageInitialInsertionPoint(); // for a new new page, the offset is from 0,0, so it needs to be re-calculated from // the origin, not from the "previous" position on the page xoffset = (float) textInsertionPoint.getX(); yoffset = (float) textInsertionPoint.getY() - formatting.getFontSize(); previousY = yoffset; } // move from the previous text position (within the beginText() + endText()) by the // appropriate delta.. // and draw.. contentStream.moveTextPositionByAmount(xoffset, yoffset); contentStream.drawString(lineOfText); // update the text insertion point for the next line, using 1.5 spacing.. textInsertionPoint = new Point2D.Float((float) textInsertionPoint.getX(), (float) textInsertionPoint.getY() - (formatting.getFontSize() * 1.5f)); } contentStream.endText(); contentStream.saveGraphicsState(); contentStream.close(); return textInsertionPoint; }
From source file:org.zaproxy.zap.extension.alertReport.AlertReportExportPDF.java
License:Apache License
/** * adds content to the PDF report for the list of alerts provided, which are all for the same * alert category/*from w w w. j a v a2s . c om*/ * * @param alerts * @param extensionExport * @throws IOException */ private void addContent(java.util.List<Alert> alerts, ExtensionAlertReportExport extensionExport) throws IOException { String labelDescription = extensionExport.getMessages() .getString("alertreport.export.message.export.pdf.description"); String labelRisk = extensionExport.getMessages().getString("alertreport.export.message.export.pdf.risk"); String labelReliability = extensionExport.getMessages() .getString("alertreport.export.message.export.pdf.reability"); String labelURLs = extensionExport.getMessages().getString("alertreport.export.message.export.pdf.urls"); String labelParameter = extensionExport.getMessages() .getString("alertreport.export.message.export.pdf.parameters"); String labelAttack = extensionExport.getMessages() .getString("alertreport.export.message.export.pdf.attack"); String labelEvidence = extensionExport.getMessages() .getString("alertreport.export.message.export.pdf.evidence"); String labelOtherInfo = extensionExport.getMessages() .getString("alertreport.export.message.export.pdf.otherinfo"); String labelSolution = extensionExport.getMessages() .getString("alertreport.export.message.export.pdf.solution"); String labelReferences = extensionExport.getMessages() .getString("alertreport.export.message.export.pdf.references"); Alert alert = alerts.get(0); page = new PDPage(pageSize); document.addPage(page); // calculate initial positioning on the page (origin = bottom left) // Point2D.Float textInsertionPoint = new Point2D.Float(page.findMediaBox().getLowerLeftX() // + marginPoints, page.findMediaBox().getUpperRightY() - marginPoints); Point2D.Float textInsertionPoint = getPageInitialInsertionPoint(); textInsertionPoint = addText(alertCategoryLabelFormatting, alert.getName(), textInsertionPoint); textInsertionPoint = addText(alertLabelFormatting, labelDescription, textInsertionPoint); textInsertionPoint = addText(alertTextFormatting, getFieldAlertProperty(alert.getPluginId(), "description", alert.getDescription(), extensionExport), textInsertionPoint); textInsertionPoint = addText(alertTextFormatting, " ", textInsertionPoint); textInsertionPoint = addText(alertLabelFormatting, labelRisk, textInsertionPoint); textInsertionPoint = addText(alertTextFormatting, getFieldAlertProperty(alert.getPluginId(), "risk." + String.valueOf(alert.getRisk()), Alert.MSG_RISK[alert.getRisk()], extensionExport), textInsertionPoint); textInsertionPoint = addText(alertTextFormatting, " ", textInsertionPoint); textInsertionPoint = addText(alertLabelFormatting, labelReliability, textInsertionPoint); textInsertionPoint = addText(alertTextFormatting, getFieldAlertProperty(alert.getPluginId(), "reliability." + String.valueOf(alert.getConfidence()), Alert.MSG_CONFIDENCE[alert.getConfidence()], extensionExport), textInsertionPoint); textInsertionPoint = addText(alertTextFormatting, " ", textInsertionPoint); textInsertionPoint = addText(alertLabelFormatting, labelURLs, textInsertionPoint); // TODO: binary data (Base64 decoded data is the only example I can find) flows onto the // margin.. // can we do something about it?? // for each alert within this category for (int i = 0; i < alerts.size(); i++) { Alert alertAux = alerts.get(i); // output the URL, and parameter information for each alert for this category textInsertionPoint = addText(alertTextFormatting, (i + 1) + "-" + alertAux.getUri(), textInsertionPoint); if (!alertAux.getParam().isEmpty()) { textInsertionPoint = addText(alertTextFormatting, labelParameter + ": " + alertAux.getParam(), textInsertionPoint); } if (alertAux.getAttack() != null && !alertAux.getAttack().isEmpty()) { textInsertionPoint = addText(alertTextFormatting, labelAttack + ": " + alertAux.getAttack(), textInsertionPoint); } if (alertAux.getEvidence() != null && !alertAux.getEvidence().isEmpty()) { textInsertionPoint = addText(alertTextFormatting, labelEvidence + ": " + alertAux.getEvidence(), textInsertionPoint); } if (!alertAux.getOtherInfo().isEmpty()) { textInsertionPoint = addText(alertTextFormatting, labelOtherInfo + ": " + alertAux.getOtherInfo(), textInsertionPoint); } // put a blank line after each URL's worth of information textInsertionPoint = addText(alertTextFormatting, " ", textInsertionPoint); } String solution = getFieldAlertProperty(alert.getPluginId(), "solution", alert.getSolution(), extensionExport); if (!solution.isEmpty()) { textInsertionPoint = addText(alertLabelFormatting, labelSolution, textInsertionPoint); textInsertionPoint = addText(alertTextFormatting, getFieldAlertProperty(alert.getPluginId(), "solution", alert.getSolution(), extensionExport), textInsertionPoint); textInsertionPoint = addText(alertTextFormatting, " ", textInsertionPoint); } if (!alert.getReference().isEmpty()) { textInsertionPoint = addText(alertLabelFormatting, labelReferences, textInsertionPoint); textInsertionPoint = addText(alertTextFormatting, alert.getReference(), textInsertionPoint); textInsertionPoint = addText(alertTextFormatting, " ", textInsertionPoint); } }
From source file:pdfboxtest.PDFBoxTest.java
/** *//*from w ww .jav a2 s . co m*/ public static void main(String[] args) throws Exception { GetFundamentals go = new GetFundamentals(); // Create a document and add a page to it go.ticker = "AAN"; String outputFileName = go.ticker + ".pdf"; PDDocument document = new PDDocument(); PDPage page1 = new PDPage(PDPage.PAGE_SIZE_LETTER); PDPage page2 = new PDPage(PDPage.PAGE_SIZE_LETTER); PDPage page3 = new PDPage(PDPage.PAGE_SIZE_LETTER); PDPage page4 = new PDPage(PDPage.PAGE_SIZE_LETTER); // PDPage.PAGE_SIZE_LETTER is also possible // rect can be used to get the page width and height document.addPage(page1); document.addPage(page2); document.addPage(page3); document.addPage(page4); // Create a new font object selecting one of the PDF base fonts PDFont fontPlain = PDType1Font.HELVETICA_BOLD; // Start a new content stream which will "hold" the to be created content PDPageContentStream cos = new PDPageContentStream(document, page1); // Define a text content stream using the selected font, move the cursor and draw some text setupDefault(cos, fontPlain, document, go); setupP1(cos, fontPlain, document, go.ticker); cos.close(); cos = new PDPageContentStream(document, page2); setupDefault(cos, fontPlain, document, go); setupP2(cos, fontPlain, document); cos.close(); cos = new PDPageContentStream(document, page3); setupDefault(cos, fontPlain, document, go); setupP3(cos, fontPlain, document); cos.close(); cos = new PDPageContentStream(document, page4); setupDefault(cos, fontPlain, document, go); setupP4(cos, fontPlain, document); cos.close(); // Make sure that the content stream is closed: // Save the results and ensure that the document is properly closed: document.save(outputFileName); document.close(); }
From source file:pdfsplicer.SplicerModel.java
License:Open Source License
/** * Create the new PDF, and save it.//from ww w. j av a 2 s . c o m * * @param saveFile the file to save it as * @throws IOException if it cannot save the file */ public void makeFinalizedPDF(File saveFile) throws IOException { PDDocument doc = null; PDDocument newdoc = new PDDocument(); for (int i = 0; i < pageEntryPDFList.size(); ++i) { doc = pdfList.get(pageEntryPDFList.get(i)); if (doc.isEncrypted()) { System.out.println("Error: Encrypted PDF"); System.exit(1); } List<Integer> pRange = pageRangeList.get(i); PDFCloneUtility pdfCloner = new PDFCloneUtility(newdoc); for (int pNum : pRange) { PDPage page = doc.getPage(pNum - 1); COSDictionary clonedDict = (COSDictionary) pdfCloner.cloneForNewDocument(page); newdoc.addPage(new PDPage(clonedDict)); } } newdoc.save(saveFile); if (newdoc != null) { newdoc.close(); } }