class Office
{
/// <summary>
/// 添加文字水印
/// </summary>
/// <param name=”loadFromFile”>需要加水印的文档</param>
/// <param name=”saveToFileName”>加水印后保存的新文档</param>
/// <param name=”watermarkText”>图片水印路径</param>
/// <param name=”FontFamily”></param>
/// <param name=”Width”>宽</param>
/// <param name=”Height”>高</param>
/// <param name=”Rotation”>旋转角度 正负100</param>
///
public static void SetTextWatermark(string loadFromFile, string saveToFileName, string watermarkText, string FontFamily, int Width, int Height, int Rotation)
{
Aspose.Words.Document doc = new Aspose.Words.Document(loadFromFile);
Aspose.Words.Drawing.Shape watermark = new Aspose.Words.Drawing.Shape(doc, Aspose.Words.Drawing.ShapeType.TextPlainText);
watermark.TextPath.Text = watermarkText;
watermark.TextPath.FontFamily = FontFamily;
watermark.Width = Width;
watermark.Height = Height;
AddWatermark(doc, watermark, Rotation);
Save(doc, saveToFileName);
}
/// <summary>
/// 添加图片水印
/// </summary>
/// <param name=”loadFromFile”>需要加水印的文档</param>
/// <param name=”saveToFileName”>加水印后保存的新文档</param>
/// <param name=”watermarkImgPath”>图片水印路径</param>
/// <param name=”Width”>宽</param>
/// <param name=”Height”>高</param>
/// <param name=”Rotation”>旋转角度 正负100</param>
public static void SetImgWatermark(string loadFromFile, string saveToFileName, string watermarkImgPath, int Width, int Height, int Rotation)
{
Aspose.Words.Document doc = new Aspose.Words.Document(loadFromFile);
var a = doc.BackgroundShape;
Aspose.Words.Drawing.Shape watermark = new Aspose.Words.Drawing.Shape(doc, Aspose.Words.Drawing.ShapeType.Image);
watermark.ImageData.SetImage(watermarkImgPath);
watermark.Width = Width;
watermark.Height = Height;
watermark.HorizontalAlignment = Aspose.Words.Drawing.HorizontalAlignment.Right;
AddWatermark(doc, watermark, Rotation);
Save(doc, saveToFileName);
}
/// <summary>
/// 添加水印服务
/// </summary>
/// <param name=”doc”></param>
/// <param name=”watermark”></param>
/// <param name=”rotation”></param>
private static void AddWatermark(Aspose.Words.Document doc, Aspose.Words.Drawing.Shape watermark, int rotation)
{
watermark.Rotation = rotation;
watermark.Fill.Color = System.Drawing.Color.Gray;
watermark.StrokeColor = System.Drawing.Color.Gray;
watermark.RelativeHorizontalPosition = Aspose.Words.Drawing.RelativeHorizontalPosition.Page;
watermark.RelativeVerticalPosition = Aspose.Words.Drawing.RelativeVerticalPosition.Page;
watermark.WrapType = Aspose.Words.Drawing.WrapType.None;
watermark.VerticalAlignment = Aspose.Words.Drawing.VerticalAlignment.Center;
watermark.HorizontalAlignment = Aspose.Words.Drawing.HorizontalAlignment.Center;
Aspose.Words.Paragraph watermarkPara = new Aspose.Words.Paragraph(doc);
watermarkPara.AppendChild(watermark);
foreach (Aspose.Words.Section sect in doc.Sections)
{
InsertWatermarkIntoHeader(watermarkPara, sect, Aspose.Words.HeaderFooterType.HeaderPrimary);
InsertWatermarkIntoHeader(watermarkPara, sect, Aspose.Words.HeaderFooterType.HeaderFirst);
InsertWatermarkIntoHeader(watermarkPara, sect, Aspose.Words.HeaderFooterType.HeaderEven);
}
}
private static void Save(Aspose.Words.Document doc, string saveToFileName)
{
doc.Save(saveToFileName);
}
/// <summary>
/// 插入水印
/// </summary>
/// <param name=”doc”>要插入水印的文档对象</param>
/// <param name=”isTextWatermaker”>是否文字水印(否的话需要传入图片的绝对地址)</param>
/// <param name=”watermarkText”>水印内容(文字或图片的绝对地址)</param>
/// <param name=”rotation”></param>
private static void InsertWatermarkText(Aspose.Words.Document doc, bool isTextWatermaker, string watermarkText, int rotation)
{
// 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 = null;
if (isTextWatermaker)
{
watermark = new Aspose.Words.Drawing.Shape(doc, Aspose.Words.Drawing.ShapeType.TextPlainText);
// Set up the text of the watermark.
watermark.TextPath.Text = watermarkText;
watermark.TextPath.FontFamily = “微软雅黑”;
watermark.Width = 500;
watermark.Height = 100;
}
else
{
watermark = new Aspose.Words.Drawing.Shape(doc, Aspose.Words.Drawing.ShapeType.Image);
watermark.ImageData.SetImage(watermarkText);
watermark.Width = 595;// watermark.ImageData.ImageSize.WidthPixels;
watermark.Height = 842;//watermark.ImageData.ImageSize.HeightPixels;
watermark.HorizontalAlignment = Aspose.Words.Drawing.HorizontalAlignment.Right; //靠右对齐
//watermark.BehindText = true;
}
// Text will be directed from the bottom-left to the top-right corner.
watermark.Rotation = rotation;
// 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 = Aspose.Words.Drawing.RelativeHorizontalPosition.Page;
watermark.RelativeVerticalPosition = Aspose.Words.Drawing.RelativeVerticalPosition.Page;
watermark.WrapType = Aspose.Words.Drawing.WrapType.None;
watermark.VerticalAlignment = Aspose.Words.Drawing.VerticalAlignment.Center;
watermark.HorizontalAlignment = Aspose.Words.Drawing.HorizontalAlignment.Center;
// Create a new paragraph and append the watermark to this paragraph.
Aspose.Words.Paragraph watermarkPara = new Aspose.Words.Paragraph(doc);
watermarkPara.AppendChild(watermark);
// Insert the watermark into all headers of each document section.
foreach (Aspose.Words.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, Aspose.Words.HeaderFooterType.HeaderPrimary);
InsertWatermarkIntoHeader(watermarkPara, sect, Aspose.Words.HeaderFooterType.HeaderFirst);
InsertWatermarkIntoHeader(watermarkPara, sect, Aspose.Words.HeaderFooterType.HeaderEven);
}
}
private static void InsertWatermarkIntoHeader(Aspose.Words.Paragraph watermarkPara, Aspose.Words.Section sect, Aspose.Words.HeaderFooterType headerType)
{
Aspose.Words.HeaderFooter header = sect.HeadersFooters[headerType];
if (header == null)
{
// There is no header of the specified type in the current section, create it.
header = new Aspose.Words.HeaderFooter(sect.Document, headerType);
sect.HeadersFooters.Add(header);
}
// Insert a clone of the watermark into the header.
header.AppendChild(watermarkPara.Clone(true));
}
}
本文地址: C#给word添加水印Aspose.Words