Search in sources :

Example 1 with PDTextField

use of org.apache.pdfbox.pdmodel.interactive.form.PDTextField in project pdfbox by apache.

the class PDAnnotationTest method createWidgetAnnotationFromField.

@Test
public void createWidgetAnnotationFromField() {
    PDDocument document = new PDDocument();
    PDAcroForm acroForm = new PDAcroForm(document);
    PDTextField textField = new PDTextField(acroForm);
    PDAnnotation annotation = textField.getWidgets().get(0);
    assertEquals(COSName.ANNOT, annotation.getCOSObject().getItem(COSName.TYPE));
    assertEquals(PDAnnotationWidget.SUB_TYPE, annotation.getCOSObject().getNameAsString(COSName.SUBTYPE));
}
Also used : PDDocument(org.apache.pdfbox.pdmodel.PDDocument) PDTextField(org.apache.pdfbox.pdmodel.interactive.form.PDTextField) PDAcroForm(org.apache.pdfbox.pdmodel.interactive.form.PDAcroForm) Test(org.junit.Test)

Example 2 with PDTextField

use of org.apache.pdfbox.pdmodel.interactive.form.PDTextField in project pdfbox by apache.

the class SetField method setField.

/**
 * This will set a single field in the document.
 *
 * @param pdfDocument The PDF to set the field in.
 * @param name The name of the field to set.
 * @param value The new value of the field.
 *
 * @throws IOException If there is an error setting the field.
 */
public void setField(PDDocument pdfDocument, String name, String value) throws IOException {
    PDDocumentCatalog docCatalog = pdfDocument.getDocumentCatalog();
    PDAcroForm acroForm = docCatalog.getAcroForm();
    PDField field = acroForm.getField(name);
    if (field != null) {
        if (field instanceof PDCheckBox) {
            field.setValue("Yes");
        } else if (field instanceof PDComboBox) {
            field.setValue(value);
        } else if (field instanceof PDListBox) {
            field.setValue(value);
        } else if (field instanceof PDRadioButton) {
            field.setValue(value);
        } else if (field instanceof PDTextField) {
            field.setValue(value);
        }
    } else {
        System.err.println("No field found with name:" + name);
    }
}
Also used : PDField(org.apache.pdfbox.pdmodel.interactive.form.PDField) PDRadioButton(org.apache.pdfbox.pdmodel.interactive.form.PDRadioButton) PDCheckBox(org.apache.pdfbox.pdmodel.interactive.form.PDCheckBox) PDTextField(org.apache.pdfbox.pdmodel.interactive.form.PDTextField) PDComboBox(org.apache.pdfbox.pdmodel.interactive.form.PDComboBox) PDListBox(org.apache.pdfbox.pdmodel.interactive.form.PDListBox) PDAcroForm(org.apache.pdfbox.pdmodel.interactive.form.PDAcroForm) PDDocumentCatalog(org.apache.pdfbox.pdmodel.PDDocumentCatalog)

Example 3 with PDTextField

use of org.apache.pdfbox.pdmodel.interactive.form.PDTextField in project pdfbox by apache.

the class CreateMultiWidgetsForm method main.

public static void main(String[] args) throws IOException {
    // Create a new document with 2 empty pages.
    try (PDDocument document = new PDDocument()) {
        PDPage page1 = new PDPage(PDRectangle.A4);
        document.addPage(page1);
        PDPage page2 = new PDPage(PDRectangle.A4);
        document.addPage(page2);
        // Adobe Acrobat uses Helvetica as a default font and
        // stores that under the name '/Helv' in the resources dictionary
        PDFont font = PDType1Font.HELVETICA;
        PDResources resources = new PDResources();
        resources.put(COSName.getPDFName("Helv"), font);
        // Add a new AcroForm and add that to the document
        PDAcroForm acroForm = new PDAcroForm(document);
        document.getDocumentCatalog().setAcroForm(acroForm);
        // Add and set the resources and default appearance at the form level
        acroForm.setDefaultResources(resources);
        // Acrobat sets the font size on the form level to be
        // auto sized as default. This is done by setting the font size to '0'
        String defaultAppearanceString = "/Helv 0 Tf 0 g";
        acroForm.setDefaultAppearance(defaultAppearanceString);
        // Add a form field to the form.
        PDTextField textBox = new PDTextField(acroForm);
        textBox.setPartialName("SampleField");
        // Acrobat sets the font size to 12 as default
        // This is done by setting the font size to '12' on the
        // field level.
        // The text color is set to blue in this example.
        // To use black, replace "0 0 1 rg" with "0 0 0 rg" or "0 g".
        defaultAppearanceString = "/Helv 12 Tf 0 0 1 rg";
        textBox.setDefaultAppearance(defaultAppearanceString);
        // add the field to the AcroForm
        acroForm.getFields().add(textBox);
        // Specify 1st annotation associated with the field
        PDAnnotationWidget widget1 = new PDAnnotationWidget();
        PDRectangle rect = new PDRectangle(50, 750, 250, 50);
        widget1.setRectangle(rect);
        widget1.setPage(page1);
        widget1.setParent(textBox);
        // Specify 2nd annotation associated with the field
        PDAnnotationWidget widget2 = new PDAnnotationWidget();
        PDRectangle rect2 = new PDRectangle(200, 650, 100, 50);
        widget2.setRectangle(rect2);
        widget2.setPage(page2);
        widget2.setParent(textBox);
        // set green border and yellow background for 1st widget
        // if you prefer defaults, just delete this code block
        PDAppearanceCharacteristicsDictionary fieldAppearance1 = new PDAppearanceCharacteristicsDictionary(new COSDictionary());
        fieldAppearance1.setBorderColour(new PDColor(new float[] { 0, 1, 0 }, PDDeviceRGB.INSTANCE));
        fieldAppearance1.setBackground(new PDColor(new float[] { 1, 1, 0 }, PDDeviceRGB.INSTANCE));
        widget1.setAppearanceCharacteristics(fieldAppearance1);
        // set red border and green background for 2nd widget
        // if you prefer defaults, just delete this code block
        PDAppearanceCharacteristicsDictionary fieldAppearance2 = new PDAppearanceCharacteristicsDictionary(new COSDictionary());
        fieldAppearance2.setBorderColour(new PDColor(new float[] { 1, 0, 0 }, PDDeviceRGB.INSTANCE));
        fieldAppearance2.setBackground(new PDColor(new float[] { 0, 1, 0 }, PDDeviceRGB.INSTANCE));
        widget2.setAppearanceCharacteristics(fieldAppearance2);
        List<PDAnnotationWidget> widgets = new ArrayList<>();
        widgets.add(widget1);
        widgets.add(widget2);
        textBox.setWidgets(widgets);
        // make sure the annotations are visible on screen and paper
        widget1.setPrinted(true);
        widget2.setPrinted(true);
        // Add the annotations to the pages
        page1.getAnnotations().add(widget1);
        page2.getAnnotations().add(widget2);
        // set the field value
        textBox.setValue("Sample field");
        document.save("MultiWidgetsForm.pdf");
    }
}
Also used : PDFont(org.apache.pdfbox.pdmodel.font.PDFont) PDAppearanceCharacteristicsDictionary(org.apache.pdfbox.pdmodel.interactive.annotation.PDAppearanceCharacteristicsDictionary) PDPage(org.apache.pdfbox.pdmodel.PDPage) COSDictionary(org.apache.pdfbox.cos.COSDictionary) ArrayList(java.util.ArrayList) PDResources(org.apache.pdfbox.pdmodel.PDResources) PDAcroForm(org.apache.pdfbox.pdmodel.interactive.form.PDAcroForm) PDColor(org.apache.pdfbox.pdmodel.graphics.color.PDColor) PDDocument(org.apache.pdfbox.pdmodel.PDDocument) PDTextField(org.apache.pdfbox.pdmodel.interactive.form.PDTextField) PDAnnotationWidget(org.apache.pdfbox.pdmodel.interactive.annotation.PDAnnotationWidget) PDRectangle(org.apache.pdfbox.pdmodel.common.PDRectangle)

Example 4 with PDTextField

use of org.apache.pdfbox.pdmodel.interactive.form.PDTextField in project pdfbox by apache.

the class CreateSimpleForm method main.

public static void main(String[] args) throws IOException {
    // Create a new document with an empty page.
    try (PDDocument document = new PDDocument()) {
        PDPage page = new PDPage(PDRectangle.A4);
        document.addPage(page);
        // Adobe Acrobat uses Helvetica as a default font and
        // stores that under the name '/Helv' in the resources dictionary
        PDFont font = PDType1Font.HELVETICA;
        PDResources resources = new PDResources();
        resources.put(COSName.getPDFName("Helv"), font);
        // Add a new AcroForm and add that to the document
        PDAcroForm acroForm = new PDAcroForm(document);
        document.getDocumentCatalog().setAcroForm(acroForm);
        // Add and set the resources and default appearance at the form level
        acroForm.setDefaultResources(resources);
        // Acrobat sets the font size on the form level to be
        // auto sized as default. This is done by setting the font size to '0'
        String defaultAppearanceString = "/Helv 0 Tf 0 g";
        acroForm.setDefaultAppearance(defaultAppearanceString);
        // Add a form field to the form.
        PDTextField textBox = new PDTextField(acroForm);
        textBox.setPartialName("SampleField");
        // Acrobat sets the font size to 12 as default
        // This is done by setting the font size to '12' on the
        // field level.
        // The text color is set to blue in this example.
        // To use black, replace "0 0 1 rg" with "0 0 0 rg" or "0 g".
        defaultAppearanceString = "/Helv 12 Tf 0 0 1 rg";
        textBox.setDefaultAppearance(defaultAppearanceString);
        // add the field to the acroform
        acroForm.getFields().add(textBox);
        // Specify the widget annotation associated with the field
        PDAnnotationWidget widget = textBox.getWidgets().get(0);
        PDRectangle rect = new PDRectangle(50, 750, 200, 50);
        widget.setRectangle(rect);
        widget.setPage(page);
        // set green border and yellow background
        // if you prefer defaults, just delete this code block
        PDAppearanceCharacteristicsDictionary fieldAppearance = new PDAppearanceCharacteristicsDictionary(new COSDictionary());
        fieldAppearance.setBorderColour(new PDColor(new float[] { 0, 1, 0 }, PDDeviceRGB.INSTANCE));
        fieldAppearance.setBackground(new PDColor(new float[] { 1, 1, 0 }, PDDeviceRGB.INSTANCE));
        widget.setAppearanceCharacteristics(fieldAppearance);
        // make sure the widget annotation is visible on screen and paper
        widget.setPrinted(true);
        // Add the widget annotation to the page
        page.getAnnotations().add(widget);
        // set the field value
        textBox.setValue("Sample field");
        document.save("target/SimpleForm.pdf");
    }
}
Also used : PDFont(org.apache.pdfbox.pdmodel.font.PDFont) PDAppearanceCharacteristicsDictionary(org.apache.pdfbox.pdmodel.interactive.annotation.PDAppearanceCharacteristicsDictionary) PDPage(org.apache.pdfbox.pdmodel.PDPage) COSDictionary(org.apache.pdfbox.cos.COSDictionary) PDDocument(org.apache.pdfbox.pdmodel.PDDocument) PDTextField(org.apache.pdfbox.pdmodel.interactive.form.PDTextField) PDResources(org.apache.pdfbox.pdmodel.PDResources) PDAnnotationWidget(org.apache.pdfbox.pdmodel.interactive.annotation.PDAnnotationWidget) PDRectangle(org.apache.pdfbox.pdmodel.common.PDRectangle) PDAcroForm(org.apache.pdfbox.pdmodel.interactive.form.PDAcroForm) PDColor(org.apache.pdfbox.pdmodel.graphics.color.PDColor)

Example 5 with PDTextField

use of org.apache.pdfbox.pdmodel.interactive.form.PDTextField in project pdfbox by apache.

the class CreateSimpleFormWithEmbeddedFont method main.

public static void main(String[] args) throws IOException {
    // Create a new document with an empty page.
    try (PDDocument doc = new PDDocument()) {
        PDPage page = new PDPage();
        doc.addPage(page);
        PDAcroForm acroForm = new PDAcroForm(doc);
        doc.getDocumentCatalog().setAcroForm(acroForm);
        // Note that the font is fully embedded. If you use a different font, make sure that
        // its license allows full embedding.
        PDFont formFont = PDType0Font.load(doc, CreateSimpleFormWithEmbeddedFont.class.getResourceAsStream("/org/apache/pdfbox/resources/ttf/LiberationSans-Regular.ttf"), false);
        // Add and set the resources and default appearance at the form level
        final PDResources resources = new PDResources();
        acroForm.setDefaultResources(resources);
        final String fontName = resources.add(formFont).getName();
        // Acrobat sets the font size on the form level to be
        // auto sized as default. This is done by setting the font size to '0'
        acroForm.setDefaultResources(resources);
        String defaultAppearanceString = "/" + fontName + " 0 Tf 0 g";
        PDTextField textBox = new PDTextField(acroForm);
        textBox.setPartialName("SampleField");
        textBox.setDefaultAppearance(defaultAppearanceString);
        acroForm.getFields().add(textBox);
        // Specify the widget annotation associated with the field
        PDAnnotationWidget widget = textBox.getWidgets().get(0);
        PDRectangle rect = new PDRectangle(50, 700, 200, 50);
        widget.setRectangle(rect);
        widget.setPage(page);
        page.getAnnotations().add(widget);
        // set the field value. Note that the last character is a turkish capital I with a dot,
        // which is not part of WinAnsiEncoding
        textBox.setValue("Sample field İ");
        doc.save("target/SimpleFormWithEmbeddedFont.pdf");
    }
}
Also used : PDFont(org.apache.pdfbox.pdmodel.font.PDFont) PDPage(org.apache.pdfbox.pdmodel.PDPage) PDDocument(org.apache.pdfbox.pdmodel.PDDocument) PDTextField(org.apache.pdfbox.pdmodel.interactive.form.PDTextField) PDResources(org.apache.pdfbox.pdmodel.PDResources) PDAnnotationWidget(org.apache.pdfbox.pdmodel.interactive.annotation.PDAnnotationWidget) PDRectangle(org.apache.pdfbox.pdmodel.common.PDRectangle) PDAcroForm(org.apache.pdfbox.pdmodel.interactive.form.PDAcroForm)

Aggregations

PDAcroForm (org.apache.pdfbox.pdmodel.interactive.form.PDAcroForm)7 PDTextField (org.apache.pdfbox.pdmodel.interactive.form.PDTextField)7 PDDocument (org.apache.pdfbox.pdmodel.PDDocument)6 PDResources (org.apache.pdfbox.pdmodel.PDResources)4 PDFont (org.apache.pdfbox.pdmodel.font.PDFont)4 PDAnnotationWidget (org.apache.pdfbox.pdmodel.interactive.annotation.PDAnnotationWidget)4 PDPage (org.apache.pdfbox.pdmodel.PDPage)3 PDRectangle (org.apache.pdfbox.pdmodel.common.PDRectangle)3 File (java.io.File)2 COSDictionary (org.apache.pdfbox.cos.COSDictionary)2 PDColor (org.apache.pdfbox.pdmodel.graphics.color.PDColor)2 PDAppearanceCharacteristicsDictionary (org.apache.pdfbox.pdmodel.interactive.annotation.PDAppearanceCharacteristicsDictionary)2 PDField (org.apache.pdfbox.pdmodel.interactive.form.PDField)2 ArrayList (java.util.ArrayList)1 COSName (org.apache.pdfbox.cos.COSName)1 PDDocumentCatalog (org.apache.pdfbox.pdmodel.PDDocumentCatalog)1 PDCheckBox (org.apache.pdfbox.pdmodel.interactive.form.PDCheckBox)1 PDComboBox (org.apache.pdfbox.pdmodel.interactive.form.PDComboBox)1 PDListBox (org.apache.pdfbox.pdmodel.interactive.form.PDListBox)1 PDRadioButton (org.apache.pdfbox.pdmodel.interactive.form.PDRadioButton)1