One location in a WordprocessingML document can be linked to another location in that document via a bookmark. A bookmark is a way to mark a particular place in a WordprocessingML document.It identifies a location or a selection of text that you name and identify for future reference.
A bookmarked text in a document,can prove useful in maintaining that portion of document confidential and invisible to unintended users .It can further highlight a text portion which has to be translated from one language to another in Translation Service Sector
For example, you might use a bookmark to identify text that you want to revise at a later time. Instead of scrolling through the document to locate the text, you can go to it by using the Bookmark dialog box.An entire text, or the location of the text can be bookmarked You can add a bookmark from the 'Insert tab', in the 'Links' group
Coming down to the XML structure of the word document,here's an example of inserting a book mark,to a text selected,i.e. a run.
<w:p>
<w:bookmarkStart w:id="1" w:name="bookmarkName1" />
<w:r >
<w:t>This is a book mark with name Name1.</w:t>
</w:r>
<w:bookmarkEnd w:id="1" />
</w:p>
If a location is to be bookmarked,choose the location and insert the following XML portion
<w:r>
<w:t>This is</w:t>
<w:bookmarkStart w:id="2" w:name="bookmarkLocation1" />
<w:bookmarkEnd w:id="2" />
<w:t>Location with a bookmark</w:t>
As you can see each of the bookmarks are given a distinct name,and an Id,for easy referance.The bookmarkStart and bookmarkEnd nodes distinguish if the bookmark is for the entire text or is based on location.
You can also get back to a particular bookmark,form a differant location in a document through a Hyperlink,by just mentioning the particular bookmark name as 'anchor' ,as shown,
<w:hyperlink w:anchor="bookmarkName" w:history="1">
<w:rPr>
<w:rStyle w:val="Hyperlink" />
</w:rPr>
<w:t>Link to a Bookmark</w:t>
</w:hyperlink>
This can be extended to presentation and Excel Documents as well
Regards,
Vijayeta
how you do to create a BookMarkStart? I tried to create instance BookMarkStart but id is nothing, Can you help me?
You must always set id attributes manually, like all other attributes. This is not the 'Office COM' world with its hight level of abstraction and this is why the programming against OpenXML APIs is so tedious and error prone.
For most IDs you can usually use:
Guid.NewGuid().ToString("N");
in order to create new IDs.
But the bookmark id values must be unique subsequent integers starting with 1. When adding a new bookmark, you must first find all 'w:bookmarkStart' elements and then find the max 'w:id' value. So, the ID value for the new bookmarkStart element is 'maxID+1'.
IEnumerable<WP.BookmarkStart> bsElements = myDocument.MainDocumentPart.RootElement.Descendants<WP.BookmarkStart>();
int newBookmarkId = bsElements.Any() ? bsElements.Max(e => int.Parse(e.Id.Value)) : 1;
Whenever possible I would choose content controls over bookmarks to tag document content. Content controls are not so flexible (you cannot e.g. use them to tag the content from the middle of the first paragraph to the middle of the second one) but the content wrapped by them is in most cases much easier to parse.
I hope this helps.
-- Jure
Thanks you Jure, I think that OpenXml was a robust framework, for this reason I thought it was not necessary to set the ID manually.
See my code:
Public Function ReplaceHiddenTextByBookMark(ByVal nombreOculto As String) As Boolean
Dim oculto As Run = Nothing
Dim elementoPadre As OpenXmlElement = Nothing
Dim marcadorStart As BookmarkStart = Nothing
Dim marcadorEnd As BookmarkEnd = Nothing
oculto = Me.FindHiddenText(nombreOculto)
If oculto IsNot Nothing Then
Try
elementoPadre = oculto.Parent
Dim marcadorMaximo As Integer = Me._wdDoc.MainDocumentPart.RootElement.Descendants(Of BookmarkStart).ToList.Count
marcadorStart = New BookmarkStart() With {.Id = marcadorMaximo.ToString}
marcadorStart.Name = "Principio" & nombreOculto
elementoPadre.InsertAfter(marcadorStart, oculto)
marcadorEnd = New BookmarkEnd() With {.Id = marcadorMaximo.ToString}
elementoPadre.InsertAfter(marcadorEnd, marcadorStart)
marcadorMaximo += 1
marcadorStart.Name = "Fin" & nombreOculto
elementoPadre.InsertAfter(marcadorStart, marcadorEnd)
oculto.Parent.RemoveChild(oculto)
Return True
Catch ex As Exception
End Try
End If
Return False
End Function
attribute ID can be "0", I tested it.
Thanks one more time for your help