iText创建PDF文档时,某些HTML和CSS样式未应用

3

我在使用iText将HTML转换成PDF时遇到了问题。我正在传递来自Locize的“导入信息部分”,其中包含粗体标签和列表符号。

<li>\n                    
   <b>Toll-free</b> (within Canada and the USA): 
   <b>1.800.xxxxxxx</b>\n                
</li>\n  

PDF文件是在Confirmation.cs中生成的。从Confirmation.cs传递给ImportantInformation.cs的参数"medSrc"如下所示。但不知何故,粗体样式和列表符号都没有应用到最终的PDF文件中。

我已经调试了一段时间,仍然不知道出了什么问题。有谁知道我错过了什么吗?

提前感谢您!

Confirmation.cs

    ...
    namespace xxx.Pdf.xxx.xxx
    {
    public partial class Confirmation
    {
        private IApplication application;

        public Confirmation(IApplication application)
        {
            this.application = application;
        }

        public byte[] Create()
        {
            var memoryStream = new MemoryStream();
            var css = Content.Css;

            //Create Document
            PdfDocument pdfDocument = new PdfDocument(new PdfWriter(memoryStream));
            PageSize pageSize = PageSize.LETTER;
            Document document = new Document(pdfDocument, pageSize);

            Header headerHandler = new Header(document);
            PageXofY footerHandler = new PageXofY(pdfDocument);
            document.SetTopMargin(headerHandler.GetTableHeight());

            //Assign event-handlers
            pdfDocument.AddEventHandler(PdfDocumentEvent.START_PAGE, headerHandler);
            pdfDocument.AddEventHandler(PdfDocumentEvent.END_PAGE, footerHandler);

           ...
            // ======>Important Information <=======
            var importantInfo = new ImportantInformation();
            Table impInfoTable = importantInfo.CreateTable(
                HtmlUtility.MergeCssWithHtml(css, Content.ImportantInformationEmergencyMedical),
                HtmlUtility.MergeCssWithHtml(css, Content.ImportantInformationTripCancellation));

            document.Add(impInfoTable).Add(new Paragraph("\n"));

            //set column parameters
            float offset = 36;
            float columnWidth = (pageSize.GetWidth() - offset * 2 + 15) / 2;
            float columnHeight = pageSize.GetHeight() - offset * 2;
            float tableWidth = columnWidth - 10;

            //define column area
            Rectangle[] columns = new Rectangle[]
            {
                new Rectangle(offset , offset, columnWidth, columnHeight),
                new Rectangle(columnWidth + 30, offset, columnWidth, columnHeight)
            };
            document.SetRenderer(new ColumnDocumentRenderer(document, columns));

            document.Add(new AreaBreak(AreaBreakType.LAST_PAGE));

            foreach (Applicant applicant in application.Applicants)
            {
                ApplicantTable applicantTable = new ApplicantTable();
                Table table = applicantTable.CreateTable(applicant, tableWidth);
                //Table another = applicantTable.CreateTable(application.Applicants[1], tableWidth);

                document.Add(table).Add(new Paragraph("\n").SetFontSize(3));
            }

            footerHandler.WriteTotal(pdfDocument);

            document.Close();

            pdfDocument.Close();

            var pdf = memoryStream.ToArray();

            return pdf;
        }
    }
}

ImportantInformation.cs

...
namespace xxx.xxx.xxx.Pdf
{
    public class ImportantInformation
    {
    public Table CreateTable(string medSrc, string tciSrc)
    {
        float[] colWidth = new float[] { 50f, 50f };
        Table table = new Table(UnitValue.CreatePercentArray(colWidth));
        table.SetWidth(new UnitValue(UnitValue.PERCENT, 100));

        PdfFont avenir = FontFactory.CreateAvenirLightStandardMedium();

        Paragraph title = new Paragraph().Add("**IMPORTANT INFORMATION**").SetFontSize(12).SetFont(avenir).SetBold().SetTextAlignment(TextAlignment.CENTER);
        Cell titleCell = new Cell(1, 2).Add(title).SetBorder(Border.NO_BORDER);
        table.AddCell(titleCell);

        Cell medImportantinfo = new Cell()
            .SetBorder(Border.NO_BORDER)
            .Add(new Paragraph("Paragraph A")
            .SetFirstLineIndent(10f)
            .SetBold()
            .SetFontSize(9)
            .SetFont(avenir));
        Cell tciImportantInfo = new Cell()
            .SetBorder(Border.NO_BORDER)
            .Add(new Paragraph("Paragraph B")
            .SetFirstLineIndent(10f)
            .SetBold()
            .SetFontSize(9)
            .SetFont(avenir));

        IList<IElement> medInfo = HtmlToCellFormat.HtmlToElements(medSrc);

        IList<IElement> tciInfo = HtmlToCellFormat.HtmlToElements(tciSrc);

        foreach (IElement e in medInfo)
        {
            medImportantinfo.ElementToCell(e);
        }

        foreach (IElement e in tciInfo)
        {
            tciImportantInfo.ElementToCell(e);
        }

        table.AddCell(medImportantinfo).AddCell(tciImportantInfo);

        return table;
        }
    }
}

medSrc:

<html>
<body>\n    
    <ul style=\"text-align: justify; list-style-type: disc; font-family: avenir, Arial, Helvetica, sans-serif; font-size: 10px\">\n        
        <li>\n            In the event of a medical emergency contact:\n            
            <ul style=\"text-align: justify; list-style-type: circle; font-family: avenir, Arial, Helvetica, sans-serif; font-size: 10px\">\n                
                <li>\n                    
                    <b>Toll-free</b> (within Canada and the USA): 
                    <b>1.800xxxxxx</b>\n                
                </li>\n                
                <li>\n                    
                    <b>Collect</b> (from all other locations): 
                    <b>1.xxxxxx</b>\n                
                </li>\n            
            </ul>\n        
        </li>\n        
        ...
    </body>
</html>

更新: 以下是ElementToCell和HtmlToElements函数的定义。
public static class HtmlToCellFormat
{
    public static string baseUri = "/";

    public static void ElementToCell(this Cell cell, IElement e)
    {
        var type = e.GetType().ToString();
        if (e.GetType() == typeof(Image))
        {

            cell.Add((Image)e);
        }
        else if (e.GetType() == typeof(Paragraph) || (e.GetType() == typeof(List)))
        {
            cell.Add((IBlockElement)e);
        }
    }

    public static IList<IElement> HtmlToElements(string content)
    {
        ConverterProperties properties = new ConverterProperties();
        properties.SetBaseUri(baseUri);
        FontProvider fontProvider = new DefaultFontProvider(false, false, false);
        FontProgram fontProgram = FontProgramFactory.CreateFont();
        fontProvider.AddFont(fontProgram, "");
        properties.SetFontProvider(fontProvider);
        IList<IElement> elements = HtmlConverter.ConvertToElements(content, properties);
        return elements;
    }
}

变量medImportantinfotciImportantInfo的类型为Cell,但我在这些类中找不到ElementToCell()方法。虽然我不是C#开发人员,但我在iText 7 for .NET中也找不到该方法:https://github.com/itext/itext7-dotnet/blob/develop/itext/itext.layout/itext/layout/element/Cell.cs 那个方法是关于什么的?如果我在谷歌上搜索它,我只能找到与您的问题相关的有效参考:https://www.google.com/search?q=iText+ElementToCell+cell - Bruno Lowagie
很奇怪你会在 Cell 上使用 ElementToCell() 方法,而不是使用 Add() 方法。当然,为了让 Add() 方法正常工作,你可能需要将 IElement 强制转换为 IBlockElementImage 元素。 - Bruno Lowagie
@BrunoLowagie,我刚刚添加了你所询问的函数的定义。我们自己定义了ElementToCell函数,以便处理不同类型的数据。 - Julie C.
好的,谢谢你澄清。在Java中,我会使用类似于if (e instanceof IBlockElement)的语句,但我不知道C#中是否存在这样的语句。对我来说,比较string值看起来很奇怪,但由于我不了解C#,所以无法再帮助你了。 - Bruno Lowagie
@BrunoLowagie 没问题,谢谢你的时间。 - Julie C.
1个回答

1

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接