Aspose Word(.doc,docx)文件加水印


官方文檔地址:https://docs.aspose.com/display/wordsnet/Home

官方Demo代碼:https://github.com/aspose-words/Aspose.Words-for-.NET

2、Word文件加水印代碼

注意:除了引用Aspose.Words.dll,還要用Nuget安裝System.Text.Encoding.CodePages和SkiaSharp

 1 using Aspose.Words;
 2 using Aspose.Words.Drawing;
 3 using System;
 4 using System.Drawing;
 5 namespace WordWatermark
 6 {
 7     class Program
 8     {
 9         static void Main(string[] args)
10         {
11             var sourceFilePath = @"F:\1.doc";
12             var objectFilePath = @"F:\2.doc";
13             //Aspose.Words.License lic = new Aspose.Words.License();
14             //lic.SetLicense("Aspose.Total.lic");破解版不用設置license
15             // open word file
16             var doc = new Document(sourceFilePath);
17             InsertWatermarkText(doc, "hello world!");
18             doc.Save(objectFilePath);
19         }
20         private static void InsertWatermarkText(Document doc, string watermarkText)
21         {
22             // 創建一個水印形狀。這將是一個WordArt形狀。
23             // 可以隨意嘗試其他形狀類型作為水印。
24             Shape watermark = new Shape(doc, ShapeType.TextPlainText);
25             watermark.Name = "WaterMark";
26             // 設置水印文本。
27             watermark.TextPath.Text = watermarkText;
28             watermark.TextPath.FontFamily = "Arial";
29             watermark.Width = 500;
30             watermark.Height = 100;
31             // 文本將從左下角指向右上角。
32             watermark.Rotation = -40;
33             // 如果需要純黑色文本,請刪除以下兩行。
34             watermark.Fill.Color = Color.Gray; // 嘗試LightGray得到更多文字風格的水印
35             watermark.StrokeColor = Color.Gray; // 嘗試LightGray得到更多文字風格的水印
36             // 將水印放在頁面中心。
37             watermark.RelativeHorizontalPosition = RelativeHorizontalPosition.Page;
38             watermark.RelativeVerticalPosition = RelativeVerticalPosition.Page;
39             watermark.WrapType = WrapType.None;
40             watermark.VerticalAlignment = VerticalAlignment.Center;
41             watermark.HorizontalAlignment = HorizontalAlignment.Center;
42             //創建一個新的段落,並附加水印到這段。
43             Paragraph watermarkPara = new Paragraph(doc);
44             watermarkPara.AppendChild(watermark);
45             //在每個文檔部分的所有標題中插入水印。
46             foreach (Section sect in doc.Sections)
47             {
48               //每個部分可能有三個不同的標題,因為我們需要
49               //水印將出現在所有頁面,插入到所有頁眉。
50                 InsertWatermarkIntoHeader(watermarkPara, sect, HeaderFooterType.HeaderPrimary);
51                 InsertWatermarkIntoHeader(watermarkPara, sect, HeaderFooterType.HeaderFirst);
52                 InsertWatermarkIntoHeader(watermarkPara, sect, HeaderFooterType.HeaderEven);
53             }
54         }
55         private static void InsertWatermarkIntoHeader(Paragraph watermarkPara, Section sect, HeaderFooterType headerType)
56         {
57             HeaderFooter header = sect.HeadersFooters[headerType];
58             if (header == null)
59             {
60                 // There is no header of the specified type in the current section, create it.
61                 header = new HeaderFooter(sect.Document, headerType);
62                 sect.HeadersFooters.Add(header);
63             }
64             //在標題中插入一個水印的克隆。
65             header.AppendChild(watermarkPara.Clone(true));
66         }
67     }
68 }

 

 

3、本文項目代碼下載

下載地址:https://www.cjavapy.com/download/5be40359dc72d915fc310687/

相關文檔:.NET Core Aspose Word(.doc,docx)轉成pdf文件

                  VS(Visual Studio)中Nuget的使用

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM