在现有PDF中添加页脚

时间:2016-02-23 06:28:58

标签: java pdf itext

我正在尝试在现有PDF中添加页脚。我确实在PDF中添加了一个页脚。

有没有添加2行页脚?这是我的代码:

Document document = new Document(); 

PdfCopy copy = new PdfCopy(document, new FileOutputStream(new File("D:/TestDestination/Merge Output1.pdf")));
document.open();

PdfReader reader1 = new PdfReader("D:/TestDestination/Merge Output.pdf");
int n1 = reader1.getNumberOfPages();

PdfImportedPage page;
PdfCopy.PageStamp stamp;
Font ffont = new Font(Font.FontFamily.UNDEFINED, 5, Font.ITALIC);

for (int i = 0; i < n1; ) {
    page = copy.getImportedPage(reader1, ++i);
    stamp = copy.createPageStamp(page);
    ColumnText.showTextAligned(stamp.getUnderContent(), Element.ALIGN_CENTER,new Phrase(String.format("page %d of %d", i, n1)),297.5f, 28, 0);
    stamp.alterContents();
    copy.addPage(page);
}

document.close();
reader1.close();

1 个答案:

答案 0 :(得分:1)

请转到the official documentation,然后点击Q&A转到常见问题解答。选择Absolute positioning of text

您目前正在使用ColumnText以允许添加单行文本的方式。您正在使用我在问题How to rotate a single line of text?

的回答中所述的ColumnText.showTextAligned(...)

您应该阅读以下问题的答案:

假设您无法访问官方网站(否则您不会发布您的问题),我添加了一个简短的代码段:

ColumnText ct = new ColumnText(stamp.getUnderContent());
ct.setSimpleColumn(rectangle);
ct.addElement(new Paragraph("Whatever text needs to fit inside the rectangle"));
ct.go();

在此代码段中,stamp是您在代码中创建的对象。 rectangle对象的类型为Rectangle。它的参数是要在其中渲染多行文本的矩形的左下角和右上角的坐标。

警告:所有不适合rectangle的文字都将被删除。您可以先通过在模拟模式下添加文本来避免这种情况。如果文本适合,请将其添加为真实。如果它不合适,请使用较小的字体或较大的矩形重新尝试。

相关问题