Inserting text at bookmark

I’m trying to create a new document, by placing some additional text into the document, but the formatting is getting messed up.
After the line that is inserted into the document at DB.Write(line 12), there are a couple of extra line breaks that don’t belong.

WorkDocClone.Range.Bookmarks["Body"].Text = string.Empty;
DocumentBuilder DB = new DocumentBuilder(WorkDocClone);
DB.MoveToBookmark("Body", true, true);
DB.InsertHtml(_ESignHTML);
if (_ESignCCs != null)
    WorkDocClone.Range.Bookmarks["CC"].Text = _ESignCCs;
if (_ESignData.Signature != null)
{
    DB.MoveToBookmark("InsertESign", true, true);
    //DB.InsertHtml(_ESignData.GetSignatureLine(true));
    //DB.MoveToBookmark("InsertEsign");
    DB.Write("Leah Behl, MD");
}

The original document is attached.

Please advise. Thank you.

Hi
Thanks for your iniquity. These extra line breaks that you were talking about are placed inside “Body” bookmark and they are removed when you set doc.Range.Bookmarks[“Body”].Text = string.Empty;
Best regards.

I have that in my code. It seems that what’s actually happening is, the space before the paragraph is being changed to “Auto” when I insert the tet this way. Is there any better way to insert the text?

Hi
Thanks for your inquiry. Most probably this occurs because your HTML contains
or
tags. Try using
instead these tags. For example see the following code:

string _ESignHTML = "<b>test</b.test";
Document doc = new Document(@"Test224\in.doc");
doc.Range.Bookmarks["Body"].Text = string.Empty;
DocumentBuilder builder = new DocumentBuilder(doc);
builder.MoveToBookmark("Body", true, true);
builder.InsertHtml(_ESignHTML);

Also please show me your HTML.
Hope this helps.

I am getting the HTML, by pulling the original contents of the bookmark using the GetHTMLFromBookmark function which I found on the forums(see code below).
This returns a HTML string(see below).
What I need to do is - 1 - pull the text from a bookmark. 2 - allow user on the web to edit this text(this is done on a webpage, using FreeTextBox, and works nicely). 3 - insert the updated HTML back into the document.
How would I go about doing this, so that the formatting of the final document looks ok, without getting messed up?
HTML String returned:

String BodyHTML = @"<html><head><meta http-equiv=""Content-Type"" content=""text/html; charset=utf-8"" /><meta name=""generator"" content=""Aspose.Words for .NET 5.1.0.0"" /><title></title></head><body><div class=""Section1""><p style=""margin-left:0pt; margin-right:0pt; margin-top:0pt; margin-bottom:0pt; ""><span style=""font-family:'Times New Roman'; font-size:12pt; "">&#xa0;</span></p><p style=""margin-left:252pt; margin-right:0pt; margin-top:0pt; margin-bottom:0pt; ""><span style=""font-family:'Times New Roman'; font-size:12pt; "">May 19, 2008</span></p><p style=""margin-left:0pt; margin-right:0pt; margin-top:0pt; margin-bottom:0pt; ""><span style=""font-family:'Times New Roman'; font-size:12pt; "">&#xa0;</span></p><p style=""margin-left:0pt; margin-right:0pt; margin-top:0pt; margin-bottom:0pt; ""><a name=""Addressee""></a><span style=""font-family:'Times New Roman'; font-size:12pt; "">Dr. Jay Vida </span></p><p style=""margin-left:0pt; margin-right:0pt; margin-top:0pt; margin-bottom:0pt; ""><span style=""font-family:'Times New Roman'; font-size:12pt; "">Lakewood , NJ  08701</span></p><p style=""margin-left:216pt; margin-right:0pt; margin-top:0pt; margin-bottom:0pt; text-indent:36pt; ""><span style=""font-family:'Times New Roman'; font-size:12pt; "">Re:</span><span style=""font-family:'Times New Roman'; font-size:12pt; "">              </span><span style=""font-family:'Times New Roman'; font-size:12pt; "">Last</span><span style=""font-family:'Times New Roman'; font-size:12pt; "">, </span><span style=""font-family:'Times New Roman'; font-size:12pt; "">First</span></p><p style=""margin-left:216pt; margin-right:0pt; margin-top:0pt; margin-bottom:0pt; text-indent:36pt; ""><span style=""font-family:'Times New Roman'; font-size:12pt; "">&#xa0;</span></p><p style=""margin-left:0pt; margin-right:0pt; margin-top:0pt; margin-bottom:0pt; ""><span style=""font-family:'Times New Roman'; font-size:12pt; "">Dear </span><a name=""Start""></a></p><p style=""margin-left:0pt; margin-right:0pt; margin-top:0pt; margin-bottom:0pt; ""><span style=""font-family:'Times New Roman'; font-size:12pt; "">&#xa0;</span></p><p style=""margin-left:0pt; margin-right:0pt; margin-top:0pt; margin-bottom:0pt; ""><span style=""font-family:'Times New Roman'; font-size:12pt; "">&#xa0;</span></p><p style=""margin-left:0pt; margin-right:0pt; margin-top:0pt; margin-bottom:0pt; ""><span style=""font-family:'Times New Roman'; font-size:12pt; "">              </span><span style=""font-family:'Times New Roman'; font-size:12pt; "">Many thanks for this kind referral.</span></p><p style=""margin-left:0pt; margin-right:0pt; margin-top:0pt; margin-bottom:0pt; ""><span style=""font-family:'Times New Roman'; font-size:12pt; "">&#xa0;</span></p><p style=""margin-left:0pt; margin-right:0pt; margin-top:0pt; margin-bottom:0pt; ""><span style=""font-family:'Times New Roman'; font-size:12pt; "">              </span><span style=""font-family:'Times New Roman'; font-size:12pt; "">              </span><span style=""font-family:'Times New Roman'; font-size:12pt; "">              </span><span style=""font-family:'Times New Roman'; font-size:12pt; "">              </span><span style=""font-family:'Times New Roman'; font-size:12pt; "">              </span><span style=""font-family:'Times New Roman'; font-size:12pt; "">              </span><span style=""font-family:'Times New Roman'; font-size:12pt; "">              </span><span style=""font-family:'Times New Roman'; font-size:12pt; "">Sincerely,</span></p><p style=""margin-left:0pt; margin-right:0pt; margin-top:0pt; margin-bottom:0pt; ""><span style=""font-family:'Times New Roman'; font-size:12pt; "">&#xa0;</span></p><p style=""margin-left:0pt; margin-right:0pt; margin-top:0pt; margin-bottom:0pt; ""><span style=""font-family:'Times New Roman'; font-size:12pt; "">&#xa0;</span></p><p style=""margin-left:0pt; margin-right:0pt; margin-top:0pt; margin-bottom:0pt; ""><span style=""font-family:'Times New Roman'; font-size:12pt; "">&#xa0;</span></p></div></body></html>";
private static string GetHtmlFromBookmark(string bookmarkName, Document doc, SaveFormat ThisSaveFormat)
{
    Document docClone = doc.Clone();
    Document doc1 = new Document();
    Bookmark mark = docClone.Range.Bookmarks[bookmarkName];
    Node node = mark.BookmarkStart.ParentNode;
    Node endNode = mark.BookmarkEnd.ParentNode.NextSibling;
    while (!node.Equals(endNode))
    {
        if ((node as CompositeNode).ChildNodes.Contains(mark.BookmarkStart))
        {
            Node child = (node as CompositeNode).FirstChild;
            Node endChild = mark.BookmarkStart.NextSibling;
            while (!child.Equals(endChild))
            {
                child = child.NextSibling;
                child.PreviousSibling.Remove();
            }
        }
        if ((node as CompositeNode).ChildNodes.Contains(mark.BookmarkEnd))
        {
            Node child = mark.BookmarkEnd;
            while (!child.Equals(child.ParentNode.LastChild))
            {
                child = child.NextSibling;
                child.PreviousSibling.Remove();
            }
            child.Remove();
        }
        doc1.FirstSection.Body.AppendChild(doc1.ImportNode(node, true, ImportFormatMode.KeepSourceFormatting));
        node = node.NextSibling;
        if (node == null)
            break;
    }
    MemoryStream stream = new MemoryStream();
    doc1.Save(stream, ThisSaveFormat);
    string html = Encoding.UTF8.GetString(stream.GetBuffer());
    return html;
}

Hi
Thank you for additional information. I think that in this case you should not use HTML. I think you should use intermediate document and InsertDocument method.
Here is code that you can use to get intermediate document.

private static Document GetDocumentFromBookmark(string bookmarkName, Document doc)
{
    Document docClone = doc.Clone();
    Document doc1 = new Document();
    Bookmark mark = docClone.Range.Bookmarks[bookmarkName];
    Node node = mark.BookmarkStart.ParentNode;
    Node endNode = mark.BookmarkEnd.ParentNode.NextSibling;
    while (!node.Equals(endNode))
    {
        if ((node as CompositeNode).ChildNodes.Contains(mark.BookmarkStart))
        {
            Node child = (node as CompositeNode).FirstChild;
            Node endChild = mark.BookmarkStart.NextSibling;
            while (!child.Equals(endChild))
            {
                child = child.NextSibling;
                child.PreviousSibling.Remove();
            }
        }
        if ((node as CompositeNode).ChildNodes.Contains(mark.BookmarkEnd))
        {
            Node child = mark.BookmarkEnd;
            while (!child.Equals(child.ParentNode.LastChild))
            {
                child = child.NextSibling;
                child.PreviousSibling.Remove();
            }
            child.Remove();
        }
        doc1.FirstSection.Body.AppendChild(doc1.ImportNode(node, true, ImportFormatMode.KeepSourceFormatting));
        node = node.NextSibling;
        if (node == null)
            break;
    }
    return dco1;
}

You can find InsertDocument method here.
https://docs.aspose.com/words/java/insert-and-append-documents/
I hope this could help you.
Best regards.

I need to be able to edit the text in the document that was returned from a webpage? How would I do that?

Hi
Thanks for your inquiry. Do you mean that you need to edit document on web page? If so you can use Aspose.Editor. Please see the following link for more information.
https://products.groupdocs.com/editor/net/
Please clarify your requirements and I will try to help you.
Best regards.

I took a look. It looks like it may work for what I need. Is there a list somewhere of which features are currently not yet included that will not appear in the editor(ex: tables, etc.)? And if/when the plan is to implement these features. Allowing editing of tables is something that we will need later on.

Hi
Unknown document items are marked by red crosses. You can read about this here: https://products.groupdocs.com/editor/
Current version of Aspose.Editor treats images, fields and tables as unknown items. Next release will exclude tables from the “list of unknown items”.
Best regards.