Free Support Forum - aspose.com

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.

  1. WorkDocClone.Range.Bookmarks["Body"].Text = string.Empty;
  2. DocumentBuilder DB = new DocumentBuilder(WorkDocClone);
  3. DB.MoveToBookmark("Body", true, true);
  4. DB.InsertHtml(_ESignHTML);
  5. if (_ESignCCs != null)
  6. WorkDocClone.Range.Bookmarks["CC"].Text = _ESignCCs;
  7. if (_ESignData.Signature != null)
  8. {
  9. DB.MoveToBookmark("InsertESign", true, true);
  10. //DB.InsertHtml(_ESignData.GetSignatureLine(true));
  11. //DB.MoveToBookmark("InsertEsign");
  12. DB.Write("Leah Behl, MD");
  13. }

The original document is attached.

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

Please advise. Thank you.

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

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<?xml:namespace prefix = o ns = "urn:schemas-microsoft-com:office:office" />

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 = "test
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 = @"

 

May 19, 2008

 

Dr. Jay Vida

Lakewood , NJ 08701

Re: Last, First

 

Dear

 

 

Many thanks for this kind referral.

 

Sincerely,

 

 

 

";

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<?xml:namespace prefix = o ns = "urn:schemas-microsoft-com:office:office" />

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.

http://www.aspose.com/documentation/file-format-components/aspose.words-for-.net-and-java/insert-a-document-into-another-document.html

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<?xml:namespace prefix = o ns = "urn:schemas-microsoft-com:office:office" />

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.

http://www.aspose.com/documentation/visual-components/aspose.editor-for-.net/aspose-editor-getting-started.html

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<?xml:namespace prefix = o ns = "urn:schemas-microsoft-com:office:office" />

Unknown document items are marked by red crosses. You can read about this here: http://www.aspose.com/documentation/visual-components/aspose.editor-for-.net/lossless-editing.html

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.