Need help getting started

need help getting started

I have a document that is created through our web application. My data is stored in a database that allows HTML tags. There are several sections in my document that has HTML tags. Currently, my document does not “convert” those tags - the actual tags print out and I want to “convert” those tags to display the text accordingly. Can this be accomplished with your product? If so, would you please send me some sample code to get me started?

I have attached a sample document. Thanking you in advance for any help/suggestions that you may offer.

Valerie Davis-Binder

Hi Valerie,

Thanks for your inquiry. In your case, I suggest you please insert the html contents inside bookmarks while creating the document via your web application. Once you have contents inside the bookmark, you can easily get the bookmark contents (html) and inserted back the html into document at same position by using Aspose.Words.

Following code example extract the bookmark contents (in your case, the html contents) and insert the HTML into document by using DocumentBuilder.InsertHtml method. E.g your html contents are inside bm_html bookmark.

// open the document
Document doc = new Document(MyDir + "val2.doc");
DocumentBuilder builder = new DocumentBuilder(doc);
//Get the html 
string html = doc.Range.Bookmarks["bm_html"].Text;
doc.Range.Bookmarks["bm_html"].Text = "";
//Move cursor to bookmark and isnert html
builder.MoveToBookmark("bm_html");
builder.InsertHtml(html);
doc.Save(MyDir + "Out.doc");

Please read following documentation links for your kind reference.
https://docs.aspose.com/words/net/document-builder-overview/
https://docs.aspose.com/words/net/working-with-bookmarks/

I have attached the output Docx file with this post for your kind reference. Please let us know if you have any more queries.

Would you please send above code in vb?

Thank you.

The Out.doc file you attached is exactly what I am looking for. I need the code in vb. We do not use C# in our unit.

Just so that I understand, my HTML tags should go here?

doc.Range.Bookmarks["bm_html"].Text = "";

Thank you for your help!
Valerie

I have attached a file with an error message - please advise.

Hi Valerie,

Thanks for your inquiry. Your document does not contain the bookmark named ‘bm_html’.

Tahir Manzoor:

I suggest you please insert the html contents inside bookmarks while creating the document via your web application.

In your scenario, you need to enclosed the html contents of your document inside bookmark. As per your first post, you are creating the document (val2.DOC) via your web application. You can insert bookmark while creating the val2.DOC.

Could you please share some more detail about your complete scenario? We will then provide you more information on this along with code.

Would you please see this code to me in vb? We do not use C# in our office.

// open the document
Document doc = new Document(MyDir + "val2.doc");
DocumentBuilder builder = new DocumentBuilder(doc);
//Get the html
string html = doc.Range.Bookmarks["bm_html"].Text;
doc.Range.Bookmarks["bm_html"].Text = ""; 
//Move cursor to bookmark and isnert html
builder.MoveToBookmark("bm_html");
builder.InsertHtml(html); 
doc.Save(MyDir + "Out.doc");

Do you have any sample code that you could share with me on how to insert bookmark while creating the val2.DOC, dynamically? Also, I will need this code in vb, please.

Here is my code where I am creating my *.doc

Private Sub CreatePacketDetail()
For i As Integer = 0 To reqIds.Count - 1
AmendmentRequests(i) = reqPntProc.GetRequestData(reqIds(i).ToString(), meetnDate)
Next

'build and pass back the detail packet file
Dim wordDocBuilder As New RequestWordMLBuilder()
'Dim reqPrntProc As New RequestPrintingProcess()
wordDocBuilder.BuildPacketDetailDocument(AmendmentRequests, "LEG_packet.xsl")
MemoryStreamHelper.WriteToHttpResponse(PreConditionDoc(wordDocBuilder.Document), Me.Page.Response, ".DOC")

End Sub

Thank you!

Hi Valerie,

Thanks for your inquiry.

ValerieDavis-Binder:

Would you please see this code to me in vb? We do not use C# in our office.

Please check the following vb.net code to replace bookmark’s html contents into html output.

'open the document
Dim doc As New Document("E:\val2.doc")
Dim builder As New DocumentBuilder(doc)
' Use the indexer of the Bookmarks collection to obtain the desired bookmark.
Dim bookmark As Bookmark = doc.Range.Bookmarks("bm_html")
' Get the name and text of the bookmark.
Dim name As String = bookmark.Name
Dim html As String = bookmark.Text
bookmark.Text = ""
'Move cursor to bookmark and isnert html
builder.MoveToBookmark("bm_html")
builder.InsertHtml(html)
doc.Save("E:\Out.doc")

ValerieDavis-Binder:

*Here is my code where I am creating my .doc

You can insert bookmark into MS Word doc/docx by using following code snippet. You need to use thie code where you are insrting the html into your Doc file. Hope this helps you.

Dim builder As New DocumentBuilder()
builder.StartBookmark("bm_html")
builder.Writeln("Text inside a bookmark.")
builder.EndBookmark("bm_html")

Any idea why I am getting this message “declaration expected” - I am using the code you sent previously. Please see attached file. What am I missing?

Thanks!

Valerie

Hi Valerie,

Thanks for your inquiry. Please do not use the complete code in declaration section.

You may use the shared code in Page_Load event as shown below. Please let us know if you have any more queries.

Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
'open the document
Dim doc As New Document("E:\val2.doc")
Dim builder As New DocumentBuilder(doc)
' Use the indexer of the Bookmarks collection to obtain the desired bookmark.
Dim bookmark As Bookmark = doc.Range.Bookmarks("bm_html")
' Get the name and text of the bookmark.
Dim name As String = bookmark.Name
Dim html As String = bookmark.Text
bookmark.Text = ""
'Move cursor to bookmark and isnert html
builder.MoveToBookmark("bm_html")
builder.InsertHtml(html)
doc.Save("E:\Out.doc")
End Sub

Is it possible to have more than 1 bookmark in your document with the same name? My document consists of one to many separate files saved into the one “Master File”. Each separate file within the “Master File” has a section named “Problem Statement” and I need to have a bookmark for each of those “Problem Statement” sections.

If you can not have multiple bookmarks with identical names, how would you suggest that I accomplish this task? Any sample code would be greatly appreciated.

Also, how would you retrieve all the bookmarks from the “Master File”?

I have attached an example of my “Master File” and you will see the “Problem Statement” sections highlighted in yellow.

Thank you for your continued support!

Valerie

Hi Valerie,

Thanks for your inquiry. Please note that Aspose.Words tries to mimic the same behaviour as MS Word do. MS Word allows only unique names for bookmarks. If you change the name of a bookmark to a name that already exists in the document, no error will be given and only the first bookmark will be stored when you save the document. When you add a cloned Paragraph into the document, its bookmark will not be stored due to same name.

A complete bookmark in a Word document consists of a BookmarkStart and a matching BookmarkEnd with the same bookmark name. In your case, I suggest you please add the bookmark into the document with name starts with text e.g ‘html’ as ‘html_bookmarkname’. Following code example iterate through all bookmarks in the document and convert the html contents into pure html output in output Doc. Hope this helps you. Please let us know if you have any more queries.

'open the document
Dim doc As New Document("d:\multi.doc")
Dim builder As New DocumentBuilder(doc)
For Each bm As Bookmark In doc.Range.Bookmarks
If bm.Name.StartsWith("html") Then
Dim html As String = bm.Text
bm.Text = ""
builder.MoveToBookmark(bm.Name)
builder.InsertHtml(html)
End If
Next
doc.Save("d:\Out.doc")