是否可以使用IText从PDF文档中获取页眉和页脚?

时间:2019-02-05 09:51:21

标签: java itext pdf-generation

我正在使用IText将两个单独的PDF文件合并为一个PDF文件。但是问题是我需要在所有页面中替换第一页的页眉和页脚。是否可以分隔PDF Itext的页眉,页脚和正文内容?

我正在使用Itext 5

到目前为止,我已经完成

 PdfReader reader1 = new PdfReader("C:\\Users\\user1\\Downloads\\generatedSample.pdf");
 PdfReader reader2 = new PdfReader("C:\\Users\\user1\\Downloads\\tobeconv.pdf");

 Document document = new Document();
    document.addHeader("Header Text", "");
    FileOutputStream fos = new FileOutputStream("C:\\Users\\user1\\Downloads\\MergeFile.pdf");
    PdfCopy copy = new PdfCopy(document, fos);
    document.open();
    PdfImportedPage page;
    PdfCopy.PageStamp stamp;
    Phrase phrase;
    BaseFont bf = BaseFont.createFont();
    Font font = new Font(bf, 9);
    int n = reader1.getNumberOfPages();
    for (int i = 1; i <= reader1.getNumberOfPages(); i++) 
    {
        page = copy.getImportedPage(reader1, i);
        stamp = copy.createPageStamp(page);
        // phrase = new Phrase("page"+i+"of "+n,font);
        ColumnText.showTextAligned(stamp.getOverContent(), Element.ALIGN_CENTER, null, 520, 5, 0);
        stamp.alterContents();
        copy.addPage(page);
    }

    for (int i = 1; i <= reader2.getNumberOfPages(); i++) {

        page = copy.getImportedPage(reader2, i);
        stamp = copy.createPageStamp(page);
        // phrase = new Phrase("page " + (n + i), font);
        ColumnText.showTextAligned(stamp.getOverContent(), 
     Element.ALIGN_CENTER, null, 520, 5, 0);
        stamp.alterContents();
        copy.addPage(page);
    }

有什么建议吗?

感谢您的提前帮助

0 个答案:

没有答案