Second try:

I have many different Pdfs, lets say chapters from a book. I want to put
them together into one Pdf. Furthermore I want to create a table of
contents.

The final Pdf exists of a table of contents and all chapters of the book.
The table of contents should have links to the particular chapter.

Now the code (I haven´t considered the links yet):

In the first step I create the table of contents. I saved the names of the
chapters in one array (called "namesOfPages"). Creating the table of
contents works without problems.

In the second step I want to append each Pdf (each chapter of the book) to
the final Pdf. When I open the final Pdf (called "all.pdf") it contains only
the chapter, not the table of contents. When I uncomment the second step the
final Pdf contains the table of content. Because of this I think, that the
table of contents will be overwritten. You understand?

Now I want to know, what I can to, to get this two parts (table of contents
and chapters) into one Pdf. Thanks for help!



//--------------------------FIRST STEP----------------------------------

String outFile = OUTPUT_FILE;

Document document = new Document();

PdfWriter writer2 = null;
writer2.getInstance(document, new FileOutputStream("C:\\Dokumente und
Einstellungen\\jergi\\Desktop\\pdf\all.pdf"));

document.open();

Paragraph tx = new Paragraph("TABLE OF CONTENTS");
document.add(tx);

while(namesOfPages[w] != null)
{
    pagenumber = pagenumber + numberOfPages[w];
    Paragraph txx = new Paragraph(namesOfPages[w] + "\t" + pagenumber);
    document.add(txx);
    w++;
}
document.close();



//--------------------------SECOND STEP-----------------------------------

//path to the pdf which will be copied next. The Pdfs are called 1.pdf 2.pdf
3.pdf......
String generatedPDF = OOFFICE_OUTPUT_FOLDER + File.separator +
PdfNameCounter + ".pdf";

reader = new PdfReader( generatedPDF );

PdfCopy writer = null;
writer = new PdfCopy( document, new FileOutputStream( outFile, true ) );

while ( PdfNameCounter < numberPDFs )
{
    //pdf which will be copied next
    generatedPDF = OOFFICE_OUTPUT_FOLDER + File.separator + PdfNameCounter +
".pdf";

    File pdf = new File( generatedPDF );
    if( !pdf.exists() )
    {
        PdfNameCounter++; //if this pdf doesn´t exist, take the next one
        continue;
    }

    reader = new PdfReader( generatedPDF );

    document.open();

    int n = reader.getNumberOfPages();
    numberOfPages[PdfNameCounter - 1] = n;

    PdfImportedPage page;
    for ( int i = 1; i <= n; i++)
    {
        page = writer.getImportedPage( reader, i );
        writer.addPage( page );
    }
    //pdf.delete();
    PdfNameCounter++;
}
document.close(); 
-- 
View this message in context: 
http://www.nabble.com/appending-pdfs-to-existing-pdf-tp15206262p15206262.html
Sent from the iText - General mailing list archive at Nabble.com.


-------------------------------------------------------------------------
This SF.net email is sponsored by: Microsoft
Defy all challenges. Microsoft(R) Visual Studio 2008.
http://clk.atdmt.com/MRT/go/vse0120000070mrt/direct/01/
_______________________________________________
iText-questions mailing list
iText-questions@lists.sourceforge.net
https://lists.sourceforge.net/lists/listinfo/itext-questions
Buy the iText book: http://itext.ugent.be/itext-in-action/

Reply via email to