Another way to fix this might be changing PdfCopy.getImportedPage. Here you can 
also try to remember all PdfReaderInstance instances.

BTW, I'm not so sure anyway that closing the reader and readerFile here is such 
a good idea if re-use is intended.

-----Original Message-----
From: Michael Klink [mailto:[EMAIL PROTECTED] 
Sent: Friday, November 14, 2008 12:32 PM
To: [email protected]
Subject: Re: [iText-questions] Policy for adding unittests?


Kevin,

I think the problem is due to PdfCopy.getImportedPage requesting a new 
PdfReaderInstance from the PdfReader (because of switching from reader to 
reader in your test case) and PdfReader.getPdfReaderInstance generating and 
returning a new instance each time. This makes 
PdfCopy.getImportedPage(stampReader, stampPageNum++) import the one stamp page 
time and time again giving it different indirect references.

PdfWriter.addDirectTemplateSimple() on the other hand only adds 
PdfReaderInstance instances to importedPages if there is not yet a 
PdfReaderInstance for the same reader already there.

PdfWriter.addSharedObjectsToBody eventually tries to serialize the imported 
pages and finds only the first PdfReaderInstance in importedPages, thus only 
serializes the first XObject.

So, when adding page 1 from pageReader a second time, the new PdfReaderInstance 
generates the XObject a second time with a different indirect reference which 
is referenced in the second result page. As this PdfReaderInstance is not added 
to PdfWriter.importedPages, though, the second XObject is not serialized into 
the document.

This might be fixed by making PdfReader.getPdfReaderInstance(PdfWriter writer) 
remember the instances returned and return the same instance when called for 
the same writer. Obviously it should remember them only as long as there still 
is a (Java object) reference pointing to the instance in question, otherwise 
this could be quite a memory leak. -> WeakReferenceMap variants.

Alternatively PdfWriter.addDirectTemplateSimple may not reject multiple 
PdfReaderInstances for the same PdfReader, at least keeping the duplicates for 
serialization by PdfWriter.addSharedObjectsToBody. This unfortunately allows 
for unnecessarily repeated PDF objects.

Hope this helps.

Xavier,

maybe JpdfUnit (http://jpdfunit.sourceforge.net/) would make writing tests 
easier.

Best regards,   Michael.
-- 
Sensationsangebot nur bis 30.11: GMX FreeDSL - Telefonanschluss + DSL 
für nur 16,37 Euro/mtl.!* http://dsl.gmx.de/?ac=OM.AD.PD003K11308T4569a

-------------------------------------------------------------------------
This SF.Net email is sponsored by the Moblin Your Move Developer's challenge
Build the coolest Linux based applications with Moblin SDK & win great prizes
Grand prize is a trip for two to an Open Source event anywhere in the world
http://moblin-contest.org/redirect.php?banner_id=100&url=/
_______________________________________________
iText-questions mailing list
[email protected]
https://lists.sourceforge.net/lists/listinfo/itext-questions

Buy the iText book: http://www.1t3xt.com/docs/book.php

Reply via email to