You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@pdfbox.apache.org by "Md.Farooq" <fa...@gmail.com> on 2015/07/12 07:58:37 UTC

Need help to start

Hi Team,

I am searching for PdbBox cookbook and sample java code for creating
interactive forms and read the form data from pdf.

i request dev team to please provide me the cook book and sample code for
creating interactive form with simple form example.

Thanks in advance.

-- 
Thanks & Regards
Mohammed Farooq
9742537865

Re: Need help to start

Posted by Tilman Hausherr <TH...@t-online.de>.
Am 12.07.2015 um 07:58 schrieb Md.Farooq:
> Hi Team,
>
> I am searching for PdbBox cookbook and sample java code for creating
> interactive forms and read the form data from pdf.
>
> i request dev team to please provide me the cook book and sample code for
> creating interactive form with simple form example.

Hmm, yes, it's true, that is missing, i.e. an example for reading a 
field and writing a field would be useful. And maybe creating a field. 
However, had you read this
https://pdfbox.apache.org/support.html
you would have looked on stackoverflow or in the examples already :-)

In the examples, look for

PrintFields.java
SetField.java

About creating a field, here's some code that creates a field, this 
appeared in the user mailing list a few weeks ago by the user "Phiroc", 
for you "generateSimpleTemplate" is relevant:


import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.IOException;
import java.nio.file.Files;
import java.util.ArrayList;
import java.util.List;
import org.apache.pdfbox.cos.COSArray;
import org.apache.pdfbox.cos.COSDictionary;
import org.apache.pdfbox.cos.COSFloat;
import org.apache.pdfbox.cos.COSName;
import org.apache.pdfbox.cos.COSString;
import org.apache.pdfbox.exceptions.COSVisitorException;
import org.apache.pdfbox.pdmodel.PDDocument;
import org.apache.pdfbox.pdmodel.PDDocumentCatalog;
import org.apache.pdfbox.pdmodel.PDPage;
import org.apache.pdfbox.pdmodel.PDResources;
import org.apache.pdfbox.pdmodel.font.PDType1Font;
import org.apache.pdfbox.pdmodel.interactive.annotation.PDAnnotationWidget;
import org.apache.pdfbox.pdmodel.interactive.form.PDAcroForm;
import org.apache.pdfbox.pdmodel.interactive.form.PDField;
import org.apache.pdfbox.pdmodel.interactive.form.PDTextbox;

/**
  *
  *
  */
public class Phiroc2
{

     final static File RESULT_FOLDER = new File("XXXX");
     final static File TEMPLATE_FOLDER = new File("XXXX");

     public static void main(String[] args)
     {
         new Phiroc2();
     }

     public Phiroc2()
     {
         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 = 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)
         {
             System.out.println("page: " + i);
             final PDDocumentCatalog templateDocCatalog = 
templateDoc.getDocumentCatalog();
             final PDAcroForm templateAcroForm = 
templateDocCatalog.getAcroForm();

             List<PDField> templatePdfFields = templateAcroForm.getFields();
             System.out.println("field count: " + templatePdfFields.size());
             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");
             System.out.println("templateField0_null: " + 
templateField0_null);

             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: dev-unsubscribe@pdfbox.apache.org
For additional commands, e-mail: dev-help@pdfbox.apache.org