Hi Joshua,
creating new fields is tricky. I searched a long time in Google and in the
mailing list and found only questions and no working answer. Then with a lot
help of the PDFDebugger and some source code reading I found a solution. This
ist of course not the best but for the moment it is working for me and I will
optimize it later.
One thing still not works: The font is not set. Don't know why.
Here is the code:
PDTextbox newField = new PDTextbox(acroForm);
PDFont font = PDType1Font.TIMES_ITALIC;
PDResources dr = new PDResources();
dr.addFont(font);
COSDictionary drDict;
drDict = dr.getCOSDictionary();
newField.getWidget().getDictionary().setItem(COSName.getPDFName("DR"),
drDict);
PDAppearance ap = new PDAppearance(acroForm, newField);
// the line above is the important part I think
PDRectangle rect = new PDRectangle();
rect.setLowerLeftX((float) 90.7);
rect.setLowerLeftY((float) 450.0);
rect.setUpperRightX((float) 250.0);
rect.setUpperRightY((float) 470.0);
newField.getWidget().setRectangle(rect);
newField.setPartialName("newField");
newField.setValue("New Field");
newField.getWidget().setPage(page);
newField.getDictionary().setName(COSName.TYPE, "Annot");
newField.getDictionary().setName(COSName.SUBTYPE, "Widget");
newField.getDictionary().setName(COSName.FT, "Tx");
// these three lines makes the field visible
acroForm.getFields().add(newField);
page.getAnnotations().add(newField.getWidget());
Hope, it will help you.
Best regards
Manfred
> Gesendet: Dienstag, 23. Juli 2013 um 16:59 Uhr
> Von: [email protected]
> An: [email protected]
> Betreff: Fw: NPE setValue
>
> CODE CORRECTION: However still does not work...
>
> // Create a document and add a page to it
> PDDocument document = new PDDocument();
> document =
> PDDocument.load("C:\\Users\\garrisj\\Desktop\\Disciplinary_TardyLateForm.pdf");
> out.println("Number of pages: " +
> document.getNumberOfPages() + "<br />");
>
> System.out.println("TroubleShoot 1");
> PDDocumentCatalog docCatalog =
> document.getDocumentCatalog();
> System.out.println("TroubleShoot 2");
> PDAcroForm acroForm = docCatalog.getAcroForm();
> System.out.println("TroubleShoot 3");
> PDField field = acroForm.getField("FULL NAME");
> System.out.println("TroubleShoot 4");
> try{
> field.setValue("Garrison, Joshua C");
> System.out.println("TroubleShoot 5");
> } catch (NullPointerException ex) {
> ex.printStackTrace();
> }
>
> // Save the newly created document
> document.save("C:\\Users\\garrisj\\Desktop\\BlankPage.pdf");
>
> // finally make sure that the document is properly
> // closed.
> document.close();
>
> Thanks,
>
> Joshua Garrison - Mt. Holly TMP
> IT Technician / Application Developer
> Daimler Trucks North America, LLC
> Office - (704) 822-7297
> [email protected]
>
> ----- Forwarded by Joshua Garrison/164/INT/EEC on 07/23/2013 10:58 AM
> -----
>
> Joshua Garrison/164/INT/EEC
> 07/23/2013 10:46 AM
>
> To
> [email protected]
> cc
>
> Subject
> NPE setValue
>
>
>
>
>
>
> Hello,
>
> I am curious if there is an issue with the setValue method in the current
> version of PDFBox. If not, do you have a fix for this code?
>
> I am getting the following stack error....
> SEVERE: java.lang.NullPointerException
> at
> org.apache.pdfbox.pdmodel.interactive.form.PDAppearance.calculateFontSize(PDAppearance.java:558)
> at
> org.apache.pdfbox.pdmodel.interactive.form.PDAppearance.setAppearanceValue(PDAppearance.java:303)
> at
> org.apache.pdfbox.pdmodel.interactive.form.PDVariableText.setValue(PDVariableText.java:131)
> at org.apache.jsp.index_jsp._jspService(index_jsp.java:91)
> at
> org.apache.jasper.runtime.HttpJspBase.service(HttpJspBase.java:111)
> at javax.servlet.http.HttpServlet.service(HttpServlet.java:770)
> at
> org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:411)
> at
> org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:473)
> at
> org.apache.jasper.servlet.JspServlet.service(JspServlet.java:377)
> at javax.servlet.http.HttpServlet.service(HttpServlet.java:770)
> at
> org.apache.catalina.core.StandardWrapper.service(StandardWrapper.java:1550)
> at
> org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:281)
> at
> org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:175)
> at
> org.apache.catalina.core.StandardPipeline.doInvoke(StandardPipeline.java:655)
> at
> org.apache.catalina.core.StandardPipeline.invoke(StandardPipeline.java:595)
> at
> org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:161)
> at
> org.apache.catalina.connector.CoyoteAdapter.doService(CoyoteAdapter.java:331)
> at
> org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:231)
> at
> com.sun.enterprise.v3.services.impl.ContainerMapper$AdapterCallable.call(ContainerMapper.java:317)
> at
> com.sun.enterprise.v3.services.impl.ContainerMapper.service(ContainerMapper.java:195)
> at
> com.sun.grizzly.http.ProcessorTask.invokeAdapter(ProcessorTask.java:860)
> at
> com.sun.grizzly.http.ProcessorTask.doProcess(ProcessorTask.java:757)
> at
> com.sun.grizzly.http.ProcessorTask.process(ProcessorTask.java:1056)
> at
> com.sun.grizzly.http.DefaultProtocolFilter.execute(DefaultProtocolFilter.java:229)
> at
> com.sun.grizzly.DefaultProtocolChain.executeProtocolFilter(DefaultProtocolChain.java:137)
> at
> com.sun.grizzly.DefaultProtocolChain.execute(DefaultProtocolChain.java:104)
> at
> com.sun.grizzly.DefaultProtocolChain.execute(DefaultProtocolChain.java:90)
> at
> com.sun.grizzly.http.HttpProtocolChain.execute(HttpProtocolChain.java:79)
> at
> com.sun.grizzly.ProtocolChainContextTask.doCall(ProtocolChainContextTask.java:54)
> at
> com.sun.grizzly.SelectionKeyContextTask.call(SelectionKeyContextTask.java:59)
> at com.sun.grizzly.ContextTask.run(ContextTask.java:71)
> at
> com.sun.grizzly.util.AbstractThreadPool$Worker.doWork(AbstractThreadPool.java:532)
> at
> com.sun.grizzly.util.AbstractThreadPool$Worker.run(AbstractThreadPool.java:513)
> at java.lang.Thread.run(Thread.java:722)
>
>
> And here is my code...
>
> // Create a document and add a page to it
> PDDocument document = new PDDocument();
> document =
> PDDocument.load("C:\\Users\\garrisj\\Desktop\\Disciplinary_TardyLateForm.pdf");
> out.println("Number of pages: " +
> document.getNumberOfPages() + "<br />");
>
> System.out.println("TroubleShoot 1");
> PDDocumentCatalog docCatalog =
> document.getDocumentCatalog();
> System.out.println("TroubleShoot 2");
> PDAcroForm acroForm = docCatalog.getAcroForm();
> System.out.println("TroubleShoot 3");
> PDField field = acroForm.setFields("FULL NAME");)
> System.out.println("TroubleShoot 4");
> try{
> field.setValue("Garrison, Joshua C");
> System.out.println("TroubleShoot 5");
> } catch (NullPointerException ex) {
> ex.printStackTrace();
> }
>
> // Save the newly created document
> document.save("C:\\Users\\garrisj\\Desktop\\BlankPage.pdf");
>
> // finally make sure that the document is properly
> // closed.
> document.close();
>
>
>
> Thanks,
>
> Joshua Garrison - Mt. Holly TMP
> IT Technician / Application Developer
> Daimler Trucks North America, LLC
> Office - (704) 822-7297
> [email protected]
>
>
> If you are not the intended addressee, please inform us immediately that you
> have received this e-mail in error, and delete it. We thank you for your
> cooperation.