I am trying to load in a pdf template x number of times and modify the loaded 
template.  The 2nd page of the pdf does not get modified but the first page and 
all pages after the 2nd do get modified.

My code
package org.apache.pdfbox.pdmodel;
import org.apache.pdfbox.exceptions.COSVisitorException;
import org.apache.pdfbox.pdmodel.PDDocument;
import org.apache.pdfbox.pdmodel.edit.PDPageContentStream;
import org.apache.pdfbox.pdmodel.font.PDFont;
import org.apache.pdfbox.pdmodel.font.PDType1Font;

import java.io.IOException;
import java.io.File;

public class HRreports {

      /**
      * @param args
      * @throws IOException
       * @throws COSVisitorException
       */
      public static void main(String[] args) throws IOException, 
COSVisitorException
      {
            int numPages = 3;
            String path = "Test string";
            String label = "I am the 2nd sentence";
            PDDocument document = null;
            File file = new File("/Users/Desktop/PDFtemplate.pdf");
            document = PDDocument.load(file);
            PDPage page = (PDPage) 
document.getDocumentCatalog().getAllPages().get(0);
            PDPageContentStream contentStream = new 
PDPageContentStream(document, page, true, false);

      int n = 0;
      while (n < numPages)
      {
        n++;
        document.importPage(page);
      draw(document, page, path, 85, 650);
      draw(document, page, label, 85, 641);


      }
      document.save("/Users/Desktop/HRReports.pdf");
      document.close();
      }

      public static void draw(PDDocument document, PDPage page, String text, 
int indent, int offset) throws IOException
      {

      PDPageContentStream contentStream = new PDPageContentStream(document, 
page, true, true);
      PDFont font = PDType1Font.COURIER;
      contentStream.setNonStrokingColor(0,0,0);
        contentStream.beginText();
      contentStream.setFont(font, 11);
      contentStream.moveTextPositionByAmount(indent, offset);
      contentStream.drawString(text);
      contentStream.endText();
      contentStream.close();
      }
}




I have also tried not using a loop and create a contentstream write the text, 
close the stream.  Then create a new stream import the page write the text, 
close the stream.  This worked until I added the String label to the 2nd 
content stream I had created and then even it added that string to the first 
page and all pages after the 2nd (the String path was being added to all pages 
but the String label skipped the 2nd page)

I was wondering if anyone else has ran into this problem, am I doing something 
wrong, or is there just a better way to create a multi-page pdf where I want 
all pages to follow a template but I want to modify the templates.

Full discloser I am going to do this in RPG but was trying to figure this out 
in Java since I didn't have to worry about prototypes and if it was failing 
because I did the prototype wrong.  Also the modification is a mailing label if 
that matters.

Thank you in advance for any help.
This e-mail and any files transmitted with it are confidential and solely for 
the use of the individual or entity to which they are addressed and intended. 
If you have received this e-mail in error, please notify the sender by return 
e-mail. If you are not the intended recipient, you may not read, copy, retain, 
print, disclose, or distribute this message or its contents to any other 
individual, for such actions may be unlawful. WARNING: We take certain 
precautions to prevent viruses, but we are not responsible for loss or damage 
arising from the use of this e-mail or attachments.

Reply via email to