ParagraphFormat.StyleName not retained for docbuilder.InsertHtml


#1

I'm trying to develop a proof-of-concept application using an evaluation version of Aspose.Word and so far the component does almost everything I would need. However, I've come across an issue when using the InsertHtml method and the ParagraphFormat.StyleName property.

Here's a sample code snippet showing the problem:

--------------------------
string dummyHTML = "

Here's some text.

What happened to ContentStyle.

";

Document doc = new Document(@"e:\test\template.doc");
DocumentBuilder builder = new DocumentBuilder(doc);

builder.ParagraphFormat.StyleName = "HeadlineStyle";
builder.Writeln("My Headline");

builder.ParagraphFormat.StyleName = "ContentStyle";
builder.Writeln("Got my ContentStyle");
builder.InsertHtml(dummyHTML);
builder.Writeln("We're back to Normal style?");
--------------------------

The InsertHtml routine will set the content style to "Normal" instead of "ContentStyle". Is there a way around this issue? Thanks for any insight you can provide.

-Anthony

#2

Sorry for the delay, I will reply asap.


#3

I’m afraid its by design and resets text and paragraph formatting to the way it will look if you load this HTML into the browser or import into MS Word.


#4

Hmm… ok. What about a programmatic way for me to select the newly inserted text and change the style? For example, something like:

// Write out the text we want
builder.StartBookmark(“HtmlText”);
builder.InsertHtml(MyHhtmlText);
builder.EndBookmark(“HtmlText”);
builder.MoveToBookmark(“HtmlText”);

// Somehow select the text within the bookmark & set the style




// Move to end of bookmark & continue
builder.MoveToBookmark(“HtmlText”, false, true);




#5

Good idea, but at the moment you can only change style of the current (and inserted further) paragraph. Cannot select a bookmark and set style. This would be easy if we had a richer object model with Range etc, similar to MS Word. We will get there, although it will take time.

So you want to import some HTML text and make it look according to some style you have in the document, is that right?

Since all HTML imported text gets assigned Normal style, what you can do is change Normal style in your document to look the way you want imported text to look and assign other styles to all other text in the document.

One other idea comes to mind. I might be able to add an option that specifies what style to assign to all HTML imported text. I don’t promise we’ll do it at this stage, but we will think about it more.



#6

Thanks for the prompt response. I had also thought about the latter idea of having an overloaded InsertHTML routine. The reason is that I can forsee requiring different styles within the same document.

One more interesting quirk… I found that if I strip all

tags and replace them with
tags, the style I had set is retained. So it appears that the “Normal” style is only applied once a

tag is encountered. Maybe this info helps in simplifying a code change that enables you to retain the last set stylename.

-Anthony


#7

Found a workaround!! Woohoo!!! Here’s some example code to retain the style I want. Hope this helps anyone else that may run into the same issue. :slight_smile:

-Anthony

================================

string f1 = null;
string f2 = null;

DataRow dr = null;
DataRow [] rows = myDataTable.Select();

for (int x = 0; x < rows.Length; x++) {
dr = rows[x];

// Reformat HTML snippets to remove

tags
// NOTE:

tags in the string cause the style to be set to “Normal”, that’s why
// we strip and replace them with 2
tags.
f1 = (string) dr[“field1”];
f1 = f1.replace("

", “”); f1.replace("

", “

”);

f2 = (string) dr[“field2”];
f2 = f2.replace("

", “”); f2.replace("

", “

”);

// NOTE: We need to call the write statement with an empty string so that
// the style initally gets set.
builder.ParagraphFormat.StyleName = “Style1”;
builder.Write("");
builder.InsertHtml(f1);
builder.InsertParagraph();

builder.ParagraphFormat.StyleName = “Style2”;
builder.Write("");
builder.InsertHtml(f2);
builder.InsertParagraph();
}