You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@pdfbox.apache.org by "Maruan Sahyoun (JIRA)" <ji...@apache.org> on 2014/08/23 19:52:11 UTC
[jira] [Comment Edited] (PDFBOX-1750) PDTextbox and
PDAnnotationWidget are not correct initialized from it's own constructor .
[ https://issues.apache.org/jira/browse/PDFBOX-1750?page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel&focusedCommentId=14108071#comment-14108071 ]
Maruan Sahyoun edited comment on PDFBOX-1750 at 8/23/14 5:50 PM:
-----------------------------------------------------------------
This example code will generate a new document with a single textfield from scratch. There is nothing visible on the page as there is no appearance but the field is fillable in Adobe Reader and others.
{code}
// Generate a new document
PDDocument document = new PDDocument();
// Add a new page
PDPage page = new PDPage();
document.addPage(page);
// add a new AcroForm
PDAcroForm form = new PDAcroForm(document);
document.getDocumentCatalog().setAcroForm(form);
// Create a rectangle for the fields position
PDRectangle rectangle = new PDRectangle();
rectangle.setLowerLeftX(10);
rectangle.setLowerLeftY(710);
rectangle.setUpperRightX(200);
rectangle.setUpperRightY(730);
// Create an annotation to represent the field
PDAnnotationWidget fieldAnnotation = new PDAnnotationWidget();
fieldAnnotation.setRectangle(rectangle);
// create a textfield
PDVariableText textField = new PDTextbox(form, fieldAnnotation.getDictionary());
// setting the field type is needed as this should be a real field
// an not a simple node in the AcroForm field hierarchy
textField.getDictionary().setName(COSName.FT, "Tx");
textField.setPartialName("Textfield");
// add the fields to the form
List fields = new ArrayList();
fields.add(textField);
form.setFields(fields);
// add the annotations to the page
List annotations = new ArrayList();
annotations.add(fieldAnnotation);
page.setAnnotations(annotations);
// save the document
document.save(..);
{code}
I’ll clean it up a bit and add it to the examples as well as the cookbook. As can be seen there is room for improvement in our API e.g.
why is a new AcroForm generated with the document in the constructor
{code}
PDAcroForm form = new PDAcroForm(document);
{code}
but it’s still needed to explicitly add the form to the document catalog
{code}
document.getDocumentCatalog().setAcroForm(form);
{code}
I’ll be looking into addressing that for 2.x.
was (Author: msahyoun):
This example code will generate a new document with a single textfield from scratch. There is nothing visible on the page as there is no appearance but the field is fillable in Adobe Reader and others.
{code}
// Generate a new document
PDDocument document = new PDDocument();
// Add a new page
PDPage page = new PDPage();
document.addPage(page);
// add a new AcroForm
PDAcroForm form = new PDAcroForm(document);
document.getDocumentCatalog().setAcroForm(form);
// Create a rectangle for the fields position
PDRectangle rectangle = new PDRectangle();
rectangle.setLowerLeftX(10);
rectangle.setLowerLeftY(710);
rectangle.setUpperRightX(200);
rectangle.setUpperRightY(730);
// Create an annotation to represent the field
PDAnnotationWidget fieldAnnotation = new PDAnnotationWidget();
fieldAnnotation.setRectangle(rectangle);
// create a textfield
PDVariableText textField = new PDTextbox(form, fieldAnnotation.getDictionary());
// setting the field type is needed as this should be a real field
// an not a simple node in the AcroForm field hierarchy
textField.getDictionary().setName(COSName.FT, "Tx");
textField.setPartialName("Textfield");
// add the fields to the form
List fields = new ArrayList();
fields.add(textField);
form.setFields(fields);
// add the annotations to the page
List annotations = new ArrayList();
annotations.add(fieldAnnotation);
page.setAnnotations(annotations);
// save the document
document.save("/Users/msahyoun/Desktop/OpenSource/pdfbox/issues/PDFBOX-2249/NewForm.pdf");
{code}
I’ll clean it up a bit and add it to the examples as well as the cookbook. As can be seen there is room for improvement in our API e.g.
why is a new AcroForm generated with the document in the constructor
{code}
PDAcroForm form = new PDAcroForm(document);
{code}
but it’s still needed to explicitly add the form to the document catalog
{code}
document.getDocumentCatalog().setAcroForm(form);
{code}
I’ll be looking into addressing that for 2.x.
> PDTextbox and PDAnnotationWidget are not correct initialized from it's own constructor .
> ----------------------------------------------------------------------------------------
>
> Key: PDFBOX-1750
> URL: https://issues.apache.org/jira/browse/PDFBOX-1750
> Project: PDFBox
> Issue Type: Bug
> Components: AcroForm
> Affects Versions: 1.8.2
> Reporter: chen zhenyu
> Labels: AcroForm, Widget
>
> I find 2 Bugs in interactive.form, And I have resolved this 2 Bug. Here are the Code
> 1. I want make new Textbox in pdf. It always failed because the type flag “COSName.FT,="Tx"” never be set in the constructor.
> The PDTextbox can only be correct initialized from COSDictionary.( That is only initialized from exist pdf file) .
> I fixed this by add “getDictionary().setName(COSName.FT, "Tx");” in the constructor of PDTextbox.
> Maybe it’s not right place, but works in my project.
> public PDTextbox( PDAcroForm theAcroForm )
> {
> super( theAcroForm );
> getDictionary().setName(COSName.FT, "Tx");
> }
> public PDTextbox( PDAcroForm theAcroForm, COSDictionary field)
> {
> super( theAcroForm, field);
> getDictionary().setName(COSName.FT, "Tx");
> }
> 2. Same Bug in PDAnnotationWidget. Only the default constructor initialize the type flag “COSName.SUBTYPE,= "Widget"”.
> But the default constructor were never be used. So I must use the PDAnnotationWidget to initialize the new PDTextbox, like blow.
> PDAnnotationWidget Widget = new PDAnnotationWidget();
> PDTextbox textBox = new PDTextbox(acroForm,Widget.getDictionary());
> Otherwise I got an empty PDAnnotationWidget from PDTextbox .getWidget().
> Something not correct in PDField .getWidget().
> If the the PDField create from user(not read from exist pdf file), the getWidget cannot get an correct initialized PDAnnotationWidget. The COSName.SUBTYPE will never be set.
> Maybe the similar Bug in the whole org.apache.pdfbox.pdmodel.interactive.form: the COSDictionary of element are not initialized when user create it manually (Typical to insert new acroform element).
> I hope that I can give something help to this opensource Project.
> Could you check this and fix in next Version?
--
This message was sent by Atlassian JIRA
(v6.2#6252)