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 like table, 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 Header/Footer of each section.
The following code examples shows how to insert image in Header and Footer of each section in existing Word document using Essential DocIO.
C#
//Loads an existing Word document WordDocument wordDocument = new WordDocument("Essential DocIO.docx"); //Iterates each section and appends image in header of each section //It will display image in each page of Word document foreach (WSection section in wordDocument.Sections) { //Creates new WPicture instance and gets Image from file WPicture picture = new WPicture(wordDocument); picture.LoadImage(Image.FromFile("AdventureCycle.jpg")); picture.Height = 50; picture.Width = 250; //Insert image in Header and Footer of section section.HeadersFooters.Header.AddParagraph().ChildEntities.Add(picture); } //Saves Word document and closes 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 image in header of each section 'It will display image in each page of Word document For Each section As WSection In wordDocument.Sections 'Creates new WPicture instance and gets Image from file Dim picture As WPicture = New WPicture(wordDocument) picture.LoadImage(System.Drawing.Image.FromFile("AdventureCycle.jpg")) picture.Height = 50 picture.Width = 250 'Insert image in Header and Footer of section section.HeadersFooters.Header.AddParagraph().ChildEntities.Add(picture) Next 'Saves Word document and closes WordDocument instance wordDocument.Save("Sample.docx") wordDocument.Close()
Insert image in each section of Word document.
The following code examples shows how to insert image in each section of existing Word document using Essential DocIO.
C#
//Loads an existing Word document WordDocument wordDocument = new WordDocument("Essential DocIO.docx"); //Iterates each section and appends image in each section //It will display image in each section of Word document foreach (WSection section in wordDocument.Sections) { //Creates new WPicture instance and gets Image from file WPicture picture = new WPicture(wordDocument); picture.LoadImage(Image.FromFile("AdventureCycle.jpg")); picture.Height = 50; picture.Width = 250; //Gets paragraph using index and adds picture into that paragraph WParagraph paragraph = section.Body.LastParagraph; if (paragraph != null) paragraph.ChildEntities.Add(picture); } //Saves Word document and closes 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 image in each section 'It will display image in each section of Word document For Each section As WSection In wordDocument.Sections 'Creates new WPicture instance and gets Image from 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 Word document and closes WordDocument instance wordDocument.Save("Sample.docx") wordDocument.Close()
Insert image as picture Watermark in Word document.
The following code examples shows how to insert image as picture watermark in existing Word document using Essential DocIO.
C#
//Loads an existing Word document WordDocument wordDocument = new WordDocument("Essential DocIO.docx"); //Gets the image from file Image image = Image.FromFile("AdventureCycle.jpg"); //Sets the picture watermark to document wordDocument.Watermark = new PictureWatermark(image, true); //Saves Word document and closes 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 file Dim image As System.Drawing.Image = System.Drawing.Image.FromFile("AdventureCycle.jpg") 'Sets the picture watermark to document wordDocument.Watermark = New PictureWatermark(image, True) 'Saves Word document And closes WordDocument instance wordDocument.Save("Sample.docx") wordDocument.Close()
Please find the complete sample with above mentioned use cases from the below link.
Refer the Assemblies required section to refer and run the sample without error.