List of usage examples for com.itextpdf.text Rectangle getRight
public float getRight()
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) */// w w w . jav a 2 s. co 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
/** * ??// ww w.j a v a2 s. c om * * @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/*from w ww . j ava 2 s . c om*/ 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(); }
From source file:timeclock.reports.HeaderFooter.java
/** * Adds the header and the footer.//w w w.j av a 2 s. c o m * * @see com.itextpdf.text.pdf.PdfPageEventHelper#onEndPage( * com.itextpdf.text.pdf.PdfWriter, com.itextpdf.text.Document) */ @Override public void onEndPage(PdfWriter writer, Document document) { Rectangle rect = writer.getBoxSize("art"); // Show page header at top center ColumnText.showTextAligned(writer.getDirectContent(), Element.ALIGN_CENTER, header, (rect.getLeft() + rect.getRight()) / 2, rect.getTop() + 12, 0); // Show page number at bottom center ColumnText.showTextAligned(writer.getDirectContent(), Element.ALIGN_CENTER, new Phrase(String.format("page %d", pagenumber)), (rect.getLeft() + rect.getRight()) / 2, rect.getBottom() - 18, 0); }
From source file:watermarkpdf.FXMLDocumentController.java
@FXML private void handleButtonAction(ActionEvent event) throws DocumentException, IOException { if (tfPhrase.getLength() > 0) { lbl_Result.setText(""); PdfReader reader = new PdfReader(tf_PathFile.getText()); Rectangle mediabox = reader.getPageSize(1); PdfStamper stamper = new PdfStamper(reader, new FileOutputStream( tf_PathFile.getText().substring(0, tf_PathFile.getLength() - 4) + "_out.pdf")); stamper.setRotateContents(false); BaseFont bf = BaseFont.createFont("TNR.ttf", BaseFont.IDENTITY_H, BaseFont.EMBEDDED); Font font = new Font(bf); int iPos = tfPhrase.getText().indexOf('#'); for (int i = 1; i <= reader.getNumberOfPages(); i++) { PdfContentByte canvas = stamper.getOverContent(i); if (iPos >= 0) { if (cbox_LU.isSelected()) { switch (i) { case 1: ColumnText.showTextAligned(canvas, Element.ALIGN_LEFT, new Phrase((tfPhrase.getText().substring(0, iPos - 5) + "? ? " + tfPhrase.getText().substring(iPos + 1)), font), mediabox.getRight() / 3, mediabox.getTop() - 40, 0); break; case 2: ColumnText.showTextAligned(canvas, Element.ALIGN_LEFT, new Phrase((tfPhrase.getText().substring(0, iPos - 5) + " ? " + tfPhrase.getText().substring(iPos + 1)), font), mediabox.getRight() / 3, mediabox.getTop() - 40, 0); break; default: ColumnText.showTextAligned(canvas, Element.ALIGN_LEFT, new Phrase((tfPhrase.getText().substring(0, iPos) + (i - 1) + tfPhrase.getText().substring(iPos + 1)), font), mediabox.getRight() / 3, mediabox.getTop() - 40, 0); }/*from w ww . jav a 2 s.c o m*/ } else { if (i == 1) ColumnText.showTextAligned(canvas, Element.ALIGN_LEFT, new Phrase((tfPhrase.getText().substring(0, iPos - 5) + " ? " + tfPhrase.getText().substring(iPos + 1)), font), mediabox.getRight() / 3, mediabox.getTop() - 40, 0); else ColumnText.showTextAligned(canvas, Element.ALIGN_LEFT, new Phrase((tfPhrase.getText().substring(0, iPos) + (i) + tfPhrase.getText().substring(iPos + 1)), font), mediabox.getRight() / 3, mediabox.getTop() - 40, 0); } } else { ColumnText.showTextAligned(canvas, Element.ALIGN_LEFT, new Phrase(tfPhrase.getText(), font), mediabox.getRight() / 3, mediabox.getTop() - 40, 0); } ColumnText.showTextAligned(canvas, Element.ALIGN_LEFT, new Phrase(tf_append.getText(), font), mediabox.getRight() * 2 / 3, mediabox.getTop() - 20, 0); } stamper.close(); reader.close(); if (tf_PagePrint.getLength() > 0) { reader = new PdfReader( tf_PathFile.getText().substring(0, tf_PathFile.getLength() - 4) + "_out.pdf"); reader.selectPages(tf_PagePrint.getText()); stamper = new PdfStamper(reader, new FileOutputStream( tf_PathFile.getText().substring(0, tf_PathFile.getLength() - 4) + "_out_cut.pdf")); stamper.close(); reader.close(); } lbl_Result.setText(" "); } }