HTML到PDF-使用PdfSharp和HtmlRenderer进行分页 [英] HTML to PDF - page break with PdfSharp and HtmlRenderer

查看:605
本文介绍了HTML到PDF-使用PdfSharp和HtmlRenderer进行分页的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我尝试使用PdfSharp和HtmlRenderer将HTML转换为PDF.这是代码的一部分:

I try to convert HTML to PDF using PdfSharp and HtmlRenderer. This is part of code:

private byte[] CreateHtmlContent()
{
    string htmlContent = File.ReadAllText(@"htmlExample.txt");

    using (MemoryStream ms = new MemoryStream())
    {
        PdfDocument pdfDocument = new PdfDocument();
        PdfDocument pdf = PdfGenerator.GeneratePdf(htmlContent, PdfSharp.PageSize.A4, 60);
        pdf.Save(ms);
        res = ms.ToArray();
    }
    return res;
}

除分页符外,一切都正常.在某些页面上,我得到的结果类似于此图片

Everything works fine except page break. On some pages I have result like on this image

是否可以解决此问题? HTML内容是简单的html,仅包含标题和段落,没有其他标签.我在iTextSharp上没有这个问题,但是在这个项目上,我必须使用PdfSharp和MigraDoc.

Is it possible to fix this? HTML content is simple html that contains only headings and paragraphs and no other tags. I had no this problem with iTextSharp but on this project I have to use PdfSharp and MigraDoc.

推荐答案

当我在github上找到此pull请求时,我遇到了类似的挑战并解决了它: https://github.com/ArthurHub/HTML-Renderer/pull/41

I had a similar challenge and resolved it as I found this pull request on github: https://github.com/ArthurHub/HTML-Renderer/pull/41

您可以设置custom-css-property

You can set the custom-css-property

td { page-break-inside: avoid; }

在您要控制分页符的所有元素或选择器(td,p,.my-class等)上

on all elements or selectors you want (td, p, .my-class, etc.) to control the page breaking.

如果希望库控制某些元素的分页,则可以使用值"auto"

You can use the value "auto" if you want the library to control your page breaking on certain elements

td { page-break-inside: auto; }

还有一个在运行的文本中分页的示例.

There is also a example for page breaking in running text.

这篇关于HTML到PDF-使用PdfSharp和HtmlRenderer进行分页的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆