word font library:C# Set different text watermarks for each page of Word-Font Tutorial免费ppt模版下载-道格办公

C# Set different text watermarks for each page of Word

When setting the watermark in Word, you can use the preset text or custom text to set the watermark effect, but usually when adding the watermark effect, it will All pages are set to a uniform effect. If you need to set a different watermark effect for each page or a certain page, you can refer to the method in this article. Below, we will take the C# code as an example to introduce in detail how to set different text watermark effects for each page of Word.


Methodology

Before adding a text watermark to each page of Word, you first need to add a text watermark to the Word documentEvery pageAfter the last character of the textInsert a "continuous" section break , then Add a WordArt shape to the header paragraph of each section, and set the shape size, alignment, etc. Finally save the document.

dll reference

Method 1

Introduce Spire.Doc into the program .dll file; download Spire.Doc for .NET locally, unzip it, and find Spire.Doc.dll under the BIN folder. Then open the "Solution Explorer" in Visual Studio, right-click "References", "Add Reference", and add references to the dll files in the BIN folder of the local path to the program.

Method 2

Install via NuGet. There are two ways to install:

1. Can be used in Visual Studio Open the "Solution Explorer", right-click "References", "Manage NuGet Packages", then search for "Spire.Doc", click "Install". Wait for the program installation to complete.

2. Copy the following into PM console installation.

Install-Package Spire.Doc -Version 10.1.14

< br>

Code example

When adding a text watermark to each page, you can refer to the following steps:

  • CreateDocumentClass object, and passed LoadFromFile(string fileName) method to load a Word document.
  • Pass< span style="color: #000000; --tt-darkmode-color: #A3A3A3;">Document.Sections[] The property gets the specified section.
  • Pass< span style="color: #000000; --tt-darkmode-color: #A3A3A3;">HeadersFooters.HeaderProperty to get header,HeaderFooter.AddParagraph() method to add a paragraph to the header.
  • Create< span style="color: #000000; --tt-darkmode-color: #A3A3A3;">ShapeObject class object, and pass in parameters to set the shape type to TextPlainText type of WordArt. And call the method to set the WordArt style, such as the height, width, rotation, color, alignment, etc. of the WordArt.
  • use< span style="color: #000000; --tt-darkmode-color: #A3A3A3;">DocumentObjectCollection.Add(IDocumentObject) method adds WordArt to a paragraph.
  • Finally, passDocument.SaveToFile(string fileName, FileFormat fileFormat) method to save the document.

To set different text watermark effects on different pages, you only need to obtain 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;using System.Drawing;namespace TextWatermark2{ 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 text float y = section1.PageSetup.PageSize.Height/3; //Add text watermark 1 HeaderFooter header1 = section1.HeadersFooters.Header;//Get the header header1.Paragraphs.Clear();//Delete the paragraph Paragraph para1 in the original header format = header1.AddParagraph();//Re-add paragraph//Add WordArt and set size ShapeObject shape1 = new ShapeObject(doc, ShapeType.TextPlainText); shape1.Width = 362; shape1.Height = 118; //Set WordArt Text content, position and style (i.e. text watermark) shape1.Rotation = 315; shape1.WordArt.Text = "internal use"; shape1.FillColor = Color.ForestGreen; shape1.LineStyle = ShapeLineStyle.Single; shape1.StrokeColor = Color .ForestGreen; shape1.StrokeWeight = 0.5; shape1.VerticalPosition = y; shape1.HorizontalAlignment = ShapeHorizontalAlignment.Center; para1.ChildObjects.Add(shape1); //Set the text watermark 2 in the header of the second section in the same way Section section2 = doc.Sections[1]; HeaderFooter header2 = section2.HeadersFooters.Header; header2.Paragraphs.Clear(); Paragraph para2 = header2.AddParagraph(); ShapeObject shape2 = new ShapeObject(doc, ShapeType.TextPlainText); shape2.Width = 362; shape2.Height = 118; shape2.Rotation = 315; shape2.WordArt.Text = "Top Secret"; shape2.FillColor = Color.HotPink; shape2.LineStyle = ShapeLineStyle.Single; shape2.StrokeColor = Color.HotPink; shape2 .StrokeWeight = 0.5; shape2.VerticalPosition = y; shape2.HorizontalAlignment = ShapeHorizontalAlignment.Center; para2.ChildObjects.Add(shape2); //Set the text watermark in the header of the third section in the same way 3 Section section3 = doc. Sections[2]; HeaderFooter header3 = section3.HeadersFooters.Header; header3.Paragraphs.Clear(); Paragraph para3 = header3.AddParagraph(); ShapeObject shape3 = new ShapeObject(doc, ShapeType.TextPlainText); shape3.Width = 362; shape3.Height = 118; shape3.Rotation = 315; shape3.WordArt.Text = "No circulation"; shape3.FillColor = Color.DarkOrange; shape3.LineStyle = ShapeLineStyle.Single; shape3.StrokeColor = Color.DarkOrange; shape3.StrokeWeight = 0.5; shape3.VerticalPosition = y; shape3.HorizontalAlignment = ShapeHorizontalAlignment.Center; para3.ChildObjects.Add(shape3); //Save the document doc.SaveToFile("DifferentTextWatermark.docx", FileFormat.Docx2013); System.Diagnostics.Process .Start("DifferentTextWatermark.docx"); } }}

VB.NET

Imports Spire.DocImports Spire.Doc.DocumentsImports Spire.Doc.FieldsImports System.DrawingNamespace TextWatermark2 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 text Dim y As Single = section1.PageSetup.PageSize.Height / 3 'Add Text watermark 1 Dim header1 As HeaderFooter = section1.HeadersFooters.Header 'Get header header1.Paragraphs.Clear() 'Delete the paragraph in the original header format Dim para1 As Paragraph = header1.AddParagraph() 'Re-add paragraph'Add art Dim shape1 As New ShapeObject(doc, ShapeType.TextPlainText) shape1.Width = 362 shape1.Height = 118 'Set the content, position and style of the WordArt text (that is, the text watermark) shape1.Rotation = 315 shape1.WordArt .Text = "Internal use" shape1.FillColor = Color.ForestGreen shape1.LineStyle = ShapeLineStyle.[Single] shape1.StrokeColor = Color.ForestGreen shape1.StrokeWeight = 0.5 shape1.VerticalPosition = y shape1.HorizontalAlignment = ShapeHorizontalAlignment. Center para1. ChildObjects .Add(shape1) 'Similarly set the text watermark 2 in the header of the second section Dim section2 As Section = doc.Sections(1) Dim header2 As HeaderFooter = section2.HeadersFooters.Header header2.Paragraphs.Clear() Dim para2 As Paragraph = header2.AddParagraph() Dim shape2 As New ShapeObject(doc, ShapeType.TextPlainText) shape2.Width = 362 shape2.Height = 118 shape2.Rotation = 315 shape2.WordArt.Text = "Top Secret" shape2.FillColor = Color. HotPink shape2.LineStyle = ShapeLineStyle.[Single] shape2.StrokeColor = Color.HotPink shape2.StrokeWeight = 0.5 shape2.VerticalPosition = y shape2.HorizontalAlignment = ShapeHorizontalAlignment.Center para2.ChildObjects.Add(shape2) 'same Management settings in the third section Text watermark in header3 Dim section3 As Section = doc.Sections(2) Dim header3 As HeaderFooter = section3.HeadersFooters.Header header3.Paragraphs.Clear() Dim para3 As Paragraph = header3.AddParagraph() Dim shape3 As New ShapeObject(doc, ShapeType.TextPlainText) shape3.Width = 362 shape3.Height = 118 shape3.Rotation = 315 shape3.WordArt.Text = "No circulation" shape3.FillColor = Color.DarkOrange shape3.LineStyle = ShapeLineStyle.[Single] shape3 .StrokeColor = Color.DarkOrange shape3.StrokeWeight = 0.5 shape3.VerticalPosition = y shape3.HorizontalAlignment = ShapeHorizontalAlignment.Center para3.ChildObjects.Add(shape3) 'Save document doc.SaveToFile("DifferentTextWatermark.docx", FileFormat.Docx 2013) System. Diagnostics.Process.Start("DifferentTextWatermark.docx") End Sub End ClassEnd Namespace

As shown in the figure, each page can display different text watermark effects:

Articles are uploaded by users and are for non-commercial browsing only. Posted by: Lomu, please indicate the source: https://www.daogebangong.com/en/articles/detail/C%20Set%20different%20text%20watermarks%20for%20each%20page%20of%20Word.html

Like (810)
Reward 支付宝扫一扫 支付宝扫一扫
single-end

Related Suggestion