private void addCell_WithTextField( PdfPTable table, Rectangle rect, PdfWriter writer, PdfPCell cell, String strfldName, int fieldCellType, String value ) throws IOException, DocumentException { TextField nameField = new TextField( writer, rect, strfldName ); nameField.setBorderWidth( 1 ); nameField.setBorderColor( Color.BLACK ); nameField.setBorderStyle( PdfBorderDictionary.STYLE_SOLID ); nameField.setBackgroundColor( COLOR_BACKGROUDTEXTBOX ); nameField.setText( value ); nameField.setAlignment( Element.ALIGN_RIGHT ); nameField.setFont( pdfFormFontSettings.getFont( PdfFormFontSettings.FONTTYPE_BODY ).getBaseFont() ); cell.setCellEvent( new PdfFieldCell( nameField.getTextField(), rect.getWidth(), rect.getHeight(), fieldCellType, writer ) ); table.addCell( cell ); }
private void addCell_WithDropDownListField( PdfPTable table, Rectangle rect, PdfWriter writer, PdfPCell cell, String strfldName, String[] optionList, String[] valueList ) throws IOException, DocumentException { TextField textList = new TextField( writer, rect, strfldName ); textList.setChoices( optionList ); textList.setChoiceExports( valueList ); textList.setBorderWidth( 1 ); textList.setBorderColor( Color.BLACK ); textList.setBorderStyle( PdfBorderDictionary.STYLE_SOLID ); textList.setBackgroundColor( COLOR_BACKGROUDTEXTBOX ); PdfFormField dropDown = textList.getComboField(); cell.setCellEvent( new PdfFieldCell( dropDown, rect.getWidth(), rect.getHeight(), writer ) ); table.addCell( cell ); }
protected void showHelpText( float x, float y, float width, float height, String helpText ) { Rectangle rectangle = new Rectangle( x, y, x + width, y + height ); PdfAnnotation annotation = PdfAnnotation.createSquareCircle( writer, rectangle, helpText, true ); PdfBorderDictionary borderStyle = new PdfBorderDictionary( 0, PdfBorderDictionary.STYLE_SOLID, null ); annotation.setBorderStyle( borderStyle ); annotation.setFlags( 288 ); writer.addAnnotation( annotation ); }
/** * Demonstrates the use of layers. * * @param args * no arguments needed */ @Test public void main() throws Exception { // step 1: creation of a document-object Document document = new Document(PageSize.A4, 50, 50, 50, 50); // step 2: creation of the writer PdfWriter writer = PdfWriter.getInstance(document, PdfTestBase.getOutputStream("optionalcontent.pdf")); writer.setPdfVersion(PdfWriter.VERSION_1_5); writer.setViewerPreferences(PdfWriter.PageModeUseOC); // step 3: opening the document document.open(); // step 4: content PdfContentByte cb = writer.getDirectContent(); Phrase explanation = new Phrase( "Automatic layers, form fields, images, templates and actions", new Font(Font.HELVETICA, 18, Font.BOLD, Color.red)); ColumnText.showTextAligned(cb, Element.ALIGN_LEFT, explanation, 50, 650, 0); PdfLayer l1 = new PdfLayer("Layer 1", writer); PdfLayer l2 = new PdfLayer("Layer 2", writer); PdfLayer l3 = new PdfLayer("Layer 3", writer); PdfLayer l4 = new PdfLayer("Form and XObject Layer", writer); PdfLayerMembership m1 = new PdfLayerMembership(writer); m1.addMember(l2); m1.addMember(l3); Phrase p1 = new Phrase("Text in layer 1"); Phrase p2 = new Phrase("Text in layer 2 or layer 3"); Phrase p3 = new Phrase("Text in layer 3"); cb.beginLayer(l1); ColumnText.showTextAligned(cb, Element.ALIGN_LEFT, p1, 50, 600, 0f); cb.endLayer(); cb.beginLayer(m1); ColumnText.showTextAligned(cb, Element.ALIGN_LEFT, p2, 50, 550, 0); cb.endLayer(); cb.beginLayer(l3); ColumnText.showTextAligned(cb, Element.ALIGN_LEFT, p3, 50, 500, 0); cb.endLayer(); TextField ff = new TextField(writer, new Rectangle(200, 600, 300, 620), "field1"); ff.setBorderColor(Color.blue); ff.setBorderStyle(PdfBorderDictionary.STYLE_SOLID); ff.setBorderWidth(TextField.BORDER_WIDTH_THIN); ff.setText("I'm a form field"); PdfFormField form = ff.getTextField(); form.setLayer(l4); writer.addAnnotation(form); Image img = Image.getInstance(PdfTestBase.RESOURCES_DIR + "pngnow.png"); img.setLayer(l4); img.setAbsolutePosition(200, 550); cb.addImage(img); PdfTemplate tp = cb.createTemplate(100, 20); Phrase pt = new Phrase("I'm a template", new Font(Font.HELVETICA, 12, Font.NORMAL, Color.magenta)); ColumnText.showTextAligned(tp, Element.ALIGN_LEFT, pt, 0, 0, 0); tp.setLayer(l4); tp.setBoundingBox(new Rectangle(0, -10, 100, 20)); cb.addTemplate(tp, 200, 500); ArrayList<Object> state = new ArrayList<Object>(); state.add("toggle"); state.add(l1); state.add(l2); state.add(l3); state.add(l4); PdfAction action = PdfAction.setOCGstate(state, true); Chunk ck = new Chunk("Click here to toggle the layers", new Font( Font.HELVETICA, 18, Font.NORMAL, Color.yellow)).setBackground( Color.blue).setAction(action); ColumnText.showTextAligned(cb, Element.ALIGN_CENTER, new Phrase(ck), 250, 400, 0); cb.sanityCheck(); // step 5: closing the document document.close(); }
/** * Generates an Acroform with a TextField */ @Test public void main() throws Exception { // step 1: creation of a document-object Document document = new Document(PageSize.A4); // step 2: PdfWriter writer = PdfWriter.getInstance(document, PdfTestBase.getOutputStream("textfield.pdf")); // step 3: we open the document document.open(); // step 4: BaseFont helv = BaseFont.createFont("Helvetica", "winansi", false); PdfContentByte cb = writer.getDirectContent(); cb.moveTo(0, 0); String text = "Some start text"; float fontSize = 12; Color textColor = new GrayColor(0f); PdfFormField field = PdfFormField.createTextField(writer, false, false, 0); field.setWidget(new Rectangle(171, 750, 342, 769), PdfAnnotation.HIGHLIGHT_INVERT); field.setFlags(PdfAnnotation.FLAGS_PRINT); field.setFieldName("ATextField"); field.setValueAsString(text); field.setDefaultValueAsString(text); field.setBorderStyle(new PdfBorderDictionary(2, PdfBorderDictionary.STYLE_SOLID)); field.setPage(); PdfAppearance tp = cb.createAppearance(171, 19); PdfAppearance da = (PdfAppearance) tp.getDuplicate(); da.setFontAndSize(helv, fontSize); da.setColorFill(textColor); field.setDefaultAppearanceString(da); tp.beginVariableText(); tp.saveState(); tp.rectangle(2, 2, 167, 15); tp.clip(); tp.newPath(); tp.beginText(); tp.setFontAndSize(helv, fontSize); tp.setColorFill(textColor); tp.setTextMatrix(4, 5); tp.showText(text); tp.endText(); tp.restoreState(); tp.endVariableText(); field.setAppearance(PdfAnnotation.APPEARANCE_NORMAL, tp); writer.addAnnotation(field); // step 5: we close the document document.close(); }
/** * Generates an Acroform with a TextField * * @param args * no arguments needed here */ public static void main(String[] args) { System.out.println("Textfield"); // step 1: creation of a document-object Document document = new Document(PageSize.A4); try { // step 2: PdfWriter writer = PdfWriter.getInstance(document, new FileOutputStream(android.os.Environment.getExternalStorageDirectory() + java.io.File.separator + "droidtext" + java.io.File.separator + "textfield.pdf")); // step 3: we open the document document.open(); // step 4: BaseFont helv = BaseFont.createFont("Helvetica", "winansi", false); PdfContentByte cb = writer.getDirectContent(); cb.moveTo(0, 0); String text = "Some start text"; float fontSize = 12; Color textColor = new GrayColor(0f); PdfFormField field = PdfFormField.createTextField(writer, false, false, 0); field.setWidget(new Rectangle(171, 750, 342, 769), PdfAnnotation.HIGHLIGHT_INVERT); field.setFlags(PdfAnnotation.FLAGS_PRINT); field.setFieldName("ATextField"); field.setValueAsString(text); field.setDefaultValueAsString(text); field.setBorderStyle(new PdfBorderDictionary(2, PdfBorderDictionary.STYLE_SOLID)); field.setPage(); PdfAppearance tp = cb.createAppearance(171, 19); PdfAppearance da = (PdfAppearance) tp.getDuplicate(); da.setFontAndSize(helv, fontSize); da.setColorFill(textColor); field.setDefaultAppearanceString(da); tp.beginVariableText(); tp.saveState(); tp.rectangle(2, 2, 167, 15); tp.clip(); tp.newPath(); tp.beginText(); tp.setFontAndSize(helv, fontSize); tp.setColorFill(textColor); tp.setTextMatrix(4, 5); tp.showText(text); tp.endText(); tp.restoreState(); tp.endVariableText(); field.setAppearance(PdfAnnotation.APPEARANCE_NORMAL, tp); writer.addAnnotation(field); } catch (DocumentException de) { System.err.println(de.getMessage()); } catch (IOException ioe) { System.err.println(ioe.getMessage()); } // step 5: we close the document document.close(); }