• <menu id="w2i4a"></menu>
  • logo Aspose.Words開發(fā)者指南

    文檔首頁>>Aspose.Words開發(fā)者指南>>Word格式處理控件Aspose.Words for .NET水印處理教程——如何添加和刪除水印

    Word格式處理控件Aspose.Words for .NET水印處理教程——如何添加和刪除水印


    Aspose.Words For .NET是一種高級Word文檔處理API,用于執(zhí)行各種文檔管理和操作任務(wù)。API支持生成,修改,轉(zhuǎn)換,呈現(xiàn)和打印文檔,而無需在跨平臺應(yīng)用程序中直接使用Microsoft Word。此外,API支持所有流行的Word處理文件格式,并允許將Word文檔導(dǎo)出或轉(zhuǎn)換為固定布局文件格式和最常用的圖像/多媒體格式。

    接下來我們將進(jìn)入關(guān)于“水印處理”的介紹,在Aspose.Words中學(xué)會如何添加和刪除水印。

    >>Aspose.Words for .NET已經(jīng)更新至v20.2,新增6大新功能,包括支持動態(tài)復(fù)選框值設(shè)置,支持字段格式,支持導(dǎo)出bookmarkStart的w:colFirst和w:colLast屬性等,點(diǎn)擊下載體驗(yàn)


    在文檔中添加水印

    有時,如果想打印草稿文件或?qū)⑵錁?biāo)記為機(jī)密文件,則需要在Word文檔中插入水印。在Microsoft Word中,可以使用“插入水印”命令快速插入水印。使用此命令的人很少意識到這種“水印”只是一種文本插入到頁眉或頁腳并位于頁面中心的形狀。

    盡管在Aspose.Words中沒有像Microsoft Word中那樣的單個“插入水印”命令,但是很容易將任何形狀或圖像插入頁眉或頁腳中,從而創(chuàng)建任何可以想象的類型的水印。下面的示例在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屬性。此屬性獲取或設(shè)置一個標(biāo)志,該標(biāo)志指示形狀是顯示在表內(nèi)還是表外。請注意,僅當(dāng)使用CompatibilityOptions.OptimizeFor方法為MS Word 2010優(yōu)化文檔時,此屬性才起作用。下面的代碼示例演示如何使用此屬性。

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

    從文檔中刪除水印

    要從文檔中刪除水印,您只需在插入過程中設(shè)置水印形狀的名稱,然后通過分配的名稱刪除水印形狀。以下代碼段向您展示了如何設(shè)置水印形狀的名稱以及如何從文檔中刪除。

    // 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();
                }
            }
        }
    }
        }
    還想要更多嗎?您可以點(diǎn)擊閱讀【2019 · Aspose最新資源整合】,查找需要的教程資源。如果您有任何疑問或需求,請隨時加入Aspose技術(shù)交流群(642018183),我們很高興為您提供查詢和咨詢。
    掃碼咨詢


    添加微信 立即咨詢

    電話咨詢

    客服熱線
    023-68661681

    TOP
    三级成人熟女影院,欧美午夜成人精品视频,亚洲国产成人乱色在线观看,色中色成人论坛 (function(){ var bp = document.createElement('script'); var curProtocol = window.location.protocol.split(':')[0]; if (curProtocol === 'https') { bp.src = 'https://zz.bdstatic.com/linksubmit/push.js'; } else { bp.src = 'http://push.zhanzhang.baidu.com/push.js'; } var s = document.getElementsByTagName("script")[0]; s.parentNode.insertBefore(bp, s); })();