Aspose.Words使用教程之如何操作主題屬性,如何在文件中新增水印

風靈使發表於2018-08-16

我們已經在Aspose.Words 15.7.0新增了基本API來訪問文件的主題屬性,現在,這API包括以下幾個公共物件:

  • 主題
  • 主題字型
  • 主題顏色

以下介紹如何獲取主題屬性:

Document doc = new Document(MyDir + @"in.docx");

Theme theme = doc.Theme;

Console.WriteLine(theme.MajorFonts.Latin); // Major (Headings) font for Latin characters.

Console.WriteLine(theme.MinorFonts.EastAsian);    // Minor (Body) font for EastAsian characters.

Console.WriteLine(theme.Colors.Accent1);          // Color for theme color Accent 1.

以下介紹如何設定主題屬性:

Document doc = new Document(MyDir + @"in.docx");

Theme theme = doc.Theme;

theme.MinorFonts.Latin = "Times New Roman";     // Set Times New Roman font as Body theme font for Latin Character.

theme.Colors.Hyperlink = Color.Gold;            // Set Color.Gold for theme color Hyperlink.

有時你需要在一個Word文件中插入一個水印,例如如果你想列印草稿文件或將其標記為機密。

Microsoft Word中,您可以使用插入水印命令快速插入水印。沒有多少人使用這個命令認識到這樣的“水印”只是一個形狀與文字一起插入到頁首或頁尾,或在頁面的中心位置。

而在Aspose.Words中,沒有單一的“插入水印”命令就像Microsoft Word,它很容易將任何形狀或影象插入到頁首或頁尾,從而建立一個任何可以想象型別的水印。

Example

把水印插入一個Word文件。

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

相關文章