ITextSharp无法正确转换有序列表

时间:2019-06-19 11:37:07

标签: c# list itext pdf-generation html-lists

我正在使用ITextSharp制作pdf文件,问题是当我尝试使ITextSharp将html文本转换为pdf时出现错误。

我想知道是否可以按原样打印ol列表,如果可以,我应该怎么做!

我正在使用HTMLWorker将html字符串解析为列表, 然后我遍历列表,然后将文件写入文件中。

这是我正在使用的代码:

    using (StringReader sr = new StringReader(txtConvertido))
{
    string txtreplace = txtConvertido;
    List<IElement> elements = HTMLWorker.ParseToList(sr, styles);
    foreach (IElement e in elements)
    {
        if (e is List)
        {
            List newlist = null;
            if (((List)e).Symbol.Content == "-")
            {
                newlist = new List(List.ORDERED);
                newlist.SetListSymbol("\u2022");
            }
            else if (((List)e).Symbol.Content == "• ")
            {
                newlist = new List(List.UNORDERED);
                newlist.SetListSymbol("•");
            }

            foreach (ListItem item in ((List)e).Items)
            {
                Chunk newchk = new Chunk(item.Chunks[0]);

                float fontsize = item.Chunks[0].Font.Size;
                if (fontsize == 1)
                    fontsize = font_Texto.Size;

                newchk.Font = new Font(font_Texto.BaseFont, fontsize, item.Chunks[0].Font.Style, item.Chunks[0].Font.Color);
                ListItem newitem = new ListItem() { Font = newchk.Font };
                newitem.Add(newchk);
                newlist.Add(newitem);
            }

            _p.Add(newlist);
        }
        else if (e is Paragraph)
        {
            foreach (Chunk chk in e.Chunks)
            {
                float fontsize = chk.Font.Size;
                if (fontsize == 1)
                    fontsize = font_Texto.Size;

                chk.Font = new Font(font_Texto.BaseFont, fontsize, chk.Font.Style, chk.Font.Color);
            }

            if (e is Paragraph)
            {
                if (((Paragraph)e).Alignment == -1)
                    ((Paragraph)e).Alignment = alinhamento;

                ((Paragraph)e).SetLeading(0, multipliedLeading);
            }
            _p.Add(e);
        }
    }
}

未正确转换的html文件部分是标记«ol start =“ x”»:

所以,而不是打印:

1. ASDASD
2. gadsafasa
3. dsfa ggga
4. asdfa sdf

...

它打印为:

1. ASDASD
1. gadsafasa
1. dsfa ggga
1. asdfa sdf

...

$这是html:

<ol>
    <li><strong>A «Nome Empresa»</strong></li>
</ol>
<br />
---[Descrever a atividade da empresa, o seu histórico, entre outras informações relevantes.]---<br />
<br />
&nbsp;

<ol start="2">
    <li><strong>&nbsp;O que é o código de ética e de conduta</strong></li>
</ol>
<br />
O Código de Ética e de Conduta
<br />
&nbsp;

<ol start="3">
    <li><strong>Aplicação</strong></li>
</ol>
<br />
O Código de Ética e de Conduta
<br />
&nbsp;

<ol start="4">
    <li><strong>Objetivos</strong></li>
</ol>
<br />
O Código de Ética e Conduta 
<br />
&nbsp;
<ol start="5">
    <li><strong>Acompanhamento</strong></li>
</ol>
<br />
Todos os colaboradores, clientes, parceiros e fornecedores
&nbsp;

'

0 个答案:

没有答案