使用Aspose.Words组件给word加水印

使用Aspose.Words组件给word加水印

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
/// <summary>
       /// Inserts a watermark into a document.
       /// </summary>
       /// <param name="doc">The input document.</param>
       /// <param name="watermarkText">Text of the watermark.</param>
       private static void InsertWatermarkText(Document doc, string watermarkText)
       {
           // Create a watermark shape. This will be a WordArt shape.
           // You are free to try other shape types as watermarks.
           Shape watermark = new Shape(doc, ShapeType.TextPlainText);
            
           // Set up the text of the watermark.
           watermark.TextPath.Text = watermarkText;
           watermark.TextPath.FontFamily = "Arial";
           watermark.Width = 500;
           watermark.Height = 100;
           // Text will be directed from the bottom-left to the top-right corner.
           watermark.Rotation = -40;
           // Remove the following two lines if you need a solid black text.
           watermark.Fill.Color = Color.Gray; // Try LightGray to get more Word-style watermark
           watermark.StrokeColor = Color.Gray; // Try LightGray to get more Word-style watermark
 
           // Place the watermark in the page center.
           watermark.RelativeHorizontalPosition = RelativeHorizontalPosition.Page;
           watermark.RelativeVerticalPosition = RelativeVerticalPosition.Page;
           watermark.WrapType = WrapType.None;
           watermark.VerticalAlignment = VerticalAlignment.Center;
           watermark.HorizontalAlignment = HorizontalAlignment.Center;
 
           // Create a new paragraph and append the watermark to this paragraph.
           Paragraph watermarkPara = new Paragraph(doc);
           watermarkPara.AppendChild(watermark);
 
           // Insert the watermark into all headers of each document section.
           foreach (Section sect in doc.Sections)
           {
               // There could be up to three different headers in each section, since we want
               // the watermark to appear on all pages, insert into all headers.
               InsertWatermarkIntoHeader(watermarkPara, sect, HeaderFooterType.HeaderPrimary);
               InsertWatermarkIntoHeader(watermarkPara, sect, HeaderFooterType.HeaderFirst);
               InsertWatermarkIntoHeader(watermarkPara, sect, HeaderFooterType.HeaderEven);
           }
       }
 
       private static void InsertWatermarkIntoHeader(Paragraph watermarkPara, Section sect, HeaderFooterType headerType)
       {
           HeaderFooter header = sect.HeadersFooters[headerType];
 
           if (header == null)
           {
               // There is no header of the specified type in the current section, create it.
               header = new HeaderFooter(sect.Document, headerType);
               sect.HeadersFooters.Add(header);
           }
 
           // Insert a clone of the watermark into the header.
           header.AppendChild(watermarkPara.Clone(true));
       }

 调用方法:

1
2
3
4
5
6
string exeDir = Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location) + Path.DirectorySeparatorChar;
           string dataDir = new Uri(new Uri(exeDir), @"../../Data/").LocalPath;
 
           Document doc = new Document(dataDir + "TestFile.doc");
           InsertWatermarkText(doc, "CONFIDENTIAL");
           doc.Save(dataDir + "TestFile Out.doc");

posted on   itjeff  阅读(1573)  评论(0编辑  收藏  举报

编辑推荐:
· AI与.NET技术实操系列(二):开始使用ML.NET
· 记一次.NET内存居高不下排查解决与启示
· 探究高空视频全景AR技术的实现原理
· 理解Rust引用及其生命周期标识(上)
· 浏览器原生「磁吸」效果!Anchor Positioning 锚点定位神器解析
阅读排行:
· DeepSeek 开源周回顾「GitHub 热点速览」
· 物流快递公司核心技术能力-地址解析分单基础技术分享
· .NET 10首个预览版发布:重大改进与新特性概览!
· AI与.NET技术实操系列(二):开始使用ML.NET
· .NET10 - 预览版1新功能体验(一)

导航

< 2025年3月 >
23 24 25 26 27 28 1
2 3 4 5 6 7 8
9 10 11 12 13 14 15
16 17 18 19 20 21 22
23 24 25 26 27 28 29
30 31 1 2 3 4 5
点击右上角即可分享
微信分享提示