微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

iText 7 HTML 到 PDF 转换,正文内容块标记为在页脚上保持重叠

如何解决iText 7 HTML 到 PDF 转换,正文内容块标记为在页脚上保持重叠

基于使用 iText 7 生成 PDF 的 html 模板,我面临着样式为“page-break-inside:avoid”的 html 表格元素与页面页脚文本之间的重叠问题。

>

添加了用于管理页眉/页脚的事件处理程序,这里是部分代码

ConverterProperties converterProperties = new ConverterProperties();

// Create a Temp PDF file temporary,remove this when the whole invoice is created to manaqge it as stream byte[]
string file = System.IO.Path.Combine(basePath,$@"Documents\Temp_Invoice_{invoice.FacilityInvoiceId}.pdf");
var writer = new PdfWriter(file);
var pdf = new PdfDocument(writer);
Document doc = new Document(pdf);
pdf.AddEventHandler(PdfDocumentEvent.END_PAGE,new GenerateInvoiceTextFooterEventHandler(doc));
pdf.SetDefaultPageSize(PageSize.LEgal);
doc.SetBottomMargin(75); // To avoid overlapping with footer
var document = HtmlConverter.ConvertTodocument(invoiceHtml,pdf,converterProperties);
document.Close();

这里是事件处理程序:

internal class GenerateInvoiceTextFooterEventHandler : IEventHandler
{
    private readonly Document _doc;

    public GenerateInvoiceTextFooterEventHandler(Document doc)
    {
        _doc = doc;
    }

    public void HandleEvent(Event @event)
    {
        PdfDocumentEvent docEvent = (PdfDocumentEvent)@event;
        Rectangle pageSize = docEvent.GetPage().GetPageSize();
        PdfFont font = null;

        try
        {
            string basePath = AppDomain.CurrentDomain.BaseDirectory;
            font = PdfFontFactory.CreateFont(System.IO.Path.Combine(basePath,@"fonts\Roboto\Roboto-Regular.ttf"));
        }
        catch (IOException e)
        {
            Console.Error.WriteLine(e.Message);
        }

        float coordX = ((pageSize.GetLeft() + _doc.GetLeftMargin())
                            + (pageSize.GetRight() - _doc.GetRightMargin())) / 2;
        float footerY = _doc.GetBottomMargin();
        Canvas canvas = new Canvas(docEvent.GetPage(),pageSize);
        canvas
            .SetFont(font)
            .SetFontSize(11) // 15px
            .showtextAligned("This provider invoice is prepared by Company LLC based on information supplied by",coordX,footerY,TextAlignment.CENTER)
            .showtextAligned("insured,POA (if applicable) and provider(s).For more information,contact Company at",footerY - 15,TextAlignment.CENTER)
            .showtextAligned("1-XXX-YYYY-8742.",footerY - 30,TextAlignment.CENTER)
            .showtextAligned("copyright ©2020 Company LLC,All Rights Reserved",footerY - 55,TextAlignment.CENTER)
            .Close();
    }
}

尽管有这种配置,打印的页面显示了与页脚重叠的表格块:

enter image description here

enter image description here

看起来内容没有考虑 C# 设置的底部边距的新值。我遵循文档 here 和堆栈溢出 link,我尝试更改底部边距值,并相应地设置页脚位置但没有成功,请记住底部边距应大于整个页脚文本.

我在 HTML 模板中所做的就是使用 @Page 指令样式:

@page {
            margin-top: 20px;
            margin-bottom: 20px;
            margin-left: 60px;
            padding: 0;
        }

不过,c#设置下边距比@Page样式设置更重要。

感谢您的帮助。

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。