Search in sources :

Example 6 with PDTextField

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

the class DetermineTextFitsField method main.

public static void main(String[] args) throws IOException {
    try (PDDocument document = PDDocument.load(new File("target/SimpleForm.pdf"))) {
        PDAcroForm acroForm = document.getDocumentCatalog().getAcroForm();
        // Get the field and the widget associated to it.
        // Note: there might be multiple widgets
        PDField field = acroForm.getField("SampleField");
        PDAnnotationWidget widget = field.getWidgets().get(0);
        // Get the width of the fields box
        float widthOfField = widget.getRectangle().getWidth();
        // Get the font and the font size setting
        // This is currently a little awkward and needs improvement to have a better API
        // for that. In many cases the string will be built like that:
        // /Helv 12 Tf 0 g
        // We could use PDFStreamParser to do the parsing. For the sample we split the
        // string.
        String defaultAppearance = ((PDTextField) field).getDefaultAppearance();
        String[] parts = defaultAppearance.split(" ");
        // Get the font name
        COSName fontName = COSName.getPDFName(parts[0].substring(1));
        float fontSize = Float.parseFloat(parts[1]);
        // Get the font resource.
        // First look up the font from the widgets appearance stream.
        // This will be the case if there is already a value.
        // If the value hasn't been set yet the font resource needs to be looked up from
        // the AcroForm default resources
        PDFont font = null;
        PDResources resources = widget.getNormalAppearanceStream().getResources();
        if (resources != null) {
            font = resources.getFont(fontName);
        }
        if (font == null) {
            font = acroForm.getDefaultResources().getFont(fontName);
        }
        String willFit = "short string";
        String willNotFit = "this is a very long string which will not fit the width of the widget";
        // calculate the string width at a certain font size
        float willFitWidth = font.getStringWidth(willFit) * fontSize / 1000;
        float willNotFitWidth = font.getStringWidth(willNotFit) * fontSize / 1000;
        assert willFitWidth < widthOfField;
        assert willNotFitWidth > widthOfField;
    }
}
Also used : PDFont(org.apache.pdfbox.pdmodel.font.PDFont) PDField(org.apache.pdfbox.pdmodel.interactive.form.PDField) COSName(org.apache.pdfbox.cos.COSName) PDDocument(org.apache.pdfbox.pdmodel.PDDocument) PDTextField(org.apache.pdfbox.pdmodel.interactive.form.PDTextField) PDAnnotationWidget(org.apache.pdfbox.pdmodel.interactive.annotation.PDAnnotationWidget) PDResources(org.apache.pdfbox.pdmodel.PDResources) PDAcroForm(org.apache.pdfbox.pdmodel.interactive.form.PDAcroForm) File(java.io.File)

Example 7 with PDTextField

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

the class FillFormField method main.

public static void main(String[] args) throws IOException {
    String formTemplate = "src/main/resources/org/apache/pdfbox/examples/interactive/form/FillFormField.pdf";
    try (PDDocument pdfDocument = PDDocument.load(new File(formTemplate))) {
        // get the document catalog
        PDAcroForm acroForm = pdfDocument.getDocumentCatalog().getAcroForm();
        // as there might not be an AcroForm entry a null check is necessary
        if (acroForm != null) {
            // Retrieve an individual field and set its value.
            PDTextField field = (PDTextField) acroForm.getField("sampleField");
            field.setValue("Text Entry");
            // If a field is nested within the form tree a fully qualified name
            // might be provided to access the field.
            field = (PDTextField) acroForm.getField("fieldsContainer.nestedSampleField");
            field.setValue("Text Entry");
        }
        // Save and close the filled out form.
        pdfDocument.save("target/FillFormField.pdf");
    }
}
Also used : PDDocument(org.apache.pdfbox.pdmodel.PDDocument) PDTextField(org.apache.pdfbox.pdmodel.interactive.form.PDTextField) PDAcroForm(org.apache.pdfbox.pdmodel.interactive.form.PDAcroForm) File(java.io.File)

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