官方文档地址: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文件