Aspose.Words For .NET是一种高级Word文档处理API,用于执行各种文档管理和操作任务。API支持生成,修改,转换,呈现和打印文档,而无需在跨平台应用程序中直接使用Microsoft Word。此外,API支持所有流行的Word处理文件格式,并允许将Word文档导出或转换为固定布局文件格式和最常用的图像/多媒体格式。
接下来我们将进入关于“水印处理”的介绍,在Aspose.Words中学会如何添加和删除水印。
在文档中添加水印
有时,如果想打印草稿文件或将其标记为机密文件,则需要在Word文档中插入水印。在Microsoft Word中,可以使用“插入水印”命令快速插入水印。使用此命令的人很少意识到这种“水印”只是一种文本插入到页眉或页脚并位于页面中心的形状。
尽管在Aspose.Words中没有像Microsoft Word中那样的单个“插入水印”命令,但是很容易将任何形状或图像插入页眉或页脚中,从而创建任何可以想象的类型的水印。下面的示例在Word文档中插入一个水印。
class AddWatermark
{
public static void Run()
{
// The path to the documents directory.
string dataDir = RunExamples.GetDataDir_WorkingWithImages();
string fileName = "TestFile.Watermark.doc";
Document doc = new Document(dataDir + fileName);
InsertWatermarkText(doc, "CONFIDENTIAL");
dataDir = dataDir + RunExamples.GetOutputFilePath(fileName);
doc.Save(dataDir);Console.WriteLine("\nAdded watermark to the document successfully.\nFile saved at " + dataDir);
}// 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);
watermark.Name= "WaterMark";
// 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));
}
}
在表格单元格中添加水印
有时需要在表的单元格中插入水印/图像并将其显示在表外部,可以使用ShapeBase.IsLayoutInCell属性。此属性获取或设置一个标志,该标志指示形状是显示在表内还是表外。请注意,仅当使用CompatibilityOptions.OptimizeFor方法为MS Word 2010优化文档时,此属性才起作用。下面的代码示例演示如何使用此属性。
Document doc = new Document(dataDir + @"LayoutInCell.docx");
DocumentBuilder builder = new DocumentBuilder(doc);Shape watermark = new Shape(doc, ShapeType.TextPlainText);
watermark.RelativeHorizontalPosition = RelativeHorizontalPosition.Page;
watermark.RelativeVerticalPosition = RelativeVerticalPosition.Page;
watermark.IsLayoutInCell = true; // Display the shape outside of table cell if it will be placed into a cell.watermark.Width = 300;
watermark.Height = 70;
watermark.HorizontalAlignment = HorizontalAlignment.Center;
watermark.VerticalAlignment = VerticalAlignment.Center;watermark.Rotation = -40;
watermark.Fill.Color = Color.Gray;
watermark.StrokeColor = Color.Gray;watermark.TextPath.Text = "watermarkText";
watermark.TextPath.FontFamily = "Arial";watermark.Name = string.Format("WaterMark_{0}", Guid.NewGuid());
watermark.WrapType = WrapType.None;Run run = doc.GetChildNodes(NodeType.Run, true)[doc.GetChildNodes(NodeType.Run, true).Count - 1] as Run;
builder.MoveTo(run);
builder.InsertNode(watermark);
doc.CompatibilityOptions.OptimizeFor(MsWordVersion.Word2010);
dataDir = dataDir + "Shape_IsLayoutInCell_out.docx";// Save the document to disk.
doc.Save(dataDir);
从文档中删除水印
要从文档中删除水印,您只需在插入过程中设置水印形状的名称,然后通过分配的名称删除水印形状。以下代码段向您展示了如何设置水印形状的名称以及如何从文档中删除。
// For complete examples and data files, please go to https://github.com/aspose-words/Aspose.Words-for-.NET
public static void Run()
{
// The path to the documents directory.
string dataDir = RunExamples.GetDataDir_WorkingWithImages();
string fileName = "RemoveWatermark.docx";
Document doc = new Document(dataDir + fileName);
RemoveWatermarkText(doc);
dataDir = dataDir + RunExamples.GetOutputFilePath(fileName);
doc.Save(dataDir);
}private static void RemoveWatermarkText(Document doc)
{
foreach (HeaderFooter hf in doc.GetChildNodes(NodeType.HeaderFooter, true))
{
foreach (Shape shape in hf.GetChildNodes(NodeType.Shape, true))
{
if (shape.Name.Contains("WaterMark"))
{
shape.Remove();
}
}
}
}
}