Need to replace bookmark with HTML formatted text

Hi,

I need to replace a bookmark on existing doc file with html formatted text from a rich text box. Please help, its very urgent.

Thanks

Hi,

Thanks for your inquiry. You need to use MoveToBookmark() method to move cursor to desired bookmark and then InsertHtml() method for inserting html. Please refer to the following articles for further details. Hopefully it will serve the purpose.

https://reference.aspose.com/words/net/aspose.words/documentbuilder/movetobookmark/

https://docs.aspose.com/words/net/programming-with-documents/

Please feel free to contact us for any further assitacne.

Best Regards,

I tried this method and it replaced bookmark with this

<h4>Admission
Information:</h4><ul><li><b>Date of
Admission:</b>                     
05/24/2012</li><li><b>Date of
Discharge:</b>                     
05/25/2012</li></ul><h4>Provider
Information:</h4><ul><li><b>Hopkins Discharge Attending:</b>            Dr. Kim Reiss,
MD</li><li><b>Hopkins Discharge Attending Tel
No:</b>     443-287-3127</li><li><b>Primary
Care Provider:</b>                 
Christine Fleurimond, MD</li><li><b>Primary Care
Provider Tel No:</b>           410-383-8300</li></ul><h4>Medications
(Verify medication names with your
Pharmacist)</h4><ul><li>Vitamin, Multi  1 tablet Daily By
mouth.</li><li>Ferrous 
Sulfate  325 mg With meals By
mouth.</li><li>Vitamin B12 1000 mcg by mouth
daily</li></ul><h4>Discharge Patient
To:</h4><ul><li>Home</li></ul><h4>Appointments:</h4><ul><li><b>Provider's
Name:</b> Total Healthcare Division Street</li></ul><h4>Discharge
Diets:</h4><ul><li>Diet Instructions
Regular</li></ul><h4>Discharge
Activities/Restrictions:</h4><ul><li>Resume normal activity
as tolerated</li></ul><h4>Other
Issues:</h4><ul><li><b>-      Please review the following:</b> IF YOU NEED HELP FROM A
MEMBER OF THE

HOSPITALIST TEAM FOLLOWING YOUR DISCHARGE TO
CLARIFY YOUR POST-DISCHARGE

PLANS, MEDICATIONS OR TREATMENTS, PLEASE CALL
MAGGIE NEELY (CASE MANAGER)

AT 410-502-5302. IF YOU NEED HELP DURING
NON-BUSINESS HOURS, CALL THE

HOSPITALIST UNIT FRONT DESK AT 443-287-3127. IF
IT IS AN EMERGENCY, CALL

911.</li><li>PLEASE BE SURE TO TAKE
ALL MEDICATIONS AND YOUR BLUE DISCHARGE FOLDER WITH

YOU, WHEN YOU GO IN TO SEE
YOUR PRIMARY DOCTOR.</li></ul><h4>Authored/Entered By:Williams, Lisa (C) Entered On 2012-05-25
12:41</h4><ul><li><b>Authored/Entered By:</b>
Williams, Lisa (C) Entered On 2012-05-25
12:41</li><li><b>Last Edited/Revised By:</b> Reiss
Binder, Kim (MD) Entered On 2012-05-25 15:52</li></ul>

I need formatted html dont want to show the tags on the doucment. Hope I get a solution asap.

Thanks

Hi,

Thanks for your inquiry. Please check following code snippet, for moving cursor to a specific bookmark and to insert formatted html. Hopefully it will serve the purpose.

Document doc = new Document(MyDir+"bookmarktest.doc");
DocumentBuilder builder= new DocumentBuilder(doc);
builder.MoveToBookmark("bm_1",true,false);
builder.InsertHtml("<h4>Admission Information:</h4><ul><li><b>Date of Admission:</b>05/24/2012</li><li><b>Date of Discharge:</b>                      05/25/2012</li></ul><h4>Provider Information:</h4><ul><li><b>Hopkins Discharge Attending:</b>Dr. Kim Reiss, MD</li><li><b>Hopkins Discharge Attending Tel No:</b>     443-287-3127</li><li><b>Primary Care Provider:</b>Christine Fleurimond, MD</li>");
doc.Save(MyDir+"out.doc");

Please feel free to contact us for any further assistance.

Best Regards,

The string is coming from FreeRichText box .

db.MoveToBookmark("BookMarkMedicine");
db.InsertHtml(FreeTextBox1.Text);
doc.Save(Response, "Report Out.doc", Aspose.Words.ContentDisposition.Inline, null);

Hi,

Thanks for your inquiry. Could you please share your sample working solution? So we will have a close look into it and suggest you solution accordingly.

Please feel free to contact us for any further assistance.

Best Regards,

Hi Tilal,

vSaral is asking for How to get a formatted Text in

in bookmark1.Text

Suppose if we assigned a bookmark to a formatted text …and made some formatting changes .After that How to assign this formatted text to bookmark.Text???

or

formattedText.Replace(FormattedText,bookmark.Text)…

I also need to know Is it possible ?

Thanks

sreejesh

Hi Sreejesh,

Thanks for your inquiry.

The Bookmark.Text property gets the text
enclosed in the bookmark without formatting. If you want to get the
formatted text inside a bookmark, please extract the content from a bookmark using the code shared in following documentation link:
https://docs.aspose.com/words/net/how-to-extract-selected-content-between-nodes-in-a-document/

In your case, you need to use MoveToBookmark method to move cursor to desired bookmark and use InsertDocument method to insert the extracted contents to bookmark. Please check the code of InsertDocument method from here:
https://docs.aspose.com/words/java/insert-and-append-documents/

Hope this answers your query. Please let us know if you have any more queries.

Hi,

I am trying to insert HTML into bookmark but I am seeing it is being inserted all time outside of the bookmark. I am using following code

// Retrieve the bookmark.
var bookmark = wordDoc.Range.Bookmarks[bookmarkName];
if (!bookmark.IsNull())
{
    // Create a document builder so we can insert the new value as html.
    // wordDoc.Range.Bookmarks[bookmark.Name].Text = string.Empty;
    var documentBuilder = new Aspose.Words.DocumentBuilder(wordDoc);
    documentBuilder.MoveToBookmark(bookmark.Name, true, false);
    documentBuilder.Writeln(bookmarkValue);
}

I am using Alfresco version v4.0.30319. I am attaching word document where you can see result.

Please guide me in this issue.

Regards,

Robert

Hi Dinesh,

Thanks for your inquiry. Could you please share bookmark name here for testing? We will investigate the issue on our side and provide you more information.

Moreover, please manually create your expected Word document using Microsoft Word and attach it here for our reference. We will investigate as to how you want your final Word output be generated like. We will then provide you more information on this along with code.

We have used following code example to test your scenario and have not found any issue with output document.

Document wordDoc = new Document(MyDir + "TestResearch_T_F.docx");
var bookmark = wordDoc.Range.Bookmarks["bmkDocAuthor"];
wordDoc.Range.Bookmarks[bookmark.Name].Text = string.Empty;
var documentBuilder = new Aspose.Words.DocumentBuilder(wordDoc);
documentBuilder.MoveToBookmark(bookmark.Name, true, false);
documentBuilder.InsertHtml("**new text**");
wordDoc.Save(MyDir + "Out.docx");

Hi,

Please find expected word document and sample template as an attachment. My issue is when I insert html in bookmark it alltime display outside of the bookmark. I have used all option combination for “InsertHtml” method.As html is inserting outside of bookmark so user is not able to edit it. I given option that text inside the bookmark user can edit it.

I created bookmark with protection with paragraph so user can not delete bookmark but can edit text between bookmark. In my sample template I given two bookmark “bmkSupportingAnalysis” this bookmark is protected and user cannot delete it.

Other bookmark “bmkTest” is unprotected and so user can delete it. Template is protected and password is “test”.

Please provide solution for this, that after inserting html user should be able to edit it.

Regards,

Robert

Hi Robert,

Thanks for sharing the detail. In your case, we suggest you please insert the html contents between EditableRangeStart and EditableRangeEnd. Please check following code example for your kind reference. Hope this helps you.

Document wordDoc = new Document(MyDir + "SampleTemplate.docx");
var bookmark = wordDoc.Range.Bookmarks["bmkSupportingAnalysis"];
var documentBuilder = new Aspose.Words.DocumentBuilder(wordDoc);
if (bookmark.BookmarkStart.NextSibling.NodeType == NodeType.EditableRangeStart)
{
    EditableRangeStart ers = (EditableRangeStart)bookmark.BookmarkStart.NextSibling;
    documentBuilder.MoveTo(ers.EditableRange.EditableRangeEnd);
}
documentBuilder.InsertHtml("**new text**");
wordDoc.Save(MyDir + "Out.docx");

Hi Tahir,

Thanks your solution is working for me. I am seeing only one issue that when I am inserting html into bookmark seeing two blank line in starting while HTML didn’t have blank lines. Please see in attached document.

Regards,

Robert

Hi Robert,

Thanks for your inquiry. You are facing the expected behavior of Aspose.Words. There are thee empty paragraph inside bookmark “bmkSupportingAnalysis”. Please check the attached image for detail. We suggest you please remove the empty paragraphs between BookmarkStart and BookmarkEnd nodes in your template document.