文档半岛外围网上直营>>Aspose中文文档>>添加水印
添加水印
Aspose.Words是一种高级Word文档处理API,用于执行各种文档管理和操作任务。API支持生成,修改,转换,呈现和打印文档,而无需在跨平台应用程序中直接使用Microsoft Word。
Aspose API支持流行文件格式处理,并允许将各类文档导出或转换为固定布局文件格式和最常用的图像/多媒体格式。
有时您需要在文档中插入水印,例如,如果您想打印草稿文档或将其标记为机密。在 Microsoft Word 中,您可以使用“插入水印”选项快速插入水印。使用此命令的人很少意识到这种“水印”只是一种将文本插入页眉或页脚并位于页面中心的形状。
以下代码示例展示了如何使用 Aspose.Words 处理水印:
static void Main(string[] args)
{
Document doc = new Document("../../data/document.doc");
string watermarkText = "Aspose.Words for .NET";
// 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 = System.Drawing.Color.Gray;
// Try LightGray to get more Word-style watermark
watermark.StrokeColor = System.Drawing.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);
}
doc.Save("waterMarks.doc");
}
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));
}
点击复制
运行代码
如需下载产品Aspose.Words ,请点击产品名进入下载页面

QQ交谈
在线咨询

渝公网安备
50010702500608号

客服热线