- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我正在开发一个工具,用于生成代表评估结果的 PDF 文档。这些文档的结构以及一些文本和图像是由非技术用户定义的(这就是为什么不选择 Apache FOP 和 XSL 的原因之一)。
OpenPDF 似乎是一个很有前途的库(除了 Apache PDFBox,它的级别太低)。但是,生成的文档必须包含目录。
预期的文档结构是这样的:
1. Cover
2. Abstract
3. Table of Contents
4. Chapter 1 .. n
由于我不知道文档最终将有多少页或不同章节将在哪些页上开始,因此我无法在将每一章添加到文档之前定义目录。
由于 OpenPDF 直接将元素写入文档,因此似乎不可能保留示例性目录元素的引用并在添加所有章节后添加其内容。
最佳答案
我通过使用 com.lowagie.text.pdf.PdfWriter
的 reorderPages(int[])
方法找到了一种适用于预期结构的解决方案:
首先,我保留预期的目录第一页(摘要后的第一页):
int intendedTocFirstPage = pdfWriter.getCurrentPageNumber() - 1; // - 1 because of a necessary `document.newPage();` before that
将所有章节添加到文档后,我最后添加目录并保留第一页和最后一页(因为可能需要多个页面,具体取决于章节和子章节的数量):
int tocFirstPage = pdfWriter.getCurrentPageNumber();
document.add(new Paragraph("TBA: Actual Table of Contents")); // TODO replace with the table of contents based on the existing chapters and sections
document.newPage();
int tocLastpage = pdfWriter.getCurrentPageNumber();
然后我创建一个数组,它基于三个 int
变量来表示页面的新顺序:
private int[] getReorderedPagesForTableOfContents(int intendedTocFirstPage, int tocFirstPage, int tocLastpage) {
int[] pages = IntStream
.range(1, tocLastpage)
.toArray();
/*
* Reorder the pages array by placing the toc page numbers at
* the indexes starting from targetedTocFirstPage (should be
* the page directly after the summary)
*/
int numberOfTocPages = tocLastpage - tocFirstPage;
if (numberOfTocPages >= 0) {
System.arraycopy(pages, tocFirstPage - 1, pages, intendedTocFirstPage, numberOfTocPages);
}
/* Shift the page numbers of all pages after the last toc page */
for (int i = intendedTocFirstPage + numberOfTocPages; i < pages.length; i++) {
pages[i] = i - numberOfTocPages + 1; // `+ 1` because page numbers start with 1 not 0
}
return pages;
}
最后,我对文档的页面进行重新排序:
int[] reorderedPages = getReorderedPagesForTableOfContents(targetedTocFirstPage, tocFirstPage, tocLastpage);
pdfWriter.reorderPages(reorderedPages);
这可行,但会产生另一个问题:
使用页脚显示页码将不再正常工作,因为重新排序之前的页码将被保留。
一个可能的解决方案是首先创建完整的文档,包括页面的重新排序,然后使用 PdfReader 添加页码,如本答案所述:https://stackoverflow.com/a/759972/10551549
如果有人有更好的解决方案,我会很高兴听到它(因为在我看来,这个有点困惑)。 :)
关于java - 如何使用 OpenPDF 添加目录,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61815826/
当我将表格添加到页面的页脚时,页脚会调整到合适的大小,但是,表格不会保留在该页脚内,而是位于页面顶部。 我创建了一个测试场景来说明我的意思。 public class TestClass {
我一直在尝试在 Scala 中重新实现 OpenPDF 1.2.4 和 1.2.11 中的连接示例: def mergePdfs(docs: Seq[Array[Byte]]): Array[Byte
我正在使用 OpenPDF 生成 PDF 文档,并将页眉和页脚图像添加到 PDF 中。需要将一个段落添加到 PDF 的中心,其中一些文本内容是基于输入请求的动态内容,并且所有内容都需要用边框包围。我能
我的公司需要实现一种在官方 PDF 上加盖电子印章的方法,以确保其完整性。我听一些同事说已经用OpenPDF完成了,但我在网上搜索目前还没有找到任何结果。 仅使用 OpenPDF 是否可以有效地实现这
我正在开发一个工具,用于生成代表评估结果的 PDF 文档。这些文档的结构以及一些文本和图像是由非技术用户定义的(这就是为什么不选择 Apache FOP 和 XSL 的原因之一)。 OpenPDF 似
我一直在尝试从 iText v5.5.11(许可版本)库迁移到 OpenPDF v1.2.8(开源) 库。我认为两者具有相似的功能。但是,iText 库中的 renderImage 函数在 OpenP
我在 Java 中使用 OpenPDF 1.3.20,想更改表格单元格中文本/段落的对齐方式。到目前为止,无论我尝试了什么,都改变了文本在任何地方的定位。 我只了解到添加为 table.addCell
我正在使用 OpenPDF 生成 PDF 发票报告。在 PDF 上,我必须在每一页上为页眉/页脚设置一个矩形 block 。我已使用 HeaderFooter 类在每个页面上添加页眉/页脚,但这仅适用
如何使用 OpenPDF 将 HTML 转换为 PDF ? 据我所知,OpenPdf 是 Itext 4 的一个分支。不幸的是我找不到 Itext 4 文档。 最佳答案 好吧,看来你不能只用 Open
我似乎有一个我无法自己解决的问题。 我正在使用 flysaucer-9.1.20 和 openpdf-1.3.22 从 XHTML 源生成 PDF。 我客户的 CI 要求 Verdana 作为他们所有
我是一名优秀的程序员,十分优秀!