Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/267.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 使用iText7向页面添加标题_C#_Itext_Itext7 - Fatal编程技术网

C# 使用iText7向页面添加标题

C# 使用iText7向页面添加标题,c#,itext,itext7,C#,Itext,Itext7,我需要添加一个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 yHead

我需要添加一个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);
}
其中,
CreateHtmlParagraph
的定义如下:

Paragraph headerBlock = String.IsNullOrWhiteSpace(header) ? null : CreateHtmlParagraph(header);
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
方法添加它时,所有页面中仅呈现图像


顺便问一下,有没有办法得到标题段落的实际高度?我认为,一旦解决了标题定位问题,其他内容块就会被标题重叠。

我认为您需要使用页面事件。这是有据可查的。创建一个实现IEventHandler的类,该类将处理特定事件

为特定事件添加事件处理程序

pdf.AddEventHandler(PdfDocumentEvent.START_PAGE, new StartPageEventHandler());
StartPageEventHandler是您创建的一个类,用于实现IEEventHandler。对于页眉和页脚,您可能都需要采用这种方法