`

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

阅读更多

 

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

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

       而在Aspose.Words中,没有单一的“插入水印”命令就像Microsoft Word,它很容易将任何形状或图像插入到页眉或页脚,从而创建一个任何可以想象类型的水印。叫喊

Example

把水印插入一个Word文档。酷

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

 

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
分享到:
评论

相关推荐

    Word文档加水印(利用Aspose.Words.dll)

    本项目聚焦于使用C#编程语言,在WPF(Windows Presentation Foundation)环境中,结合Aspose.Words.dll库来实现Word文档的水印添加以及接受修订功能。下面将详细介绍这个过程及其相关知识点。 首先,Aspose.Words....

    Aspose.Words for .NET 18.7(.net+.net core) 去水印,学习使用

    Aspose.Words for .NET 18.7是一款强大的文档处理库,专为.NET和.NET Core框架设计,用于在各种应用程序中实现高级文档操作,包括创建、编辑、转换和渲染Microsoft Word文档。在这个版本中,重点是去水印功能,这...

    aspose.words.dll 18.7 ,netcore

    描述中提到"aspose.words 18.7,亲测无水印,添加依赖 SkiaSharp, Version=1.60.1",这意味着该版本的Aspose.Words不仅没有预览或试用版通常附带的水印,而且开发团队可能已经集成了一个名为SkiaSharp的图形库。...

    aspose.words导出word文件demo,含dll库

    总的来说,这个压缩包提供了一个使用Aspose.Words库进行Word文档处理的实战示例,特别是无水印的导出功能,对于需要在项目中处理大量Word文档的开发者来说,这是一个非常有价值的资源。通过学习和理解这个demo,...

    Aspose.Words.dll文件.zip

    在实际开发中,使用Aspose.Words.dll 需要通过编程接口调用其方法。这需要具备一定的C#或其他.NET语言基础,理解DLL中的类、方法和属性,以便正确地操作Word文档。同时,开发者还需要了解如何处理可能出现的异常和...

    Aspose.Words 19.11(.net core 可用)

    在使用Aspose.Words的过程中,开发人员需要引用`Aspose.Words.dll`文件,这是库的核心组件,包含了所有必需的功能。通过适当的代码引用和调用,可以轻松地实现文档处理任务。 总之,Aspose.Words 19.11是一个针对...

    unity导出word所需dll Aspose.Words.dll 版本22.4 (带水印.net 2.0-6.0配适)

    值得注意的是,这个版本还支持添加水印功能,这意味着开发者可以轻松地在导出的Word文档上添加自定义的水印,增强了文档的安全性和版权保护。 在Unity中使用Aspose.Words.dll时,首先需要确保你的Unity项目已经配置...

    aspose-words word添加水印的jar和licence文件

    在这个特定的案例中,"aspose-words word添加水印的jar和licence文件"是Aspose.Words的一个组件,专门用于在Word文档中添加水印功能。 水印是文档保护和标识的一种常见方法,通常用于在不遮挡文本的情况下向文档...

    积分-Aspose.Words21.11 简单无水印 jar包+使用说明

    Aspose.Words是一款强大的文档处理库,主要用于在Java应用程序中创建、编辑和操作Microsoft Word文档。这个21.11版本的jar包是Aspose.Words的一个无水印版本,适用于那些希望在不显示任何品牌标识的情况下使用其功能...

    aspose-words去水印jar包

    Java平台的开发者可以通过引入这个jar包,使用Java代码来调用Aspose.Words的相关方法,实现对Word文档水印的去除。 4. **License.xml**:在描述中提到了“license.xml”,这通常是指软件许可文件,包含了关于软件...

    Aspose.Words 11.7 去水印版

    去除了“Evaluation Only. Created with Aspose.Words. Copyright 2003-2011 Aspose Pty Ltd.”水印 word,pdf等相互转换,相信对需要的朋友有帮组

    Aspose.Words 18.7

    Aspose.Words是一款强大的文档处理库,主要用于在.NET环境中创建、操作和转换Microsoft Word文档。这个18.7版本的Aspose.Words以其卓越的性能和兼容性赢得了用户的高度评价,特别是其无水印功能,使得用户在处理文档...

    Aspose.Words for Net破解版无水印附有API开发文档

    Aspose.Words for Net破解版的类库,生成的word文档没有水印和广告等字样,附有API开发文档,在此贡献给开发者们。

    aspose-words_license_jar_调用方法.rar

    在IT行业中,Aspose.Words是一款著名的文档处理库,它提供了强大的API,使得开发者能够方便地在各种应用程序中创建、编辑、转换和显示Microsoft Word文档。在这个“aspose-words_license_jar_调用方法.rar”压缩包中...

    Aspose.Words-17.6.rar

    Aspose.Words提供商业许可,购买后可以在生成的文件中去除试用版的水印和广告。免费版本通常会有这些限制。 4. **解决PDF乱码问题** Aspose.Words在转换过程中,通过正确设置字符集和编码,可以避免PDF输出的乱码...

    aspose-words添加word水印的jar包,licence文件,实例代码

    提供的`实例代码`可能是Java或.NET平台上的,它演示了如何使用Aspose.Words API在Word文档中添加水印的具体步骤。代码示例可能会包含如下的关键部分: ```java // 加载文档 Document doc = new Document("input....

    Aspose.Words.dll

    Aspose.Words.dll 是一个由...总结来说,Aspose.Words.dll是.NET开发中处理Word文档的强大工具,17.7版本虽然不支持.NET Core,但在.NET Framework下依然表现出色,提供无水印的转换和处理功能,是开发中的理想选择。

    Aspose.Words for .NET示例

    要在PDF文档中添加水印,我们需要使用Aspose.Words的`WatermarkOptions`类。水印可以是文本、图像或者组合。以下是一个添加文本水印的例子: ```csharp WatermarkOptions watermarkOptions = new ...

    Aspose.Words for net _19.3.0.0无水印版.zip

    - **创建和编辑**:Aspose.Words提供API,允许开发者在代码中创建全新的Word文档,或者打开现有文档进行编辑,添加文本、图片、表格、形状等元素。 - **格式转换**:强大的文档转换功能,能够将Word文档转换为PDF...

    aspose.cells aspose.words aspose.pdf

    Aspose 是一个强大的文档处理工具集,主要用于在各种应用程序中创建、编辑、转换和呈现不同格式的文件。这个压缩包包含三个主要组件:Aspose.Cells、Aspose.Words 和 Aspose.Pdf,这些都是Aspose产品系列的核心部分...

Global site tag (gtag.js) - Google Analytics