向现有 PDF 添加页脚
Adding footer to existing PDF
我正在尝试向我现有的 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();
请转到the official documentation and click Q&A to go to the Frequently Asked Questions. Select Absolute positioning of text。
您目前使用 ColumnText
的方式允许您添加单行文本。正如我对问题 How to rotate a single line of text?
的回答中所解释的,您正在使用 ColumnText.showTextAligned(...)
您应该阅读以下问题的答案:
- How to add text at an absolute position on the top of the first page?
- How to add text inside a rectangle?
- How to truncate text within a bounding box?
- How to fit a String inside a rectangle?
- How to reduce redundant code when adding content at absolute positions?
假设您无法访问官方网站(否则您不会发布您的问题),我将添加一个简短的代码片段:
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
的文本都将被删除。您可以通过首先在模拟模式下添加文本来避免这种情况。如果文本适合,请添加它。如果不合适,请使用较小的字体或较大的矩形重新尝试。
我正在尝试向我现有的 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();
请转到the official documentation and click Q&A to go to the Frequently Asked Questions. Select Absolute positioning of text。
您目前使用 ColumnText
的方式允许您添加单行文本。正如我对问题 How to rotate a single line of text?
ColumnText.showTextAligned(...)
您应该阅读以下问题的答案:
- How to add text at an absolute position on the top of the first page?
- How to add text inside a rectangle?
- How to truncate text within a bounding box?
- How to fit a String inside a rectangle?
- How to reduce redundant code when adding content at absolute positions?
假设您无法访问官方网站(否则您不会发布您的问题),我将添加一个简短的代码片段:
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
的文本都将被删除。您可以通过首先在模拟模式下添加文本来避免这种情况。如果文本适合,请添加它。如果不合适,请使用较小的字体或较大的矩形重新尝试。