Home > Software engineering >  Set Bold to a Paragraph in GemBox Document ASP.Net c#
Set Bold to a Paragraph in GemBox Document ASP.Net c#

Time:05-22

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

CodePudding user response:

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 } });
  • Related