将BOLD设置为Gembox文档ASP.NET C#中的段落

发布于 2025-01-30 00:52:58 字数 345 浏览 2 评论 0原文

我在我的asp.net页面中使用gembox.document库。我有一个包含线路断裂的段落,我还需要将段落设置为粗体。 我的下面代码可变str包含换行字符。

尝试1 线路断裂在以下代码中运行良好 var p3 =新段落(WDOC,str); 如何对本段设置大胆

尝试2 大胆在以下代码中效果很好,

    var p3 = new Paragraph(wDoc, 
        new Run(wDoc, str) { CharacterFormat = { Bold = true } }
   );

这不允许划线,

请帮助解决方案

I am using GemBox.Document library in my ASP.Net Page. I have a paragraph which contains line breaks and I also need to set the paragraph to bold.
I my below code the variable str contains line break characters.

TRY 1
Line breaks work well in the below code
var p3 = new Paragraph(wDoc, str);
How to set BOLD to this paragraph

TRY 2
Bold work well in the below code

    var p3 = new Paragraph(wDoc, 
        new Run(wDoc, str) { CharacterFormat = { Bold = true } }
   );

This doesn't allow line breaks

Please help for a solution

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

掩耳倾听 2025-02-06 00:52:58

可能是这样最简单的方法是这样的:

var paragraph = new Paragraph(wDoc);
paragraph.Content.LoadText(str, new CharacterFormat() { Bold = true });

或这样:

var paragraph = new Paragraph(wDoc);
paragraph.CharacterFormatForParagraphMark.Bold = true;
paragraph.Content.LoadText(str);

但是,以防万一您有兴趣,这里要注意的是,line breaks用special> specialcharacter对象表示,而不是运行对象。

因此,以下是您需要自己处理这些休息时间的“手动”方式,您需要将正确的元素添加到段段

string str = "Sample 1\nSample 2\nSample 3";
string[] strLines = str.Split('\n');

var paragraph = new Paragraph(wDoc);

for (int i = 0; i < strLines.Length; i++)
{
    paragraph.Inlines.Add(
        new Run(wDoc, strLines[i]) { CharacterFormat = { Bold = true } });

    if (i != strLines.Length - 1)
        paragraph.Inlines.Add(
            new SpecialCharacter(wDoc, SpecialCharacterType.LineBreak));
}

。正在使用此构造函数:

var paragraph = new Paragraph(wDoc,
    new Run(wDoc, "Sample 1") { CharacterFormat = { Bold = true } },
    new SpecialCharacter(wDoc, SpecialCharacterType.LineBreak),
    new Run(wDoc, "Sample 2") { CharacterFormat = { Bold = true } },
    new SpecialCharacter(wDoc, SpecialCharacterType.LineBreak),
    new Run(wDoc, "Sample 3") { CharacterFormat = { Bold = true } });

Probably the easiest way to do this is something like this:

var paragraph = new Paragraph(wDoc);
paragraph.Content.LoadText(str, new CharacterFormat() { Bold = true });

Or this:

var paragraph = new Paragraph(wDoc);
paragraph.CharacterFormatForParagraphMark.Bold = true;
paragraph.Content.LoadText(str);

But just in case you're interested, the thing to note here is that line breaks are represented with SpecialCharacter objects, not with Run objects.

So the following would be the "manual" way in which you would need to handle those breaks yourself, you would need to add the correct elements to the Paragraph.Inlines collection:

string str = "Sample 1\nSample 2\nSample 3";
string[] strLines = str.Split('\n');

var paragraph = new Paragraph(wDoc);

for (int i = 0; i < strLines.Length; i++)
{
    paragraph.Inlines.Add(
        new Run(wDoc, strLines[i]) { CharacterFormat = { Bold = true } });

    if (i != strLines.Length - 1)
        paragraph.Inlines.Add(
            new SpecialCharacter(wDoc, SpecialCharacterType.LineBreak));
}

That is the same as if you were using this Paragraph constructor:

var paragraph = new Paragraph(wDoc,
    new Run(wDoc, "Sample 1") { CharacterFormat = { Bold = true } },
    new SpecialCharacter(wDoc, SpecialCharacterType.LineBreak),
    new Run(wDoc, "Sample 2") { CharacterFormat = { Bold = true } },
    new SpecialCharacter(wDoc, SpecialCharacterType.LineBreak),
    new Run(wDoc, "Sample 3") { CharacterFormat = { Bold = true } });
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文