com.lowagie.text.pdf.PdfFormField Java Examples

The following examples show how to use com.lowagie.text.pdf.PdfFormField. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: OptionalContentTest.java    From itext2 with GNU Lesser General Public License v3.0 7 votes vote down vote up
/**
 * 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();
}
 
Example #2
Source File: DefaultPdfDataEntryFormService.java    From dhis2-core with BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
private void setCheckboxAppearance( PdfFormField checkboxfield, PdfContentByte canvas, float width )
{
    PdfAppearance[] onOff = new PdfAppearance[2];
    onOff[0] = canvas.createAppearance( width + 2, width + 2 );
    onOff[0].rectangle( 1, 1, width, width );
    onOff[0].stroke();
    onOff[1] = canvas.createAppearance( width + 2, width + 2 );
    onOff[1].setRGBColorFill( 255, 128, 128 );
    onOff[1].rectangle( 1, 1, width, width );
    onOff[1].fillStroke();
    onOff[1].moveTo( 1, 1 );
    onOff[1].lineTo( width + 1, width + 1 );
    onOff[1].moveTo( 1, width + 1 );
    onOff[1].lineTo( width + 1, 1 );
    onOff[1].stroke();

    checkboxfield.setAppearance( PdfAnnotation.APPEARANCE_NORMAL, "Off", onOff[0] );
    checkboxfield.setAppearance( PdfAnnotation.APPEARANCE_NORMAL, "On", onOff[1] );
}
 
Example #3
Source File: FieldPositioningEvents.java    From itext2 with GNU Lesser General Public License v3.0 6 votes vote down vote up
/**
 * @see com.lowagie.text.pdf.PdfPageEvent#onGenericTag(com.lowagie.text.pdf.PdfWriter, com.lowagie.text.Document, com.lowagie.text.Rectangle, java.lang.String)
 */
public void onGenericTag(PdfWriter writer, Document document,
		Rectangle rect, String text) {
	rect.setBottom(rect.getBottom() - 3);
	PdfFormField field = (PdfFormField) genericChunkFields.get(text);
	if (field == null) {
		TextField tf = new TextField(writer, new Rectangle(rect.getLeft(padding), rect.getBottom(padding), rect.getRight(padding), rect.getTop(padding)), text);
		tf.setFontSize(14);
		try {
			field = tf.getTextField();
		} catch (Exception e) {
			throw new ExceptionConverter(e);
		}
	}
	else {
		field.put(PdfName.RECT,  new PdfRectangle(rect.getLeft(padding), rect.getBottom(padding), rect.getRight(padding), rect.getTop(padding)));
	}
	if (parent == null)
		writer.addAnnotation(field);
	else
		parent.addKid(field);
}
 
Example #4
Source File: JRPdfExporter.java    From jasperreports with GNU Lesser General Public License v3.0 6 votes vote down vote up
/**
 *
 */
protected void exportPage(JRPrintPage page) throws JRException, DocumentException, IOException
{
	tagHelper.startPage();
	
	Collection<JRPrintElement> elements = page.getElements();
	exportElements(elements);

	if (radioGroups != null)
	{
		for (PdfFormField radioGroup : radioGroups.values())
		{
			pdfWriter.addAnnotation(radioGroup);
		}
		radioGroups = null;
		radioFieldFactories = null; // radio groups that overflow unto next page don't seem to work; reset everything as it does not make sense to keep them
	}
	
	tagHelper.endPage();

	JRExportProgressMonitor progressMonitor = getCurrentItemConfiguration().getProgressMonitor();
	if (progressMonitor != null)
	{
		progressMonitor.afterPageExport();
	}
}
 
Example #5
Source File: FormComboTest.java    From itext2 with GNU Lesser General Public License v3.0 6 votes vote down vote up
/**
 * Generates an Acroform with a Combobox
 */
@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("combo.pdf"));

	// step 3: we open the document
	document.open();

	// step 4:
	PdfContentByte cb = writer.getDirectContent();
	cb.moveTo(0, 0);
	String options[] = { "Red", "Green", "Blue" };
	PdfFormField field = PdfFormField.createCombo(writer, true, options, 0);
	field.setWidget(new Rectangle(100, 700, 180, 720), PdfAnnotation.HIGHLIGHT_INVERT);
	field.setFieldName("ACombo");
	field.setValueAsString("Red");
	writer.addAnnotation(field);

	// step 5: we close the document
	document.close();
}
 
Example #6
Source File: FieldPositioningEvents.java    From gcs with Mozilla Public License 2.0 6 votes vote down vote up
/**
 * @see com.lowagie.text.pdf.PdfPageEvent#onGenericTag(com.lowagie.text.pdf.PdfWriter,
 *      com.lowagie.text.Document, com.lowagie.text.Rectangle, java.lang.String)
 */
@Override
public void onGenericTag(PdfWriter writer, Document document, Rectangle rect, String text) {
	rect.setBottom(rect.getBottom() - 3);
	PdfFormField field = (PdfFormField) genericChunkFields.get(text);
	if (field == null) {
		TextField tf = new TextField(writer, new Rectangle(rect.getLeft(padding), rect.getBottom(padding), rect.getRight(padding), rect.getTop(padding)), text);
		tf.setFontSize(14);
		try {
			field = tf.getTextField();
		} catch (Exception e) {
			throw new ExceptionConverter(e);
		}
	} else {
		field.put(PdfName.RECT, new PdfRectangle(rect.getLeft(padding), rect.getBottom(padding), rect.getRight(padding), rect.getTop(padding)));
	}
	if (parent == null) {
		writer.addAnnotation(field);
	} else {
		parent.addKid(field);
	}
}
 
Example #7
Source File: DefaultPdfDataEntryFormService.java    From dhis2-core with BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
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 );
}
 
Example #8
Source File: PdfFieldCell.java    From dhis2-core with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
public PdfFieldCell( PdfFormField parent, String[] texts, String[] values, String checkValue, float width, float height,
    int type, PdfWriter writer )
{
    this.writer = writer;
    this.type = type;
    this.parent = parent;
    this.texts = texts;
    this.values = values;
    this.checkValue = checkValue;
    this.width = width;
    this.height = height;
}
 
Example #9
Source File: PdfFieldCell.java    From dhis2-core with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
public PdfFieldCell( PdfFormField formField, String jsAction, String name, String text, int type, PdfWriter writer )
{
    this.formField = formField;
    this.writer = writer;
    this.type = type;
    this.name = name;
    this.text = text;
    this.jsAction = jsAction;
}
 
Example #10
Source File: PdfFieldCell.java    From dhis2-core with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
public PdfFieldCell( PdfFormField formField, float width, float height, int type, PdfWriter writer )
{
    this.formField = formField;
    this.width = width;
    this.height = height;
    this.writer = writer;
    this.type = type;
}
 
Example #11
Source File: PdfFieldCell.java    From dhis2-core with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
public PdfFieldCell( PdfFormField formField, float width, float height, PdfWriter writer )
{
    this.formField = formField;
    this.width = width;
    this.height = height;
    this.writer = writer;
    this.type = TYPE_DEFAULT;
}
 
Example #12
Source File: DefaultPdfDataEntryFormService.java    From dhis2-core with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
@SuppressWarnings( "unused" )
private void addCell_WithRadioButton( PdfPTable table, PdfWriter writer, PdfPCell cell, String strfldName )
{
    PdfFormField radiogroupField = PdfFormField.createRadioButton( writer, true );
    radiogroupField.setFieldName( strfldName );

    cell.setCellEvent( new PdfFieldCell( radiogroupField, new String[]{ "Yes", "No", "null" }, new String[]{
        "true", "false", "" }, "", 30.0f, PdfDataEntryFormUtil.UNITSIZE_DEFAULT, PdfFieldCell.TYPE_RADIOBUTTON, writer ) );

    table.addCell( cell );

    writer.addAnnotation( radiogroupField );
}
 
Example #13
Source File: PdfAnnotationsImp.java    From itext2 with GNU Lesser General Public License v3.0 5 votes vote down vote up
public void addAnnotation(PdfAnnotation annot) {
    if (annot.isForm()) {
        PdfFormField field = (PdfFormField)annot;
        if (field.getParent() == null)
            addFormFieldRaw(field);
    }
    else
        annotations.add(annot);
}
 
Example #14
Source File: PdfAnnotationsImp.java    From itext2 with GNU Lesser General Public License v3.0 5 votes vote down vote up
void addFormFieldRaw(PdfFormField field) {
    annotations.add(field);
    ArrayList kids = field.getKids();
    if (kids != null) {
        for (int k = 0; k < kids.size(); ++k)
            addFormFieldRaw((PdfFormField)kids.get(k));
    }
}
 
Example #15
Source File: PdfAnnotationsImp.java    From gcs with Mozilla Public License 2.0 5 votes vote down vote up
void addFormFieldRaw(PdfFormField field) {
	annotations.add(field);
	ArrayList kids = field.getKids();
	if (kids != null) {
		for (int k = 0; k < kids.size(); ++k) {
			addFormFieldRaw((PdfFormField) kids.get(k));
		}
	}
}
 
Example #16
Source File: PdfAnnotationsImp.java    From gcs with Mozilla Public License 2.0 5 votes vote down vote up
public void addAnnotation(PdfAnnotation annot) {
	if (annot.isForm()) {
		PdfFormField field = (PdfFormField) annot;
		if (field.getParent() == null) {
			addFormFieldRaw(field);
		}
	} else {
		annotations.add(annot);
	}
}
 
Example #17
Source File: FormPushButtonTest.java    From itext2 with GNU Lesser General Public License v3.0 5 votes vote down vote up
/**
 * Generates an Acroform with a PushButton
 */
@Test
public void main() throws Exception {

	Document.compress = false;
	// step 1: creation of a document-object
	Document document = new Document(PageSize.A4);

	// step 2:
	PdfWriter writer = PdfWriter.getInstance(document, PdfTestBase.getOutputStream("pushbutton.pdf"));

	// step 3: we open the document
	document.open();

	// step 4:
	PdfFormField pushbutton = PdfFormField.createPushButton(writer);
	PdfContentByte cb = writer.getDirectContent();
	cb.moveTo(0, 0);
	PdfAppearance normal = cb.createAppearance(100, 50);
	normal.setColorFill(Color.GRAY);
	normal.rectangle(5, 5, 90, 40);
	normal.fill();
	PdfAppearance rollover = cb.createAppearance(100, 50);
	rollover.setColorFill(Color.RED);
	rollover.rectangle(5, 5, 90, 40);
	rollover.fill();
	PdfAppearance down = cb.createAppearance(100, 50);
	down.setColorFill(Color.BLUE);
	down.rectangle(5, 5, 90, 40);
	down.fill();
	pushbutton.setFieldName("PushMe");
	pushbutton.setAppearance(PdfAnnotation.APPEARANCE_NORMAL, normal);
	pushbutton.setAppearance(PdfAnnotation.APPEARANCE_ROLLOVER, rollover);
	pushbutton.setAppearance(PdfAnnotation.APPEARANCE_DOWN, down);
	pushbutton.setWidget(new Rectangle(100, 700, 200, 750), PdfAnnotation.HIGHLIGHT_PUSH);
	writer.addAnnotation(pushbutton);

	// step 5: we close the document
	document.close();
}
 
Example #18
Source File: FieldPositioningEvents.java    From itext2 with GNU Lesser General Public License v3.0 5 votes vote down vote up
/** Creates a new event. This constructor will be used if you need to position fields with a Cell Event. 
    * @throws DocumentException
    * @throws IOException*/
   public FieldPositioningEvents(PdfWriter writer, PdfFormField parent, String text) throws IOException, DocumentException {
   	this.parent = parent;
   	TextField tf = new TextField(writer, new Rectangle(0, 0), text);
	tf.setFontSize(14);
	cellField = tf.getTextField();
}
 
Example #19
Source File: SimpleRegistrationFormTest.java    From itext2 with GNU Lesser General Public License v3.0 5 votes vote down vote up
/**
 * @see com.lowagie.text.pdf.PdfPCellEvent#cellLayout(com.lowagie.text.pdf.PdfPCell,
 *      com.lowagie.text.Rectangle, com.lowagie.text.pdf.PdfContentByte[])
 */
public void cellLayout(PdfPCell cell, Rectangle position, PdfContentByte[] canvases) {
	TextField tf = new TextField(writer, position, fieldname);
	tf.setFontSize(12);
	try{
		PdfFormField field = tf.getTextField();
		writer.addAnnotation(field);
	} catch (Exception e) {
		throw new ExceptionConverter(e);
	}
}
 
Example #20
Source File: FieldPositioningEvents.java    From itext2 with GNU Lesser General Public License v3.0 4 votes vote down vote up
/**
 * @param parent The parent to set.
 */
public void setParent(PdfFormField parent) {
	this.parent = parent;
}
 
Example #21
Source File: StudentCardFormTest.java    From itext2 with GNU Lesser General Public License v3.0 4 votes vote down vote up
private static StudentCardFormTest createForm (PdfFormField field) {
	StudentCardFormTest form = new StudentCardFormTest();
	form.field = field;
	return form;
}
 
Example #22
Source File: FormRadioButtonTest.java    From itext2 with GNU Lesser General Public License v3.0 4 votes vote down vote up
/**
 * Generates an Acroform with a RadioButton
 */
@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("radiobutton.pdf"));

	// step 3: we open the document
	document.open();

	// step 4:
	PdfContentByte cb = writer.getDirectContent();
	cb.moveTo(0, 0);
	PdfFormField radio = PdfFormField.createRadioButton(writer, true);
	PdfAppearance tpOff = cb.createAppearance(20, 20);
	PdfAppearance tpOn = cb.createAppearance(20, 20);

	tpOff.circle(10, 10, 9);
	tpOff.stroke();

	tpOn.circle(10, 10, 9);
	tpOn.stroke();
	tpOn.circle(10, 10, 3);
	tpOn.fillStroke();

	radio.setFieldName("CreditCard");
	radio.setValueAsName("MasterCard");

	PdfFormField radio1 = PdfFormField.createEmpty(writer);
	radio1.setWidget(new Rectangle(100, 700, 120, 720), PdfAnnotation.HIGHLIGHT_INVERT);
	radio1.setAppearanceState("MasterCard");
	radio1.setAppearance(PdfAnnotation.APPEARANCE_NORMAL, "Off", tpOff);
	radio1.setAppearance(PdfAnnotation.APPEARANCE_NORMAL, "MasterCard", tpOn);
	radio.addKid(radio1);

	PdfFormField radio2 = PdfFormField.createEmpty(writer);
	radio2.setWidget(new Rectangle(100, 660, 120, 680), PdfAnnotation.HIGHLIGHT_INVERT);
	radio2.setAppearanceState("Off");
	radio2.setAppearance(PdfAnnotation.APPEARANCE_NORMAL, "Off", tpOff);
	radio2.setAppearance(PdfAnnotation.APPEARANCE_NORMAL, "Visa", tpOn);
	radio.addKid(radio2);

	PdfFormField radio3 = PdfFormField.createEmpty(writer);
	radio3.setWidget(new Rectangle(100, 620, 120, 640), PdfAnnotation.HIGHLIGHT_INVERT);
	radio3.setAppearanceState("Off");
	radio3.setAppearance(PdfAnnotation.APPEARANCE_NORMAL, "Off", tpOff);
	radio3.setAppearance(PdfAnnotation.APPEARANCE_NORMAL, "American", tpOn);
	radio.addKid(radio3);

	writer.addAnnotation(radio);

	// step 5: we close the document
	document.close();
}
 
Example #23
Source File: JRPdfExporter.java    From jasperreports with GNU Lesser General Public License v3.0 4 votes vote down vote up
/**
 *
 */
public void exportFieldCheck(JRPrintElement element) throws DocumentException
{
	Rectangle rectangle = new Rectangle(
		element.getX() + exporterContext.getOffsetX(),
		jasperPrint.getPageHeight() - element.getY() - exporterContext.getOffsetY(),
		element.getX() + exporterContext.getOffsetX() + element.getWidth(),
		jasperPrint.getPageHeight() - element.getY() - exporterContext.getOffsetY() - element.getHeight()
		);
	
	String fieldName = element.getPropertiesMap().getProperty(PDF_FIELD_NAME);
	fieldName = fieldName == null || fieldName.trim().length() == 0 ? "FIELD_" + element.getUUID() : fieldName;
	
	RadioCheckField checkField = new RadioCheckField(pdfWriter, rectangle, fieldName, "checked");
	
	PdfFieldCheckTypeEnum checkType = PdfFieldCheckTypeEnum.getByName(element.getPropertiesMap().getProperty(PDF_FIELD_CHECK_TYPE));
	if (checkType != null)
	{
		checkField.setCheckType(checkType.getValue());
	}

	if (ModeEnum.OPAQUE == element.getModeValue())
	{
		checkField.setBackgroundColor(element.getBackcolor());
	}
	checkField.setTextColor(element.getForecolor());

	JRPen pen = getFieldPen(element);
	if (pen != null)
	{
		float borderWidth = Math.round(pen.getLineWidth());
		borderWidth = borderWidth > BaseField.BORDER_WIDTH_THICK ? BaseField.BORDER_WIDTH_THICK : borderWidth;
		if (borderWidth > 0)
		{
			checkField.setBorderColor(pen.getLineColor());
			checkField.setBorderWidth(borderWidth);
			String strBorderStyle = propertiesUtil.getProperty(PDF_FIELD_BORDER_STYLE, element, jasperPrint);
			PdfFieldBorderStyleEnum borderStyle = PdfFieldBorderStyleEnum.getByName(strBorderStyle);
			if (borderStyle == null)
			{
				borderStyle = pen.getLineStyleValue() == LineStyleEnum.DASHED ? PdfFieldBorderStyleEnum.DASHED : PdfFieldBorderStyleEnum.SOLID;
			}
			checkField.setBorderStyle(borderStyle.getValue());
		}
	}
	
	String checked = element.getPropertiesMap().getProperty(PDF_FIELD_CHECKED);
	if (checked != null)
	{
		checkField.setChecked(Boolean.valueOf(checked));
	}

	String readOnly = element.getPropertiesMap().getProperty(PDF_FIELD_READ_ONLY);
	if (readOnly != null)
	{
		if (Boolean.valueOf(readOnly))
		{
			checkField.setOptions(checkField.getOptions() | TextField.READ_ONLY);
		}
	}
	
	PdfFormField ck = null;

	try
	{
		ck = checkField.getCheckField();
	}
	catch (Exception e)
	{
		throw new JRRuntimeException(e);
	}

	pdfWriter.addAnnotation(ck);
}
 
Example #24
Source File: JRPdfExporter.java    From jasperreports with GNU Lesser General Public License v3.0 4 votes vote down vote up
/**
 *
 */
public void exportFieldRadio(JRPrintElement element) throws DocumentException
{
	Rectangle rectangle = new Rectangle(
		element.getX() + exporterContext.getOffsetX(),
		jasperPrint.getPageHeight() - element.getY() - exporterContext.getOffsetY(),
		element.getX() + exporterContext.getOffsetX() + element.getWidth(),
		jasperPrint.getPageHeight() - element.getY() - exporterContext.getOffsetY() - element.getHeight()
		);
	
	String fieldName = element.getPropertiesMap().getProperty(PDF_FIELD_NAME);
	fieldName = fieldName == null || fieldName.trim().length() == 0 ? "FIELD_" + element.getUUID() : fieldName;
	
	RadioCheckField radioField = radioFieldFactories == null ? null : radioFieldFactories.get(fieldName);
	if (radioField == null)
	{
		radioField = new RadioCheckField(pdfWriter, rectangle, fieldName, "FIELD_" + element.getUUID());
		if (radioFieldFactories == null)
		{
			radioFieldFactories = new HashMap<String, RadioCheckField>();
		}
		radioFieldFactories.put(fieldName, radioField);
	}

	PdfFieldCheckTypeEnum checkType = PdfFieldCheckTypeEnum.getByName(element.getPropertiesMap().getProperty(PDF_FIELD_CHECK_TYPE));
	if (checkType != null)
	{
		radioField.setCheckType(checkType.getValue());
	}

	radioField.setBox(rectangle);

	if (ModeEnum.OPAQUE == element.getModeValue())
	{
		radioField.setBackgroundColor(element.getBackcolor());
	}
	radioField.setTextColor(element.getForecolor());

	JRPen pen = getFieldPen(element);
	if (pen != null)
	{
		float borderWidth = Math.round(pen.getLineWidth());
		borderWidth = borderWidth > BaseField.BORDER_WIDTH_THICK ? BaseField.BORDER_WIDTH_THICK : borderWidth;
		if (borderWidth > 0)
		{
			radioField.setBorderColor(pen.getLineColor());
			radioField.setBorderWidth(borderWidth);
			String strBorderStyle = propertiesUtil.getProperty(PDF_FIELD_BORDER_STYLE, element, jasperPrint);
			PdfFieldBorderStyleEnum borderStyle = PdfFieldBorderStyleEnum.getByName(strBorderStyle);
			if (borderStyle == null)
			{
				borderStyle = pen.getLineStyleValue() == LineStyleEnum.DASHED ? PdfFieldBorderStyleEnum.DASHED : PdfFieldBorderStyleEnum.SOLID;
			}
			radioField.setBorderStyle(borderStyle.getValue());
		}
	}
	
	radioField.setOnValue("FIELD_" + element.getUUID());

	String checked = element.getPropertiesMap().getProperty(PDF_FIELD_CHECKED);
	radioField.setChecked(Boolean.valueOf(checked)); // need to set to false if previous button was checked

	// setting the read-only option has to occur before the getRadioGroup() call
	String readOnly = element.getPropertiesMap().getProperty(PDF_FIELD_READ_ONLY);
	if (readOnly != null)
	{
		if (Boolean.valueOf(readOnly))
		{
			radioField.setOptions(radioField.getOptions() | TextField.READ_ONLY);
		}
	}
	
	PdfFormField radioGroup = radioGroups == null ? null : radioGroups.get(fieldName);
	if (radioGroup == null)
	{
		if (radioGroups == null)
		{
			radioGroups = new HashMap<String, PdfFormField>();
		}
		radioGroup = radioField.getRadioGroup(true, false);
		radioGroups.put(fieldName, radioGroup);
	}

	try
	{
		radioGroup.addKid(radioField.getRadioField());
	}
	catch (Exception e)
	{
		throw new JRRuntimeException(e);
	}
}
 
Example #25
Source File: DefaultPdfDataEntryFormService.java    From dhis2-core with BSD 3-Clause "New" or "Revised" License 4 votes vote down vote up
private void addCell_WithCheckBox( PdfPTable table, PdfWriter writer, PdfPCell cell, String strfldName )
    throws IOException, DocumentException
{
    float sizeDefault = PdfDataEntryFormUtil.UNITSIZE_DEFAULT;

    RadioCheckField checkbox = new RadioCheckField( writer, new Rectangle( sizeDefault, sizeDefault ), "Yes", "On" );

    checkbox.setBorderWidth( 1 );
    checkbox.setBorderColor( Color.BLACK );

    PdfFormField checkboxfield = checkbox.getCheckField();
    checkboxfield.setFieldName( strfldName + "_" + PdfFieldCell.TPYEDEFINE_NAME + PdfFieldCell.TYPE_CHECKBOX );

    setCheckboxAppearance( checkboxfield, writer.getDirectContent(), sizeDefault );

    cell.setCellEvent( new PdfFieldCell( checkboxfield, sizeDefault, sizeDefault, PdfFieldCell.TYPE_CHECKBOX, writer ) );

    table.addCell( cell );
}
 
Example #26
Source File: FormTextFieldTest.java    From itext2 with GNU Lesser General Public License v3.0 4 votes vote down vote up
/**
 * 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();
}
 
Example #27
Source File: FormCheckboxTest.java    From itext2 with GNU Lesser General Public License v3.0 4 votes vote down vote up
/**
 * Generates an Acroform with a Checkbox
 */
@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("checkbox.pdf"));

	// step 3: we open the document
	document.open();

	// step 4:
	PdfContentByte cb = writer.getDirectContent();
	cb.moveTo(0, 0);
	PdfFormField field = PdfFormField.createCheckBox(writer);
	PdfAppearance tpOff = cb.createAppearance(20, 20);
	PdfAppearance tpOn = cb.createAppearance(20, 20);
	tpOff.rectangle(1, 1, 18, 18);
	tpOff.stroke();

	tpOn.setRGBColorFill(255, 128, 128);
	tpOn.rectangle(1, 1, 18, 18);
	tpOn.fillStroke();
	tpOn.moveTo(1, 1);
	tpOn.lineTo(19, 19);
	tpOn.moveTo(1, 19);
	tpOn.lineTo(19, 1);
	tpOn.stroke();

	field.setWidget(new Rectangle(100, 700, 120, 720), PdfAnnotation.HIGHLIGHT_INVERT);
	field.setFieldName("Urgent");
	field.setValueAsName("Off");
	field.setAppearanceState("Off");
	field.setAppearance(PdfAnnotation.APPEARANCE_NORMAL, "Off", tpOff);
	field.setAppearance(PdfAnnotation.APPEARANCE_NORMAL, "On", tpOn);
	writer.addAnnotation(field);

	// step 5: we close the document
	document.close();
}
 
Example #28
Source File: FieldPositioningEvents.java    From itext2 with GNU Lesser General Public License v3.0 4 votes vote down vote up
/** Creates a new event. This constructor will be used if you need to position fields with a Cell Event. */
public FieldPositioningEvents(PdfFormField parent, PdfFormField field) {
	this.cellField = field;
	this.parent = parent;
}
 
Example #29
Source File: FieldPositioningEvents.java    From itext2 with GNU Lesser General Public License v3.0 4 votes vote down vote up
/** Creates a new event. This constructor will be used if you need to position fields with a Cell Event. */
public FieldPositioningEvents(PdfWriter writer, PdfFormField field) {
	this.cellField = field;
	this.fieldWriter = writer;
}
 
Example #30
Source File: FieldPositioningEvents.java    From itext2 with GNU Lesser General Public License v3.0 4 votes vote down vote up
/**
 * Add a PdfFormField that has to be tied to a generic Chunk.
 */
public void addField(String text, PdfFormField field) {
	genericChunkFields.put(text, field);
}