List of usage examples for org.apache.pdfbox.pdmodel PDPage PDPage
public PDPage()
From source file:openstitcher.core.PDFRenderer.java
License:Open Source License
public static void render(Design design, String location, ArrayList<LegendEntry> legend) throws IOException, COSVisitorException { PDDocument document = new PDDocument(); PDPage page = new PDPage(); document.addPage(page);/*from w w w. jav a2 s . c o m*/ PDFont font = PDType1Font.HELVETICA; PDPageContentStream contentStream = new PDPageContentStream(document, page); float pageWidth = page.getMediaBox().getWidth(); float pageHeight = page.getMediaBox().getHeight(); float sideMargin = 70.0f; float capsMargin = 30.0f; // draw the document title contentStream.beginText(); contentStream.setFont(font, 24.0f); contentStream.moveTextPositionByAmount(sideMargin, pageHeight - capsMargin - 24.0f); contentStream.drawString(design.title); contentStream.endText(); // draw the document author contentStream.beginText(); contentStream.setFont(font, 12.0f); contentStream.moveTextPositionByAmount(sideMargin, pageHeight - capsMargin - (24.0f + 12.0f)); contentStream.drawString(design.author); contentStream.endText(); // draw the document license contentStream.beginText(); contentStream.setFont(font, 12.0f); contentStream.moveTextPositionByAmount(sideMargin, pageHeight - capsMargin - (24.0f + 12.0f + 12.0f)); contentStream.drawString(design.license); contentStream.endText(); // draw the physical size contentStream.beginText(); contentStream.setFont(font, 12.0f); contentStream.moveTextPositionByAmount(pageWidth - sideMargin - 100.0f, pageHeight - capsMargin - (24.0f + 12.0f + 12.0f)); contentStream.drawString(design.physicalSize); contentStream.endText(); // draw the pattern float gridWidth = pageWidth - (sideMargin * 2); float widthPerCell = gridWidth / (float) design.pattern.getPatternWidth(); float gridStartX = sideMargin; float gridStopX = sideMargin + (widthPerCell * design.pattern.getPatternWidth()); float gridStartY = pageHeight - capsMargin - (24.0f + 12.0f + 12.0f + 12.0f); float gridStopY = (pageHeight - capsMargin - (24.0f + 12.0f + 12.0f + 12.0f)) - (widthPerCell * design.pattern.getPatternHeight()); // draw the pattern: background for (int i = 0; i < design.pattern.getPatternWidth(); i++) { for (int j = 0; j < design.pattern.getPatternHeight(); j++) { Yarn cellYarn = design.pattern.getPatternCell(i, j); if (cellYarn != null) { contentStream.setNonStrokingColor(cellYarn.color); contentStream.fillRect(gridStartX + (widthPerCell * i), gridStartY - (widthPerCell * j) - widthPerCell, widthPerCell, widthPerCell); } } } // draw the pattern: grid outline contentStream.setStrokingColor(Color.black); for (int i = 0; i < design.pattern.getPatternWidth() + 1; i++) { // draw vertical lines float xCoord = gridStartX + (widthPerCell * i); if (i % 5 == 0) { contentStream.setLineWidth(2.0f); } else { contentStream.setLineWidth(1.0f); } contentStream.drawLine(xCoord, gridStartY, xCoord, gridStopY); } for (int i = 0; i < design.pattern.getPatternHeight() + 1; i++) { // draw horizontal lines float yCoord = gridStartY - (widthPerCell * i); if (i % 5 == 0) { contentStream.setLineWidth(2.0f); } else { contentStream.setLineWidth(1.0f); } contentStream.drawLine(gridStartX, yCoord, gridStopX, yCoord); } // draw the pattern: characters contentStream.setNonStrokingColor(Color.black); float centeringOffset = widthPerCell / 5.0f; for (int i = 0; i < design.pattern.getPatternWidth(); i++) { for (int j = 0; j < design.pattern.getPatternHeight(); j++) { Yarn cellYarn = design.pattern.getPatternCell(i, j); if (cellYarn != null) { int index = LegendEntry.findIndexByYarn(legend, cellYarn); if (index == -1) { throw new RuntimeException("Cell did not exist in legend."); } contentStream.beginText(); contentStream.setFont(font, widthPerCell); contentStream.moveTextPositionByAmount(gridStartX + (widthPerCell * i) + centeringOffset, gridStartY - (widthPerCell * j) - widthPerCell + centeringOffset); contentStream.drawString(legend.get(index).character); contentStream.endText(); } } } // draw the legend float legendWidth = pageWidth - (sideMargin * 2); float widthPerLegendCell = legendWidth / (float) legend.size(); float legendStartX = sideMargin; float legendStopX = pageWidth - sideMargin; float legendStartY = capsMargin + 12.0f; float legendStopY = capsMargin; float legendCellPadding = 1.0f; float exampleCellWidth = 10.0f; for (int i = 0; i < legend.size(); i++) { // draw box contentStream.setNonStrokingColor(legend.get(i).yarn.color); contentStream.fillRect(legendStartX + legendCellPadding + (i * widthPerLegendCell), legendStopY + legendCellPadding, exampleCellWidth, exampleCellWidth); // draw character contentStream.beginText(); contentStream.setNonStrokingColor(legend.get(i).fontColor); contentStream.setFont(font, 10.0f); contentStream.moveTextPositionByAmount(legendStartX + legendCellPadding + (i * widthPerLegendCell), legendStopY + legendCellPadding); contentStream.drawString(legend.get(i).character); contentStream.endText(); // draw yarn name contentStream.beginText(); contentStream.setNonStrokingColor(Color.black); contentStream.setFont(font, 10.0f); contentStream.moveTextPositionByAmount(legendStartX + legendCellPadding + exampleCellWidth + legendCellPadding + (i * widthPerLegendCell), legendStopY + legendCellPadding); contentStream.drawString(legend.get(i).yarn.name); contentStream.endText(); } contentStream.close(); document.save(location); document.close(); }
From source file:org.alfresco.repo.content.transform.OOoContentTransformerHelper.java
License:Open Source License
/** * This method produces an empty PDF file at the specified File location. * Apache's PDFBox is used to create the PDF file. *//* w w w .j a va 2 s. co m*/ private void produceEmptyPdfFile(File tempToFile) { // If improvement PDFBOX-914 is incorporated, we can do this with a straight call to // org.apache.pdfbox.TextToPdf.createPDFFromText(new StringReader("")); // https://issues.apache.org/jira/browse/PDFBOX-914 PDDocument pdfDoc = null; PDPageContentStream contentStream = null; try { pdfDoc = new PDDocument(); PDPage pdfPage = new PDPage(); // Even though, we want an empty PDF, some libs (e.g. PDFRenderer) object to PDFs // that have literally nothing in them. So we'll put a content stream in it. contentStream = new PDPageContentStream(pdfDoc, pdfPage); pdfDoc.addPage(pdfPage); // Now write the in-memory PDF document into the temporary file. pdfDoc.save(tempToFile.getAbsolutePath()); } catch (COSVisitorException cvx) { throw new ContentIOException("Error creating empty PDF file", cvx); } catch (IOException iox) { throw new ContentIOException("Error creating empty PDF file", iox); } finally { if (contentStream != null) { try { contentStream.close(); } catch (IOException ignored) { // Intentionally empty } } if (pdfDoc != null) { try { pdfDoc.close(); } catch (IOException ignored) { // Intentionally empty. } } } }
From source file:org.apache.fop.render.pdf.PageParentTreeFinderTestCase.java
License:Apache License
@Test public void testNoparentTreePresent() throws IOException { PDPage srcPage = new PDPage(); srcPage.getCOSObject().setItem(COSName.STRUCT_PARENTS, COSInteger.get(-1)); PDResources res = new PDResources(); srcPage.setResources(res);//from w w w.ja v a 2 s. c o m PageParentTreeFinder finder = new PageParentTreeFinder(srcPage); COSArray parentTree = finder.getPageParentTreeArray(null); int test = parentTree.size(); Assert.assertEquals(test, 0); }
From source file:org.apache.fop.render.pdf.PDFRotateTestCase.java
License:Apache License
@Test public void test() throws Exception { ImageConverterPDF2G2D i = new ImageConverterPDF2G2D(); ImageInfo imgi = new ImageInfo("a", "b"); PDDocument doc = new PDDocument(); PDPage page = new PDPage(); page.setRotation(90);// www. ja v a 2 s .c om doc.addPage(page); Image img = new ImagePDF(imgi, doc); ImageGraphics2D ig = (ImageGraphics2D) i.convert(img, null); Rectangle2D rect = new Rectangle2D.Float(0, 0, 100, 100); PSGraphics2D g2d = new PSPDFGraphics2D(true); GraphicContext gc = new GraphicContext(); g2d.setGraphicContext(gc); ig.getGraphics2DImagePainter().paint(g2d, rect); Assert.assertEquals(g2d.getTransform().getShearX(), 0.16339869281045735); }
From source file:org.apache.fop.render.pdf.StructureTreeMergerTestCase.java
License:Apache License
@Test public void testDirectDescedants() throws IOException { PDFStructElem elem = new PDFStructElem(); elem.setObjectNumber(100);//from w w w . j av a 2 s .c om setUp(); adapter = new PDFBoxAdapter(pdfPage, new HashMap(), new HashMap<Integer, PDFArray>()); PDFLogicalStructureHandler handler = setUpPDFLogicalStructureHandler(); PDPage srcPage = new PDPage(); StructureTreeMerger merger = new StructureTreeMerger(elem, handler, adapter, srcPage); COSArray array = new COSArray(); COSDictionary dict = new COSDictionary(); dict.setItem(COSName.S, COSName.P); COSObject obj = new COSObject(dict); obj.setObjectNumber(200); obj.setGenerationNumber(0); array.add(0, obj); merger.createDirectDescendants(array, elem); List<PDFObject> list = elem.getKids(); PDFStructElem kid = (PDFStructElem) list.get(0); PDFName name = (PDFName) kid.get("S"); String test = name.getName(); Assert.assertEquals(test, "P"); }
From source file:org.apache.pdflens.example.HelloWorld.java
License:Apache License
/** * create the second sample document from the PDF file format specification. * * @param file The file to write the PDF to. * @param message The message to write in the file. * * @throws IOException If there is an error writing the data. * @throws COSVisitorException If there is an error writing the PDF. */// w w w. ja v a2 s. co m public void doIt(String file, String message) throws IOException, COSVisitorException { // the document PDDocument doc = null; try { doc = new PDDocument(); PDPage page = new PDPage(); doc.addPage(page); PDFont font = PDType1Font.HELVETICA_BOLD; PDPageContentStream contentStream = new PDPageContentStream(doc, page); contentStream.beginText(); contentStream.setFont(font, 12); contentStream.moveTextPositionByAmount(100, 700); contentStream.drawString(message); contentStream.endText(); contentStream.close(); doc.save(file); } finally { if (doc != null) { doc.close(); } } }
From source file:org.apache.pdflens.example.HelloWorldMutiPage.java
License:Apache License
private PDPage generatePage(String message, PDDocument doc) throws IOException { PDPage page = new PDPage(); PDFont font = PDType1Font.HELVETICA_BOLD; PDPageContentStream contentStream = new PDPageContentStream(doc, page); contentStream.beginText();/*from w w w . j a v a 2 s . c o m*/ contentStream.setFont(font, 12); contentStream.moveTextPositionByAmount(100, 700); contentStream.drawString(message); contentStream.endText(); contentStream.close(); return page; }
From source file:org.data2semantics.annotate.D2S_SampleAnnotation.java
License:Apache License
/** * This will create a doucument showing various annotations. * /*from w w w.ja va 2 s .c om*/ * @param args * The command line arguments. * * @throws Exception * If there is an error parsing the document. */ public static void main(String[] args) throws Exception { PDDocument document = new PDDocument(); try { PDPage page = new PDPage(); document.addPage(page); List annotations = page.getAnnotations(); // Setup some basic reusable objects/constants // Annotations themselves can only be used once! float inch = 72; PDGamma colourRed = new PDGamma(); colourRed.setR(1); PDGamma colourBlue = new PDGamma(); colourBlue.setB(1); PDGamma colourBlack = new PDGamma(); PDBorderStyleDictionary borderThick = new PDBorderStyleDictionary(); borderThick.setWidth(inch / 12); // 12th inch PDBorderStyleDictionary borderThin = new PDBorderStyleDictionary(); borderThin.setWidth(inch / 72); // 1 point PDBorderStyleDictionary borderULine = new PDBorderStyleDictionary(); borderULine.setStyle(PDBorderStyleDictionary.STYLE_UNDERLINE); borderULine.setWidth(inch / 72); // 1 point float pw = page.getMediaBox().getUpperRightX(); float ph = page.getMediaBox().getUpperRightY(); // First add some text, two lines we'll add some annotations to this // later PDFont font = PDType1Font.HELVETICA_BOLD; PDPageContentStream contentStream = new PDPageContentStream(document, page); contentStream.beginText(); contentStream.setFont(font, 18); contentStream.moveTextPositionByAmount(inch, ph - inch - 18); contentStream.drawString("PDFBox"); contentStream.moveTextPositionByAmount(0, -(inch / 2)); contentStream.drawString("Click Here"); contentStream.endText(); contentStream.close(); // Now add the markup annotation, a highlight to PDFBox text PDAnnotationTextMarkup txtMark = new PDAnnotationTextMarkup(PDAnnotationTextMarkup.SUB_TYPE_HIGHLIGHT); txtMark.setColour(colourBlue); txtMark.setConstantOpacity((float) 0.2); // Make the highlight 20% // transparent // Set the rectangle containing the markup float textWidth = (font.getStringWidth("PDFBox") / 1000) * 18; PDRectangle position = new PDRectangle(); position.setLowerLeftX(inch); position.setLowerLeftY(ph - inch - 18); position.setUpperRightX(72 + textWidth); position.setUpperRightY(ph - inch); txtMark.setRectangle(position); // work out the points forming the four corners of the annotations // set out in anti clockwise form (Completely wraps the text) // OK, the below doesn't match that description. // It's what acrobat 7 does and displays properly! float[] quads = new float[8]; quads[0] = position.getLowerLeftX(); // x1 quads[1] = position.getUpperRightY() - 2; // y1 quads[2] = position.getUpperRightX(); // x2 quads[3] = quads[1]; // y2 quads[4] = quads[0]; // x3 quads[5] = position.getLowerLeftY() - 2; // y3 quads[6] = quads[2]; // x4 quads[7] = quads[5]; // y5 txtMark.setQuadPoints(quads); txtMark.setContents("Highlighted since it's important"); annotations.add(txtMark); // Now add the link annotation, so the clickme works PDAnnotationLink txtLink = new PDAnnotationLink(); txtLink.setBorderStyle(borderULine); // Set the rectangle containing the link textWidth = (font.getStringWidth("Click Here") / 1000) * 18; position = new PDRectangle(); position.setLowerLeftX(inch); position.setLowerLeftY(ph - (float) (1.5 * inch) - 20); // down a // couple of // points position.setUpperRightX(72 + textWidth); position.setUpperRightY(ph - (float) (1.5 * inch)); txtLink.setRectangle(position); // add an action PDActionURI action = new PDActionURI(); action.setURI("http://www.pdfbox.org"); txtLink.setAction(action); annotations.add(txtLink); // Now draw a few more annotations PDAnnotationSquareCircle aCircle = new PDAnnotationSquareCircle( PDAnnotationSquareCircle.SUB_TYPE_CIRCLE); aCircle.setContents("Circle Annotation"); aCircle.setInteriorColour(colourRed); // Fill in circle in red aCircle.setColour(colourBlue); // The border itself will be blue aCircle.setBorderStyle(borderThin); // Place the annotation on the page, we'll make this 1" round // 3" down, 1" in on the page position = new PDRectangle(); position.setLowerLeftX(inch); position.setLowerLeftY(ph - (3 * inch) - inch); // 1" height, 3" // down position.setUpperRightX(2 * inch); // 1" in, 1" width position.setUpperRightY(ph - (3 * inch)); // 3" down aCircle.setRectangle(position); // add to the annotations on the page annotations.add(aCircle); // Now a square annotation PDAnnotationSquareCircle aSquare = new PDAnnotationSquareCircle( PDAnnotationSquareCircle.SUB_TYPE_SQUARE); aSquare.setContents("Square Annotation"); aSquare.setColour(colourRed); // Outline in red, not setting a fill aSquare.setBorderStyle(borderThick); // Place the annotation on the page, we'll make this 1" (72points) // square // 3.5" down, 1" in from the right on the page position = new PDRectangle(); // Reuse the variable, but note it's a // new object! position.setLowerLeftX(pw - (2 * inch)); // 1" in from right, 1" // wide position.setLowerLeftY(ph - (float) (3.5 * inch) - inch); // 1" height, 3.5" // down position.setUpperRightX(pw - inch); // 1" in from right position.setUpperRightY(ph - (float) (3.5 * inch)); // 3.5" down aSquare.setRectangle(position); // add to the annotations on the page annotations.add(aSquare); // Now we want to draw a line between the two, one end with an open // arrow PDAnnotationLine aLine = new PDAnnotationLine(); aLine.setEndPointEndingStyle(PDAnnotationLine.LE_OPEN_ARROW); aLine.setContents("Circle->Square"); aLine.setCaption(true); // Make the contents a caption on the line // Set the rectangle containing the line position = new PDRectangle(); // Reuse the variable, but note it's a // new object! position.setLowerLeftX(2 * inch); // 1" in + width of circle position.setLowerLeftY(ph - (float) (3.5 * inch) - inch); // 1" height, 3.5" // down position.setUpperRightX(pw - inch - inch); // 1" in from right, and // width of square position.setUpperRightY(ph - (3 * inch)); // 3" down (top of circle) aLine.setRectangle(position); // Now set the line position itself float[] linepos = new float[4]; linepos[0] = 2 * inch; // x1 = rhs of circle linepos[1] = ph - (float) (3.5 * inch); // y1 halfway down circle linepos[2] = pw - (2 * inch); // x2 = lhs of square linepos[3] = ph - (4 * inch); // y2 halfway down square aLine.setLine(linepos); aLine.setBorderStyle(borderThick); aLine.setColour(colourBlack); // add to the annotations on the page annotations.add(aLine); // Finally all done document.save("testAnnotation.pdf"); } finally { document.close(); } }
From source file:org.gfbio.idmg.util.PDFUtil.java
private PDPage createNewPage() throws IOException { PDPage page = new PDPage(); document.addPage(page);//from w w w.j a v a 2 s . c o m content = new PDPageContentStream(document, page); // GFBio Logo printing printGFBioLogo(); // Begin the Content stream content.beginText(); // Setting line heigt for newLine method content.setLeading(leading); // Header printHeader(); // Set remaining yCoordinate for determining end of page yCoordinate = 731f; // Setting the position for the line content.newLineAtOffset(10, -70); yCoordinate -= 70f; return page; }
From source file:org.kay.ini.ExportPDF.java
public void createPDF(ArrayList<Connection> exportList) { PDFont font = PDType1Font.HELVETICA_BOLD; try {/*from w w w. j a v a 2s. c o m*/ PDDocument document = new PDDocument(); PDPage page = new PDPage(); document.addPage(page); PDPageContentStream content = new PDPageContentStream(document, page, true, true); content.beginText(); content.setFont(font, 12); content.moveTextPositionByAmount(100, 700); for (Connection con : exportList) { content.drawString(con.getKey() + " = " + con.getValue()); content.moveTextPositionByAmount(0, -15); } content.endText(); content.close(); document.save(saveTo); JOptionPane.showMessageDialog(null, "success"); } catch (Exception e) { JOptionPane.showMessageDialog(null, e.getMessage()); } }