【发布时间】:2018-06-20 17:00:35
【问题描述】:
大家好,提前感谢您的帮助。我正在学习 iText 7,并正在创建一个从 URL 中提取网页并将其转换为 PDF 的应用程序。我已经完成了非常基本的工作,即抓取页面,将其转换为 PDF,然后将其写入磁盘。我接下来要完成的是添加页眉和页脚以及y的第x页。我试过了:
@media print {
@page {
size: 8.5in 5.5in portrait;
margin-left: 0.5cm;
margin-right: 0.5cm;
margin-bottom: .5cm;
padding-bottom: 3.5cm;
background: #FFF;
}
thead { display: table-header-group; }
tfoot { display: table-footer-group; }
}
这似乎将页眉和页脚放在每一页上,尽管文档的最后一页将页脚放在最后一个元素出现的位置之后(不理想)。由于我正在抓取页面,因此我不确定如何添加页码和计数。所以我的问题是如何解决这个问题,即我应该坚持使用媒体查询,还是应该生成 PDF,通过使用某种类型的处理程序插入页眉和页脚的循环(或者我应该在创建初始文件)。我现在有一些关于分页的教程,但没有与转换网页相结合,所以我对如何继续感到困惑(也许是不必要的)。任何帮助将不胜感激。
我试过了:
@media print {
@page {
size: 8.5in 5.5in portrait;
margin-left: 0.5cm;
margin-right: 0.5cm;
margin-bottom: .5cm;
padding-bottom: 3.5cm;
background: #FFF;
@bottom-right {
content: "Page " counter(page) " of " counter(pages);
}
}
thead { display: table-header-group; }
tfoot { display: table-footer-group; }
}
但在 HTML 或转换后的 PDF 中不显示页码。我需要使用:thead { display: table-header-group; }
tfoot { display: table-footer-group; }
to force the header and footer to display on each page. Obviously I'm not handling this correctly, but don't know what to try next.
`
在此处输入代码
【问题讨论】:
-
我已经更新了我的答案。 我强烈建议您阅读本教程。 如果您定义了
new MediaDeviceDescription(MediaType.PRINT),则 iText 只会查看 CSS 的@page部分!你这样做了吗? -
@BrunoLowagie 为了使页面工作,我们是否应该在将 HTML 转换为 PDF 后定义这个新的 MediaDeviceDescription(MediaType.PRINT)?
-
@BrunoLowagie 我正在使用这个 css ol.anchorLink a::after { content: leader('.') target-counter(attr(href), page); } 在目录中的链接之后显示页码我们有什么要在 mediaDeviceDescription 中配置的吗?
标签: itext7