c# - 使用 iText7 创建新 PDF 时如何将 HTML 放入表格单元格?

标签 c# html pdf itext7

我正在创建一个包含大表格的 PDF。在此表中,有一个可以用 HTML 填充的单元格。如果是,则 HTML 必须被解释为 HTML 而不是显示为常规文本。但是,当我这样做时,布局/样式变得杂乱无章,并且一些图片没有显示。 (在我给出的示例中,项目符号破折号被 9 替换。)

我使用 C# 编程并使用 iText7 创建 PDF。

在我的项目中,我有以下要显示的 HTML 代码。 HTML 代码看起来像这样的原因是因为它是 RTF 转换为 HTML:

<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<meta name="generator" content="SautinSoft.RtfToHtml.dll">
<title>Untitled document</title>
<style type="text/css">
.st1{font-family:Arial;font-size:12pt;color:#000000;}
.list-marker1 li:before {
    content: "\F02D\9";
width: 30px;
font-family: Symbol;
font-size: 11pt;
}

.st2{font-family:Calibri;font-size:11pt;color:#000000;}
</style>
</head>
<body>
<div>
<p style="margin:0pt 0pt 0pt 0pt;"><span class="st1">&nbsp;</span></p>
<ul style="list-style-type:none; list-style-image:none; list-style-    position:outside; margin-left:0px; padding-left:0px; text-indent:0px; margin-top:0px; margin-bottom:0px;" class="list-marker1">
<li value="1" style="margin-left:36pt;text-indent:-18pt;"><span class="st2">Dishwasher soap container consumption is adjusted</span></li></ul>
<ul style="list-style-type:none; list-style-image:none; list-style-position:outside; margin-left:0px; padding-left:0px; text-indent:0px; margin-top:0px; margin-bottom:0px;" class="list-marker1">
<li value="2" style="margin-left:36pt;text-indent:-18pt;"><span class="st2">Power UP ballast water treatmant unit, adjusted what necessary. GPS signal error still pending</span></li></ul>
<ul style="list-style-type:none; list-style-image:none; list-style-position:outside; margin-left:0px; padding-left:0px; text-indent:0px; margin-top:0px; margin-bottom:0px;" class="list-marker1">
<li value="3" style="margin-left:36pt;text-indent:-18pt;"><span class="st2">Installed new insect killer in galley</span></li></ul>
<ul style="list-style-type:none; list-style-image:none; list-style-position:outside; margin-left:0px; padding-left:0px; text-indent:0px; margin-top:0px; margin-bottom:0px;" class="list-marker1">
<li value="4" style="margin-left:36pt;text-indent:-18pt;"><span class="st2">Adjust securing and highest ladder position after its tilted to low trunnion position</span></li></ul>
<ul style="list-style-type:none; list-style-image:none; list-style-position:outside; margin-left:0px; padding-left:0px; text-indent:0px; margin-top:0px; margin-bottom:0px;" class="list-marker1">
<li value="5" style="margin-left:36pt;text-indent:-18pt;"><span class="st2">SW spooling device test is done and no isses </span></li></ul>
<ul style="list-style-type:none; list-style-image:none; list-style-position:outside; margin-left:0px; padding-left:0px; text-indent:0px; margin-top:0px; margin-bottom:0px;" class="list-marker1">
<li value="6" style="margin-left:36pt;text-indent:-18pt;"><span class="st2">Sewage unit dosage pump; make permanent installation + drawing update</span></li></ul>
<ul style="list-style-type:none; list-style-image:none; list-style-position:outside; margin-left:0px; padding-left:0px; text-indent:0px; margin-top:0px; margin-bottom:0px;" class="list-marker1">
<li value="7" style="margin-left:36pt;text-indent:-18pt;"><span class="st2">Intervention on DN203: installed tracker box on ICT request</span></li></ul>
<ul style="list-style-type:none; list-style-image:none; list-style-position:outside; margin-left:0px; padding-left:0px; text-indent:0px; margin-top:0px; margin-bottom:0px;" class="list-marker1">
<li value="8" style="margin-left:36pt;text-indent:-18pt;"><span class="st2">Installed tracker box on ICT request </span></li></ul>
<ul style="list-style-type:none; list-style-image:none; list-style-position:outside; margin-left:0px; padding-left:0px; text-indent:0px; margin-top:0px; margin-bottom:0px;" class="list-marker1">
<li value="9" style="margin-left:36pt;text-indent:-18pt;"><span class="st2">Regular greasing emotors according ship specific grease list</span></li></ul>
<ul style="list-style-type:none; list-style-image:none; list-style-position:outside; margin-left:0px; padding-left:0px; text-indent:0px; margin-top:0px; margin-bottom:0px;" class="list-marker1">
<li value="10" style="margin-left:36pt;text-indent:-18pt;"><span class="st2">Regular maintenance of portable tools</span></li></ul>
<ul style="list-style-type:none; list-style-image:none; list-style-position:outside; margin-left:0px; padding-left:0px; text-indent:0px; margin-top:0px; margin-bottom:0px;" class="list-marker1">
<li value="11" style="margin-left:36pt;text-indent:-18pt;"><span class="st2">Intervention on DN203 due to problem with SB M/E</span></li></ul>
<p style="margin:0pt 0pt 0pt 0pt;"><span class="st2">&nbsp;</span></p>
<p style="margin:0pt 0pt 0pt 0pt;"><span class="st1">&nbsp;</span></p>
</div>
</body>
</html>

当我将此文件另存为 HTML 文件并在浏览器中打开它时,您会得到以下结果(正确):Correct result (website)

但是,当我将它添加到 PDF 单元格时,我得到以下结果:Bad result (PDF)

如您所见,用于项目符号的破折号变成了 9。

我用来将 HTML 代码添加到 PDF 文件的代码如下:

private void AddCellToTable(table table, string HTMLContent) {
    Cell newCell = new Cell();

    foreach (var element in HtmlConverter.ConvertToElements(HTMLContent))
    {
        var test = (IBlockElement)element;
        newCell.Add(test);
    }

    table.AddCell(newCell);
 }

此代码类似于 https://itextpdf.com/en/resources/books/itext-7-converting-html-pdf-pdfhtml/chapter-1-hello-html-pdf 建议的代码(他们使用 java 而不是 C#)。

我想将项目符号显示为破折号,而不是这些 9。任何帮助或建议将不胜感激。

提前谢谢你。

最佳答案

首先,问题与布局(表格/单元格/等处理)无关 - 它仅与字体以及 iText 如何处理它们有关。

其次,问题在于 iText 使用的 PDF 标准符号字体与浏览器使用的字体不同。

第三,iText 没有正确处理 "\F02D\9"(尤其是 "\9"部分)。

您可以做些什么来改进生成的 pdf?不要使用标准 PDF 的 Symbol 字体 - 请改用您自己的 Symbol 字体。

怎么做?

让我向您介绍FontProvider 类。

FontProvider is responsible to handle the fonts which may be used while processing an html file. Its instance could be passed as a parameter of ConverterProperties, otherwise it'd be created by iText. By default iText adds to FontProvider all standard pdf fonts (FontProvider#addStandardPdfFonts) and some free sans fonts (DefaultFontProvider#SHIPPED_FONT_NAMES).

您想使用自己的 Symbol 字体:这意味着您应该阻止 iText 在转换过程中考虑标准 Symbol 字体。为此,请创建一个 DefaultFontProvider 实例,并将第一个构造函数的参数作为 false 传递。 (!) 在 99% 的情况下,您希望在转换过程中考虑一些其他标准字体。所以请手动添加它们,如下所示:

provider.addFont(StandardFonts.TIMES_ROMAN);
// some other fonts to be added

现在将您自己的 Symbol 字体添加到此 FontProvider 实例,方法与我在上面的 Times 中所做的相同:

provider.addFont("C:\\Windows\\Fonts\\symbol.ttf", PdfEncodings.IDENTITY_H);

在此处使用 IDENTITY_H 很重要,因为在 Symbol 字体中“-”的 unicode 值是“F02D”。

完成所有这些更改后,我设法获得了以下文件: enter image description here

第二个问题来了:默认情况下,iText 没有正确处理“\9”。至于这个,我不知道有什么直接的解决方案。可能,从您的 html 中删除它是最好的。

关于c# - 使用 iText7 创建新 PDF 时如何将 HTML 放入表格单元格?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56543269/

相关文章:

c# - 接口(interface)属性的内部修饰符

html - 更改 HTML 中的换行符

html - 为什么伪元素上的滤镜渐变在 IE8 中不起作用?

html - 如何添加内联样式宽度:calc(100%/var) in reactjs?

javascript - 只有 PDF 文件通过 jQuery 打开空白页

c# - DryIoc.WebApi 设置

c# - 在 IIS 下保持 WCF 服务事件

html - 在 IE6 和 IE7 的 IFrame 中加载 PDF

c# - 如何使用参数调用线程中的方法并返回一些值

java - 如何在java中打开和替换apache PDFBox lib中PDF流中的数据?