The sample demonstrates how to Set bookmark in Word document.
private void button1_Click(object sender, EventArgs e) { //Create word document Document document = new Document(); Bookmark(document.AddSection()); //Save doc file. document.SaveToFile("Sample.doc",FileFormat.Doc); //Launching the MS Word file. WordDocViewer("Sample.doc"); } private void Bookmark(Section section) { Paragraph paragraph = section.AddParagraph(); paragraph.AppendText("The sample demonstrates how to using bookmark."); paragraph.ApplyStyle(BuiltinStyle.Heading2); section.AddParagraph(); paragraph = section.AddParagraph(); paragraph.AppendText("Simple bookmark."); paragraph.ApplyStyle(BuiltinStyle.Heading4); // Writing simple bookmarks section.AddParagraph(); paragraph = section.AddParagraph(); paragraph.AppendBookmarkStart("SimpleBookMark"); paragraph.AppendText("This is a simple book mark."); paragraph.AppendBookmarkEnd("SimpleBookMark"); section.AddParagraph(); paragraph = section.AddParagraph(); paragraph.AppendText("Nested bookmark."); paragraph.ApplyStyle(BuiltinStyle.Heading4); // Writing nested bookmarks section.AddParagraph(); paragraph = section.AddParagraph(); paragraph.AppendBookmarkStart("Root"); paragraph.AppendText(" Root data "); paragraph.AppendBookmarkStart("NestedLevel1"); paragraph.AppendText(" Nested Level1 "); paragraph.AppendBookmarkStart("NestedLevel2"); paragraph.AppendText(" Nested Level2 "); paragraph.AppendBookmarkEnd("NestedLevel2"); paragraph.AppendText(" Data Level1 "); paragraph.AppendBookmarkEnd("NestedLevel1"); paragraph.AppendText(" Data Root "); paragraph.AppendBookmarkEnd("Root"); } private void WordDocViewer(string fileName) { try { System.Diagnostics.Process.Start(fileName); } catch { } }
Private Sub button1_Click(ByVal sender As Object, ByVal e As EventArgs) Handles button1.Click 'Create word document Dim document_Renamed As New Document() Bookmark(document_Renamed.AddSection()) 'Save doc file. document_Renamed.SaveToFile("Sample.doc",FileFormat.Doc) 'Launching the MS Word file. WordDocViewer("Sample.doc") End Sub Private Sub Bookmark(ByVal section_Renamed As Section) Dim paragraph_Renamed As Paragraph = section_Renamed.AddParagraph() paragraph_Renamed.AppendText("The sample demonstrates how to using bookmark.") paragraph_Renamed.ApplyStyle(BuiltinStyle.Heading2) section_Renamed.AddParagraph() paragraph_Renamed = section_Renamed.AddParagraph() paragraph_Renamed.AppendText("Simple bookmark.") paragraph_Renamed.ApplyStyle(BuiltinStyle.Heading4) ' Writing simple bookmarks section_Renamed.AddParagraph() paragraph_Renamed = section_Renamed.AddParagraph() paragraph_Renamed.AppendBookmarkStart("SimpleBookMark") paragraph_Renamed.AppendText("This is a simple book mark.") paragraph_Renamed.AppendBookmarkEnd("SimpleBookMark") section_Renamed.AddParagraph() paragraph_Renamed = section_Renamed.AddParagraph() paragraph_Renamed.AppendText("Nested bookmark.") paragraph_Renamed.ApplyStyle(BuiltinStyle.Heading4) ' Writing nested bookmarks section_Renamed.AddParagraph() paragraph_Renamed = section_Renamed.AddParagraph() paragraph_Renamed.AppendBookmarkStart("Root") paragraph_Renamed.AppendText(" Root data ") paragraph_Renamed.AppendBookmarkStart("NestedLevel1") paragraph_Renamed.AppendText(" Nested Level1 ") paragraph_Renamed.AppendBookmarkStart("NestedLevel2") paragraph_Renamed.AppendText(" Nested Level2 ") paragraph_Renamed.AppendBookmarkEnd("NestedLevel2") paragraph_Renamed.AppendText(" Data Level1 ") paragraph_Renamed.AppendBookmarkEnd("NestedLevel1") paragraph_Renamed.AppendText(" Data Root ") paragraph_Renamed.AppendBookmarkEnd("Root") End Sub Private Sub WordDocViewer(ByVal fileName As String) Try Process.Start(fileName) Catch End Try End Sub