使用 iText 添加标题到 PDF 和 RTF

发布于 2024-09-01 03:33:50 字数 1389 浏览 0 评论 0原文

我正在使用 iText 生成 PDF 和 RTF。我知道使用 iText 生成 RTF 并不流行,但我仍然需要使代码同时适用于两者。从技术上讲,我可以使用不同的代码来处理每种类型,但它需要能够包含在同一文件/类中。

我可以使用这样的代码:

String outputData = getFieldValue(myRecord, myFieldName); 
if (exportToPDF) {
  iTextPdfPCell = new PdfPCell(pdfPTable.getDefaultCell());  
  iTextPdfPCell.setPhrase(outputData);
  pdfPTable.addCell(iTextPdfPCell);
}
if (exportToRTF) {
  iTextCell = new RtfCell(outputData);
  iTextTable.addCell(iTextCell);
}

我可以使用 HeaderFooter 添加一个标头,该标头将出现在 PDF 和 RTF 上

Phrase headerPhrase = new Phrase ("This is a page header.");
HeaderFooter header = new HeaderFooter(headerPhrase, false);
iTextDoc.setHeader(header);

问题是当标头变得复杂时。我想添加一个包含表格和图像的标题(图像可能位于表格内)。我在不同的论坛上看到一些代码建议这样做:

Phrase headerPhrase = new Phrase();
headerPhrase.add(iTextTable);
HeaderFooter header = new HeaderFooter(headerPhrase, false);
iTextDoc.setHeader(header);

虽然我的初始测试表明这不会导致任何编译错误并且在技术上“有效”,但它在与文件正文相同的位置启动标头表,因此它们重叠。当我将页边距设置得更宽以便为页眉留出空间时,它只会将页眉与正文一起向下移动。

我在线(和手册中)阅读的大部分信息似乎都指向使用页面事件,但这意味着我的类必须扩展 PdfPageEventHelper。如果我这样做,

1. RTF有类似的东西吗?

2. 如果有,同一个类可以同时扩展“RtfPageEventHelper”(如果存在)和 PdfPageEventHelper 吗?我无法为每种输出类型编写单独的类

3. 如果没有,有没有办法可以在 HeaderFooter 类中使用表格/图像,该类已经适用于 PDF 和 RTF?

4.(我什至不想问这个)由于 iText 似乎已经放弃了 RTF 生成,我是否应该使用其他东西来生成 RTF(最好也适用于 PDF)?

I am using iText to generate both PDF and RTFs. I know RTF generation with iText is not popular, but I still need to make the code work for both. Technically, I can have different pieces of code to handle each type, but it needs to be able to be contained within the same file/class.

I can work with code like this:

String outputData = getFieldValue(myRecord, myFieldName); 
if (exportToPDF) {
  iTextPdfPCell = new PdfPCell(pdfPTable.getDefaultCell());  
  iTextPdfPCell.setPhrase(outputData);
  pdfPTable.addCell(iTextPdfPCell);
}
if (exportToRTF) {
  iTextCell = new RtfCell(outputData);
  iTextTable.addCell(iTextCell);
}

I can add a header using HeaderFooter that will appear on both PDF and RTFs

Phrase headerPhrase = new Phrase ("This is a page header.");
HeaderFooter header = new HeaderFooter(headerPhrase, false);
iTextDoc.setHeader(header);

The trouble is when the header gets complex. I would like to add a header that contains a table and images (images may be within the table). I saw some code on a different forum that suggested doing this:

Phrase headerPhrase = new Phrase();
headerPhrase.add(iTextTable);
HeaderFooter header = new HeaderFooter(headerPhrase, false);
iTextDoc.setHeader(header);

While my initial testing shows that this does not cause any compile errors and technically "works", it starts the header table in the same position as the body text of the file, so they overlap. When I set the page margins wider to allow for space for the header, it just moves the header down with the body.

Most of the information I read online (and in the manual) seems to be pointing to using page events, but that means my class has to extend PdfPageEventHelper. If I do that,

1. Is there anything similar for RTF?

2. If there is, can the same class extend both the 'RtfPageEventHelper' (if it exists) and PdfPageEventHelper? I cannot write separate classes for each output type

3. If not, is there a way that I can use tables/images within the HeaderFooter class, which already works for both PDF and RTF?

4. (I don't even want to ask this) Since iText seems to have left RTF generating behind, should I be using something else to generate RTFs (preferably works with PDF as well)?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

优雅的叶子 2024-09-08 03:33:50

我是用这种格式做的。适用于 RTF,但我认为与 PDF 相同。仅将 RtfHeader 更改为 PdfHeader

document.open();
Paragraph head=new Paragraph("Head");
Paragraph foot=new Paragraph("Foot");
HeaderFoot header=new RtfHeaderFooter(head);
HeaderFoot footer=new RtfHeaderFooter(foot);
document.setHeader(header);
document.setFooter(footer);
document.close();

我等待帮助。

I did it in this format. Is for RTF, but i think is same with PDF. Only change RtfHeader for PdfHeader

document.open();
Paragraph head=new Paragraph("Head");
Paragraph foot=new Paragraph("Foot");
HeaderFoot header=new RtfHeaderFooter(head);
HeaderFoot footer=new RtfHeaderFooter(foot);
document.setHeader(header);
document.setFooter(footer);
document.close();

I wait be help.

小镇女孩 2024-09-08 03:33:50

每个页面上都会显示带有 HeaderFooter 的页眉或页脚。如果您只想要一个文档标题,只需在页面内容之前添加一个标准元素(段落/等)即可。

例如,我正在对我的文档执行此操作:

Paragraph Header = new Paragraph("Document Header", FontFactory.getFont(FontFactory.HELVETICA, 18, Font.BOLDITALIC, new Color(0, 0, 0)));

document.open();
document.add(Header);
document.add(otherContent);
document.close();

A header or footer with HeaderFooter is displayed on every page. If you want simply a document header just add a standard element(paragraph/etc) to the page before your content.

For instance I'm doing this with my document:

Paragraph Header = new Paragraph("Document Header", FontFactory.getFont(FontFactory.HELVETICA, 18, Font.BOLDITALIC, new Color(0, 0, 0)));

document.open();
document.add(Header);
document.add(otherContent);
document.close();
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文