How do you place HTML in a table cell while creating a new PDF with iText7?
我正在创建一个带有大表的PDF。在此表中,有一个可以用HTML填充的单元格。如果是这样,则HTML必须解释为HTML,并且不能显示为常规文本。但是,当我这样做时,布局/样式变得混乱,并且某些图片没有显示。 (在我给出的示例中,项目符号破折号由9代替。)
我使用C#编程,并且正在使用iText7创建PDF。
在我的项目中,我想显示以下HTML代码。 HTML代码看起来像这样的原因是因为它已将RTF转换为HTML:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 | <!DOCTYPE html> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> <meta name="generator" content="SautinSoft.RtfToHtml.dll"> Untitled document <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> <p style="margin:0pt 0pt 0pt 0pt;"><span class="st1"></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"></span></p> <p style="margin:0pt 0pt 0pt 0pt;"><span class="st1"></span></p> </body> </html> |
当我将此文件另存为HTML文件并在浏览器中打开时,您会得到以下结果(正确):正确结果(网站)
但是,当我将其添加到PDF单元格时,会得到以下结果:不良结果(PDF)
如您所见,用于项目符号的破折号已变成9。
我用于将HTML代码添加到PDF文件中的代码如下:
1 2 3 4 5 6 7 8 9 10 11 | 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的标准Symbol字体与浏览器使用的字体不同。
第三,iText无法正确处理
您如何做才能改善生成的pdf?不要使用标准PDF的Symbol字体-而是使用您自己的Symbol字体。
如何做?
让我为您介绍
FontProvider is responsible to handle the fonts which may be used
while processing an html file. Its instance could be passed as a
parameter ofConverterProperties , otherwise it'd be created by
iText. By default iText adds toFontProvider all standard pdf fonts
(FontProvider#addStandardPdfFonts ) and some free sans fonts
(DefaultFontProvider#SHIPPED_FONT_NAMES ).
您要使用自己的Symbol字体:这意味着您应该防止iText在转换过程中考虑标准Symbol字体。为此,请创建一个
(!)在99%的情况下,您希望在转换过程中考虑其他一些标准字体。因此,请按如下所示手动添加它们:
1 2 | provider.addFont(StandardFonts.TIMES_ROMAN); // some other fonts to be added |
现在将您自己的
1 | provider.addFont("C:\\\\Windows\\\\Fonts\\\\symbol.ttf", PdfEncodings.IDENTITY_H); |
在此使用
所有这些更改之后,我设法获得了以下文件:
现在出现了第二个问题:默认情况下,iText无法正确处理" \\\\\\\\\\ 9 "。关于这一点,我不知道任何直接的解决方案。最好将其从html中删除是最好的。