Hi,
I have a problem with ToC links in documents.
Everything is fine after creating Toc using function below, ToC is created
with links and that links work.

This is part of my code:

        public static int addTableOfContents(File pdfFile, String tocTitle, int
additionalPageOffset) throws Exception {

                PdfReader reader = new PdfReader(pdfFile.toString());
                List bookmarks = SimpleBookmark.getBookmark(reader);
                if (bookmarks == null || bookmarks.size()==0) return 0;
                
                File fileResult = UtilEx.createTempFile(FormatEx.PDF);

                // step 1: creation of a document-object
                Document document = new Document();
                PdfWriter writer = PdfWriter.getInstance(document, new
FileOutputStream(fileResult));
                document.open();
                generateTableOfContents(document, null, bookmarks, tocTitle, 0, 
false);
                document.close();
                writer.close();
                PdfReader tmpReader = 
PdfUtils.getPdfReader(fileResult.toString());
                int tocPageOffset = tmpReader.getNumberOfPages();
                tmpReader.close();

                try {
                        fileResult.delete();
                        fileResult.createNewFile();
                } catch (Exception e) {}
                writer.close();
                
                document = new Document();
                writer = PdfWriter.getInstance(document, new
FileOutputStream(fileResult));
                document.open();

                iterateBookmarks(bookmarks, tocPageOffset);
                
                /**
                * in this method I'm creating ToC with something like this:
chunk.setLocalGoto("page"+pageDestination);
                */
                generateTableOfContents(document, writer, bookmarks, tocTitle,
additionalPageOffset, true);

                int pageOfCurrentReaderPDF = 0;

                PdfImportedPage page = null;
                PdfContentByte cb = writer.getDirectContent();
                while (pageOfCurrentReaderPDF < reader.getNumberOfPages()) {
                        document.newPage();
                        pageOfCurrentReaderPDF++;
                
document.setPageSize(reader.getPageSizeWithRotation(pageOfCurrentReaderPDF));
                        page = writer.getImportedPage(reader, 
pageOfCurrentReaderPDF);
                        if (page != null)
                                cb.addTemplate(page, 0, 0);
                        
                        PdfDestination d1 = new 
PdfDestination(PdfDestination.FIT);
                        writer.getDirectContent().localDestination("page" +
(pageOfCurrentReaderPDF + additionalPageOffset), d1);
                }
                document.close();
                writer.close();
                
                reader = new PdfReader(fileResult.toString());
                PdfStamper stamper = new PdfStamper(reader, new 
BufferedOutputStream(new
FileOutputStream(pdfFile)));
                stamper.setOutlines(bookmarks);
                stamper.close();
                reader.close();
                
                return tocPageOffset;
        }


But! After merging this file with some other, links in ToC are broken, I get
an error in Acrobat that page tree node is invalid or something like this.
Google doesn't know anything about this Acrobat message:)

This is part of code after which the file (ToC) is broken:

                                if (document == null) {
                                        // step 1: creation of a document-object
                                        document = new 
Document(reader.getPageSizeWithRotation(1));
                                        // step 2: we create a writer that 
listens to the document
                                        writer = new PdfCopy(document, new
FileOutputStream(tempFile.toString()));
                                        //temporary disabled
                                        
writer.setViewerPreferences(PdfCopy.PageModeUseOutlines);
                                        // step 3: we open the document
                                        document.open();
                                }

                                // step 4: we add content
                                for (int j = 1; j <= numberPages; j++) {

                                        PdfImportedPage page = 
writer.getImportedPage(reader, j);
                                        if (page != null) {
                                                document.newPage();
                                                writer.addPage(page);
                                                writer.flush();
                                        }
                                }

So I've found out that the problem is because in PdfCopy file merging method
named destination are not copied. I have to use PdfCopy, cause I have to
merge also AcroForms. But is there any way to make it work?

I have to say, that in old version of iText (I don't remember exact version)
this piece of code was working!
But I've encountered problem with big files, and I've decided to use new
iText. Big files work fine, but there is a problem with links in ToC - named
destinations merging.

Please help me..

Thanks in advance,
Mariusz
-- 
View this message in context: 
http://www.nabble.com/Problem-with-ToC-in-PDF-after-merging-using-PdfCopy-tf3278267.html#a9117117
Sent from the iText - General mailing list archive at Nabble.com.
-------------------------------------------------------------------------
Take Surveys. Earn Cash. Influence the Future of IT
Join SourceForge.net's Techsay panel and you'll get the chance to share your
opinions on IT & business topics through brief surveys-and earn cash
http://www.techsay.com/default.php?page=join.php&p=sourceforge&CID=DEVDEV
_______________________________________________
iText-questions mailing list
[email protected]
https://lists.sourceforge.net/lists/listinfo/itext-questions
Buy the iText book: http://itext.ugent.be/itext-in-action/

Reply via email to