Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to set a different picture watermark for each page of Word by C #

2025-02-14 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/01 Report--

This article introduces the knowledge of "how to set a different picture watermark for each page of Word by C#". In the operation of actual cases, many people will encounter such a dilemma. Next, let the editor lead you to learn how to deal with these situations. I hope you can read it carefully and be able to achieve something!

Methods and ideas

Before adding a watermark to each page of the Word, you first need to insert a "continuous" section break after the last character in the body of each page of the Word document, and then add the watermark picture in the header paragraph of each section, and set the coordinate position of the picture, alignment, lining under the text, and so on. Finally, save the document.

Dll introduction method 1

Introduce the Spire.Doc.dll file into the program; download Free Spire.Doc for .NET locally, extract it, and find the Spire.Doc.dll under the BIN folder. Then open solution Explorer in Visual Studio, right-click "reference" and "add reference" to add a reference to the dll file under the local path BIN folder.

Method 2

Install via NuGet. It can be installed in the following two ways:

1. You can open solution Explorer in Visual Studio, right-click "references", "manage NuGet packages", then search for "Free Spire.Doc" and click "install". Wait for the program to be installed.

two。 Copy the following to the PM console installation.

Install-Package FreeSpire.Doc-Version 10.2.0

Code example

When adding image watermarks to each page, you can refer to the following steps:

Create an object of the Document class and load the Word document through the LoadFromFile (string fileName) method.

Gets the specified section through the Document.Sections [] property.

Get the header through the HeadersFooters.Header property, and the HeaderFooter.AddParagraph () method adds a paragraph to the header.

Add the picture to the paragraph through the Paragraph.AppendPicture (string imgFile) method, the DocPicture.VerticalPosition property sets the watermark image position, and the DocPicture.HorizontalAlignment property sets the picture alignment.

Finally, save the document through the Document.SaveToFile (string fileName, FileFormat fileFormat) method.

If you set different image watermarking effects on different pages, just get the corresponding section of the page, and then refer to the method used above to add it.

C#using Spire.Doc;using Spire.Doc.Documents;using Spire.Doc.Fields;namespace ImageWatermark2 {class Program {static void Main (string [] args) {/ / load Word test document Document doc = new Document (); doc.LoadFromFile ("test.docx"); / / get the first section of the document Section section1 = doc.Sections [0] / / define the vertical coordinate position of the watermark image float y = section1.PageSetup.PageSize.Height/3; / / add the image watermark 1 HeaderFooter header1 = section1.HeadersFooters.Header;// to get the header header1.Paragraphs.Clear (); / / delete the paragraph Paragraph para1 = header1.AddParagraph () in the original header format / / add the paragraph DocPicture pic1 = para1.AppendPicture ("logo1.png"); / / add the picture pic1.TextWrappingStyle = TextWrappingStyle.Behind;// and put the picture under the text pic1.VerticalPosition = y; pic1.HorizontalAlignment = ShapeHorizontalAlignment.Center / / set the picture alignment / / similarly set the picture watermark 2 Section section2 = doc.Sections [1] in the header of section 2; HeaderFooter header2 = section2.HeadersFooters.Header; header2.Paragraphs.Clear (); Paragraph para2 = header2.AddParagraph (); DocPicture pic2 = para2.AppendPicture ("logo2.png"); pic2.TextWrappingStyle = TextWrappingStyle.Behind Pic2.VerticalPosition = y; pic2.HorizontalAlignment = ShapeHorizontalAlignment.Center; / / similarly set the picture watermark 3 Section section3 = doc.Sections [2] in the header in section 3; HeaderFooter header3 = section3.HeadersFooters.Header; header3.Paragraphs.Clear (); Paragraph para3 = header3.AddParagraph (); DocPicture pic3 = para3.AppendPicture ("logo3.png") Pic3.TextWrappingStyle = TextWrappingStyle.Behind; pic3.VerticalPosition = y; pic3.HorizontalAlignment = ShapeHorizontalAlignment.Center; / / Save document doc.SaveToFile ("DifferentImageWatermark.docx", FileFormat.Docx2013); System.Diagnostics.Process.Start ("DifferentImageWatermark.docx") } vb.netImports Spire.DocImports Spire.Doc.DocumentsImports Spire.Doc.FieldsNamespace ImageWatermark2 Class Program Private Shared Sub Main (args As String ()) 'load Word test document Dim doc As New Document () doc.LoadFromFile ("test.docx")' get the first section of the document Dim section1 As Section = doc.Sections (0) 'define the vertical coordinate position of the watermark image Dim y As Single = section1.PageSetup.PageSize.Height / 3' add the image watermark 1 Dim header1 As HeaderFooter = section1.HeadersFooters.Header 'get header header1.Paragraphs.Clear ()' delete the paragraph Dim para1 As Paragraph = header1 in the original header format .AddParagraph ()'re-add paragraph Dim pic1 As DocPicture = para1.AppendPicture ("logo1.png") 'add picture pic1.TextWrappingStyle = TextWrappingStyle.Behind' picture under the text pic1.VerticalPosition = y pic1.HorizontalAlignment = ShapeHorizontalAlignment.Center 'set picture alignment 'similarly set the image watermark 2 Dim section2 As Section = doc.Sections (1) Dim header2 As HeaderFooter = section2.HeadersFooters.Header header2.Paragraphs.Clear () Dim para2 As Paragraph = header2.AddParagraph () Dim pic2 As DocPicture = para2.AppendPicture ("logo2.png") pic2.TextWrappingStyle = TextWrappingStyle.Behind pic2.VerticalPosition in the header of section 2 = y pic2.HorizontalAlignment = ShapeHorizontalAlignment.Center 'set the picture watermark in the header in section 3 Dim section3 As Section = doc.Sections (2) Dim header3 As HeaderFooter = section3.HeadersFooters.Header header3.Paragraphs.Clear () Dim para3 As Paragraph = header3.AddParagraph () Dim pic3 As DocPicture = para3.AppendPicture ("logo3.png") Pic3.TextWrappingStyle = TextWrappingStyle.Behind pic3.VerticalPosition = y pic3.HorizontalAlignment = ShapeHorizontalAlignment.Center 'Save document doc.SaveToFile ("DifferentImageWatermark.docx" FileFormat.Docx2013) System.Diagnostics.Process.Start ("DifferentImageWatermark.docx") End Sub End ClassEnd Namespace

As shown in the figure, each page can show a different image watermarking effect:

This is the end of the content of "how to set a different picture watermark for each page of Word by C#". Thank you for reading. If you want to know more about the industry, you can follow the website, the editor will output more high-quality practical articles for you!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report