List of usage examples for com.itextpdf.text Rectangle getLeft
public float getLeft()
From source file:mkl.testarea.itext5.pdfcleanup.PdfCleanUpRegionFilter.java
License:Open Source License
private Point2D[] getVertices(Rectangle rect) { Point2D[] points = { new Point2D.Double(rect.getLeft(), rect.getBottom()), new Point2D.Double(rect.getRight(), rect.getBottom()), new Point2D.Double(rect.getRight(), rect.getTop()), new Point2D.Double(rect.getLeft(), rect.getTop()) }; return points; }
From source file:mkl.testarea.itext5.pdfcleanup.PdfCleanUpRegionFilter.java
License:Open Source License
/** * Transforms the given Rectangle into the image coordinate system which is [0,1]x[0,1] by default */// ww w .ja va2s . co m private Rectangle transformIntersection(Matrix imageCTM, Rectangle rect) { Point2D[] points = transformPoints(imageCTM, true, new Point(rect.getLeft(), rect.getBottom()), new Point(rect.getLeft(), rect.getTop()), new Point(rect.getRight(), rect.getBottom()), new Point(rect.getRight(), rect.getTop())); return getRectangle(points[0], points[1], points[2], points[3]); }
From source file:mkl.testarea.itext5.pdfcleanup.PdfCleanUpRenderListener.java
License:Open Source License
private void cleanImage(BufferedImage image, List<Rectangle> areasToBeCleaned) { Graphics2D graphics = image.createGraphics(); graphics.setColor(CLEANED_AREA_FILL_COLOR); // A rectangle in the areasToBeCleaned list is treated to be in standard [0, 1]x[0,1] image space // (y varies from bottom to top and x from left to right), so we should scale the rectangle and also // invert and shear the y axe for (Rectangle rect : areasToBeCleaned) { int scaledBottomY = (int) Math.ceil(rect.getBottom() * image.getHeight()); int scaledTopY = (int) Math.floor(rect.getTop() * image.getHeight()); int x = (int) Math.ceil(rect.getLeft() * image.getWidth()); int y = scaledTopY * -1 + image.getHeight(); int width = (int) Math.floor(rect.getRight() * image.getWidth()) - x; int height = scaledTopY - scaledBottomY; graphics.fillRect(x, y, width, height); }//w ww .j a va 2s .c o m graphics.dispose(); }
From source file:mkl.testarea.itext5.pdfcleanup.StrictPdfCleanUpProcessor.java
License:Open Source License
private void addColoredRectangle(PdfContentByte canvas, PdfCleanUpLocation cleanUpLocation) { Rectangle cleanUpRegion = cleanUpLocation.getRegion(); canvas.saveState();//from w w w . j a v a 2s . co m canvas.setColorFill(cleanUpLocation.getCleanUpColor()); canvas.moveTo(cleanUpRegion.getLeft(), cleanUpRegion.getBottom()); canvas.lineTo(cleanUpRegion.getRight(), cleanUpRegion.getBottom()); canvas.lineTo(cleanUpRegion.getRight(), cleanUpRegion.getTop()); canvas.lineTo(cleanUpRegion.getLeft(), cleanUpRegion.getTop()); canvas.closePath(); canvas.fill(); canvas.restoreState(); }
From source file:mkl.testarea.itext5.pdfcleanup.StrictPdfCleanUpProcessor.java
License:Open Source License
private void insertFormXObj(PdfContentByte canvas, PdfDictionary pageDict, PdfStream formXObj, List<Rectangle> clippingRects, Rectangle annotRect) throws IOException { PdfName xobjName = generateNameForXObj(pageDict); canvas.saveState();//from w w w . jav a 2 s . co m for (Rectangle rect : clippingRects) { canvas.rectangle(rect.getLeft(), rect.getBottom(), rect.getWidth(), rect.getHeight()); } canvas.clip(); canvas.newPath(); canvas.addFormXObj(formXObj, xobjName, 1, 0, 0, 1, annotRect.getLeft(), annotRect.getBottom()); canvas.restoreState(); }
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 ww w. ja v a 2s. c om 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.PDFAddSignatureFieldActionExecuter.java
License:Open Source License
/** * @see org.alfresco.repo.action.executer.ActionExecuterAbstractBase#executeImpl(org.alfresco.service.cmr.repository.NodeRef, * org.alfresco.service.cmr.repository.NodeRef) *//*from w w w . j a v a 2 s. c o m*/ protected void executeImpl(Action ruleAction, NodeRef actionedUponNodeRef) { NodeService ns = serviceRegistry.getNodeService(); if (ns.exists(actionedUponNodeRef) == false) { // node doesn't exist - can't do anything return; } String fieldName = (String) ruleAction.getParameterValue(PARAM_FIELDNAME); String position = (String) ruleAction.getParameterValue(PARAM_POSITION); JSONObject box; int page = -1; // parse out the position JSON JSONObject positionObj = null; try { positionObj = (JSONObject) parser.parse(position); } catch (ParseException e) { logger.error("Could not parse position JSON from Share"); throw new AlfrescoRuntimeException("Could not parse position JSON from Share"); } // get the page page = Integer.parseInt(String.valueOf(positionObj.get("page"))); // get the box box = (JSONObject) positionObj.get("box"); try { // open original pdf ContentReader pdfReader = getReader(actionedUponNodeRef); PdfReader reader = new PdfReader(pdfReader.getContentInputStream()); OutputStream cos = serviceRegistry.getContentService() .getWriter(actionedUponNodeRef, ContentModel.PROP_CONTENT, true).getContentOutputStream(); PdfStamper stamp = new PdfStamper(reader, cos); // does a field with this name already exist? AcroFields allFields = stamp.getAcroFields(); // if this doc is already signed, cannot add a new sig field without if (allFields.getSignatureNames() != null && allFields.getSignatureNames().size() > 0) { throw new AlfrescoRuntimeException("This document has signatures applied, " + "adding a new signature field would invalidate existing signatures"); } // cant create duplicate field names if (allFields.getFieldType(fieldName) == AcroFields.FIELD_TYPE_SIGNATURE) { throw new AlfrescoRuntimeException( "A signature field named " + fieldName + " already exists in this document"); } // create the signature field Rectangle pageRect = reader.getPageSizeWithRotation(page); Rectangle sigRect = positionBlock(pageRect, box); PdfFormField sigField = stamp.addSignature(fieldName, page, sigRect.getLeft(), sigRect.getBottom(), sigRect.getRight(), sigRect.getTop()); // style the field (no borders) sigField.setBorder(new PdfBorderArray(0, 0, 0)); sigField.setBorderStyle(new PdfBorderDictionary(0, PdfBorderDictionary.STYLE_SOLID)); allFields.regenerateField(fieldName); // apply the change and close streams stamp.close(); reader.close(); cos.close(); // once the signature field has been added, apply the sig field aspect if (!ns.hasAspect(actionedUponNodeRef, CounterSignSignatureModel.ASPECT_SIGNABLE)) { ns.addAspect(actionedUponNodeRef, CounterSignSignatureModel.ASPECT_SIGNABLE, null); } // now update the signature fields metadata Serializable currentFields = ns.getProperty(actionedUponNodeRef, CounterSignSignatureModel.PROP_SIGNATUREFIELDS); ArrayList<String> fields = new ArrayList<String>(); if (currentFields != null) { fields.addAll((List<String>) currentFields); } fields.add(fieldName); ns.setProperty(actionedUponNodeRef, CounterSignSignatureModel.PROP_SIGNATUREFIELDS, fields); } catch (IOException ioex) { throw new AlfrescoRuntimeException(ioex.getMessage()); } catch (DocumentException dex) { throw new AlfrescoRuntimeException(dex.getMessage()); } }
From source file:org.h819.commons.file.MyPDFUtils.java
/** * ??//from w ww .j a v a 2 s . co m * * @param srcPdf ? * @param destPdf * @param waterMarkText ? * @param waterMarkImage ? */ public static void addWaterMarkFile(File srcPdf, File destPdf, String waterMarkText, File waterMarkImage) throws IOException, DocumentException { if (waterMarkText == null && waterMarkImage == null) throw new FileNotFoundException(waterMarkText + " " + waterMarkImage + " all null."); if (srcPdf == null || !srcPdf.exists() || !srcPdf.isFile()) throw new FileNotFoundException("pdf file : '" + srcPdf + "' does not exsit."); if (!FilenameUtils.getExtension(srcPdf.getAbsolutePath()).toLowerCase().equals("pdf")) return; if (waterMarkImage != null) { if (!waterMarkImage.exists() || !waterMarkImage.isFile()) throw new FileNotFoundException("img file : '" + srcPdf + "' does not exsit."); if (!FilenameUtils.getExtension(waterMarkImage.getAbsolutePath()).toLowerCase().equals("png")) throw new FileNotFoundException("image file '" + srcPdf + "' not png.(???? pdf )"); } PdfReader reader = getPdfReader(srcPdf); int n = reader.getNumberOfPages(); PdfStamper stamper = getPdfStamper(srcPdf, destPdf); // // HashMap<String, String> moreInfo = new HashMap<String, String>(); // moreInfo.put("Author", "H819 create"); // moreInfo.put("Producer", "H819 Producer"); // Key = CreationDate, Value = D:20070425182920 // Key = Producer, Value = TH-OCR 2000 (C++/Win32) // Key = Author, Value = TH-OCR 2000 // Key = Creator, Value = TH-OCR PDF Writer // stamp.setMoreInfo(moreInfo); // text Phrase text = null; if (waterMarkText != null) { // Font bfont = getPdfFont(); bfont.setSize(35); bfont.setColor(new BaseColor(192, 192, 192)); text = new Phrase(waterMarkText, bfont); } // image watermark Image img = null; float w = 0; float h = 0; if (waterMarkImage != null) { img = Image.getInstance(waterMarkImage.getAbsolutePath()); w = img.getScaledWidth(); h = img.getScaledHeight(); // img. img.setRotationDegrees(45); } // transparency PdfGState gs1 = new PdfGState(); gs1.setFillOpacity(0.5f); // properties PdfContentByte over; Rectangle pageSize; float x, y; // loop over every page for (int i = 1; i <= n; i++) { pageSize = reader.getPageSizeWithRotation(i); x = (pageSize.getLeft() + pageSize.getRight()) / 2; y = (pageSize.getTop() + pageSize.getBottom()) / 2; // pdf pdf ??? over = stamper.getOverContent(i); // ? // over = stamp.getUnderContent(i); // ?? over.beginText(); over.endText(); ? // ,?,:???? over.saveState(); //?? over.setGState(gs1); if (waterMarkText != null && waterMarkImage != null) { // if (i % 2 == 1) { ColumnText.showTextAligned(over, Element.ALIGN_CENTER, text, x, y, 45); } else over.addImage(img, w, 0, 0, h, x - (w / 2), y - (h / 2)); } else if (waterMarkText != null) { //? ColumnText.showTextAligned(over, Element.ALIGN_CENTER, text, x, y, 45); //?? ,?, :????? // ... } else { //? over.addImage(img, w, 0, 0, h, x - (w / 2), y - (h / 2)); } over.restoreState();//??? } stamper.close(); reader.close(); }
From source file:org.me.modelos.HeaderFooterPageEvent.java
@Override public void onStartPage(PdfWriter writer, Document document) { Rectangle rect = writer.getBoxSize("art"); ColumnText.showTextAligned(writer.getDirectContent(), Element.ALIGN_CENTER, new Phrase(motelNombre + " " + direccion + " " + telefono, font), rect.getLeft(), rect.getTop(), 0); ColumnText.showTextAligned(writer.getDirectContent(), Element.ALIGN_CENTER, new Phrase(fecha, font), rect.getRight(), rect.getTop(), 0); }
From source file:ro.ldir.chartpackage.GarbagePackageBuilder.java
License:Open Source License
public void writePDF(OutputStream out) throws DocumentException, MalformedURLException, XPathExpressionException, IOException { BaseFont bf = BaseFont.createFont(BaseFont.HELVETICA, "Cp1250", BaseFont.NOT_EMBEDDED); final Font hfFont = new Font(bf, 8, Font.NORMAL, BaseColor.GRAY); Document document = new Document(PageSize.A4, 50, 50, 50, 50); PdfWriter writer = PdfWriter.getInstance(document, out); writer.setBoxSize("art", new Rectangle(36, 54, 559, 788)); writer.setPageEvent(new PdfPageEventHelper() { private int page = 0; @Override// w ww. j a v a 2 s. com public void onEndPage(PdfWriter writer, Document arg1) { page++; Rectangle rect = writer.getBoxSize("art"); ColumnText.showTextAligned(writer.getDirectContent(), Element.ALIGN_CENTER, new Phrase("Pachet mormane - \u00a9 Let's Do It, Romania!", hfFont), (rect.getLeft() + rect.getRight()) / 2, rect.getTop() + 18, 0); ColumnText.showTextAligned(writer.getDirectContent(), Element.ALIGN_CENTER, new Phrase("- " + page + " -", hfFont), (rect.getLeft() + rect.getRight()) / 2, rect.getBottom() - 18, 0); } }); document.open(); document.addAuthor("Let's Do It, Romania!"); document.addTitle("Pachet mormane"); document.addCreationDate(); Font titleFont = new Font(bf, 24, Font.BOLD); Font noteFont = new Font(bf, 12, Font.NORMAL, BaseColor.RED); Font headerFont = new Font(bf, 12, Font.BOLD); Font normalFont = new Font(bf, 11); Font defFont = new Font(bf, 11, Font.BOLD); Paragraph par; int page = 0; for (Garbage garbage : garbages) { par = new Paragraph(); par.setAlignment(Element.ALIGN_CENTER); par.add(new Chunk("Morman " + garbage.getGarbageId() + "\n", titleFont)); par.add(new Chunk("Citi\u0163i cu aten\u0163ie!", noteFont)); document.add(par); par = new Paragraph(); par.setSpacingBefore(20); par.add(new Chunk("1. Date generale\n", headerFont)); par.add(new Chunk("Jude\u0163ul: ", defFont)); par.add(new Chunk(garbage.getCounty().getName() + "\n", normalFont)); par.add(new Chunk("Comuna: ", defFont)); par.add(new Chunk(garbage.getTown().getName() + "\n", normalFont)); if (garbage.getChartedArea() != null) { par.add(new Chunk("Zona cartare: ", defFont)); par.add(new Chunk(garbage.getChartedArea().getName() + "\n", normalFont)); } par.add(new Chunk("Pozi\u0163ie: ", defFont)); par.add(new Chunk(garbage.getY() + ", " + garbage.getX() + "\n", normalFont)); par.add(new Chunk("Descriere:\n", defFont)); par.add(new Phrase(garbage.getDescription() + "\n", normalFont)); par.add(new Chunk("Componen\u0163\u0103 gunoi:\n", defFont)); List list = new List(); list.add(new ListItem( new Chunk("Procent plastic: " + garbage.getPercentagePlastic() + "%", normalFont))); list.add(new ListItem( new Chunk("Procent sticl\u0103: " + garbage.getPercentageGlass() + "%", normalFont))); list.add(new ListItem(new Chunk("Procent metale: " + garbage.getPercentageMetal() + "%", normalFont))); list.add(new ListItem( new Chunk("Procent nereciclabile: " + garbage.getPercentageWaste() + "%", normalFont))); par.add(list); document.add(par); par = new Paragraph(); par.setSpacingBefore(20); par.add(new Chunk("2. Indica\u0163ii rutiere\n", headerFont)); Image img = Image.getInstance(getImage(garbage)); img.scaleToFit((float) (PageSize.A4.getWidth() * .75), (float) (PageSize.A4.getHeight() * .75)); img.setAlignment(Element.ALIGN_CENTER); par.add(img); document.add(par); if (page < garbages.size() - 1) document.newPage(); page++; } document.close(); }