需求:在一些重要的Word文档需要打印时,添加水印以明出处。

方案:使用Aspose组件给word文档

代码:干货如下

/// <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.
Aspose.Words.Drawing.Shape watermark = new Aspose.Words.Drawing.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 = System.Drawing.Color.Gray; // Try LightGray to get more Word-style watermark
watermark.StrokeColor = System.Drawing.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));
}

Aspose.Words给word文档加水印的更多相关文章

  1. 如何在程序中给word文档加上标和下标

    如何在程序中给word文档加上标和下标 上标或下标是一个小于普通行格式的数字,图形,标志或者指示通常它的设置与行相比偏上或偏下.下标通常显示于或者低于基准线,而上标则高于.上标和下标通常被用于表达公式 ...

  2. WPF使用Aspose.Words导出Word文档

    一.创建Word文档模板 分析需要导出的word文档,将固定的内容和由程序生成的内容分开; 创建一个word(例如:Template.doc)文档,将固定的内容按照一定的格式写入当前文档中; 打开Te ...

  3. 使用Aspose.Words将Word文档转换为Tiff格式图片文件

    用Aspose组件的优点是操作Word文档不需要安装Office就可以实现. 首先需要引用Aspose.Words.dll,链接地址:链接:https://pan.baidu.com/s/1rJvjp ...

  4. C#使用Aspose.Words操作word文档

    最近接到个需求,由于客服这边要导出大量有一定规则的word文件,里面的内容希望系统自动填充,例如 这里我使用Aspose.Words.dll这个类库, 1.首先,我们需要创建模板文件,毕竟有规则的东西 ...

  5. Aspose.Words提取word文档中的图片文件

    /// <summary> /// 提取word中的图片 /// </summary> /// <param name="filePath">w ...

  6. ASPOSE.Cells & ASPOSE.Words 操纵Excel和Word文档的 .NET Core 实例

    Aspose.Total是Aspose公司旗下的最全的一套office文档管理方案,它提供的原生API可以对Word.Excel.PDF.Powerpoint.Outlook.CAD.图片.3D.ZI ...

  7. C# 给word文档添加水印

    和PDF一样,在word中,水印也分为图片水印和文本水印,给文档添加图片水印可以使文档变得更为美观,更具有吸引力.文本水印则可以保护文档,提醒别人该文档是受版权保护的,不能随意抄袭.前面我分享了如何给 ...

  8. C# 合并及拆分Word文档

    本文简要分析一下如何如何使用C#简单实现合并和拆分word文档.平时我们在处理多个word文档时,可能会想要将两个文档合并为一个,或者是将某个文档的一部分添加到另一个文档中,有的时候也会想要将文档拆分 ...

  9. 使用Aspose.word (Java) 填充word文档数据(包含图片填充)

    Aspose填充word数据 本文介绍了如何使用aspose进行word文档的生成,并提供了工具类供参考. 有问题欢迎 call 微信:905369866,小弟尽力而为..毕竟这玩意没吃透. 目录 A ...

随机推荐

  1. Springmvc配置时间日期转换

    1.局部日期转换 @Controller public class ProductController{ @RequestMapping(value="/test/springmvc.do& ...

  2. Pandas基本功能之选取索引和过滤

    索引.选取和过滤 大部分的查询用法 类型 说明 obj[val] 选取DataFrame的单个列或一组列 obj.ix[val] 选取DataFrame的单个行或一组行 obj.ix[:,val] 选 ...

  3. .resources文件转为可视化.resx文件

    ResGen.exe启动闪退.--方法不对 参考:http://www.opdown.com/soft/101205.html 在cmd中启动ResGen.exe. 打开cmd:输入 C:\Windo ...

  4. 2019年华南理工大学程序设计竞赛(春季赛)-C-六学家的困惑

    题目链接:https://ac.nowcoder.com/acm/contest/625/C 题意:给定两个字符串,每次只能从两个字符串的两端取字符,求依次取字符后所构成的数字最大为多少. 思路:思路 ...

  5. JETTY+NGINX

    一.Jetty搭建 1)上传jetty.tar,并且解压(支持war和文件夹) [root@localhost home]# tar -xvf jetty.tar [root@localhost ho ...

  6. AssetBundle Manager and Example Scenes

    示例 1:加载资源 使用 “Asset/AssetBundles/Simulation Mode” 菜单打开模拟模式 打开 “AssetBundleSample/Scenes/AssetLoader” ...

  7. bulk

    bulk - 必应词典 美[bʌlk]英[bʌlk] n.大部分:主体:(大)体积:大(量) v.扩展:增大:堆积起来:形成大块 网络散装:大批:大量 变形复数:bulks:现在分词:bulking: ...

  8. [剑指Offer]59-队列的最大值(题目二待补)

    题目一:滑动窗口的最大值 题目链接 https://www.nowcoder.com/practice/1624bc35a45c42c0bc17d17fa0cba788?tpId=13&tqI ...

  9. 有关html5的history api

    从Ajax翻页的问题说起 请想象你正在看一个视频下面的评论,在翻到十几页的时候,你发现一个写得稍长,但非常有趣的评论.正当你想要停下滚轮细看的时候,手残按到了F5.然后,页面刷新了,评论又回到了第一页 ...

  10. day 20 类与类之间的关系,继承2

    类与类之间的关系: 依赖关系 关联关系 聚合关系 组合关系 依赖关系: 大象与冰箱的例子 # 依赖关系,冰箱依赖大象打开 class Elephant: def __init__(self,name) ...