• Aspose.Words给word文档加水印


            需求:在一些重要的Word文档需要打印时,添加水印以明出处。

            方案:使用Aspose组件给word文档

            代码:干货如下

    /// <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.
    Aspose.Words.Drawing.Shape watermark = new Aspose.Words.Drawing.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);
    }
    }


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

  • 相关阅读:
    Netty4学习笔记
    Essential Netty in Action 《Netty 实战(精髓)》
    【转】netty案例
    【转】Delphi TClientDataSet的使用
    Macbook brew 安装软件 一直updating Homebrew
    PHP的gc_collect_cycles函数
    Android开发手记之android.content.res.Resources$NotFoundException: Resource ID #0x7f04005e
    Android开发手记之duplicate entry: res/drawable/a.xml
    Android开发手记之Native Crash
    Java与C、C++的10大区别-总结
  • 原文地址:https://www.cnblogs.com/johsan/p/7510749.html
Copyright © 2020-2023  润新知