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

Free Support Forum - aspose.com

Newlines when using InsertHtml()

Hello,


We’re using Aspose Words v11.11 to insert a mix of text and html into a docx-file. When inserting plaintext, everything works as intended. When inserting HTML, a newline is inserted after (sometimes also before). We replace the value of a bookmark in the docx-file using the following code:

“bookmark” is of type Aspose.Bookmark
"document" is of type Aspose.Document
"builder" is of type “DocumentBuilder”

// Get bookmark from document
var docBookmark = document.Range.Bookmarks[bookmark.Name];
// Move to bookmark location
builder.MoveTo(docBookmark.BookmarkStart);
// Recreate bookmark with text
builder.StartBookmark(docBookmark.Name);
// Insert HTML
builder.InsertHtml(bookmark.Value);
builder.EndBookmark(name);
docBookmark.Remove();

We use the same code for inserting plaintext, using builder.Write() instead of .InsertHtml(), and it works fine.

Does someone know why this happens? I’ve spent a day trying different solutions, but have been unsuccessful.

Best regards,
Christian

Hi Christian,


Thanks for your inquiry. I would suggest you please upgrade to the latest version of Aspose.Words (14.1.0) and see how it goes on your end. I hope this helps. In case the problem still remains, please attach the following resources here for testing:

  • Your input Word document you’re getting this problem with.
  • The Aspose.Words generated output document which shows the undesired behavior.
  • The HTML snippet you’re inserting in document using InsertHtml method.
  • Please create a standalone (runnable) simple console application that helps us reproduce your problem on our end and attach it here for testing.

As soon as you get these pieces of information ready, we’ll start investigation into your issue and provide you more information.

Best regards,

Thank you for the reply. I’ve attached a simple console application which inserts a bullet list in a docx-file, as well as the template (HtmlTmeplate.docx) and a document modified by the console application (ModifiedDocument.aspx).


In ModifiedDocument.aspx, a newline is inserted between the bullet list and the “Text after bookmark.”-line. This is the problem I’m looking for a solution to.

The console application takes no input parameters and creates a new document in the \Bin folder based on the file “HtmlTemplate.docx”, which is also located in the \Bin folder. The application replaces the value of the bookmark “htmlBookmark” with a bullet list containing two items (“Coffee” and “Milk”) and saves the document as a new docx file named “ModifiedDocument.docx”.

<o:p></o:p>

Hi Christian,


Thanks for the additional information. Please use the following code to fix this issue:

private static Document InsertBookmark(string
bookmark, string value, Document document)

{

var builder = new DocumentBuilder(document);

var docBookmark = document.Range.Bookmarks[bookmark];

docBookmark.Text = "";

if (builder.MoveToBookmark(bookmark, true, true))

{

builder.InsertHtml(value);

BookmarkEnd bmEnd = docBookmark.BookmarkEnd;

Node parentNode = bmEnd.ParentNode;

if (bmEnd.ParentNode.ChildNodes.Count == 1)

{

((Paragraph)parentNode.PreviousSibling).AppendChild(bmEnd);

parentNode.Remove();

}

}

return document;

}


I hope, this helps.

Best regards,