开发手册 欢迎您!
软件开发者资料库

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

利用Aspose组件给word文件加水印,性能比较好,也比较快,格式兼容性也挺好。另外服务器或PC上不用安装msoffice word软件就可以使用。下面分享一下.NET Core中调用Aspose组件加水印代码。

1、Aspose组件下载

Aspose下载地址:https://products.aspose.com/words/net

破解版下载地址:https://download.csdn.net/download/yedajiang44/10667863 (aspose.words 18.7破解版含net 和 .net core 2.0版本)

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

官方Demo代码:Words-for-.NET" rel="nofollow">https://github.com/aspose-words/Aspose.Words-for-.NET

2、添加引用

1)项目直接添加Aspose.Words.dll的引用

2)使用Nuget引用System.Text.Encoding.CodePages和SkiaSharp

使用Nuget的界面的管理器搜索 "System.Text.Encoding.CodePages" 和 "SkiaSharp" => 找到分别点击"安装"。

相关文档:VS(Visual Studio)中Nuget的使用

3、Word文件加水印代码

Word文件通过Aspose.Words组件添加水印并保存,相关示例代码如下:

using Aspose.Words;using Aspose.Words.Drawing;using System;using System.Drawing;namespace WordWatermark{    class Program    {        static void Main(string[] args)        {            var sourceFilePath = @"F:\1.doc";            var objectFilePath = @"F:\2.doc";            //Aspose.Words.License lic = new Aspose.Words.License();            //lic.SetLicense("Aspose.Total.lic");破解版不用设置license            // open word file            var doc = new Document(sourceFilePath);            InsertWatermarkText(doc, "hello world!");            doc.Save(objectFilePath);        }        private static void InsertWatermarkText(Document doc, string watermarkText)        {            // 创建一个水印形状。这将是一个WordArt形状。            // 可以随意尝试其他形状类型作为水印。            Shape watermark = new Shape(doc, ShapeType.TextPlainText);            watermark.Name = "WaterMark";            // 设置水印文本。            watermark.TextPath.Text = watermarkText;            watermark.TextPath.FontFamily = "Arial";            watermark.Width = 500;            watermark.Height = 100;            // 文本将从左下角指向右上角。            watermark.Rotation = -40;            // 如果需要纯黑色文本,请删除以下两行。            watermark.Fill.Color = Color.Gray; // 尝试LightGray得到更多文字风格的水印            watermark.StrokeColor = Color.Gray; // 尝试LightGray得到更多文字风格的水印            // 将水印放在页面中心。            watermark.RelativeHorizontalPosition = RelativeHorizontalPosition.Page;            watermark.RelativeVerticalPosition = RelativeVerticalPosition.Page;            watermark.WrapType = WrapType.None;            watermark.VerticalAlignment = VerticalAlignment.Center;            watermark.HorizontalAlignment = HorizontalAlignment.Center;            //创建一个新的段落,并附加水印到这段。            Paragraph watermarkPara = new Paragraph(doc);            watermarkPara.AppendChild(watermark);            //在每个文档部分的所有标题中插入水印。            foreach (Section sect in doc.Sections)            {              //每个部分可能有三个不同的标题,因为我们需要              //水印将出现在所有页面,插入到所有页眉。                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);            }            //在标题中插入一个水印的克隆。            header.AppendChild(watermarkPara.Clone(true));        }    }}

4、本文项目代码下载

下载地址:https://www.wonhero.com/download/5be40359dc72d915fc310687/

相关文档:.NET Core Aspose Word(.doc,docx)转成pdf文件

                  VS(Visual Studio)中Nuget的使用