0

I am using example MergeWithToc2.java to create the TOC. I have tried couple of things to resolve the issue but didn't succeed.

public class MergeWithToc2 {

    public static final String SRC1 = "PositionPdf.pdf";
    public static final String SRC2 = "concatenated1.pdf";
    public static final String SRC3 = "new_page.pdf";
    public static final String DEST = "test/merge_with_toc2.pdf";

    public Map<String, PdfReader> filesToMerge;

    public static void main(String[] args) throws IOException, DocumentException {
    File file = new File(DEST);
    file.getParentFile().mkdirs();
    MergeWithToc2 app = new MergeWithToc2();
    app.createPdf(DEST);
    }

    public MergeWithToc2() throws IOException {
    filesToMerge = new TreeMap<String, PdfReader>();
    for(int i=0 ; i <50 ; i++ ){
    filesToMerge.put(i + "Hello World", new PdfReader(SRC1));
    //filesToMerge.put("02 Movies / Countries", new PdfReader(SRC2));
    }
    }

    public void createPdf(String filename) throws IOException, DocumentException {

    ByteArrayOutputStream baos = new ByteArrayOutputStream();
    Map<Integer, String> toc = new TreeMap<Integer, String>();
    Document document = new Document();
    PdfCopy copy = new PdfCopy(document, baos);
    PageStamp stamp;
    document.open();
    int n;
    int pageNo = 0;
    PdfImportedPage page;
    Chunk chunk;
    for (Map.Entry<String, PdfReader> entry : filesToMerge.entrySet()) {
        n = entry.getValue().getNumberOfPages();
        toc.put(pageNo + 1, entry.getKey());
        for (int i = 0; i < n; ) {
            pageNo++;
            page = copy.getImportedPage(entry.getValue(), ++i);
            stamp = copy.createPageStamp(page);
            chunk = new Chunk(String.format("Page %d", pageNo));
            if (i == 1)
                chunk.setLocalDestination("p" + pageNo);
            ColumnText.showTextAligned(stamp.getUnderContent(),
                    Element.ALIGN_RIGHT, new Phrase(chunk),
                    559, 810, 0);
            stamp.alterContents();
            copy.addPage(page);
        }
    }
    PdfReader reader = new PdfReader(SRC3);
    page = copy.getImportedPage(reader, 1);
    stamp = copy.createPageStamp(page);       
    Paragraph p;
    PdfAction action;
    PdfAnnotation link;
    float y = 770;
    ColumnText ct = new ColumnText(stamp.getOverContent());
    ct.setSimpleColumn(36, 36, 559, y);
    for (Map.Entry<Integer, String> entry : toc.entrySet()) {

        p = new Paragraph(entry.getValue());
        p.add(new Chunk(new DottedLineSeparator()));
        p.add(String.valueOf(entry.getKey()));
        ct.addElement(p);
        ct.go();
        action = PdfAction.gotoLocalPage("p" + entry.getKey(), false);
        link = new PdfAnnotation(copy, 36, ct.getYLine(), 559, y, action);
        stamp.addAnnotation(link);
        y = ct.getYLine();
    }
    ct.go();
    stamp.alterContents();
    copy.addPage(page);
    document.close();
    for (PdfReader r : filesToMerge.values()) {
        r.close();
    }
    reader.close();

    reader = new PdfReader(baos.toByteArray());
    n = reader.getNumberOfPages();
    reader.selectPages(String.format("%d, 1-%d", n, n-1));
    PdfStamper stamper = new PdfStamper(reader, new FileOutputStream(filename));
    stamper.close(); 

      }
}
hoijui
  • 3,615
  • 2
  • 33
  • 41
  • 1
    what exactly is the problem? is there an error, is the output not what you want? (if so, explain what you want and what you get) – hoijui Jul 14 '15 at 21:07
  • 1
    I'm guessing that you didn't read [the free documentation](http://pages.itextpdf.com/ebook-stackoverflow-questions.html) and that, because of that oversight, you don't know [how to reorder the pages of a PDF file](http://stackoverflow.com/questions/30911216/how-to-reorder-the-pages-of-a-pdf-file). If that guess is incorrect, please be more specific and please read the documentation before asking a question. Please don't copy/paste code you don't understand. – Bruno Lowagie Jul 15 '15 at 00:04

0 Answers0