Knowledgebase (2329)
Children categories
Bookmarks in Microsoft Word are useful for quickly navigating to specific sections of a document. They allow you to create and name "markers" within your document that you can easily jump to. This can be particularly helpful when working with long or complex documents.
In this article, you will learn how to add and remove bookmarks in a Word document using C# and the Spire.Doc for .NET library.
- Add a Bookmark to a Paragraph in C#
- Add a Bookmark to Specific Text within a Paragraph in C#
- Remove Bookmarks from a Word Document in C#
Install Spire.Doc for .NET
To begin with, you need to add the DLL files included in the Spire.Doc for.NET package as references in your .NET project. The DLL files can be either downloaded from this link or installed via NuGet.
PM> Install-Package Spire.Doc
Add a Bookmark to a Paragraph in C#
Using Spire.Doc, you can create a bookmark for a paragraph by inserting a BookmarkStart object at the beginning of the paragraph and a BookmarkEnd object at the end of the paragraph. The space between the bookmark start and end points becomes a defined bookmark that can be referenced and accessed as needed.
The detailed steps to add a bookmark to a paragraph are as follows.
- Create a Document object.
- Load a Word document.
- Get a specific paragraph from a specified section.
- Create a BookmarkStart object using Paragraph.AppendBookmarkStart() method.
- Insert the BookmarkStart at the beginning of the selected paragraph.
- Append a BookmarkEnd object to the end of the paragraph using Paragraph.AppendBookmarkEnd() method.
- Save the document to a different Word file.
- C#
using Spire.Doc;
using Spire.Doc.Documents;
namespace AddBookmarkToParagraph
{
class Program
{
static void Main(string[] args)
{
// Create a Document object
Document doc = new Document();
// Load a Word file
doc.LoadFromFile("C:\\Users\\Administrator\\Desktop\\input.docx");
// Get a specified paragraph
Paragraph paragraph = doc.Sections[0].Paragraphs[2];
// Create a bookmark start
BookmarkStart start = paragraph.AppendBookmarkStart("myBookmark");
// Insert it at the beginning of the paragraph
paragraph.Items.Insert(0, start);
// Append a bookmark end at the end of the paragraph
paragraph.AppendBookmarkEnd("myBookmark");
// Save the file
doc.SaveToFile("AddBookmarkToParagraph.docx", FileFormat.Docx2019);
// Dispose resources
doc.Dispose();
}
}
}

Add a Bookmark to Specific Text within a Paragraph in C#
To add a bookmark to specific text, you need first to find the text and its position within the paragraph. Then, insert a BookmarkStart object before the text and a BookmarkEnd object after the text.
The following are the steps to add a bookmark to specific text within a paragraph using Spire.Doc.
- Create a Document object.
- Load a Word document.
- Find the desired text from the document, and get its position in its owner paragraph.
- Create a BookmarkStart object using Paragraph.AppendBookmarkStart() method.
- Insert the BookmarkStart before the selected text.
- Create a BookmarkEnd object using Paragraph.AppendBookmarkEnd() method.
- Insert the BookmarkEnd object behind the selected text.
- Save the document to a different Word file.
- C#
using Spire.Doc;
using Spire.Doc.Documents;
namespace AddBookmarkToText
{
class Program
{
static void Main(string[] args)
{
// Create a Document object
Document doc = new Document();
// Load a Word file
doc.LoadFromFile("C:\\Users\\Administrator\\Desktop\\input.docx");
// Specify the string to find
string stringToFind = "Privacy Policy";
// Find the selected text from the document
TextSelection[] finds = doc.FindAllString(stringToFind, false, true);
TextSelection specificText = finds[1];
// Find the paragraph where the text is located
Paragraph para = specificText.GetAsOneRange().OwnerParagraph;
// Get the index of the text in the paragraph
int index = para.ChildObjects.IndexOf(specificText.GetAsOneRange());
// Create a bookmark start
BookmarkStart start = para.AppendBookmarkStart("myBookmark");
// Insert the bookmark start at the index position
para.ChildObjects.Insert(index, start);
// Create a bookmark end
BookmarkEnd end = para.AppendBookmarkEnd("myBookmark");
// Insert the bookmark end at the end of the selected text
para.ChildObjects.Insert(index + 2, end);
// Save the document to another file
doc.SaveToFile("AddBookmarkToText.docx", FileFormat.Docx2019);
// Dispose resources
doc.Dispose();
}
}
}

Remove Bookmarks from a Word Document in C#
To remove a specific bookmark or all bookmarks from a Word document, you can use the Bookmarks.Remove() method or the Bookmarks.Clear() method. The detailed steps are as follows.
- Create a Document object.
- Load a Word document.
- Get a specific bookmark from the document by its index using Document.Bookmarks[index] property.
- Remove the bookmark using Bookmarks.Remove() method.
- To remove all bookmarks at once, use Document.Bookmarks.Clear() method.
- Save the document to a different Word file.
- C#
using Spire.Doc;
namespace RemoveBookmarks
{
class Program
{
static void Main(string[] args)
{
// Create a Document object
Document doc = new Document();
// Load a Word file
doc.LoadFromFile("C:\\Users\\Administrator\\Desktop\\Bookmarks.docx");
// Get a specific bookmark by its index
Bookmark bookmark = doc.Bookmarks[0];
// Remove the bookmark
doc.Bookmarks.Remove(bookmark);
// Remove all bookmarks at once
// doc.Bookmarks.Clear();
// Save the document.
doc.SaveToFile("RemoveBookmark.docx", FileFormat.Docx2019);
// Dispose resources
doc.Dispose();
}
}
}
Apply for a Temporary License
If you'd like to remove the evaluation message from the generated documents, or to get rid of the function limitations, please request a 30-day trial license for yourself.
In Word documents, you can add time, date, title, reference information, page number, content description and image /logo in header or footer to enrich the document. This article shows how to add headers and footers in C# and VB.NET applications using Spire.Doc for .NET.
Install Spire.Doc for .NET
To begin with, you need to add the DLL files included in the Spire.Doc for .NET package as references in your .NET project. The DLLs files can be either downloaded from this link or installed via NuGet.
- Package Manager
PM> Install-Package Spire.Doc
Add Header and Footer
The table gives a list of main classes, properties and methods used in the operation.
| Name | Description |
| Document Class | Represents a Word document model. |
| Document. LoadFromFile() Method | Load a Word document. |
| Section Class | Represents a section in a Word document. |
| Document.Sections Property | Gets document sections. |
| HeaderFooter Class | Represents a header and footer model for Word. |
| Section.HeadersFooters.Header Property | Gets headers/footers of current section. |
| Paragraph Class | Represents a paragraph in a document. |
| HeaderFooter. AddParagraph() Method | Adds paragraph at end of section. |
| TextRange Class | Represents a text range. |
| Paragraph.AppendText() Method | Appends text to the end of paragraph. |
| Document. SaveToFile() Method | Saves the document to file in Microsoft Word or another file format. |
The following are the steps about adding header and footer.
- Create an instance of Document class.
- Load the sample document using Document.LoadFromFile(string fileName) method.
- Get the specified section of Word Document using Document.Sections Property
- Add Header
- Get header using HeadersFooters.Header property.
- Add paragraph using HeaderFooter. AddParagraph() method and set paragraph alignment.
- Append text using Paragraph.AppendText(string text) method and set font name, size, color ,etc.
- Add Footer
- Get footer using HeadersFooters.Footer proterty.
- Add paragraph and text in footer.
- Save Word document using Document. SaveToFile(string filename, FileFormat fileFormat) method.
- C#
- VB.NET
using Spire.Doc;
using Spire.Doc.Documents;
using System.Drawing;
using Spire.Doc.Fields;
namespace AddHeaderAndFooter
{
class Program
{
static void Main(string[] args)
{
//Create an instance of Document class
Document document = new Document();
//Load a Word document
document.LoadFromFile("input.docx");
//Get the first section of Word Document
Section section = document.Sections[0];
//Get header via HeadersFooters.Header property
HeaderFooter header = section.HeadersFooters.Header;
//Add a paragraph and set paragraph alignment style
Paragraph headerPara = header.AddParagraph();
headerPara.Format.HorizontalAlignment = HorizontalAlignment.Left;
//Append text and set font name, size, color,etc.
TextRange textrange = headerPara.AppendText("E-iceblue Co. Ltd." + "\n Your Office Development Master");
textrange.CharacterFormat.FontName = "Arial";
textrange.CharacterFormat.FontSize = 13;
textrange.CharacterFormat.TextColor = Color.DodgerBlue;
textrange.CharacterFormat.Bold = true;
//Get footer, add paragraph and append text
HeaderFooter footer = section.HeadersFooters.Footer;
Paragraph footerPara = footer.AddParagraph();
footerPara.Format.HorizontalAlignment = HorizontalAlignment.Center;
textrange = footerPara.AppendText("Copyright © 2021 All Rights Reserved.");
textrange.CharacterFormat.Bold = false;
textrange.CharacterFormat.FontSize = 11;
//Save to file
document.SaveToFile("output.docx", FileFormat.Docx);
}
}
}
Imports Spire.Doc
Imports Spire.Doc.Documents
Imports System.Drawing
Imports Spire.Doc.Fields
Namespace AddHeaderAndFooter
Class Program
Private Shared Sub Main(args As String())
'Create an instance of Document class
Dim document As New Document()
'Load a Word document
document.LoadFromFile("input.docx")
'Get the first section of Word Document
Dim section As Section = document.Sections(0)
'Get header via HeadersFooters.Header property
Dim header As HeaderFooter = section.HeadersFooters.Header
'Add a paragraph and set paragraph alignment style
Dim headerPara As Paragraph = header.AddParagraph()
headerPara.Format.HorizontalAlignment = HorizontalAlignment.Left
'Append text and set font name, size, color ,etc.
Dim textrange As TextRange = headerPara.AppendText("E-iceblue Co. Ltd." + vbLf & " Your Office Development Master")
textrange.CharacterFormat.FontName = "Arial"
textrange.CharacterFormat.FontSize = 13
textrange.CharacterFormat.TextColor = Color.DodgerBlue
textrange.CharacterFormat.Bold = True
'Get footer, add paragraph and append text
Dim footer As HeaderFooter = section.HeadersFooters.Footer
Dim footerPara As Paragraph = footer.AddParagraph()
footerPara.Format.HorizontalAlignment = HorizontalAlignment.Center
textrange = footerPara.AppendText("Copyright © 2021 All Rights Reserved.")
textrange.CharacterFormat.Bold = False
textrange.CharacterFormat.FontSize = 11
'Save to file
document.SaveToFile("output.docx", FileFormat.Docx)
End Sub
End Class
End Namespace

Apply for a Temporary License
If you'd like to remove the evaluation message from the generated documents, or to get rid of the function limitations, please request a 30-day trial license for yourself.

Adding images to Word documents programmatically is a common requirement in document automation. Whether you're generating reports, creating invoices, or building dynamic documents, inserting and customizing images can enhance your document's visual appeal.
In this article, we'll explore how to insert images into Word documents using Spire.Doc for .NET, covering local files, byte arrays, and advanced image customization.
- .NET Library for Adding Images to Word
- Insert an Imge from Local to Word
- Insert an Image from a Byte Array to Word
- Further Customize the Image
- Conclusion
.NET Library for Adding Images to Word
Spire.Doc for .NET is a powerful library that enables developers to create, edit, and manipulate Word documents without Microsoft Office dependencies. It provides straightforward methods to insert and format images in Word files.
Key Features:
- Insert images from local storage or byte arrays.
- Adjust image size, rotation, and positioning.
- Control text wrapping around images.
- Support for various image formats (PNG, JPG, BMP, etc.).
To get started, downoad Spire.Doc from our offical website and reference the DLLs in your project. Or, you can install it via NuGet through the following command:
PM> Install-Package Spire.Doc
Insert an Imge from Local to Word
The simplest method for inserting images into a Word document is to load them directly from your file system. Using Spire.Doc's AppendPicture() method, you can easily specify the file path of the image. This method automatically detects the image format and embeds it into the document.
using Spire.Doc;
using Spire.Doc.Documents;
namespace InsertImageFromLocal
{
class Program
{
static void Main(string[] args)
{
// Create a Document object
Document document = new Document();
// Add a section
Section section = document.AddSection();
// Add a paragraph
Paragraph paragraph = section.AddParagraph();
// Append a picture from the local disk
paragraph.AppendPicture("C:\\Users\\Administrator\\Desktop\\MyPic.png");
// Save the document
document.SaveToFile("output.docx", FileFormat.Docx);
// Dispose resources
document.Dispose();
}
}
}

Insert an Image from a Byte Array to Word
In dynamic applications, integrating images from web sources or databases is often essential for enhancing user experience and content relevance.This method downloads images as byte arrays and injecte into the document using a MemoryStream .
By utilizing this technique, developers can dynamically populate documents with up-to-date content, such as logos or product images, directly from online resources or databases.
using Spire.Doc;
using Spire.Doc.Documents;
namespace InsertImageFromByteArray
{
class Program
{
static async System.Threading.Tasks.Task Main(string[] args)
{
// URL of the image
string imageUrl = "https://example.com/image.png";
// Download the image
byte[] imageBytes;
using (HttpClient client = new HttpClient())
{
imageBytes = await client.GetByteArrayAsync(imageUrl);
}
// Create a Document object
Document document = new Document();
// Add a section
Section section = document.AddSection();
// Add a paragraph
Paragraph paragraph = section.AddParagraph();
// Append a picture from the byte array
using (MemoryStream stream = new MemoryStream(imageBytes))
{
paragraph.AppendPicture(stream);
}
// Save the document
document.SaveToFile("output.docx", FileFormat.Docx);
// Dispose resources
document.Dispose();
}
}
}
Further Customize the Image
Professional documents often demand precise image formatting. Spire.Doc offers extensive controls through the DocPicture class, enabling users to manipulate images effectively.
Key features include resizing to fit layouts, rotating for optimal orientation, and text wrapping options that allow text to flow seamlessly around images. Additionally, users can specify spacing and alignment to position images accurately relative to surrounding content.
// Append an image from disk
DocPicture picture = paragraph.AppendPicture("C:\\Users\\Administrator\\Desktop\\MyPic.png");
// Resize the image to 70%
picture.SetScale(70f);
// Rotate the image 10 degrees counterclockwise
picture.Rotation = -10;
// Specify top and bottom distance to 3 units
picture.DistanceTop = picture.DistanceBottom = 3;
// Set the text wrapping style around the image
picture.TextWrappingStyle = TextWrappingStyle.TopAndBottom;
// Center align the paragraph containing the image
paragraph.Format.HorizontalAlignment = HorizontalAlignment.Center;
Conclusion
Using Spire.Doc for .NET simplifies the process of inserting and customizing images in Word documents. Whether pulling images from local storage or online sources, the library provides flexible options to enhance document presentation.
Get a Free License
To fully experience the capabilities of Spire.Doc for .NET without any evaluation limitations, you can request a free 30-day trial license.