Java Mailing List Archive

http://www.junlu.com/

Home » Home (12/2007) » iText »

[iText-questions] RE: Interleaving pages from two existing PDFs with iText?

Andrzej Jan Taramina

2003-10-31

Replies:

> The problem is that PdfCopy can't duplicate pages. You must read the
> separator.pdf as many times as you are including it. Actually read it only
> once and duplicate it with the constructor PdfReader(PdfReader).

That doesn't seem to work. I'm already reading the separator.pdf file every
time (I store the actual pdf in a byte array and then reuse the array, so
there is no extra IO going on).

Here is what the main concatenation code basically looks like (not
complete...just indicative):

byte[]         buffer;
       
try {
 reader   = new PdfReader( buffer );  
 reader.consolidateNamedDestinations();
     
 pages = reader.getNumberOfPages();
           
 if( document == null ) {
   document  = new Document( reader.getPageSizeWithRotation( 1 ) );
   writer    = new PdfCopy( document, output );
   document.open();
 }
     
 for( i = 0; i < pages; ) {       // Add the PDF content pages
   ++i;
   page = writer.getImportedPage( reader, i );
   writer.addPage( page );
   writer.addPage( getSeparatorPage() );        
 }
     
 form = reader.getAcroForm();
     
 if( form != null ) {
   writer.copyAcroForm( reader );
 }
}

and the getSeparatorPage() method looks like this:

private PdfImportedPage getSeparatorPage()
{
 PdfReader    reader;
 PdfImportedPage  page    = null;
   
 try {
   reader  = new PdfReader( separatorPageBytes );
   page  = writer.getImportedPage( reader, 1 );  
 }
   
 return( page );
}

According to what you said, this should work. However, when I looked inside
the code, the getImportedPage() method checks to see if the last reader used
is the same as the one passed in, and if not, it closes the prior reader.
This might be the cause of the problem.

Another thing I tried was to recreate a new PDFReader in the mainline routine
after each separatorPage() insertion, in an attempt to get around the closing
of the reader in the getImportedPage() method. That didn't work
either....can you do a writer.getImportedPage( reader, x );  where x > 1 on a
newly opened reader, or do you always have to read the pages sequentially?

BTW...I'm using the latest iText 1.01 libraries.

Thanks for any insight you can offer...

Andrzej Jan Taramina
Chaeron Corporation: Enterprise System Solutions
http://www.chaeron.com



-------------------------------------------------------
This SF.net email is sponsored by: SF.net Giveback Program.
Does SourceForge.net help you be more productive? Does it
help you create better code?  SHARE THE LOVE, and help us help
YOU! Click Here: http://sourceforge.net/donate/
_______________________________________________
iText-questions mailing list
iText-questions@(protected)
https://lists.sourceforge.net/lists/listinfo/itext-questions


©2008 junlu.com - Jax Systems, LLC, U.S.A.