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

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

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

立即下载Aspose.Words最新版icon-default.png?t=M7J4http://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

 

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
### 回答1: aspose.words 20.12 for java 是一款用于Java平台的文档处理工具,提供了强大的功能和丰富的API,能够轻松地处理各种常见的文档格式。 要去掉文档水印,可以使用Aspose.Words提供的替换功能,将水印替换为空字符串即可。具体操作如下: 1. 打开要去水印文档: Document doc = new Document("原始文档.docx"); 2. 遍历文档的所有水印,将其替换为空字符串: WatermarkCollection watermarks = doc.getSections().get(0).getBody().getWatermarks(); for (Watermark watermark : watermarks) { watermark.remove(); } 3. 保存修改后的文档: doc.save("去水印后的文档.docx", SaveFormat.DOCX); 这样就能够去除文档的所有水印了。需要注意的是,该方法只能去除Aspose.Words识别的水印,如果文档水印是通过其他方式添加的,则可能无法去除。 ### 回答2: Aspose.Words 20.12 for Java 是一款强大的文档处理库,可以用于去除文档水印。与传统的水印去除工具不同,Aspose.Words 可以根据文档的内容、格式和布局等特征来智能地识别和去除水印。 要使用 Aspose.Words 去除文档水印,需要先加载要处理的文档,然后通过 Document 类的 WatermarkCollection 属性获取文档所有的水印。可以使用 WatermarkCollection 的 remove 方法来删除某个特定的水印,也可以使用 clear 方法删除所有水印。 在删除水印时,需要注意以下几点: 1. 水印可能存在于文档的多个部分和页面,需要对整个文档进行遍历和处理; 2. 不同类型的水印可能需要不同的方法来删除,例如文本水印可以通过正则表达式来识别和删除,而图像水印需要使用图像处理库来处理; 3. 删除水印可能会影响文档的布局和格式,需要在删除后对文档进行校对和调整。 除了将 Aspose.Words 应用于去水印,还可以利用其丰富的文档处理功能来进行格式化、转换、合并、拆分、加密等操作,方便实用。同时,Aspose.Words 也提供了适用于 .NET、Java、C++、Python、Node.js 等多种编程语言的版本,可根据项目需要进行选择和使用。 ### 回答3: aspose.words 20.12 for java是一款非常强大的Java文档处理库。它可以让Java程序员轻松地读取、编辑和创建各种类型的文档,包括Microsoft Word、OpenOffice、PDF等。 如果你需要去除文档水印aspose.words 20.12 for java可以轻松胜任。具体的步骤如下: 1. 首先,你需要使用aspose.words 20.12 for java读取文档。你可以使用Document类来打开文档。例如: Document doc = new Document("input.docx"); 2. 然后,你需要获取文档的所有Watermark对象。Watermark是aspose.words 20.12 for java表示水印的对象。你可以使用Document类的getWatermarks方法来获取所有的Watermark对象。例如: WatermarkCollection watermarks = doc.getWatermarks(); 3. 接下来,你需要遍历所有的Watermark对象,并将它们从文档删除。你可以使用Watermark对象的remove方法来删除它。例如: for (Watermark watermark : watermarks) { watermark.remove(); } 4. 最后,当你完成了对文档的修改,你可以使用Document类的save方法将修改后的文档保存到磁盘。例如: doc.save("output.docx"); 以上就是使用aspose.words 20.12 for java去除文档水印的步骤。这个过程非常简单,并且aspose.words 20.12 for java提供了非常丰富的API,可以帮助你轻松地处理各种文档。无论你是在开发企业应用程序,还是在开发自己的个人项目,这款文档处理库都是一个非常不错的选择。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值