Am 30.04.2015 um 18:43 schrieb Maruan Sahyoun:

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.
what do you think about this output

ShortText{type: PDTextField value:COSString{AA}}

with ShortText being the fields fully qualified name?

Yes, that would be fine.



Tilman



BR
Maruan

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: users-unsubscr...@pdfbox.apache.org
For additional commands, e-mail: users-h...@pdfbox.apache.org


---------------------------------------------------------------------
To unsubscribe, e-mail: users-unsubscr...@pdfbox.apache.org
For additional commands, e-mail: users-h...@pdfbox.apache.org




---------------------------------------------------------------------
To unsubscribe, e-mail: users-unsubscr...@pdfbox.apache.org
For additional commands, e-mail: users-h...@pdfbox.apache.org

Reply via email to