Aspose.Words是一款先进的文档处理控件,在不使用Microsoft Words的情况下,它可以使用户在各个应用程序中执行各种文档处理任务,其中包括文档的生成、修改、渲染、打印,文档格式转换和邮件合并等文档处理。此外,Aspose.Words支持DOC,OOXML,RTF,HTML,OpenDocument, PDF, XPS, EPUB和其他格式。

有时你需要在一个Word文档中插入一个水印,例如如果你想打印草稿文档或将其标记为机密。

在Microsoft Word中,您可以使用插入水印命令快速插入水印。没有多少人使用这个命令认识到这样的水印只是一个形状与文本一起插入到页眉或页脚,或在页面的中心位置。

而在Aspose.Words中,没有单一的“插入水印”命令就像Microsoft Word,它很容易将

何形状或图像插入到页眉或页脚,从而创建一个任何可以想象类型的水印。

Example

把水印插入一个Word文档。

C#
 using System;
 using System.Drawing;
 using System.IO;
using System.Reflection;

using Aspose.Words;
 using Aspose.Words.Drawing;
 using Aspose.Words.Fields;

namespace AddWatermark
 {
 public class Program
 {
 public static void Main(string[] args)
 {
 // Sample infrastructure.
 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");
 }

/// <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));
 }
 }
 }

Visual BasicImports Microsoft.VisualBasicImports SystemImports System.DrawingImports System.IOImports System.Reflection
Imports Aspose.WordsImports Aspose.Words.DrawingImports Aspose.Words.Fields
Namespace AddWatermarkPublic Class ProgramPublic Shared Sub Main(ByVal args() As String)' Sample infrastructure.Dim exeDir As String = Path.GetDirectoryName(System.Reflection.Assembly.GetExecutingAssembly().Location) + Path.DirectorySeparatorCharDim dataDir As String = New Uri(New Uri(exeDir), "../../Data/").LocalPath
Dim doc As New Document(dataDir & "TestFile.doc")InsertWatermarkText(doc, "CONFIDENTIAL")doc.Save(dataDir & "TestFile Out.doc")End Sub
''' <summary>''' Inserts a watermark into a document.''' </summary>''' <param name="doc">The input document.</param>''' <param name="watermarkText">Text of the watermark.</param>Private Shared Sub InsertWatermarkText(ByVal doc As Document, ByVal watermarkText As String)' Create a watermark shape. This will be a WordArt shape. ' You are free to try other shape types as watermarks.Dim watermark As New Shape(doc, ShapeType.TextPlainText)
' Set up the text of the watermark.watermark.TextPath.Text = watermarkTextwatermark.TextPath.FontFamily = "Arial"watermark.Width = 500watermark.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 watermarkwatermark.StrokeColor = Color.Gray ' Try LightGray to get more Word-style watermark
' Place the watermark in the page center.watermark.RelativeHorizontalPosition = RelativeHorizontalPosition.Pagewatermark.RelativeVerticalPosition = RelativeVerticalPosition.Pagewatermark.WrapType = WrapType.Nonewatermark.VerticalAlignment = VerticalAlignment.Centerwatermark.HorizontalAlignment = HorizontalAlignment.Center
' Create a new paragraph and append the watermark to this paragraph.Dim watermarkPara As New Paragraph(doc)watermarkPara.AppendChild(watermark)
' Insert the watermark into all headers of each document section.For Each sect As Section 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)Next sectEnd Sub
Private Shared Sub InsertWatermarkIntoHeader(ByVal watermarkPara As Paragraph, ByVal sect As Section, ByVal headerType As HeaderFooterType)Dim header As HeaderFooter = sect.HeadersFooters(headerType)
If header Is Nothing Then' There is no header of the specified type in the current section, create it.header = New HeaderFooter(sect.Document, headerType)sect.HeadersFooters.Add(header)End If
' Insert a clone of the watermark into the header.header.AppendChild(watermarkPara.Clone(True))End SubEnd ClassEnd Namespace

查看更多Aspose.Words信息

转载于:https://blog.51cto.com/flt9999/1690120

Aspose.Words如何在文档中添加水印相关推荐

  1. 用Aspose.Words 从Word文档中提取表格数据

    用Aspose.Words 从Word文档中提取表格数据 对于某些项目,开发人员需要从Word文档中提取数据并导出到数据库.最大的挑战是必须支持现有Word文档. 相同格式且带多个数据块的Word文档 ...

  2. 如何在 Word 文档中添加水印?

    把 Word 文档中的文件直接发送给客户或者其他人时,难免可能会被直接复制,引起不要的安全隐患.因此,在 Word 当中,通常是可以直接导出为 PDF 文档的.为了降低内容被盗取的可能性,有时候我们还 ...

  3. Aspose.Words使用教程之如何在文档中添加水印

    有时你需要在一个Word文档中插入一个水印,例如如果你想打印草稿文档或将其标记为机密. 在Microsoft Word中,您可以使用插入水印命令快速插入水印.没有多少人使用这个命令认识到这样的&quo ...

  4. java aspose 加水印_Aspose.Words使用教程之如何在文档中添加水印

    有时你需要在一个Word文档中插入一个水印,例如如果你想打印草稿文档或将其标记为机密. 在Microsoft Word中,您可以使用插入水印命令快速插入水印.没有多少人使用这个命令认识到这样的&quo ...

  5. Java向word文档中添加水印

    前言: 水印可以说是一个标识,有时我们希望向文档中插入公司名称作为水印,或者将公司logo插入到文档中作为水印.先来看看本地word文档如何插水印吧! 然后选择图片水印或者文字水印即可. 那么想要通过 ...

  6. Word处理控件Aspose.Words功能演示:在C#中的Word文档中添加或删除水印

    水印通常用于显示文档的所有权或分类.在本文中,我们将学习使用Aspose.Words for .NET API 在Word文档中使用水印.让我们介绍以下与水印有关的用例. 使用C#在Word文档中添加 ...

  7. 用Aspose.Words for .NET动态生成word文档中的图片或水印

    1.概述 在项目中生成word文档,这个功能很普遍的,一般生成都是纯文字或是列表的比较多,便于客户打印,而要把图片也生成到word文档中的需求有些客户也是需要的,例如产品图片.这次我们介绍的是如何利用 ...

  8. aspose.words复制插入同一word文档中的某个页面

    选择word模板 Document doc = new Document(Server.MapPath("~\\templet") + "\\" + name. ...

  9. 【.NET】用Aspose.Words for .NET动态生成word文档中的数据表格

    1.概述 最近项目中有一个这样的需求:导出word 文档,要求这个文档的格式不是固定的,用户可以随便的调整,导出内容中的数据表格列是动态的,例如要求导出姓名和性别,你就要导出这两列的数据,而且这个文档 ...

最新文章

  1. 硬肝!超详细matplotlib基础介绍!!!
  2. 收藏!中国卫星互联网产业发展白皮书
  3. 简明python教程pdf-python简明教程中文pdf
  4. 自然语言系列学习之表示学习与知识获取(七)利用关系路径进行关系抽取
  5. phpst安装memcache扩展_在 Ubuntu/Debian 下安装 PHP7.3 教程
  6. Hbase 的javaAPI基本操作用 在idea上的实现
  7. arraylist从大到小排序_java基础算法之二叉树排序(递归)
  8. 测试开发——软件测试虚拟环境的搭建
  9. SpringBoot(尚硅谷)
  10. 运动控制卡,越来越简单了
  11. Python 加性高斯白噪声 AWGN
  12. 中国地质大学英语语音学习笔记(六):英语连读——辅音连缀与爆破音读好,让连读更顺畅
  13. 给老笔记本换固态硬盘,能提升系统速度吗?
  14. linux系统显卡显存容量,Linux下检查显存大小
  15. 集合切分:List集合按照数量切分成若干个集合
  16. 智合同丨企业数智化转型,AI技术起到了什么作用?
  17. 埃森哲:数字化转型新阶段,企业需要什么新能力
  18. 如何在 BGP 中通告网络
  19. Part8:淘宝天猫母婴产品数据分析
  20. rman如何直接备份到异地硬盘,磁带机和磁带库

热门文章

  1. 开了gomod不识别gopath_笔记本电脑开不了机原因 笔记本电脑开不了机解决方法【图文】...
  2. /opt/hbase/conf 中不能启动hbase_浅谈Hbase在用户画像上的应用
  3. SOCK_DGRAM(数据报套接字)与SOCK_STREAM(流套接口)的区别
  4. JavaScript 模块化七日谈
  5. webService学习5:Eclipse的TCP/IP工具
  6. 一个sql题目, 统计每年每月的信息
  7. 2021年程序员1月薪资大幅度上涨,你的2021有奔头了吗?
  8. vs2012打包和部署程序成可安装安装包文件(InstallShield
  9. oracle模拟试题
  10. sw接口是什么意思啊_为什么有些任务栏上的按钮一点就消失了