0

I know there is already some information online but I don't get it.

My case: I'm having a PDF of 10 pages, all page contains the same form. When I'm using my code, only the first page is filled out and the others are still blank. If I checked the field names in Adobe Livecycle, they are all the same of the different pages.

What should I do in my case to fill all the pages and not only the fist one?

My code:

//Pagecount == 10
//I was trying to loop through the pages, no success
for(int i=1; i<=pageCount; i++){    
    form.setField("RecallID", wdContext.currentGetRecallDetailsResponseOutputElement().getRecallid());
    form.setField("Afdeling", afdeling);

    String type = wdContext.currentGetRecallDetailsResponseOutputElement().getType();
    if(type.equals("D"))form.setField("Type", "DESTRUCTION");
    else if(type.equals("B"))form.setField("Type", "BLOCK");


    form.setField("Description", wdContext.currentGetRecallDetailsResponseOutputElement().getDescription());
    form.setField("CreationDate",sdf.format(wdContext.currentGetRecallDetailsResponseOutputElement().getCreationdate()));
    form.setField("Enddate", sdf.format(wdContext.currentGetRecallDetailsResponseOutputElement().getEnddate()));

    form.setField("Acties", wdContext.currentGetRecallDetailsResponseOutputElement().getAction_Nl());
    form.setField("Actions", wdContext.currentGetRecallDetailsResponseOutputElement().getAction_Fr());

    form.setField("Probleem", wdContext.currentGetRecallDetailsResponseOutputElement().getProblem_Nl());
    form.setField("Probleme", wdContext.currentGetRecallDetailsResponseOutputElement().getProblem_Fr());

}

stamper.setFormFlattening(true);
stamper.close();
user3388946
  • 49
  • 3
  • 8

2 Answers2

1

Please take a look at the FillFlattenMerge2 example. This example is explained in this video tutorial: https://www.youtube.com/watch?v=6YwDME0Fl1c (if you follow the tutorial, you'll understand why FillFlattenMerge1 is an example on how NOT to do it.)

Document document = new Document();
PdfCopy copy = new PdfSmartCopy(document, new FileOutputStream(dest));
document.open();
ByteArrayOutputStream baos;
PdfReader reader;
PdfStamper stamper;
AcroFields fields;
for (int i = 0; i < data.length; i++) {
    // create a PDF in memory
    baos = new ByteArrayOutputStream();
    reader = new PdfReader(SRC);
    stamper = new PdfStamper(reader, baos);
    fields = stamper.getAcroFields();
    tokenizer = new StringTokenizer(line, ";");
    fields.setField("name", data[i].getName());
    ...
    stamper.setFormFlattening(true);
    stamper.close();
    reader.close();
    // add the PDF to PdfCopy
    reader = new PdfReader(baos.toByteArray());
    copy.addDocument(reader);
    reader.close();
}
br.close();
document.close();

You can see the example in action here: http://demo.itextsupport.com/itextsamples/ (Click on the link "How to do it correctly" next to "Fill, flatten and merge").

Bruno Lowagie
  • 75,994
  • 9
  • 109
  • 165
  • I can't complete PdfSmartCopy. Do you know an easier way? All the fields having the same name and I know the pages...In your example code I don't know where you get the 'dest'. – user3388946 Jul 02 '14 at 09:10
  • `dest` is just the path to a file. See http://itextpdf.com/sandbox/acroforms/reporting/FillFlattenMerge2 for the full source code. In my example, I use the same field names for every page, but that doesn't matter as I flatten the PDF (which removes the fields and keeps the data). The example is fairly simple. Please watch the tutorial video to find out the complete background of the example. (You'll save time wondering how it works by spending time on getting educated by the tutorial.) – Bruno Lowagie Jul 02 '14 at 09:43
0

Create an object of form inside loop.

Also check pageCount if it is 1.

Ninad Pingale
  • 6,801
  • 5
  • 32
  • 55
  • This solution is not working...I copy paste the line: AcroFields form = stamper.getAcroFields(); inside the loop, but it's not working – user3388946 Jul 02 '14 at 08:24
  • Check 2 things - 1. now you are iterating through `for(int i=1; i<=tellerTable; i++){ ` 2. You are not using form object after setting properties. – Ninad Pingale Jul 02 '14 at 09:24
  • I'm stamping my form? I iterate through the pages I will show, if it should only show 3 pages, then I'll try to stamp only the 3 pages en not the whole 10. What do you mean with I'm filling first and not last? Codeline of this? – user3388946 Jul 02 '14 at 09:30
  • In question you said, 1st page is getting filled, But I think it should be the last page in case form is getting overwritten.. – Ninad Pingale Jul 02 '14 at 09:31
  • I want every page filled with the same values...but with this code only the first page is filled an the other 9 are still empty – user3388946 Jul 02 '14 at 09:35