文檔首頁>>Aspose中文文檔>>添加水印
添加水印
Aspose.Words是一種高級Word文檔處理API,用于執(zhí)行各種文檔管理和操作任務(wù)。API支持生成,修改,轉(zhuǎn)換,呈現(xiàn)和打印文檔,而無需在跨平臺應(yīng)用程序中直接使用Microsoft Word。
Aspose API支持流行文件格式處理,并允許將各類文檔導(dǎo)出或轉(zhuǎn)換為固定布局文件格式和最常用的圖像/多媒體格式。
有時您需要在文檔中插入水印,例如,如果您想打印草稿文檔或?qū)⑵錁?biāo)記為機密。在 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)); }
點擊復(fù)制
運行代碼
如需下載產(chǎn)品Aspose.Words ,請點擊產(chǎn)品名進入下載頁面