List of usage examples for com.lowagie.text Rectangle Rectangle
public Rectangle(float llx, float lly, float urx, float ury)
Rectangle
-object. From source file:classroom.newspaper_b.Newspaper11.java
@SuppressWarnings("unchecked") public static void main(String[] args) { try {//from w ww . ja v a 2 s . c om PdfReader reader = new PdfReader(NEWSPAPER); PdfStamper stamper = new PdfStamper(reader, new FileOutputStream(RESULT)); PdfContentByte canvas = stamper.getOverContent(1); canvas.setRGBColorFill(0xFF, 0xFF, 0xFF); canvas.rectangle(LLX1, LLY1, W1, H1); canvas.rectangle(LLX2, LLY2, W2, H2); canvas.fill(); addTextField(stamper, new Rectangle(LLX1, LLY1, URX1, URY1), "field1", 1); addTextField(stamper, new Rectangle(LLX2, LLY2, URX2, URY2), "field2", 1); stamper.close(); reader = new PdfReader(RESULT); AcroFields fields = reader.getAcroFields(); Set<String> fieldnames = fields.getFields().keySet(); for (String fieldname : fieldnames) { System.out.print(fieldname); System.out.print(": page "); float[] positions = fields.getFieldPositions(fieldname); System.out.print(positions[0]); System.out.print(" [ "); System.out.print(positions[1]); System.out.print(", "); System.out.print(positions[2]); System.out.print(", "); System.out.print(positions[3]); System.out.print(", "); System.out.print(positions[4]); System.out.println("]"); } } catch (IOException e) { e.printStackTrace(); } catch (DocumentException e) { e.printStackTrace(); } }
From source file:classroom.newspaper_b.Newspaper13.java
public static void main(String[] args) { try {//from w w w. j a va2s . c om PdfReader reader = new PdfReader(NEWSPAPER); PdfStamper stamper = new PdfStamper(reader, new FileOutputStream(RESULT)); addButton(stamper, new Rectangle(LLX1, LLY1, URX1, URY1), PATH1, "button1", 1); addButton(stamper, new Rectangle(LLX2, LLY2, URX2, URY2), PATH2, "button2", 1); stamper.close(); } catch (IOException e) { e.printStackTrace(); } catch (DocumentException e) { e.printStackTrace(); } }
From source file:com.geek.tutorial.itext.acroform.ListFieldForm.java
License:Open Source License
public ListFieldForm() throws Exception { Document document = new Document(); PdfWriter writer = PdfWriter.getInstance(document, new FileOutputStream("ListFieldForm.pdf")); document.open();//from ww w . j ava 2 s .co m // Code 1 String options[] = { "PS3", "XBOX 360", "Wii", "PSP", "NDS", "GBA" }; // Code 2 create drop-down list PdfFormField dropDown = PdfFormField.createCombo(writer, true, options, 0); dropDown.setWidget(new Rectangle(50, 785, 120, 800), PdfAnnotation.HIGHLIGHT_INVERT); dropDown.setFieldName("dropDownList"); dropDown.setValueAsString("PS3"); dropDown.setMKBorderColor(Color.BLACK); writer.addAnnotation(dropDown); // Code 3 create scrollable list TextField scrollableList = new TextField(writer, new Rectangle(150, 740, 250, 800), "scrollableList"); scrollableList.setBackgroundColor(Color.WHITE); scrollableList.setBorderColor(Color.BLUE); scrollableList.setBorderWidth(2); scrollableList.setBorderStyle(PdfBorderDictionary.STYLE_SOLID); scrollableList.setFontSize(10); scrollableList.setChoices(options); scrollableList.setChoiceSelection(0); writer.addAnnotation(scrollableList.getListField()); // Code 4 add function and button for showing state writer.addJavaScript( "function showState(){" + "app.alert('DropDown:'+ this.getField('dropDownList').value +'\\n'+" + "'Scrollable List:'+this.getField('scrollableList').value);" + "}"); PushbuttonField push = new PushbuttonField(writer, new Rectangle(70, 710, 140, 730), "pushAction"); push.setBackgroundColor(Color.LIGHT_GRAY); push.setBorderColor(Color.GRAY); push.setText("Show State"); push.setBorderStyle(PdfBorderDictionary.STYLE_BEVELED); push.setTextColor(Color.BLACK); PdfFormField pushbutton = push.getField(); pushbutton.setAction(PdfAction.javaScript("showState()", writer)); writer.addAnnotation(pushbutton); document.close(); }
From source file:com.geek.tutorial.itext.acroform.RadioCheckBoxForm.java
License:Open Source License
public RadioCheckBoxForm() throws Exception { Document document = new Document(); PdfWriter writer = PdfWriter.getInstance(document, new FileOutputStream("RadioCheckBoxForm.pdf")); document.open();/*from ww w. j a va 2 s .c om*/ PdfContentByte cb = writer.getDirectContent(); BaseFont bf = BaseFont.createFont(BaseFont.TIMES_ROMAN, BaseFont.WINANSI, BaseFont.NOT_EMBEDDED); Rectangle rect; // Code 1 create radio button String[] radios = { "Radio1", "Radio2", "Radio3" }; PdfFormField radioField = PdfFormField.createRadioButton(writer, true); radioField.setFieldName("radioField"); radioField.setValueAsName(radios[0]); for (int i = 0; i < radios.length; i++) { rect = new Rectangle(40, 815 - i * 30, 60, 797 - i * 30); addRadioButton(writer, rect, radioField, radios[i], i == 0); cb.beginText(); cb.setFontAndSize(bf, 12); cb.showTextAligned(Element.ALIGN_LEFT, radios[i], 70, 802 - i * 30, 0); cb.endText(); } writer.addAnnotation(radioField); // Code 2 create checkbox button String[] options = { "Check1", "Check2", "Check3" }; for (int i = 0; i < options.length; i++) { rect = new Rectangle(160, 815 - i * 30, 180, 797 - i * 30); addCheckbox(writer, rect, options[i]); cb.beginText(); cb.setFontAndSize(bf, 12); cb.showTextAligned(Element.ALIGN_LEFT, options[i], 190, 802 - i * 30, 0); cb.endText(); } // Code 3 add function and button for showing state writer.addJavaScript( "function showState(){" + "app.alert('Radio:'+ this.getField('radioField').value +'\\n\\n'+" + "'Check1:'+this.getField('Check1').value +'\\n'+" + "'Check2:'+this.getField('Check2').value +'\\n'+" + "'Check3:'+this.getField('Check3').value);" + "}"); PushbuttonField push = new PushbuttonField(writer, new Rectangle(80, 710, 150, 730), "pushAction"); push.setBackgroundColor(Color.LIGHT_GRAY); push.setBorderColor(Color.GRAY); push.setText("Show State"); push.setBorderStyle(PdfBorderDictionary.STYLE_BEVELED); push.setTextColor(Color.BLACK); PdfFormField pushbutton = push.getField(); pushbutton.setAction(PdfAction.javaScript("showState()", writer)); writer.addAnnotation(pushbutton); document.close(); }
From source file:com.geek.tutorial.itext.acroform.TextFieldForm.java
License:Open Source License
public TextFieldForm() throws Exception { Document document = new Document(); PdfWriter writer = PdfWriter.getInstance(document, new FileOutputStream("TextFieldForm.pdf")); document.open();/* ww w . java 2 s .co m*/ PdfPTable table = new PdfPTable(2); table.getDefaultCell().setPadding(5f); // Code 1, will only affect empty field table.setHorizontalAlignment(Element.ALIGN_LEFT); PdfPCell cell; // Code 2, add name TextField table.addCell("Name"); TextField nameField = new TextField(writer, new Rectangle(0, 0, 200, 10), "nameField"); nameField.setBackgroundColor(Color.WHITE); nameField.setBorderColor(Color.BLACK); nameField.setBorderWidth(1); nameField.setBorderStyle(PdfBorderDictionary.STYLE_SOLID); nameField.setText(""); nameField.setAlignment(Element.ALIGN_LEFT); nameField.setOptions(TextField.REQUIRED); cell = new PdfPCell(); cell.setMinimumHeight(10); cell.setCellEvent(new FieldCell(nameField.getTextField(), 200, writer)); table.addCell(cell); // force upper case javascript writer.addJavaScript("var nameField = this.getField('nameField');" + "nameField.setAction('Keystroke'," + "'forceUpperCase()');" + "" + "function forceUpperCase(){" + "if(!event.willCommit)event.change = " + "event.change.toUpperCase();" + "}"); // Code 3, add empty row table.addCell(""); table.addCell(""); // Code 4, add age TextField table.addCell("Age"); TextField ageComb = new TextField(writer, new Rectangle(0, 0, 30, 10), "ageField"); ageComb.setBorderColor(Color.BLACK); ageComb.setBorderWidth(1); ageComb.setBorderStyle(PdfBorderDictionary.STYLE_SOLID); ageComb.setText("12"); ageComb.setAlignment(Element.ALIGN_RIGHT); ageComb.setMaxCharacterLength(2); ageComb.setOptions(TextField.COMB | TextField.DO_NOT_SCROLL); cell = new PdfPCell(); cell.setMinimumHeight(10); cell.setCellEvent(new FieldCell(ageComb.getTextField(), 30, writer)); table.addCell(cell); // validate age javascript writer.addJavaScript("var ageField = this.getField('ageField');" + "ageField.setAction('Validate','checkAge()');" + "function checkAge(){" + "if(event.value < 12){" + "app.alert('Warning! Applicant\\'s age can not" + " be younger than 12.');" + "event.value = 12;" + "}}"); // add empty row table.addCell(""); table.addCell(""); // Code 5, add age TextField table.addCell("Comment"); TextField comment = new TextField(writer, new Rectangle(0, 0, 200, 100), "commentField"); comment.setBorderColor(Color.BLACK); comment.setBorderWidth(1); comment.setBorderStyle(PdfBorderDictionary.STYLE_SOLID); comment.setText(""); comment.setOptions(TextField.MULTILINE | TextField.DO_NOT_SCROLL); cell = new PdfPCell(); cell.setMinimumHeight(100); cell.setCellEvent(new FieldCell(comment.getTextField(), 200, writer)); table.addCell(cell); // check comment characters length javascript writer.addJavaScript("var commentField = " + "this.getField('commentField');" + "commentField" + ".setAction('Keystroke','checkLength()');" + "function checkLength(){" + "if(!event.willCommit && " + "event.value.length > 100){" + "app.alert('Warning! Comment can not " + "be more than 100 characters.');" + "event.change = '';" + "}}"); // add empty row table.addCell(""); table.addCell(""); // Code 6, add submit button PushbuttonField submitBtn = new PushbuttonField(writer, new Rectangle(0, 0, 35, 15), "submitPOST"); submitBtn.setBackgroundColor(Color.GRAY); submitBtn.setBorderStyle(PdfBorderDictionary.STYLE_BEVELED); submitBtn.setText("POST"); submitBtn.setOptions(PushbuttonField.VISIBLE_BUT_DOES_NOT_PRINT); PdfFormField submitField = submitBtn.getField(); submitField.setAction(PdfAction.createSubmitForm("http://www.geek-tutorials.com/java/itext/submit.php", null, PdfAction.SUBMIT_HTML_FORMAT)); cell = new PdfPCell(); cell.setMinimumHeight(15); cell.setCellEvent(new FieldCell(submitField, 35, writer)); table.addCell(cell); // Code 7, add reset button PushbuttonField resetBtn = new PushbuttonField(writer, new Rectangle(0, 0, 35, 15), "reset"); resetBtn.setBackgroundColor(Color.GRAY); resetBtn.setBorderStyle(PdfBorderDictionary.STYLE_BEVELED); resetBtn.setText("RESET"); resetBtn.setOptions(PushbuttonField.VISIBLE_BUT_DOES_NOT_PRINT); PdfFormField resetField = resetBtn.getField(); resetField.setAction(PdfAction.createResetForm(null, 0)); cell = new PdfPCell(); cell.setMinimumHeight(15); cell.setCellEvent(new FieldCell(resetField, 35, writer)); table.addCell(cell); document.add(table); document.close(); }
From source file:com.gp.cong.logisoft.lcl.report.FreightInvoiceLclPdfCreator.java
@Override public void onOpenDocument(PdfWriter writer, Document document) { total = writer.getDirectContent().createTemplate(100, 100); total.setBoundingBox(new Rectangle(-20, -20, 100, 100)); try {/*from w ww . ja v a2 s . c o m*/ helv = BaseFont.createFont(BaseFont.HELVETICA, BaseFont.WINANSI, BaseFont.NOT_EMBEDDED); } catch (Exception e) { log.info("onOpenDocument failed on " + new Date(), e); throw new ExceptionConverter(e); } }
From source file:com.kahlon.guard.controller.example.SimpleBean.java
public void createPDF() { try { //catch better your exceptions, this is just an example FacesContext context = FacesContext.getCurrentInstance(); Document document = new Document(); String fileName = "PDFFile"; String TEXT = FacesMessageUtil.getMessage("person.name") + " -- " + "These are the protagonists in 'Hero', a movie by Zhang Yimou:\n" + "\u7121\u540d (Nameless), \u6b98\u528d (Broken Sword), " + "\u98db\u96ea (Flying Snow), \u5982\u6708 (Moon), " + "\u79e6\u738b (the King), and \u9577\u7a7a (Sky)."; ByteArrayOutputStream baos = new ByteArrayOutputStream(); PdfWriter writer = PdfWriter.getInstance(document, baos); if (!document.isOpen()) { document.open();//from ww w .ja v a2 s . c o m } writer.getAcroForm().setNeedAppearances(true); TextField text = new TextField(writer, new Rectangle(36, 806, 559, 780), "description"); text.setOptions(TextField.MULTILINE); //text.setOptions(TextField.READ_ONLY); text.setText(TEXT); writer.addAnnotation(text.getTextField()); //document.add(new Phrase(TEXT)); //Keep modifying your pdf file (add pages and more) document.close(); writePDFToResponse(context.getExternalContext(), baos, fileName); context.responseComplete(); } catch (Exception e) { //e.printStackTrace(); } }
From source file:com.logiware.accounting.reports.ArDisputeReportCreator.java
@Override public void onOpenDocument(PdfWriter writer, Document document) { pageTemplate = writer.getDirectContent().createTemplate(20, 10); pageTemplate.setBoundingBox(new Rectangle(-20, -20, 20, 50)); try {/* w w w.j a v a2 s.c o m*/ helvFont = BaseFont.createFont(BaseFont.HELVETICA, BaseFont.WINANSI, BaseFont.NOT_EMBEDDED); } catch (DocumentException e) { log.info("onOpenDocument failed on " + new Date(), e); } catch (IOException e) { log.info("onOpenDocument failed on " + new Date(), e); } }
From source file:com.orange.atk.atkUI.corecli.utils.PdfUtilities.java
License:Apache License
private void signDocument(String pdfFileName) { try {//from w w w . j a v a 2 s.co m // 1. copy File tmpPDFFile = new File(tmpDir, "tmp2PDF.pdf"); copyFile(new File(pdfFileName), tmpPDFFile); // 2. sign KeyStore ks = KeyStore.getInstance(typeKeystore); FileInputStream fis = new FileInputStream(keystore); ks.load(fis, passwordKeystore.toCharArray()); PrivateKey key = (PrivateKey) ks.getKey(aliasCertificate, passwordKeystore.toCharArray()); Certificate[] chain = ks.getCertificateChain(aliasCertificate); PdfReader reader = new PdfReader(tmpPDFFile.getAbsolutePath()); FileOutputStream fout = new FileOutputStream(pdfFileName); PdfStamper stp = PdfStamper.createSignature(reader, fout, '\0'); PdfSignatureAppearance sap = stp.getSignatureAppearance(); sap.setCrypto(key, chain, null, PdfSignatureAppearance.WINCER_SIGNED); sap.setVisibleSignature(new Rectangle(450, 730, 550, 780), 1, null); stp.close(); fis.close(); } catch (Exception e) { e.printStackTrace(Out.log); } }
From source file:com.qcadoo.report.api.pdf.PdfPageNumbering.java
License:Open Source License
/** * @see com.lowagie.text.pdf.PdfPageEvent#onOpenDocument(com.lowagie.text.pdf.PdfWriter, com.lowagie.text.Document) *///from ww w . j a v a 2 s . c o m @Override public void onOpenDocument(final PdfWriter writer, final Document document) { total = writer.getDirectContent().createTemplate(100, 100); total.setBoundingBox(new Rectangle(-20, -20, 100, 100)); try { ColorUtils.prepare(); FontUtils.prepare(); } catch (Exception e) { throw new ExceptionConverter(e); } }