majiang (1) [Avatar] Offline
#1
Hi all ,
I changed the HelloWorldStampCopy.java code in chapter2 to insert 3 pages into file. but I found from the third page , the filed value become empty . I don't know the reason. anyone know reason? in the book section 2.3.1 ,it says "You can repeat this process as many times as you want:" , but looks like not.

the code like this

RandomAccessFileOrArray letter = new RandomAccessFileOrArray(
"results/in_action/chapter02/HelloLetter.pdf");
reader = new PdfReader(letter, null);
ByteArrayOutputStream baos = new ByteArrayOutputStream();
stamper = new PdfStamper(reader, baos);
form = stamper.getAcroFields();
stamper.setFormFlattening(true);
stamper.close();

reader = new PdfReader(baos.toByteArray());
Document document = new Document(reader.getPageSizeWithRotation(1));
PdfCopy writer = new PdfCopy(document, new FileOutputStream(
"results/in_action/chapter02/tempateCopy.pdf"));
document.open();
//add first page
writer.addPage(writer.getImportedPage(reader, 1));
reader = new PdfReader(letter, null);
baos = new ByteArrayOutputStream();
stamper = new PdfStamper(reader, baos);
form = stamper.getAcroFields();
stamper.close();

reader = new PdfReader(baos.toByteArray());
//add third page
writer.addPage(writer.getImportedPage(reader, 1));
reader = new PdfReader(letter, null);
baos = new ByteArrayOutputStream();
stamper = new PdfStamper(reader, baos);
form = stamper.getAcroFields();
stamper.close();

reader = new PdfReader(baos.toByteArray());
// add third page
writer.addPage(writer.getImportedPage(reader, 1));