Aspose.Words使用教程之如何在文档中添加水印
2023-09-11 14:14:49 时间
有时你需要在一个Word文档中插入一个水印,例如如果你想打印草稿文档或将其标记为机密。
在Microsoft Word中,您可以使用插入水印命令快速插入水印。没有多少人使用这个命令认识到这样的“水印”只是一个形状与文本一起插入到页眉或页脚,或在页面的中心位置。
立即下载Aspose.Words最新版http://www.evget.com/product/564
而在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 Basic Imports Microsoft.VisualBasic Imports System Imports System.Drawing Imports System.IO Imports System.Reflection Imports Aspose.Words Imports Aspose.Words.Drawing Imports Aspose.Words.Fields Namespace AddWatermark Public Class Program Public Shared Sub Main(ByVal args() As String) ' Sample infrastructure. Dim exeDir As String = Path.GetDirectoryName(System.Reflection.Assembly.GetExecutingAssembly().Location) + Path.DirectorySeparatorChar Dim 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 = 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. 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 sect End 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 Sub End Class End Namespace
相关文章
- [Qt教程] 第28篇 XML(二)使用DOM创建和操作XML文档
- [Qt教程] 第27篇 XML(一)使用DOM读取XML文档
- thymeleaf模板、thymeleaf语法相关中文文档教程
- Firefly官方教程之Netconnect使用文档
- 如何查看jsplumb.js的API文档(YUIdoc的基本使用)#华为云·寻找黑马程序员#
- 使用 jsoup 对 HTML 文档进行解析和操作
- 平时写文档的文档要求
- spring boot:swagger3文档展示分页和分栏的列表数据(swagger 3.0.0 / spring boot 2.3.3)
- 【学习总结】Word文档中怎么设置打勾选择小方框
- kaniko官方文档 - Build Images In Kubernetes
- 【PDF收藏】docker k8s文档整理
- arthas命令列表查阅文档地址
- 如何使用 ABAP 创建包含不同字体大小的 Word 文档试读版
- [BI项目记]-文档版本管理笔记
- Atitit 项目文档规范化与必备文档与推荐文档列表
- atitit.微信支付的教程文档 attilax总结
- atitit。获取表格的字段注释metadata的原理以及AND 字段表格描述文档方案
- 成功解决wps软件文档中,目录自动生成时包含图标题(多次尝试,最终解决,图文教程)
- brython文档之browser.object_storage模块
- Spring 官方教程:使用 Restdocs 创建 API 文档
- 云计算|OpenStack|社区版OpenStack安装部署文档(八--- 存储服务cinder的安装部署---Rocky版)
- 14款开源文档管理系统