如何使用java中的itext创建多页内容表

时间:2015-07-14 20:53:02

标签: itext

我使用示例 MergeWithToc2.java 来创建TOC。 我已经尝试了几个方法来解决这个问题,但没有成功。

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(); 

      }
}

0 个答案:

没有答案