【发布时间】:2018-08-08 22:59:30
【问题描述】:
我需要添加一个 HTML 块作为使用 iText7 的所有页面的标题。标题包含一个图像徽标和一些文本。
我现在有这个,在关闭文档对象之前:
for (int i = 1; i <= n; i++)
{
float x = pdf.GetPage(i).GetPageSize().GetWidth() / 2; // 297.5f
float yFooter = 20;
if (headerBlock != null)
{
float yHeader = 600;// pdf.GetPage(i).GetPageSize().GetTop() - 20;
// Header
document.ShowTextAligned(headerBlock, 0, yHeader, page, TextAlignment.LEFT, VerticalAlignment.BOTTOM, 0);
}
// Footer
Paragraph footerBlock = new Paragraph(String.Format("Página {0} de {1}", i, n));
document.ShowTextAligned(footerBlock, x, yFooter, page, TextAlignment.CENTER, VerticalAlignment.MIDDLE, 0);
}
页脚工作正常,但页眉。
标题是这样加载的:
Paragraph headerBlock = String.IsNullOrWhiteSpace(header) ? null : CreateHtmlParagraph(header);
其中,CreateHtmlParagraph 是这样定义的:
private Paragraph CreateHtmlParagraph(string html)
{
Paragraph p = new Paragraph();
ConverterProperties properties = new ConverterProperties();
properties.SetBaseUri(HttpContext.Current.Server.MapPath("/"));
var elements = HtmlConverter.ConvertToElements(html, properties);
foreach (IElement e in elements)
p.Add((IBlockElement)e);
return p;
}
当我使用document.Add 方法添加标题时,效果很好,但仅适用于第一页。所有其他内容都在它后面。
当我尝试使用ShowTextAligned 方法添加它时,所有页面中只呈现图像。
顺便问一下,有没有办法获取页眉段落的实际高度?我想,一旦解决了header定位,就会出现其他内容块会被header重叠的问题。
【问题讨论】: