开发者

Text With Superscript In MigraDoc

I have a class for writing a PDF doc via MigraDoc.

A function makes a call like this:

var p = row.Cells[1].AddParagraph();
p.AddLineBreak();
p.AddLineBreak();
_renderHtml(office["Address"], p.AddFormattedText());

The _renderHtml code looks like this:

private void _renderHtml(string html, FormattedText text)
{
      _renderHtmlElement(
         new Html.Parsable("dom", html),
         text
      );
}

The _renderHtmlElement code then does a series of checks for what HTML to actually handle. I'm thinking I should throw it into a switch case, but that doesn't really affect my question. So it looks like this:

private void _renderHtmlElement(Html.Element element, FormattedText text)
{
    if ("p" == element.Type)
    {
        //do stuff
    }
    else if ("li" == element.Type)
    {
        //do stuff
    }
    else if ("b" == element.Type || "strong" == element.Type)
    {
        text = text.AddFormattedText(TextFormat.Bold);
    }
    else if ("i" == element.Type || "em" == element.Type)
    {
        text = tex开发者_如何学编程t.AddFormattedText(TextFormat.Italic);
    }
    else if ("br" == element.Type || "em" == element.Type)
    {
        text.AddLineBreak();
    }
    else if ("text" == element.Type)
    {
        //do stuff
    }
    else if("sup" == element.Type)
    {
        FormattedText ft = text.AddFormattedText(element.ContentDecoded);
        ft.Superscript = true;
    }

    foreach (var child in element.ChildElements)
    {
        _renderHtmlElement(child, text);
    }
}

My piece is to get the superscript code working. The code I have in there now will add in the correct content, formatted as a superscript, but it then still has the original content (not superscripted) immediately following it. The methods of text seem to only allow add functions, there's no replace or substring or anything similar for me to just tear out the second instance.

Am I overlooking something obvious here? As you can see from the bold/italic examples, it's a fairly straight forward process, so I would think I'm just not passing in the text.superscript properly.

Any and all help would be greatly appreciated.

Cheers


You add the Superscript text, then you call _renderHtmlElement for the children - maybe a child gives you the same text, but without superscript attribute. Are there other tags between <sup> and </sup> in your HTML?

MigraDoc has Remove methods so you can remove items - but better not to add them in the first place.

I'd put a breakpoint on "ft.Superscript = true;" and check what _renderHtmlElement does for the children of the "sup" element.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜