Hi,

 

I have 2 pdf documents (doc1.pdf ; doc2.pdf) that I must concatenate in another 
pdf (result.pdf)

 

doc1.pdf and doc2.pdf have internal links

 

I'm using a variant of the code of ConcatenateStamp sample:

http://itextpdf.com/examples/index.php?page=example&id=128

 

The result document (result.pdf) is created but internal links of the second 
document (doc2.pdf) have the original page destination (if the first link in 
doc2.pdf point to the 3th page of doc2.pdf the same link in result.pdf point to 
the 3th page of result.pdf).

 

I've tried to change the destination using this code:

 

public class ConcatenateStamp2 {

 

 

    /**

     * Main method.

     * @param args no arguments needed

     * @throws DocumentException

     * @throws IOException

     * @throws SQLException

     */

    public static void main(String[] args)

        throws IOException, DocumentException, SQLException {

 

                int numPaginas=0;

                int numPaginasInsertadas=0;

 

        // step 1

        Document document = new Document();

 

        // step 2

        PdfCopy copy = new PdfCopy(document, new 
FileOutputStream("result.pdf"));

 

        // step 3

        document.open();

 

        numPaginasInsertadas=insertarPaginas("doc1.pdf",

                                                            copy,

                                                            numPaginas);

        numPaginas+=numPaginasInsertadas;

 

        numPaginasInsertadas=insertarPaginas("doc2.pdf",

                                                                                
                                              copy,

                                                                                
                                              numPaginas);

 

        // step 5

        document.close();

    }

 

    public static int insertPage(String sourceDoc,

                                                    PdfCopy copy,

                                                    int numPages)

       throws IOException, DocumentException, SQLException {

 

                // reader for document 1

        PdfReader reader1 = new PdfReader(sourceDoc);

        //reader1.consolidateNamedDestinations();

        int n1 = reader1.getNumberOfPages();

 

        // initializations

        PdfImportedPage page;

        PdfCopy.PageStamp stamp;

 

        // Loop over the pages of document 1

        for (int i = 0; i < n1; ) {

 

            PdfDictionary pageDic=reader1.getPageN(i);

 

            if (pageDic!=null){

                            // obtener las anotaciones

                            PdfArray 
anotaciones=pageDic.getAsArray(PdfName.ANNOTS);

 

                            // recorrer las anotaciones

                            if (anotaciones!=null){

                                           for (int 
an=0;an<anotaciones.size();an++){

                                               PdfDictionary 
anotacion=anotaciones.getAsDict(an);

                                               if 
(anotacion.contains(PdfName.SUBTYPE) &&

                                                                                
                  anotacion.getAsName(PdfName.SUBTYPE)==PdfName.LINK){

                                                              

                                                                                
                              // La anotación es un link obtener el destino

                                                              PdfArray 
destino=anotacion.getAsArray(PdfName.DEST);

 

                                                              // El primer 
elemento del destino es la página

                                                              PdfNumber 
numpagina=destino.getAsNumber(0);

                                                              int 
pag=numpagina.intValue();

 

                                                              // Calcular el 
incremento de pagina y asignarlo

                                                              PdfNumber 
nuevapagina=new PdfNumber(pag+numPages);

                                                              destino.set(0, 
nuevapagina);

                                               }

                                           }

                            }

            }

 

                                               /*

                                               Here If you inspect reader1 you 
see that new value is assigned

                                               */

                                               

            i++;

                page = copy.getImportedPage(reader1, i);

                                               

                                               /*

                                               At this time the links in read 
page does not has changed values, has original values

                                               */

                                               

            stamp = copy.createPageStamp(page);

            // add page numbers

            ColumnText.showTextAligned(

                    stamp.getUnderContent(), Element.ALIGN_CENTER,

                    new Phrase(String.format("Página %d", 
numPaginasActuales+i)),

                    297.5f, 28, 0);

            stamp.alterContents();

            copy.addPage(page);

        }

 

        return n1;

 

    }

}

 

Regards,

 

Miguel A. Maicas

------------------------------------------------------------------------------

_______________________________________________
iText-questions mailing list
iText-questions@lists.sourceforge.net
https://lists.sourceforge.net/lists/listinfo/itext-questions

Buy the iText book: http://www.itextpdf.com/book/
Check the site with examples before you ask questions: 
http://www.1t3xt.info/examples/
You can also search the keywords list: http://1t3xt.info/tutorials/keywords/

Reply via email to