Java Code Examples for com.lowagie.text.pdf.PdfContentByte#createAppearance()

The following examples show how to use com.lowagie.text.pdf.PdfContentByte#createAppearance() . 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: 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 2
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 3
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 4
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 5
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();
}