List of usage examples for com.itextpdf.text Rectangle getWidth
public float getWidth()
From source file:net.ionescu.jhocr2pdf.Jhocr2pdf.java
License:Open Source License
public void addSpan(String coords, String text) { // parse the coordinates String[] coord = coords.split(" "); Rectangle rect = new Rectangle(Float.valueOf(coord[1]), Float.valueOf(coord[4]), Float.valueOf(coord[3]), Float.valueOf(coord[2])); canvas.saveState();//from w ww .j a v a2s. c o m canvas.beginText(); canvas.setTextRenderingMode(PdfContentByte.TEXT_RENDER_MODE_FILL); canvas.setRGBColorFill(0xFF, 0, 0); canvas.setLineWidth(0); // calculate the font size float width = rect.getWidth() * xScaling; float tenWidth = bf.getWidthPointKerned(text, 10); float fontSize = 10 * width / tenWidth; canvas.setFontAndSize(bf, fontSize); canvas.showTextAlignedKerned(Element.ALIGN_LEFT, text, rect.getLeft() * xScaling, PageSize.A4.getHeight() - rect.getBottom() * yScaling - bf.getDescentPoint(text, fontSize), 0); canvas.endText(); canvas.restoreState(); }
From source file:org.alfresco.extension.countersign.action.executer.AbstractSignatureActionExecuter.java
License:Open Source License
/** * Create a rectangle for the visible stamp using the selected position and block size * /* w ww. j av a 2 s. c o m*/ * @param position * @param width * @param height * @return */ protected Rectangle positionBlock(String position, Rectangle pageRect, int width, int height) { float pageHeight = pageRect.getHeight(); float pageWidth = pageRect.getWidth(); Rectangle r = null; //Rectangle constructor(float llx, float lly, float urx, float ury) if (position.equals(POSITION_BOTTOMLEFT)) { r = new Rectangle(0, height, width, 0); } else if (position.equals(POSITION_BOTTOMRIGHT)) { r = new Rectangle(pageWidth - width, height, pageWidth, 0); } else if (position.equals(POSITION_TOPLEFT)) { r = new Rectangle(0, pageHeight, width, pageHeight - height); } else if (position.equals(POSITION_TOPRIGHT)) { r = new Rectangle(pageWidth - width, pageHeight, pageWidth, pageHeight - height); } else if (position.equals(POSITION_CENTER)) { r = new Rectangle((pageWidth / 2) - (width / 2), (pageHeight / 2) - (height / 2), (pageWidth / 2) + (width / 2), (pageHeight / 2) + (height / 2)); } return r; }
From source file:org.alfresco.extension.pdftoolkit.repo.action.executer.PDFSignatureActionExecuter.java
License:Apache License
/** * Create a rectangle for the visible signature using the selected position and signature size * //from w w w. java 2 s. co m * @param position * @param width * @param height * @return */ private Rectangle positionSignature(String position, Rectangle pageRect, int width, int height) { float pageHeight = pageRect.getHeight(); float pageWidth = pageRect.getWidth(); Rectangle r = null; if (position.equals(POSITION_BOTTOMLEFT)) { r = new Rectangle(0, height, width, 0); } else if (position.equals(POSITION_BOTTOMRIGHT)) { r = new Rectangle(pageWidth - width, pageHeight, pageWidth, pageHeight - height); } else if (position.equals(POSITION_TOPLEFT)) { r = new Rectangle(0, pageHeight, width, pageHeight - height); } else if (position.equals(POSITION_TOPRIGHT)) { r = new Rectangle(pageWidth - width, height, pageWidth, 0); } else if (position.equals(POSITION_CENTER)) { r = new Rectangle((pageWidth / 2) - (width / 2), (pageHeight / 2) - (height / 2), (pageWidth / 2) + (width / 2), (pageHeight / 2) + (height / 2)); } return r; }
From source file:org.alfresco.extension.pdftoolkit.repo.action.executer.PDFWatermarkActionExecuter.java
License:Apache License
/** * Applies an image watermark/*from www .j a v a2s .co m*/ * * @param reader * @param writer * @param options * @throws Exception */ private void imageAction(Action ruleAction, NodeRef actionedUponNodeRef, NodeRef watermarkNodeRef, ContentReader actionedUponContentReader, ContentReader watermarkContentReader, Map<String, Object> options) { PdfStamper stamp = null; File tempDir = null; ContentWriter writer = null; try { // get a temp file to stash the watermarked PDF in before moving to // repo File alfTempDir = TempFileProvider.getTempDir(); tempDir = new File(alfTempDir.getPath() + File.separatorChar + actionedUponNodeRef.getId()); tempDir.mkdir(); File file = new File(tempDir, serviceRegistry.getFileFolderService().getFileInfo(actionedUponNodeRef).getName()); // get the PDF input stream and create a reader for iText PdfReader reader = new PdfReader(actionedUponContentReader.getContentInputStream()); stamp = new PdfStamper(reader, new FileOutputStream(file)); PdfContentByte pcb; // get a com.itextpdf.text.Image object via java.imageio.ImageIO Image img = Image.getInstance(ImageIO.read(watermarkContentReader.getContentInputStream()), null); // get the PDF pages and position String pages = (String) options.get(PARAM_PAGE); String position = (String) options.get(PARAM_POSITION); String depth = (String) options.get(PARAM_WATERMARK_DEPTH); // image requires absolute positioning or an exception will be // thrown // set image position according to parameter. Use // PdfReader.getPageSizeWithRotation // to get the canvas size for alignment. img.setAbsolutePosition(100f, 100f); // stamp each page int numpages = reader.getNumberOfPages(); for (int i = 1; i <= numpages; i++) { Rectangle r = reader.getPageSizeWithRotation(i); // set stamp position if (position.equals(POSITION_BOTTOMLEFT)) { img.setAbsolutePosition(0, 0); } else if (position.equals(POSITION_BOTTOMRIGHT)) { img.setAbsolutePosition(r.getWidth() - img.getWidth(), 0); } else if (position.equals(POSITION_TOPLEFT)) { img.setAbsolutePosition(0, r.getHeight() - img.getHeight()); } else if (position.equals(POSITION_TOPRIGHT)) { img.setAbsolutePosition(r.getWidth() - img.getWidth(), r.getHeight() - img.getHeight()); } else if (position.equals(POSITION_CENTER)) { img.setAbsolutePosition(getCenterX(r, img), getCenterY(r, img)); } // if this is an under-text stamp, use getUnderContent. // if this is an over-text stamp, usse getOverContent. if (depth.equals(DEPTH_OVER)) { pcb = stamp.getOverContent(i); } else { pcb = stamp.getUnderContent(i); } // only apply stamp to requested pages if (checkPage(pages, i, numpages)) { pcb.addImage(img); } } stamp.close(); // Get a writer and prep it for putting it back into the repo //can't use BasePDFActionExecuter.getWriter here need the nodeRef of the destination NodeRef destinationNode = createDestinationNode(file.getName(), (NodeRef) ruleAction.getParameterValue(PARAM_DESTINATION_FOLDER), actionedUponNodeRef); writer = serviceRegistry.getContentService().getWriter(destinationNode, ContentModel.PROP_CONTENT, true); writer.setEncoding(actionedUponContentReader.getEncoding()); writer.setMimetype(FILE_MIMETYPE); // Put it in the repo writer.putContent(file); // delete the temp file file.delete(); } catch (IOException e) { throw new AlfrescoRuntimeException(e.getMessage(), e); } catch (DocumentException e) { throw new AlfrescoRuntimeException(e.getMessage(), e); } finally { if (tempDir != null) { try { tempDir.delete(); } catch (Exception ex) { throw new AlfrescoRuntimeException(ex.getMessage(), ex); } } if (stamp != null) { try { stamp.close(); } catch (Exception ex) { throw new AlfrescoRuntimeException(ex.getMessage(), ex); } } } }
From source file:org.alfresco.extension.pdftoolkit.repo.action.executer.PDFWatermarkActionExecuter.java
License:Apache License
/** * Writes text watermark to one of the 5 preconfigured locations * /* ww w . j a v a2 s . c o m*/ * @param pcb * @param r * @param tokens * @param size * @param position */ private void writeAlignedText(PdfContentByte pcb, Rectangle r, Vector<String> tokens, float size, String position) { // get the dimensions of our 'rectangle' for text float height = size * tokens.size(); float width = 0; float centerX = 0, startY = 0; for (int i = 0; i < tokens.size(); i++) { if (pcb.getEffectiveStringWidth(tokens.get(i), false) > width) { width = pcb.getEffectiveStringWidth(tokens.get(i), false); } } // now that we have the width and height, we can calculate the center // position for // the rectangle that will contain our text. if (position.equals(POSITION_BOTTOMLEFT)) { centerX = width / 2 + PAD; startY = 0 + PAD + height; } else if (position.equals(POSITION_BOTTOMRIGHT)) { centerX = r.getWidth() - (width / 2) - PAD; startY = 0 + PAD + height; } else if (position.equals(POSITION_TOPLEFT)) { centerX = width / 2 + PAD; startY = r.getHeight() - (PAD * 2); } else if (position.equals(POSITION_TOPRIGHT)) { centerX = r.getWidth() - (width / 2) - PAD; startY = r.getHeight() - (PAD * 2); } else if (position.equals(POSITION_CENTER)) { centerX = r.getWidth() / 2; startY = (r.getHeight() / 2) + (height / 2); } // apply text to PDF pcb.beginText(); for (int t = 0; t < tokens.size(); t++) { pcb.showTextAligned(PdfContentByte.ALIGN_CENTER, tokens.get(t), centerX, startY - (size * t), 0); } pcb.endText(); }
From source file:org.ganttproject.impex.htmlpdf.itext.ChartWriter.java
License:GNU General Public License
private Graphics2D getGraphics(ChartDimensions d) { if (myGraphics == null) { myTemplate = myWriter.getDirectContent().createTemplate(d.getFullWidth(), d.getChartHeight()); Rectangle page = myDoc.getPageSize(); final float width = page.getWidth() - myDoc.leftMargin() - myDoc.rightMargin(); final float height = page.getHeight() - myDoc.bottomMargin() - myDoc.topMargin(); final float xscale = width / d.getFullWidth(); final float yscale = height / d.getChartHeight(); myScale = Math.min(xscale, yscale); myYShift = height - d.getChartHeight() * myScale + myDoc.bottomMargin(); myGraphics = myTemplate.createGraphics(d.getFullWidth(), d.getChartHeight(), myFontCache.getFontMapper(mySubstitutions, myCharset)); myGraphics.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_OFF); }//from w w w .j a v a2 s . c om return myGraphics; }
From source file:org.ganttproject.impex.htmlpdf.itext.ThemeImpl.java
License:GNU General Public License
private void writeTitlePage() throws DocumentException { Rectangle page = myDoc.getPageSize(); PdfPTable head = new PdfPTable(1); PdfPTable colontitleTable = createColontitleTable(getProject().getProjectName(), GanttLanguage.getInstance().getMediumDateFormat().format(new Date()), getProject().getOrganization(), getProject().getWebLink()); head.setTotalWidth(page.getWidth() - myDoc.leftMargin() - myDoc.rightMargin()); {//from w w w.ja v a2 s . c om PdfPCell cell = new PdfPCell(colontitleTable); cell.setBorder(PdfPCell.NO_BORDER); head.addCell(cell); } addEmptyRow(head, 20); LinkedHashMap<String, String> attrs = new LinkedHashMap<>(); attrs.put(i18n("label.project_manager"), buildManagerString()); attrs.put(i18n("label.dates"), buildProjectDatesString()); attrs.put(" ", " "); attrs.put(i18n("label.completion"), buildProjectCompletionString()); attrs.put(i18n("label.tasks"), String.valueOf(getProject().getTaskManager().getTaskCount())); attrs.put(i18n("label.resources"), String.valueOf(getProject().getHumanResourceManager().getResources().size())); PdfPTable attrsTable = new PdfPTable(2); writeAttributes(attrsTable, attrs); PdfPCell attrsCell = new PdfPCell(attrsTable); attrsCell.setBorder(PdfPCell.NO_BORDER); head.addCell(attrsCell); addEmptyRow(head, 20); if (getProject().getDescription().length() > 0) { Paragraph p = new Paragraph(getProject().getDescription(), getSansRegular(12)); PdfPCell cell = new PdfPCell(p); cell.setBorder(PdfPCell.TOP | PdfPCell.BOTTOM); cell.setBorderColor(SORTAVALA_GREEN); cell.setBorderWidth(1); cell.setPadding(5); cell.setVerticalAlignment(PdfPCell.ALIGN_CENTER); head.addCell(cell); } myDoc.add(head); }
From source file:org.ganttproject.impex.htmlpdf.itext.ThemeImpl.java
License:GNU General Public License
private PdfPTable createColontitleTable(String topLeft, String topRight, String bottomLeft, String bottomRight) {//w w w .j a v a2s. com PdfPTable head = new PdfPTable(2); { PdfPCell cell = new PdfPCell(); cell.setBorder(Rectangle.NO_BORDER); Paragraph p = new Paragraph(topLeft, getSansRegularBold(18)); p.setAlignment(Paragraph.ALIGN_LEFT); // colontitle.setLeading(0); cell.setHorizontalAlignment(Element.ALIGN_LEFT); cell.setVerticalAlignment(Element.ALIGN_BOTTOM); // cell.setPaddingLeft(2); cell.setPaddingBottom(6); cell.setPhrase(p); head.addCell(cell); } { PdfPCell cell = new PdfPCell(); cell.setBorder(Rectangle.NO_BORDER); Paragraph p = new Paragraph(topRight, getSansRegularBold(10)); p.setAlignment(Paragraph.ALIGN_RIGHT); cell.setHorizontalAlignment(Element.ALIGN_RIGHT); cell.setVerticalAlignment(Element.ALIGN_BOTTOM); cell.setPaddingBottom(6); cell.setPhrase(p); head.addCell(cell); } { PdfPCell cell = new PdfPCell(); cell.setVerticalAlignment(Element.ALIGN_TOP); cell.setHorizontalAlignment(Element.ALIGN_LEFT); cell.setPaddingLeft(3); cell.setPaddingTop(2); cell.setPaddingBottom(6); cell.setBorder(Rectangle.TOP); cell.setBorderWidthTop(2); cell.setBorderColor(SORTAVALA_GREEN); Paragraph p = new Paragraph(bottomLeft, getSansRegularBold(18)); p.setAlignment(Paragraph.ALIGN_LEFT); p.setExtraParagraphSpace(0); p.setIndentationLeft(0); p.setSpacingBefore(0); cell.setPhrase(p); // cell.addElement(p); head.addCell(cell); } { PdfPCell cell = new PdfPCell(); cell.setVerticalAlignment(Element.ALIGN_TOP); cell.setHorizontalAlignment(Element.ALIGN_RIGHT); cell.setPaddingTop(2); cell.setPaddingBottom(6); cell.setBorder(Rectangle.TOP); cell.setBorderWidthTop(2); cell.setBorderColor(SORTAVALA_GREEN); Paragraph p = new Paragraph(bottomRight, getSansRegularBold(10)); p.setAlignment(Paragraph.ALIGN_RIGHT); cell.setPhrase(p); head.addCell(cell); } final Document document = myDoc; Rectangle page = document.getPageSize(); head.setTotalWidth(page.getWidth() - document.leftMargin() - document.rightMargin()); return head; }
From source file:org.gmdev.pdftrick.engine.MergeFiles.java
License:Open Source License
/** * Materially multiple pdf files are written merged file on a disk * @param list// w ww. ja va2s . co m * @param outputStream * @throws DocumentException * @throws IOException */ private void doMerge(List<StreamPwdContainer> list, OutputStream outputStream) throws DocumentException, IOException { HashMap<Integer, String> rotationFromPages = factory.getRotationFromPages(); Document document = new Document(); PdfWriter writer = PdfWriter.getInstance(document, outputStream); document.open(); PdfContentByte cb = writer.getDirectContent(); int z = 0; for (StreamPwdContainer boom : list) { InputStream in = boom.getIn(); PdfReader reader = null; if (!boom.getPwd().equalsIgnoreCase("")) { reader = new PdfReader(in, boom.getPwd().getBytes()); } else { reader = new PdfReader(in); } for (int i = 1; i <= reader.getNumberOfPages(); i++) { z++; int rotation = reader.getPageRotation(i); //set size Rectangle pageSize_ = reader.getPageSize(i); Rectangle pageSize = null; if (rotation == 270 || rotation == 90) { pageSize = new Rectangle(pageSize_.getHeight(), pageSize_.getWidth()); } else { pageSize = pageSize_; } document.setPageSize(pageSize); writer.setCropBoxSize(pageSize); document.newPage(); // import the page from source pdf PdfImportedPage page = writer.getImportedPage(reader, i); // add the page to the destination pdf if (rotation == 270) { cb.addTemplate(page, 0, 1.0f, -1.0f, 0, reader.getPageSizeWithRotation(i).getWidth(), 0); rotationFromPages.put(z, "" + rotation); } else if (rotation == 180) { cb.addTemplate(page, -1f, 0, 0, -1f, 0, 0); rotationFromPages.put(z, "" + rotation); } else if (rotation == 90) { cb.addTemplate(page, 0, -1f, 1f, 0, 0, reader.getPageSizeWithRotation(i).getHeight()); rotationFromPages.put(z, "" + rotation); } else { cb.addTemplate(page, 1f, 0, 0, 1f, 0, 0); } } in.close(); } outputStream.flush(); document.close(); outputStream.close(); }
From source file:org.imos.abos.plot.JfreeChartDemo.java
License:Open Source License
protected void createPDF(String filename) { try {/*w ww .j a v a 2s . c o m*/ Rectangle page = PageSize.A4.rotate(); // step 1 Document document = new Document(page); // step 2 PdfWriter writer = PdfWriter.getInstance(document, new FileOutputStream(filename)); // step 3 document.open(); // step 4 PdfContentByte cb = writer.getDirectContent(); float width = page.getWidth(); float height = page.getHeight(); // add chart PdfTemplate pie = cb.createTemplate(width, height); Graphics2D g2d1 = new PdfGraphics2D(pie, width, height); Rectangle2D r2d1 = new Rectangle2D.Double(0, 0, width, height); chart.draw(g2d1, r2d1); g2d1.dispose(); cb.addTemplate(pie, 0, 0); // step 5 document.close(); } catch (DocumentException ex) { Logger.getLogger(JfreeChartDemo.class.getName()).log(Level.SEVERE, null, ex); } catch (FileNotFoundException ex) { Logger.getLogger(JfreeChartDemo.class.getName()).log(Level.SEVERE, null, ex); } System.out.println("PDF finsihed"); }