title: Create Index File(TOC) for merged pdf using itext library in java
body: <p>I am using iText to create a <strong>single PDF by merging a number of PDFs using PDFCopy</strong>.  I need to create a <strong>TOC</strong> (not bookmarks) at the beginning of this document with clickable links to the first pages of each of the source PDFs.</p>
  
  <p><strong>Code to merge pdf</strong></p>
  
  <pre><code>Document PDFJoinInJava = new Document();
  PdfCopy PDFCombiner = new PdfCopy(PDFJoinInJava, outputStream);
  PdfCopy.PageStamp stamp;
  PDFJoinInJava.open();
  PdfReader ReadInputPDF;
  
  List&lt;InputStream&gt; pdfs = streamOfPDFFiles;
  List&lt;PdfReader&gt; readers = new ArrayList&lt;PdfReader&gt;();
  int totalPages = 0;
  Iterator&lt;InputStream&gt; iteratorPDFs = pdfs.iterator();
  for (; iteratorPDFs.hasNext(); pdfCounter++) {
      InputStream pdf = iteratorPDFs.next();
      PdfReader pdfReader = new PdfReader(pdf);
      readers.add(pdfReader);
      totalPages += pdfReader.getNumberOfPages();
      pdf.close();
  }
  int number_of_pages;
  int currentPageNumber = 0;
  int pageOfCurrentReaderPDF = 0;
  Iterator&lt;PdfReader&gt; iteratorPDFReader = readers.iterator();
  
  PdfImportedPage page;
  // Loop through the PDF files and add to the output.
  int count = 1;
  
  while (iteratorPDFReader.hasNext()) {
      PdfReader pdfReader = iteratorPDFReader.next();
      count++;
      number_of_pages = pdfReader.getNumberOfPages();
  
      // Create a new page in the target for each source page.
      for (int pageNum = 0; pageNum &lt; number_of_pages;) {
          currentPageNumber++;
          pageOfCurrentReaderPDF++;
          page = PDFCombiner.getImportedPage(pdfReader, ++pageNum);
          ColumnText.showTextAligned(stamp.getUnderContent(),
                          Element.ALIGN_RIGHT, new Phrase(String
                                  .format("%d", currentPageNumber),new Font(FontFamily.TIMES_ROMAN,3)),
                          50, 50, 0);
              stamp.alterContents();
  
          PDFCombiner.addPage(page);
      }
  }
  PDFJoinInJava.close();
  </code></pre>