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

Free Support Forum - aspose.com

Setting text at bookmark: sometimes font formatting gets lost

Dear Aspose team!

I got the following problem:

When changing the text of a bookmark by calling

public static void InjectText(Bookmark bookmark, string text)
{
if (text != null)
{
text = text.Trim();
}
else
{
text = string.Empty;
}

bookmark.Text = text;
}

sometimes the font formattings (bold, italic etc.) get lost.

By debugging, I saw that the font formattings are set correctly until the line

bookmark.Text = text;

is reached. When the text was set, in some cases the formattings got lost (and in other cases, they did not).

An interesting point is that the BookmarkStart's next sibling is a SmartTag. This SmartTag has the bookmark's text Run as child node. By the insertion, the SmartTag gets lost so that the text Run is the new BookmarkStart's next sibling.

Do you know what my problem could be?

Thanks in advance for your great support!

Please find attached a sample template:

Bookmark C2896Txt000000000000017 looses its font formatting while bookmark C6092Txt000000000000290 preserves it.

Thank you!

Cheers,

Stephan

Hi

<?xml:namespace prefix = o ns = "urn:schemas-microsoft-com:office:office" />

Thank you for reporting this problem to us. I managed to reproduce in on my side. You will be notified as soon as it is resolved.

Best regards.

Hi Alexey!

One of our customers ran into this problem again (older Aspose.Words version).

Could you please tell me the state of this issue?

If it isn't resolved yet, do you know any generic workarounds (which mind all possible node types within the bookmark like Runs, SmartTags etc.).

Thank you very much for your help,

cheers,

Stephan

Hi Stephan,

<?xml:namespace prefix = o ns = "urn:schemas-microsoft-com:office:office" />

Thanks for your request. Unfortunately, this issue is still unresolved. We will let you know one it is fixed. My apologizes for inconvenience.

Best regards.

Hi Alexey!

Thanks for the quick reply.

Could it be a suitable workaround to get the Font object before inserting text and then set it back after the insertion?

I haven't tried this yet - could it cause any further problems?

Thanks again,

cheers,

Stephan

Hi<?xml:namespace prefix = o ns = "urn:schemas-microsoft-com:office:office" />

Thanks for your inquiry. Please try using the following code:

Document doc = new Document("in.docx");

doc.RemoveSmartTags();

Bookmark bk1 = doc.Range.Bookmarks["C2896Txt000000000000017"];

Bookmark bk2 = doc.Range.Bookmarks["C6092Txt000000000000290"];

bk1.Text = "Test1";

bk2.Text = "Test2";

doc.Save("out.docx");

Best regards,

Hi Andrey!

Thank you very much - I'll try this!

Cheers,


Stephan

The issues you have found earlier (filed as WORDSNET-2941) have been fixed in this Aspose.Words for .NET 20.4 update and this Aspose.Words for Java 20.4 update.