This article will show you how to create a word document with bookmarks and then insert images and text in the location of bookmarks.
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.
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.
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.InsertAtBookmark("handy ", "secondBookmark");
Effective screenshot of the
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
//Load the generated word document by DocX.
Document document = new Document();
//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);
//Save to a new word document
string output = "D:\\sample.docx";
Effective screenshot after insert
text and images:
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
Free Spire.Doc: https://freeword.codeplex.com