在现有PDF中添加页脚 [英] Adding footer to existing PDF

查看:430
本文介绍了在现有PDF中添加页脚的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I am trying to add footer to my existing PDF. I did add one footer to the PDF.

无论如何要添加2行页脚?这是我的代码:

Is there anyway to add 2 lines of footer? This is my code below:

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


推荐答案

请转到官方文档并单击 Q& A 转到常见问题解答。选择文本的绝对定位

Please go to the official documentation and click Q&A to go to the Frequently Asked Questions. Select Absolute positioning of text.

您目前正以一种允许添加单行文本的方式使用 ColumnText 。您正在使用 ColumnText.showTextAligned(...),正如我在回答问题时所解释的那样如何旋转单行文本?

You are currently using ColumnText in a way that allows you to add a single line of text. You are using ColumnText.showTextAligned(...) as explained in my answer to the question How to rotate a single line of text?

你应该阅读问题的答案例如:

You should read the answers to questions such as:

  • 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?

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

Assuming that you don't have access to the official web site (otherwise you wouldn't have posted your question), I'm adding a short code snippet:

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

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

In this snippet, stamp is the object you created in your code. The rectangle object is of type Rectangle. Its parameters are the coordinates of the lower-left and upper-right corner of the rectangle in which you want to render the multi-line text.

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

Caveat: all text that doesn't fit the rectangle will be dropped. You can avoid this by adding the text in simulation mode first. If the text fits, add it for real. If it doesn't fit, try anew using a smaller font or a bigger rectangle.

这篇关于在现有PDF中添加页脚的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆