Bookmark

This example demonstrates how to modify a document based on bookmarks.

When there is a need to modify a document ,there are several ways to do it ,but sometimes it becomes a requirement to do it in certain way.

Depending upon the requirement to modfy a word document based upon bookmarks

Suppose in your document you have made bookmarks you can replace bookmark content with new content.

We have several version of this representation in words ,using dropdown you can make a choice within the following

After clicking generate button it will launch a word document that will have replaced content depending upon bookmark on comparing the source word document.

using System;
using System.Collections.Generic;
using System.Drawing;
using System.Drawing.Imaging;
using System.IO;
using System.Text;

using OfficeComponent.Word;


namespace OfficeComponent.Samples
{
    class BookmarkExample : WordExampleBase
    {
        public BookmarkExample(string commonDataPath, string outputDir)
            : base(commonDataPath, outputDir)
        {

        }

        public BookmarkExample(string commonDataPath, string outputDir, string xmlFile) : base(commonDataPath, outputDir, xmlFile)
        {

        }

        public override string Execute()
        {
            // Create a new instance of PdfDocument class.
            WordDocument document = new WordDocument(CommonDataPath + "\\BookmarkToolTemplate.doc");

            // Creating a bookmark navigator. Which help us to navigate through the 
            // bookmarks in the template document.
            BookmarksNavigator bk = new BookmarksNavigator(document);

            // Move to the header bookmark.
            bk.MoveToBookmark("HeaderBookmark");

            // Get the table in the document.
            TextBodyPart bodyPart = new TextBodyPart(document);
            bodyPart.BodyItems.Add(document.Sections[0].Tables[0]);
            // Move to the Word bookmark.
            bk.MoveToBookmark("HeaderBookmark");
            // Replacing content with table.
            bk.ReplaceBookmarkContent(bodyPart);


            // Move to the OfficeComponent_Word bookmark.
            bk.MoveToBookmark("OfficeComponent_Word");
            // Get the content of OfficeComponent_Word bookmark.
            bodyPart = bk.GetBookmarkContent();
            // Move to the Word bookmark.
            bk.MoveToBookmark("Word");
            // Replacing content of Word bookmark.
            bk.ReplaceBookmarkContent(bodyPart);


            // Move to the OfficeComponent_PDF bookmark.
            bk.MoveToBookmark("OfficeComponent_PDF");
            // Get the content of OfficeComponent_PDF bookmark.
            bodyPart = bk.GetBookmarkContent();
            // Move to the PDF bookmark.
            bk.MoveToBookmark("PDF");
            // Replacing content of PDF bookmark.
            bk.ReplaceBookmarkContent(bodyPart);


            // Move and get the content of Studio Bookmark.
            bk.MoveToBookmark("Studio");
            bodyPart = bk.GetBookmarkContent();
            // Repacing its content by doc.Replace() method.
            document.Replace(new System.Text.RegularExpressions.Regex("OfficeComponent_Studio"), bodyPart);

            // Move to the Text bookmark.
            bk.MoveToBookmark("Text");
            bk.DeleteBookmarkContent(true);
            // Inserting text inside the bookmark. This will preserve the source formatting
            bk.InsertText("OfficeComponent Word is a library that can be used in both ASP.NET and windows forms applications without the presence of MS Automation. The usage is common for both environments except for the part where the created spreadsheet is saved to disk or stream in the case of a windows forms application and streamed to the client browser in the case of asp.net applications.");

            bk.MoveToBookmark("Image");
            bk.DeleteBookmarkContent(true);
            // Inserting image to the bookmark.
            Picture pic = (Picture)bk.InsertParagraphItem(ParagraphItemType.Picture);
            pic.LoadImage(Image.FromFile(CommonDataPath + "\\Logo.gif"));
            pic.WidthScale = 100f;  // It reduce the image size because it don't fit 
            pic.HeightScale = 100f; // in document page.

            string fileName = Path.Combine(OutputDir, this.GetType().Name + "_" + Guid.NewGuid().ToString() + GetExtension(SaveAsFormat));
            // Save the document.
            document.Save(fileName, SaveAsFormat);


            // Close the document.
            document.Close();

            return fileName;
        }
    }

}
Imports System.Drawing.Imaging
Imports System.IO
Imports System.Text

Imports OfficeComponent.Word


Namespace OfficeComponent.Samples
	Friend Class BookmarkExample
		Inherits WordExampleBase
		Public Sub New(ByVal commonDataPath As String, ByVal outputDir As String)
			MyBase.New(commonDataPath, outputDir)

		End Sub

		Public Sub New(ByVal commonDataPath As String, ByVal outputDir As String, ByVal xmlFile As String)
			MyBase.New(commonDataPath, outputDir, xmlFile)

		End Sub

		Public Overrides Function Execute() As String
			' Create a new instance of PdfDocument class.
			Dim document As New WordDocument(CommonDataPath & "\BookmarkToolTemplate.doc")

			' Creating a bookmark navigator. Which help us to navigate through the 
			' bookmarks in the template document.
			Dim bk As New BookmarksNavigator(document)

			' Move to the header bookmark.
			bk.MoveToBookmark("HeaderBookmark")

			' Get the table in the document.
			Dim bodyPart As New TextBodyPart(document)
			bodyPart.BodyItems.Add(document.Sections(0).Tables(0))
			' Move to the Word bookmark.
			bk.MoveToBookmark("HeaderBookmark")
			' Replacing content with table.
			bk.ReplaceBookmarkContent(bodyPart)


			' Move to the OfficeComponent_Word bookmark.
			bk.MoveToBookmark("OfficeComponent_Word")
			' Get the content of OfficeComponent_Word bookmark.
			bodyPart = bk.GetBookmarkContent()
			' Move to the Word bookmark.
			bk.MoveToBookmark("Word")
			' Replacing content of Word bookmark.
			bk.ReplaceBookmarkContent(bodyPart)


			' Move to the OfficeComponent_PDF bookmark.
			bk.MoveToBookmark("OfficeComponent_PDF")
			' Get the content of OfficeComponent_PDF bookmark.
			bodyPart = bk.GetBookmarkContent()
			' Move to the PDF bookmark.
			bk.MoveToBookmark("PDF")
			' Replacing content of PDF bookmark.
			bk.ReplaceBookmarkContent(bodyPart)


			' Move and get the content of Studio Bookmark.
			bk.MoveToBookmark("Studio")
			bodyPart = bk.GetBookmarkContent()
			' Repacing its content by doc.Replace() method.
			document.Replace(New System.Text.RegularExpressions.Regex("OfficeComponent_Studio"), bodyPart)

			' Move to the Text bookmark.
			bk.MoveToBookmark("Text")
			bk.DeleteBookmarkContent(True)
			' Inserting text inside the bookmark. This will preserve the source formatting
			bk.InsertText("OfficeComponent Word is a library that can be used in both ASP.NET and windows forms applications without the presence of MS Automation. The usage is common for both environments except for the part where the created spreadsheet is saved to disk or stream in the case of a windows forms application and streamed to the client browser in the case of asp.net applications.")

			bk.MoveToBookmark("Image")
			bk.DeleteBookmarkContent(True)
			' Inserting image to the bookmark.
			Dim pic As Picture = CType(bk.InsertParagraphItem(ParagraphItemType.Picture), Picture)
			pic.LoadImage(Image.FromFile(CommonDataPath & "\Logo.gif"))
			pic.WidthScale = 100F ' It reduce the image size because it don't fit
			pic.HeightScale = 100F ' in document page.

			Dim fileName As String = Path.Combine(OutputDir, Me.GetType().Name & "_" & Guid.NewGuid().ToString() & GetExtension(SaveAsFormat))
			' Save the document.
			document.Save(fileName, SaveAsFormat)


			' Close the document.
			document.Close()

			Return fileName
		End Function
	End Class

End Namespace