Aspose.Words如何在文档中添加水印
? Aspose.Words是一款先進的文檔處理控件,在不使用Microsoft Words的情況下,它可以使用戶在各個應用程序中執行各種文檔處理任務,其中包括文檔的生成、修改、渲染、打印,文檔格式轉換和郵件合并等文檔處理。此外,Aspose.Words支持DOC,OOXML,RTF,HTML,OpenDocument, PDF, XPS, EPUB和其他格式。
? 有時你需要在一個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");
?}
/// <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?BasicImports?Microsoft.VisualBasicImports?SystemImports?System.DrawingImports?System.IOImports?System.Reflection Imports?Aspose.WordsImports?Aspose.Words.DrawingImports?Aspose.Words.Fields Namespace?AddWatermarkPublic?Class?ProgramPublic?Shared?Sub?Main(ByVal?args()?As?String)'?Sample?infrastructure.Dim?exeDir?As?String?=?Path.GetDirectoryName(System.Reflection.Assembly.GetExecutingAssembly().Location)?+?Path.DirectorySeparatorCharDim?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?=?watermarkTextwatermark.TextPath.FontFamily?=?"Arial"watermark.Width?=?500watermark.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?watermarkwatermark.StrokeColor?=?Color.Gray?'?Try?LightGray?to?get?more?Word-style?watermark '?Place?the?watermark?in?the?page?center.watermark.RelativeHorizontalPosition?=?RelativeHorizontalPosition.Pagewatermark.RelativeVerticalPosition?=?RelativeVerticalPosition.Pagewatermark.WrapType?=?WrapType.Nonewatermark.VerticalAlignment?=?VerticalAlignment.Centerwatermark.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?sectEnd?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?SubEnd?ClassEnd?Namespace查看更多Aspose.Words信息
轉載于:https://blog.51cto.com/flt9999/1690120
總結
以上是生活随笔為你收集整理的Aspose.Words如何在文档中添加水印的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: Hello JFinal World
- 下一篇: 去掉WinLicense文件效验的方法