pdfbox-users mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From Gilad Denneboom <gilad.denneb...@gmail.com>
Subject Creating new text fields using PDFBox 2.x
Date Fri, 24 Feb 2017 16:26:28 GMT
Hi all,

I'm in the process of moving from PDFBox 1.x to 2.x, but have encountered
an issue that I hope you could help me with.
One of my requirements in various projects is to add new form fields to a
In PDFBox 1.x I would do this by generating a COSDictionary object which
contained all the basic field properties (type, value, rect, font size,
text color, etc.), and would then use it to generate a new PDTextbox object
(for example), which I would then add the page's annotation list. And here
is my problem. In PDFBox 2.x the constructor for PDTextField no longer
accepts the COSDictionary as its second parameter, only a PDAcroForm as its
first. So how am I supposed to generate a new field?
Then I noticed that the PDAnnotationWidget constructor can now take a
COSDictionary object as its parameter, so I tried that, but it doesn't
work... No field is added.
Here's my my code (the coordinates might be off, but it should still add
something, I think...):

        originalDoc = PDDocument.load(new File(outputFilePath));
        PDAcroForm acroForm = new PDAcroForm(originalDoc);
        p = originalDoc.getPage(0);
        float bottledOnX = inch * 2.375f;
        float bottledOnY = inch * 0.032f;
        float bottledOnW = inch * 0.257f;
        float bottledOnH = inch * 0.231f;
        COSDictionary bottledOnFieldDic = createTextField("BottledOn",
"TEST", new float[] {bottledOnX, bottledOnY, bottledOnX+bottledOnW,
bottledOnY+bottledOnH}, "50", 1);
        PDTextField bottledOnField = new PDTextField(acroForm);
        PDAnnotationWidget bottledOnWidget = new
        bottledOnField.getWidgets().set(0, bottledOnWidget);
        bottledOnField.setQ(1); // set alignment to "center"

    private static COSDictionary createTextField(String fieldName, String
value, float[] rect, String fontSize, int textColor) {
        COSDictionary cosDict = new COSDictionary();
        COSArray rectCosArray = new COSArray();
        rectCosArray.add(new COSFloat(rect[0])); // lower x boundary
        rectCosArray.add(new COSFloat(rect[1])); // lower y boundary
        rectCosArray.add(new COSFloat(rect[2])); // upper x boundary
        rectCosArray.add(new COSFloat(rect[3])); // upper y boundary

        cosDict.setItem(COSName.RECT, rectCosArray);
        cosDict.setItem(COSName.FT, COSName.getPDFName("Tx")); // Field Type
        cosDict.setItem(COSName.TYPE, COSName.ANNOT);
        cosDict.setItem(COSName.SUBTYPE, COSName.getPDFName("Widget"));
        cosDict.setItem(COSName.T, new COSString(fieldName));

        cosDict.setString(COSName.DA, "/Helv "+fontSize+" Tf "+textColor+"

        COSString fieldValue = new COSString(value);
        cosDict.setItem(COSName.V, fieldValue);

        return cosDict;

Any ideas what I'm doing wrong here?

Thanks in advance!


  • Unnamed multipart/alternative (inline, None, 0 bytes)
View raw message