Inserting a text to a paragraph does not retain its style anymore in version 13.11.0

Hi,

Please see the attached Word document: it has one paragraph with a bookmark in it. In the code snapshot below, I use this document and insert some text before the bookmark. When using Aspose.Words 13.9.0 (and all releases before), the style of the paragraph remains unchanged after the text is inserted. In version 13.11.0, the paragraph style changes to Normal.

Code example:

Document doc = new Document("c:/insertTextRetainStyle.docx");
DocumentBuilder builder = new DocumentBuilder(doc); 
builder.moveToBookmark( "bookmark", true, false );
builder.insertHtml("Some text before"); OoxmlSaveOptions saveOptions = new
OoxmlSaveOptions( SaveFormat.DOCX ); 
saveOptions.setCompliance(OoxmlCompliance.ISO_29500_2008_TRANSITIONAL );
doc.save("c:/output.docx", saveOptions); 

Please let me know if this can be fixed.
Thanks,

Natasa

Hi Natasa,

Thanks for your inquiry.

While using the latest version of Aspose.Words i.e. 13.11.0, I managed to reproduce this issue on my side. I have logged this issue in our bug tracking system. The Id of this issue is WORDSNET-9421. Your request has also been linked to this issue and you will be notified as soon as it is resolved. Sorry for the inconvenience.

Best regards,

The issues you have found earlier (filed as WORDSNET-9421) have been fixed in this .NET update and this Java update.

This message was posted using Notification2Forum from Downloads module by aspose.notifier.

Hi,

This issue has not been resolved to match the behavior from release 13.2.0 or earlier. Neither this resolution matches the behavior I get when I manually insert a portion of a paragraph into an existing paragraph in Word. In both these cases, inserted text automatically accepts the styling and formatting of the original paragraph.

However, in 14.8.0 release, the inserted text is formatted as Normal (Web) style (or something else, as the Normal (Web) is paragraph based and it should not be allowed to be assigned to paragraph portions), while the remainder of the paragraph has its original formatting/styling.

Please see the attached document: output-14.8.0.docx - that’s the output I am getting when running the code snippet from the post above. If you click at any portion of inserted text and try to get its style, you may notice that it points to Heading 2 style, which is the style of the original paragraph. This is not OK behavior either.

Please let me know if this issue can be reopened and fixed. Also, please let me know if you have any questions or need any additional information.

Thanks,

Natasa

Hi Natasa,

Thanks for your inquiry. In version Aspose.Words (14.6.0), we have introduced a new overload of DocumentBuilder.InsertHtml method which allows you to choose what formatting will be used as a base for inserted HTML fragments.

The new overload has an argument useBuilderFormatting which when is false, formatting specified in DocumentBuilder is ignored, and formatting of inserted text is based on default HTML formatting. In this case, inserted text looks as in browsers.

When useBuilderFormatting is true, formatting of inserted text is based on formatting specified in DocumentBuilder. Note that useBuilderFormatting chooses only base formatting of inserted text, and do not affect formatting directly specified in the HTML fragment.

The following example illustrates the difference between the two modes:

DocumentBuilder builder = new DocumentBuilder();
builder.ParagraphFormat.LeftIndent = 72;
builder.Font.Name = "Arial";
builder.Font.Size = 24;
bool useBuilderFormatting = ...
builder.InsertHtml("<b>Text</b>", useBuilderFormatting);

In this example, if useBuilderFormatting is false, the inserted paragraph will have no left indent and will use the ‘Times New Roman’ 12pt font, which is the default HTML font and indent. If useBuilderFormatting is true, the inserted paragraph will be indented by 1 inch (72 points) and will use the ‘Arial’ 24pt font, as specified in DocumentBuilder. However, in both cases the inserted text will be bold and red, as specified in the HTML fragment.

So, please try using this method as follows:

builder.InsertHtml("Some text before ", true);

Best regards,

Hi Awais,

Thank you very much for your response. I have tried this out and now it works as we need it to.
I appreciate your help.

Natasa

Hi Natasa,

Thanks for your feedback. It is great you were able to find what you were looking for. Please let us know any time you have any further queries.

Best regards,