MigraDoc 中带有上标的文本
我有一个通过 MigraDoc 编写 PDF 文档的课程。
函数进行如下调用:
var p = row.Cells[1].AddParagraph();
p.AddLineBreak();
p.AddLineBreak();
_renderHtml(office["Address"], p.AddFormattedText());
_renderHtml 代码如下所示:
private void _renderHtml(string html, FormattedText text)
{
_renderHtmlElement(
new Html.Parsable("dom", html),
text
);
}
然后,_renderHtmlElement 代码对实际要处理的 HTML 进行一系列检查。我想我应该把它扔进开关盒里,但这并没有真正影响我的问题。所以它看起来像这样:
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 = text.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);
}
}
我的工作是让上标代码正常工作。我现在的代码将添加正确的内容,格式为上标,但它后面仍然有原始内容(不是上标)。 text
的方法似乎只允许添加函数,没有 replace
或 substring
或类似的东西让我撕掉第二个实例。
我在这里忽略了一些明显的事情吗?正如您从粗体/斜体示例中看到的,这是一个相当直接的过程,所以我认为我只是没有正确传递 text.superscript 。
任何和所有的帮助将不胜感激。
干杯
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 = text.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
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您添加上标文本,然后为子级调用 _renderHtmlElement - 也许子级会给您相同的文本,但没有上标属性。
之间还有其他标签吗?和在你的 HTML 中?
MigraDoc 具有删除方法,因此您可以删除项目 - 但最好不要首先添加它们。
我会在“ft.Superscript = true;”上放置一个断点并检查 _renderHtmlElement 对“sup”元素的子元素做了什么。
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.