We're sorry Aspose doesn't work properply without JavaScript enabled.

Free Support Forum - aspose.com

Set Character Style to Text Using DocumentBuilder

Hi

I am using Aspose Words 15.9, Visual Studio 2013, and .NET 4.5.

I am using DocumentBuilder to create a document, and part of the requirement is to include hyperlinks to web sites. This is pretty easy to achieve, using the DocumentBuilder function InsertHyperlink.
The trouble is, styling the links. Simply using InsertHyperlink adds the link OK, but it appears no different from the surrounding text. I have been trying to apply the inbuilt “Hyperlink” style to the text using the following code:

builder.Font.StyleName=“Hyperlink”;
builder.InsertHyperlink(“Display Text”, “[link]”, false);

That didn’t work. Neither did using:

builder.Font.Style.Name = “Hyperlink”;

for the first line. I even tried locating the Hyperlink style using the Document.Styles collection, and setting the style that way. I could find the style, but applying it had no effect.

The problem appears to be that the style is applied to the text, but it doesn’t affect the appearance - even though it should. If you open the generated document in MS Word and move the cursor to the hyperlink, the selected style appears as “Hyperlink”, even though the text appears the same as surrounding text. If I “re-apply” the style within MS Word, it suddenly appears correctly.

In the end, the only workaround I could find was to locate the style, and then apply the appropriate settings (bold, italic, underline, color) manually:

builder.Font.Bold = style.Font.Bold;
builder.Font.Italic = style.Font.Italic;
builder.Font.Underline = style.Font.Underline;
builder.Font.Color = style.Font.Color;

While this gets the job done, it is not ideal. Could you tell me what I’m doing wrong with the application of the style to the link?

Thanks
Chris.

Hi Chris,


Thanks for your inquiry. Please use following code example to achieve your requirements. Hope this helps you. Please let us know if you have any more queries.

Document doc = new Document();<o:p></o:p>

DocumentBuilder builder = new DocumentBuilder(doc);

builder.Write("Please make sure to visit ");

builder.Font.StyleIdentifier = StyleIdentifier.Hyperlink;

// Insert the link.

builder.InsertHyperlink("Aspose Website", "http://www.aspose.com", false);

// Revert to default formatting.

builder.Font.ClearFormatting();

builder.Write(" for more information.");

doc.Save(MyDir + "DocumentBuilder.InsertHyperlink Out.docx");

Hi Tahir


Thanks for your response. While the code you supplied worked, when I tried to incorporate it into my larger program, it did not. I have no idea why. But given that the code I’m using is quite complicated - it generates reports in Word and PDF format from XML generated by users - I suspect the problem lies elsewhere.
Since my temporary workaround works fine, I think I will leave that in place for the time being.

Thanks again
Chris.

A post was split to a new topic: Paragraph Format and direct formatting