Simple word API to manipulate bookmarks in Word

Gwhite
Posted by in C# category on for Beginner level | Points: 250 | Views : 2207 red flag

This article will show you how to create a word document with bookmarks and then insert images and text in the location of bookmarks.

Introduction

We often heard that if you want to operate EXCEL files for free, NPOI is your choice. Then DocX is your choice to operate word documents for free for developers. This article will show how to create a word document with bookmarks and then insert images and text in the location of bookmarks by using DocX and Free Spire.Doc together.


Application Overview

Firstly,  DocX is used to create an empty word document and then insert a paragraph with bookmarks, then save the word document. Secondly, I use Spire.Doc to insert some images and text in the location of bookmarks.

 Step 1: Use Docx to create a word documents with bookmarks.

DocX is an open source API which is fast, lightweight and best of all it does not require Microsoft Word or Office to be installed. It supports to work with Word 2007/2010 files.

 

string filename = @"docs\Bookmarks.docx";

using (var document = DocX.Create(filename))

{

var paragraph = document.InsertBookmark("firstBookmark");

var paragraph2 = document.InsertParagraph("This is a paragraph which contains a ");

paragraph2.AppendBookmark("secondBookmark");

paragraph2.Append("bookmark");

paragraph2.InsertAtBookmark("handy ", "secondBookmark");

document.Save();

}

Effective screenshot of the word document:

Step 2: Find the bookmarks and then add image and test there, DocX doesn’t support to work with it. For this specific purpose, we have free Spire.Doc. Besides bookmarks, it also support to work with other word elements in word document, such as hyperlinks, comments, fields, tables, shapes, header & footer and so on.

 Spire.Doc offers an instance of BookmarksNavigator to find the bookmarks, and then use AppendPicture to add an image. The following are the codes of insert images and text in the location of bookmarks.

 

//Load the generated word document by DocX.

Document document = new Document();

document.LoadFromFile(filename);

//Find the bookmark named firstBookmark and insert some text in its location

Bookmark book = document.Bookmarks["firstBookmark"];

Paragraph par1 = book.BookmarkStart.OwnerParagraph;

par1.AppendText("This is the first paragraph.");

//Find the bookmark named secondBookmark and insert an image in its location

Paragraph par2 = new Paragraph(document);

Image image = Image.FromFile("D:\\pic.png");

DocPicture picture = par2.AppendPicture(image);

Bookmark bookmark = document.Bookmarks["secondBookmark"];

Paragraph bookPra = bookmark.BookmarkStart.OwnerParagraph;

int index = bookPra.ChildObjects.IndexOf(bookmark.BookmarkStart);

bookPra.ChildObjects.Insert(index, picture);

//Save to a new word document

string output = "D:\\sample.docx";

document.SaveToFile(output, FileFormat.Docx);

 

Effective screenshot after insert text and images:

Summary


DocX is an open source to easily read and write word 2007-2010, but it not compatible with word 93-97 and word 2013. Free Spire.Doc supports to work with these versions of word documents, but it is limited to Free version is limited to 100 paragraphs and 5 tables. For personal use, both of them are enough. If this article helps, you can share it with your friends.


Reference

Download link:

Docx: http://docx.codeplex.com

Free Spire.Doc: https://freeword.codeplex.com


Page copy protected against web site content infringement by Copyscape

About the Author

Gwhite
Full Name: Gabrielle White
Member Level: Starter
Member Status: Member
Member Since: 7/27/2014 11:38:31 PM
Country: United States



Login to vote for this post.

Comments or Responses

Login to post response

Comment using Facebook(Author doesn't get notification)