有时你需要在一个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");

}

///

/// Inserts a watermark into a document.

///

/// The input document.

/// Text of the watermark.

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

'''

''' Inserts a watermark into a document.

'''

''' The input document.

''' Text of the watermark.

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

标签:文档管理Aspose.words

本站文章除注明转载外,均为本站原创或翻译。欢迎任何形式的转载,但请务必注明出处、不得修改原文相关链接,尊重他人劳动成果

文章转载自:慧都控件网

0

好文不易,鼓励一下吧!

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

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

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

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

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

  3. java imageio删除图片_Java 提取、替换、删除PDF文档中的图片

    在一篇文章里,配有与文本信息相得益彰的图片,不仅能够活跃与美化版面,同时也有利于提高文章的可读性和阅读效果,从而增强其吸引力.同时,对文档中已存在图片的处理也尤为重要.本文将通过使用Java程序来演示 ...

  4. Aspose.Words如何在文档中添加水印

    Aspose.Words是一款先进的文档处理控件,在不使用Microsoft Words的情况下,它可以使用户在各个应用程序中执行各种文档处理任务,其中包括文档的生成.修改.渲染.打印,文档格式转换和 ...

  5. java poi操作word2007_java使用poi解析2007以上的word文档中的表格与图片

    项目中使用到了要解析word文档中的表格与图片,网上的2003的解析方式很多,2007以上的很少,我看了官网找了资料自己写了一个简单的解析方案,大家共同学习吧!有不对的地方希望大神指教! import ...

  6. InDesign 教程:如何在文档中导航页面?

    欢迎观看indesign教程,小编带大家学习 InDesign 的基本工具和使用技巧,了解如何使用"属性"面板."页面"面板和其他方法在文档中的页面之间导航. ...

  7. java自动生成项目编号_java - Apache POI,在同一文档中创建项目符号点和编号列表 - 堆栈内存溢出...

    我试图用apache poi创建一个既包含圆点又包含编号列表的文档. 我想得到这样的结果. 名单: 清单1项目0 清单1项目1 清单1项目2 列表后的段落. 清单2: 清单2项目0 清单2项目1 清单 ...

  8. Word控件Spire.Doc 【列表】教程:在 Word 文档中插入列表

    Spire.Doc for .NET是一款专门对 Word 文档进行操作的 .NET 类库.在于帮助开发人员无需安装 Microsoft Word情况下,轻松快捷高效地创建.编辑.转换和打印 Micr ...

  9. ms office word2013教程 - 如何将A文档中的样式复制到B文档中

    将A文档中的样式复制到B文档中 选择 [文件] -> [选项],在弹出的 [Word选项] 框中选择 [加载项],然后按照图示进行操作,最后记得点击保存即可.

最新文章

  1. 打开文件和关闭文件的含义
  2. Rowhammer漏洞致“比特位翻转”,如何解决?
  3. 网页设计师应向肖像画家吸取的11个理念
  4. 一机双平面、TCP半连接攻击——SYN攻击详解
  5. java编译生成哪些文件_java编译后生成什么文件?生成的文件包括什么?
  6. [Android Studio]-基本快捷键大全
  7. 迟滞比较器及施密特触发器详解
  8. Windows系统安装Redis(详细)
  9. 作业三 使用病毒分析工具对病毒进行分析
  10. 【转载】word空白页删不掉的7种原因及解决方法
  11. python 删除指定目录_删除Python中除一个子目录外的目录
  12. lzg_ad: FBWF技术概述
  13. 利用selenium webdriver下载不同类型的文件(pdf,txt等等)
  14. i78700k配什么显卡好_i78700配什么显卡
  15. 软件构建中的设计(二)
  16. python里的jh是啥意思_JH是什么意思啊
  17. 数据库期末复习(1-5章)
  18. 如何UNI-APP中使用iconfont彩色图标
  19. Mysql(三)索引、视图、存储过程、触发器、分区表
  20. 瑞萨RH850/F1L-片上资源分配(Flash,RAM,外设资源)

热门文章

  1. Google Earth 安装及查看
  2. 嘉益仕(Litins)助力四川交通职院,打造综合实训基地智能车间
  3. 战斗回路的一键必杀宏,脚本设置
  4. “python”自测100题,快收藏起来吧!
  5. Linux下NTP时间同步服务器搭建
  6. 新的BRIGHTCOVE MARKETPLACE扩大技术合作伙伴生态系统
  7. 树莓派的入门配置,包括ftp 远程桌面,中问输入法,语音模块配置
  8. 浙江大学远程教育平台计算机基础知识,浙江大学远程教育_计算机基础_第3次作业_Word知识题...
  9. java毕设项目驾校管理系统(附源码)
  10. 进程用户态和内核态及其切换过程