use of com.lowagie.text.pdf.PdfAppearance in project itext2 by albfernandez.
the class FormTextFieldTest method main.
/**
* 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();
}
use of com.lowagie.text.pdf.PdfAppearance in project OpenPDF by LibrePDF.
the class Add3D method execute.
/**
* Executes the tool (in most cases this generates a PDF file).
*/
public void execute() {
try {
if (getValue("srcfile") == null) {
throw new InstantiationException("You need to choose a sourcefile");
}
if (getValue("srcu3dfile") == null) {
throw new InstantiationException("You need to choose a u3d file");
}
if (getValue("destfile") == null) {
throw new InstantiationException("You need to choose a destination file");
}
int pagenumber = Integer.parseInt((String) getValue("pagenumber"));
// Create 3D annotation
// Required definitions
PdfIndirectReference streamRef;
PdfIndirectObject objRef;
PdfReader reader = new PdfReader(((File) getValue("srcfile")).getAbsolutePath());
String u3dFileName = ((File) getValue("srcu3dfile")).getAbsolutePath();
PdfStamper stamp = new PdfStamper(reader, new FileOutputStream((File) getValue("destfile")));
PdfWriter wr = stamp.getWriter();
PdfContentByte cb = stamp.getUnderContent(pagenumber);
Rectangle rectori = reader.getCropBox(pagenumber);
Rectangle rect = new Rectangle(new Rectangle(100, rectori.getHeight() - 550, rectori.getWidth() - 100, rectori.getHeight() - 150));
PdfStream oni = new PdfStream(PdfEncodings.convertToBytes("runtime.setCurrentTool(\"Rotate\");", null));
oni.flateCompress();
// Create stream to carry attachment
PdfStream stream = new PdfStream(new FileInputStream(u3dFileName), wr);
stream.put(new PdfName("OnInstantiate"), wr.addToBody(oni).getIndirectReference());
// Mandatory keys
stream.put(PdfName.TYPE, new PdfName(PDF_NAME_3D));
stream.put(PdfName.SUBTYPE, new PdfName(PDF_NAME_U3D));
stream.flateCompress();
// Write stream contents, get reference to stream object, write actual stream length
streamRef = wr.addToBody(stream).getIndirectReference();
stream.writeLength();
// Create 3D view dictionary
// PDF documentation states that this can be left out, but without normally we will just get a blank 3D image because of wrong coordinate space transformations, etc.
// Instead of providing camera-to-world transformation here, we could also reference view in U3D file itself (would be U3DPath key instead of C2W key, U3D value instead of M value for MS key), but i haven't tried up to now
// We could also provide an activation dictionary (defining activation behavior), and field-of-view for P entry if needed
PdfDictionary dict = new PdfDictionary(new PdfName(PDF_NAME_3DVIEW));
dict.put(new PdfName(PDF_NAME_XN), new PdfString("Default"));
dict.put(new PdfName(PDF_NAME_IN), new PdfString("Unnamed"));
// States that we have to provide camera-to-world coordinate transformation
dict.put(new PdfName(PDF_NAME_MS), PdfName.M);
dict.put(new PdfName(PDF_NAME_C2W), new PdfArray(new float[] { 1, 0, 0, 0, 0, -1, 0, 1, 0, 3, -235, // 3d transformation matrix (demo for teapot)
28F }));
// Camera distance along z-axis (demo for teapot)
dict.put(PdfName.CO, new PdfNumber(235));
// Write view dictionary, get reference
objRef = wr.addToBody(dict);
// Create appearance
PdfAppearance ap = cb.createAppearance(rect.getRight() - rect.getLeft(), rect.getTop() - rect.getBottom());
ap.setBoundingBox(rect);
// Create annotation with reference to stream
PdfAnnotation annot = new PdfAnnotation(wr, rect);
annot.put(PdfName.CONTENTS, new PdfString("3D Model"));
// Mandatory keys
annot.put(PdfName.SUBTYPE, new PdfName(PDF_NAME_3D));
annot.put(PdfName.TYPE, PdfName.ANNOT);
// Reference to stream object
annot.put(new PdfName(PDF_NAME_3DD), streamRef);
// Reference to view dictionary object
annot.put(new PdfName(PDF_NAME_3DV), objRef.getIndirectReference());
annot.put(new PdfName("3DI"), PdfBoolean.PDFFALSE);
PdfDictionary adi = new PdfDictionary();
adi.put(PdfName.A, new PdfName("PO"));
adi.put(new PdfName("DIS"), PdfName.I);
annot.put(new PdfName("3DA"), adi);
// Assign appearance and page
annot.setAppearance(PdfAnnotation.APPEARANCE_NORMAL, ap);
annot.setPage();
// Actually write annotation
stamp.addAnnotation(annot, pagenumber);
AddButton(100, 100, "Rotate", "im = this.getAnnots3D(0)[0].context3D;\rim.runtime.setCurrentTool(\"Rotate\");", "rotate.png", wr);
AddButton(150, 100, "Pan", "im = this.getAnnots3D(0)[0].context3D;\rim.runtime.setCurrentTool(\"Pan\");", "translate.png", wr);
AddButton(200, 100, "Zoom", "im = this.getAnnots3D(0)[0].context3D;\rim.runtime.setCurrentTool(\"Zoom\");", "zoom.png", wr);
stamp.close();
} catch (Exception e) {
JOptionPane.showMessageDialog(internalFrame, e.getMessage(), e.getClass().getName(), JOptionPane.ERROR_MESSAGE);
System.err.println(e.getMessage());
}
}
use of com.lowagie.text.pdf.PdfAppearance in project OpenPDF by LibrePDF.
the class FormCheckbox method main.
/**
* Generates an Acroform with a Checkbox
* @param args no arguments needed here
*/
public static void main(String[] args) {
System.out.println("Checkbox");
// step 1: creation of a document-object
Document document = new Document(PageSize.A4);
try {
// step 2:
PdfWriter writer = PdfWriter.getInstance(document, new FileOutputStream("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);
} catch (DocumentException | IOException de) {
System.err.println(de.getMessage());
}
// step 5: we close the document
document.close();
}
use of com.lowagie.text.pdf.PdfAppearance in project OpenPDF by LibrePDF.
the class FormPushButton method main.
/**
* Generates an Acroform with a PushButton
* @param args no arguments needed here
*/
public static void main(String[] args) {
System.out.println("PushButton");
Document.compress = false;
// step 1: creation of a document-object
Document document = new Document(PageSize.A4);
try {
// step 2:
PdfWriter writer = PdfWriter.getInstance(document, new FileOutputStream("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);
} catch (DocumentException | IOException de) {
System.err.println(de.getMessage());
}
// step 5: we close the document
document.close();
}
use of com.lowagie.text.pdf.PdfAppearance in project OpenPDF by LibrePDF.
the class FormRadioButton method main.
/**
* Generates an Acroform with a RadioButton
* @param args no arguments needed here
*/
public static void main(String[] args) {
System.out.println("RadioButton");
// step 1: creation of a document-object
Document document = new Document(PageSize.A4);
try {
// step 2:
PdfWriter writer = PdfWriter.getInstance(document, new FileOutputStream("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);
} catch (DocumentException | IOException de) {
System.err.println(de.getMessage());
}
// step 5: we close the document
document.close();
}
Aggregations