Is it possible to insert image on each page of Word document?
The Word document is a flow document in which contents will not be preserved page by page; instead, the contents will be preserved sequentially, section by section. Each section may extend to various pages based on its contents, such as tables, text, images, etc.
Word viewer/editor renders the contents of the Word document page by page dynamically when opened for viewing or editing and this page wise rendered information will not be preserved in the document level as per the Word file format specification. Whereas Essential® DocIO is a non-UI component that provides a full-fledged document object model to manipulate the Word document contents.
In the following ways can insert image in a Word document.
- Insert image in Header/Footer of each section.
- Insert image in each section of Word document.
- Insert image as picture Watermark in Word document.
Please refer the below UG links to know more about the above-mentioned scenarios.
Working with headers and footers
Insert image in the Header/Footer of each section
The following code examples show how to insert an image in the Header and Footer of each section in an existing Word document using Essential® DocIO.
C#
// Loads an existing Word document
WordDocument wordDocument = new WordDocument("Essential DocIO.docx");
// Iterates each section and appends an image in the header of each section
// It will display the image on each page of the Word document
foreach (WSection section in wordDocument.Sections)
{
// Creates a new WPicture instance and gets the Image from the file
WPicture picture = new WPicture(wordDocument);
picture.LoadImage(Image.FromFile("AdventureCycle.jpg"));
picture.Height = 50;
picture.Width = 250;
// Inserts the image in the Header and Footer of the section
section.HeadersFooters.Header.AddParagraph().ChildEntities.Add(picture);
}
// Saves the Word document and closes the WordDocument instance
wordDocument.Save("Sample.docx");
wordDocument.Close();
VB
' Loads an existing Word document
Dim wordDocument As WordDocument = New WordDocument("Essential DocIO.docx")
' Iterates each section and appends an image in the header of each section
' It will display the image on each page of the Word document
For Each section As WSection In wordDocument.Sections
' Creates a new WPicture instance and gets the Image from the file
Dim picture As WPicture = New WPicture(wordDocument)
picture.LoadImage(System.Drawing.Image.FromFile("AdventureCycle.jpg"))
picture.Height = 50
picture.Width = 250
' Inserts the image in the Header and Footer of the section
section.HeadersFooters.Header.AddParagraph().ChildEntities.Add(picture)
Next
' Saves the Word document and closes the WordDocument instance
wordDocument.Save("Sample.docx")
wordDocument.Close()
Insert image in each section of the Word document.
The following code examples show how to insert an image in each section of an existing Word document using Essential® DocIO.
C#
// Loads an existing Word document
WordDocument wordDocument = new WordDocument("Essential DocIO.docx");
// Iterates each section and appends an image in each section
// It will display the image in each section of the Word document
foreach (WSection section in wordDocument.Sections)
{
// Creates a new WPicture instance and gets the Image from the file
WPicture picture = new WPicture(wordDocument);
picture.LoadImage(Image.FromFile("AdventureCycle.jpg"));
picture.Height = 50;
picture.Width = 250;
// Gets a paragraph using the index and adds the picture into that paragraph
WParagraph paragraph = section.Body.LastParagraph;
if (paragraph != null)
paragraph.ChildEntities.Add(picture);
}
// Saves the Word document and closes the WordDocument instance
wordDocument.Save("Sample.docx");
wordDocument.Close();
VB
' Loads an existing Word document
Dim wordDocument As WordDocument = New WordDocument("Essential DocIO.docx")
' Iterates each section and appends an image in each section
' It will display the image in each section of the Word document
For Each section As WSection In wordDocument.Sections
' Creates a new WPicture instance and gets the Image from the file
Dim picture As WPicture = New WPicture(wordDocument)
picture.LoadImage(System.Drawing.Image.FromFile("AdventureCycle.jpg"))
picture.Height = 50
picture.Width = 250
Dim paragraph As WParagraph = section.Body.LastParagraph
If paragraph IsNot Nothing Then
paragraph.ChildEntities.Add(picture)
End If
Next
' Saves the Word document and closes the WordDocument instance
wordDocument.Save("Sample.docx")
wordDocument.Close()
Insert image as a picture watermark in the Word document
The following code examples show how to insert an image as a picture watermark in an existing Word document using Essential® DocIO.
C#
// Loads an existing Word document
WordDocument wordDocument = new WordDocument("Essential DocIO.docx");
// Gets the image from the file
Image image = Image.FromFile("AdventureCycle.jpg");
// Sets the picture watermark to the document
wordDocument.Watermark = new PictureWatermark(image, true);
// Saves the Word document and closes the WordDocument instance
wordDocument.Save("Sample.docx");
wordDocument.Close();
VB
' Loads an existing Word document
Dim wordDocument As WordDocument = New WordDocument("Essential DocIO.docx")
' Gets the image from the file
Dim image As System.Drawing.Image = System.Drawing.Image.FromFile("AdventureCycle.jpg")
' Sets the picture watermark to the document
wordDocument.Watermark = New PictureWatermark(image, True)
' Saves the Word document and closes the WordDocument instance
wordDocument.Save("Sample.docx")
wordDocument.Close()
Please find the complete sample with the above-mentioned use cases at the below link:
Refer the Assemblies required section to refer and run the sample without error.