Hi,

> Am 26.04.2015 um 19:59 schrieb Tilman Hausherr <[email protected]>:
> 
> Hi,
> 
> 1.
> thanks for starting a new "clean" thread.
> 
> 
> 2.
> About the code: templateField0_null is null because you ask for 
> templateAcroForm.getField("field"). However, a bit later in the code, you do 
> this:
> 
> templateField0.setPartialName("field-" + i);
> 
> so the name is now "field-0".
> 
> And then you do a second loop, and of course, you find nothing when searching 
> for "field", because you changed your own field name.
> 
> I think that what you thought you were doing, was that you were working on a 
> copy. But no, you were altering the original field, and then add it a second 
> time.
> 
> 
> 3.
> One thing adds to the confusion: this code I inserted
> 
> System.out.println("templateField0_null: " + templateField0_null);
> 
> prints "null" even if the variable isn't null. The toString() method of 
> PDField is
> 
>    public String toString()
>    {
>        return "" + getDictionary().getDictionaryObject(COSName.V);
>    }
> 
> so it will show "null" if there is no value.
> 
> @Maruan, are you reading here? We need something better there, maybe the 
> field type and the value.

OK - will do tomorrow.

> 
> Tilman
> 
> 
> 
> 
> Am 26.04.2015 um 18:02 schrieb Philippe de Rochambeau:
>> 
>> Hi Tilman,
>> 
>> I am creating a new thread.
>> 
>> Here's the issues I am having with this code:
>> 
>> - when I run the program,  I get warning message stating that one of the 
>> PDDocuments hasn't been closed properly, although I close all PDDocuments
>> - the templateField0_null field is null, although the "field" field exists 
>> in the PDDocument’s acroForm
>> 
>> Thanks for your help.
>> 
>> You don't have to answer my questions on a week-end.
>> 
>> Many thanks.
>> 
>> Philippe
>> 
>> 
>> public class App {
>>      
>>      final static File RESULT_FOLDER = new 
>> File("/Users/philippe/Desktop/PDFBoxTests");
>>      final static File TEMPLATE_FOLDER = new 
>> File("/Users/philippe/Desktop/PDFBoxTests/Scribus");
>>      
>>     public static void main( String[] args ) {
>>              new App();
>>     }
>>          public App() {
>>              try {
>>                      testGeneratedTemplate();
>>                      } catch (COSVisitorException e) {
>>                              
>>                              e.printStackTrace();
>>                      } catch (IOException e) {
>>                              
>>                              e.printStackTrace();
>>                      }
>>     }
>>        public void testGeneratedTemplate() throws COSVisitorException, 
>> IOException {
>>                final byte[] generatedTemplate = generateSimpleTemplate();
>>        final PDDocument templateDoc = new PDDocument().load(new 
>> ByteArrayInputStream(generatedTemplate));
>>        Files.write(new File(TEMPLATE_FOLDER,  "template.pdf").toPath(), 
>> generatedTemplate);
>>      
>>         final PDDocument finalDoc = new PDDocument();
>>         final List<PDField> fields = new ArrayList<PDField>();
>>         final int numberOfPages = 2;
>>         final float inch = 72;
>>         final float borderThickness = inch / 48f;
>>         final float distanceFromField = inch / 2f;
>>                  for (int i = 0; i < numberOfPages; ++i) {
>>                      final PDDocumentCatalog templateDocCatalog = 
>> templateDoc.getDocumentCatalog();
>>              final PDAcroForm templateAcroForm = 
>> templateDocCatalog.getAcroForm();
>>              
>>              List<PDField> templatePdfFields = templateAcroForm.getFields();
>>              for (PDField field : templatePdfFields) {
>>                      System.out.println("fully qualified name = "  + 
>> field.getFullyQualifiedName());
>>                      System.out.println("alternate field name = "  + 
>> field.getAlternateFieldName());
>>                      System.out.println("partial name = "  + 
>> field.getPartialName());
>>              }
>>                 final PDField templateField0_null = 
>> templateAcroForm.getField("field");
>>              final PDField templateField0 = templatePdfFields.get(0);
>>             if (templateField0 != null) {
>>                              templateField0.setValue("xxx" + i);
>>                              templateField0.setPartialName("field-" + i);
>>                              templateField0.setReadonly(true);
>>                               final List<PDPage> pages = (List<PDPage>) 
>> templateDocCatalog.getAllPages();
>>                               PDPage page = pages.get(0);
>>                               finalDoc.addPage(page);
>>                    fields.add(templateField0);
>>             }
>>         }
>> 
>>         final PDAcroForm finalForm = new PDAcroForm(finalDoc);
>>         finalDoc.getDocumentCatalog().setAcroForm(finalForm);
>>         finalForm.setFields(fields);
>>                  finalDoc.save(new File(RESULT_FOLDER, 
>> "form-two-templates.pdf"));
>>         templateDoc.close();
>>                 finalDoc.close();
>>                       }
>>       byte[] generateSimpleTemplate() throws IOException, 
>> COSVisitorException {
>>                      PDDocument template = new PDDocument();
>>                              ByteArrayOutputStream resultStream = new 
>> ByteArrayOutputStream() ;
>>                                      final PDPage page = new 
>> PDPage(PDPage.PAGE_SIZE_A4);
>>              page.setRotation(90) ;
>>              template.addPage(page);
>>              
>>              final PDType1Font font = PDType1Font.HELVETICA_BOLD;
>>              
>>              // add a new AcroForm and add it to the document
>>              final PDAcroForm acroForm = new PDAcroForm(template);
>>              template.getDocumentCatalog().setAcroForm(acroForm);
>>              
>>              // Add and set the resources and default appearance
>>              final PDResources res = new PDResources();
>>              final String fontName = res.addFont(font);
>>              acroForm.setDefaultResources(res);
>> 
>>              final COSDictionary cosDict = new COSDictionary();
>>              
>>              final COSArray rect = new COSArray();
>>              rect.add(new COSFloat(250f)); // lower x boundary
>>              rect.add(new COSFloat(700f)); // lower y boundary
>>              rect.add(new COSFloat(500f)); // upper x boundary
>>              rect.add(new COSFloat(750f)); // upper y boundary
>>              
>>              cosDict.setItem(COSName.RECT, rect);
>>              cosDict.setItem(COSName.FT, COSName.getPDFName("Tx")); // Field 
>> Type
>>              cosDict.setItem(COSName.TYPE, COSName.ANNOT);
>>              cosDict.setItem(COSName.SUBTYPE, COSName.getPDFName("Widget"));
>>              final String da = "/" + fontName + " 12 Tf 0 g";
>>              cosDict.setItem(COSName.DA, new COSString(da));
>>              
>>              // add a form field to the form
>>              final PDTextbox textBox = new PDTextbox(acroForm, cosDict);
>>              textBox.setPartialName("field");
>>              acroForm.getFields().add(textBox);
>>              
>>              // specify the annotation associated with the field
>>              // and add it to the page
>>              final PDAnnotationWidget widget = textBox.getWidget();
>>              page.getAnnotations().add(widget);
>>                      
>>              template.save(resultStream);
>>              template.close();
>>              
>>              return resultStream.toByteArray();
>>                  }
>>     }
>> 
>> 
>> ---------------------------------------------------------------------
>> To unsubscribe, e-mail: [email protected]
>> For additional commands, e-mail: [email protected]
>> 
> 
> 
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: [email protected]
> For additional commands, e-mail: [email protected]
> 


---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]

Reply via email to