Aspose.Words如何在文档中添加水印

 Aspose.Words是一款先进的文档处理控件,在不使用Microsoft Words的情况下,它可以使用户在各个应用程序中执行各种文档处理任务,其中包括文档的生成、修改、渲染、打印,文档格式转换和邮件合并等文档处理。此外,Aspose.Words支持DOC,OOXML,RTF,HTML,OpenDocument, PDF, XPS, EPUB和其他格式。微笑

       有时你需要在一个Word文档中插入一个水印,例如如果你想打印草稿文档或将其标记为机密。

       在Microsoft Word中,您可以使用插入水印命令快速插入水印。没有多少人使用这个命令认识到这样的“水印”只是一个形状与文本一起插入到页眉或页脚,或在页面的中心位置。疑问

       而在Aspose.Words中,没有单一的“插入水印”命令就像Microsoft Word,它很容易将任何形状或图像插入到页眉或页脚,从而创建一个任何可以想象类型的水印。难过

Example

把水印插入一个Word文档。

 
  1. C#

  2. using System;

  3. using System.Drawing;

  4. using System.IO;

  5. using System.Reflection;

  6.  
  7. using Aspose.Words;

  8. using Aspose.Words.Drawing;

  9. using Aspose.Words.Fields;

  10.  
  11. namespace AddWatermark

  12. {

  13. public class Program

  14. {

  15. public static void Main(string[] args)

  16. {

  17. // Sample infrastructure.

  18. string exeDir = Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location) + Path.DirectorySeparatorChar;

  19. string dataDir = new Uri(new Uri(exeDir), @"../../Data/").LocalPath;

  20.  
  21. Document doc = new Document(dataDir + "TestFile.doc");

  22. InsertWatermarkText(doc, "CONFIDENTIAL");

  23. doc.Save(dataDir + "TestFile Out.doc");

  24. }

  25.  
  26. /// <summary>

  27. /// Inserts a watermark into a document.

  28. /// </summary>

  29. /// <param name="doc">The input document.</param>

  30. /// <param name="watermarkText">Text of the watermark.</param>

  31. private static void InsertWatermarkText(Document doc, string watermarkText)

  32. {

  33. // Create a watermark shape. This will be a WordArt shape.

  34. // You are free to try other shape types as watermarks.

  35. Shape watermark = new Shape(doc, ShapeType.TextPlainText);

  36.  
  37. // Set up the text of the watermark.

  38. watermark.TextPath.Text = watermarkText;

  39. watermark.TextPath.FontFamily = "Arial";

  40. watermark.Width = 500;

  41. watermark.Height = 100;

  42. // Text will be directed from the bottom-left to the top-right corner.

  43. watermark.Rotation = -40;

  44. // Remove the following two lines if you need a solid black text.

  45. watermark.Fill.Color = Color.Gray; // Try LightGray to get more Word-style watermark

  46. watermark.StrokeColor = Color.Gray; // Try LightGray to get more Word-style watermark

  47.  
  48. // Place the watermark in the page center.

  49. watermark.RelativeHorizontalPosition = RelativeHorizontalPosition.Page;

  50. watermark.RelativeVerticalPosition = RelativeVerticalPosition.Page;

  51. watermark.WrapType = WrapType.None;

  52. watermark.VerticalAlignment = VerticalAlignment.Center;

  53. watermark.HorizontalAlignment = HorizontalAlignment.Center;

  54.  
  55. // Create a new paragraph and append the watermark to this paragraph.

  56. Paragraph watermarkPara = new Paragraph(doc);

  57. watermarkPara.AppendChild(watermark);

  58.  
  59. // Insert the watermark into all headers of each document section.

  60. foreach (Section sect in doc.Sections)

  61. {

  62. // There could be up to three different headers in each section, since we want

  63. // the watermark to appear on all pages, insert into all headers.

  64. InsertWatermarkIntoHeader(watermarkPara, sect, HeaderFooterType.HeaderPrimary);

  65. InsertWatermarkIntoHeader(watermarkPara, sect, HeaderFooterType.HeaderFirst);

  66. InsertWatermarkIntoHeader(watermarkPara, sect, HeaderFooterType.HeaderEven);

  67. }

  68. }

  69.  
  70. private static void InsertWatermarkIntoHeader(Paragraph watermarkPara, Section sect, HeaderFooterType headerType)

  71. {

  72. HeaderFooter header = sect.HeadersFooters[headerType];

  73.  
  74. if (header == null)

  75. {

  76. // There is no header of the specified type in the current section, create it.

  77. header = new HeaderFooter(sect.Document, headerType);

  78. sect.HeadersFooters.Add(header);

  79. }

  80.  
  81. // Insert a clone of the watermark into the header.

  82. header.AppendChild(watermarkPara.Clone(true));

  83. }

  84. }

  85. }

 
  1. Visual Basic

  2. Imports Microsoft.VisualBasic

  3. Imports System

  4. Imports System.Drawing

  5. Imports System.IO

  6. Imports System.Reflection

  7.  
  8. Imports Aspose.Words

  9. Imports Aspose.Words.Drawing

  10. Imports Aspose.Words.Fields

  11.  
  12. Namespace AddWatermark

  13. Public Class Program

  14. Public Shared Sub Main(ByVal args() As String)

  15. ' Sample infrastructure.

  16. Dim exeDir As String = Path.GetDirectoryName(System.Reflection.Assembly.GetExecutingAssembly().Location) + Path.DirectorySeparatorChar

  17. Dim dataDir As String = New Uri(New Uri(exeDir), "../../Data/").LocalPath

  18.  
  19. Dim doc As New Document(dataDir & "TestFile.doc")

  20. InsertWatermarkText(doc, "CONFIDENTIAL")

  21. doc.Save(dataDir & "TestFile Out.doc")

  22. End Sub

  23.  
  24. ''' <summary>

  25. ''' Inserts a watermark into a document.

  26. ''' </summary>

  27. ''' <param name="doc">The input document.</param>

  28. ''' <param name="watermarkText">Text of the watermark.</param>

  29. Private Shared Sub InsertWatermarkText(ByVal doc As Document, ByVal watermarkText As String)

  30. ' Create a watermark shape. This will be a WordArt shape.

  31. ' You are free to try other shape types as watermarks.

  32. Dim watermark As New Shape(doc, ShapeType.TextPlainText)

  33.  
  34. ' Set up the text of the watermark.

  35. watermark.TextPath.Text = watermarkText

  36. watermark.TextPath.FontFamily = "Arial"

  37. watermark.Width = 500

  38. watermark.Height = 100

  39. ' Text will be directed from the bottom-left to the top-right corner.

  40. watermark.Rotation = -40

  41. ' Remove the following two lines if you need a solid black text.

  42. watermark.Fill.Color = Color.Gray ' Try LightGray to get more Word-style watermark

  43. watermark.StrokeColor = Color.Gray ' Try LightGray to get more Word-style watermark

  44.  
  45. ' Place the watermark in the page center.

  46. watermark.RelativeHorizontalPosition = RelativeHorizontalPosition.Page

  47. watermark.RelativeVerticalPosition = RelativeVerticalPosition.Page

  48. watermark.WrapType = WrapType.None

  49. watermark.VerticalAlignment = VerticalAlignment.Center

  50. watermark.HorizontalAlignment = HorizontalAlignment.Center

  51.  
  52. ' Create a new paragraph and append the watermark to this paragraph.

  53. Dim watermarkPara As New Paragraph(doc)

  54. watermarkPara.AppendChild(watermark)

  55.  
  56. ' Insert the watermark into all headers of each document section.

  57. For Each sect As Section In doc.Sections

  58. ' There could be up to three different headers in each section, since we want

  59. ' the watermark to appear on all pages, insert into all headers.

  60. InsertWatermarkIntoHeader(watermarkPara, sect, HeaderFooterType.HeaderPrimary)

  61. InsertWatermarkIntoHeader(watermarkPara, sect, HeaderFooterType.HeaderFirst)

  62. InsertWatermarkIntoHeader(watermarkPara, sect, HeaderFooterType.HeaderEven)

  63. Next sect

  64. End Sub

  65.  
  66. Private Shared Sub InsertWatermarkIntoHeader(ByVal watermarkPara As Paragraph, ByVal sect As Section, ByVal headerType As HeaderFooterType)

  67. Dim header As HeaderFooter = sect.HeadersFooters(headerType)

  68.  
  69. If header Is Nothing Then

  70. ' There is no header of the specified type in the current section, create it.

  71. header = New HeaderFooter(sect.Document, headerType)

  72. sect.HeadersFooters.Add(header)

  73. End If

  74.  
  75. ' Insert a clone of the watermark into the header.

  76. header.AppendChild(watermarkPara.Clone(True))

  77. End Sub

  78. End Class

  79. End Namespace


查看更多Aspose.Words信息

猜你喜欢

转载自blog.csdn.net/bruce135lee/article/details/81502702
今日推荐