-
Notifications
You must be signed in to change notification settings - Fork 3
/
WatermarkHelper.cs
67 lines (57 loc) · 2.38 KB
/
WatermarkHelper.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
using iDiTect.Word.Editing;
using iDiTect.Word.IO;
using iDiTect.Word.Watermarks;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Windows.Media;
namespace iDiTect.Word.Demo
{
public static class WatermarkHelper
{
public static void AddTextWatermark()
{
WordFile wordFile = new WordFile();
WordDocument document = wordFile.Import(File.ReadAllBytes("Sample.docx"));
//Customize the setting of text watermark
TextWatermarkSettings setting = new TextWatermarkSettings();
setting.Text = "watermark";
setting.Width = 100;
setting.Height = 50;
//The opacity value is between 0 and 1.
setting.Opacity = 0.7;
//Set the watermark rotation
setting.Rotation = -45;
setting.TextColor = Colors.Red;
//Create watermark with settings
Watermark textWatermark = new Watermark(setting);
//Add watermark to Header object
Header header = document.Sections[0].Headers.Add();
header.Watermarks.Add(textWatermark);
File.WriteAllBytes("AddTextWatermark.docx", wordFile.Export(document));
}
public static void AddImageWatermark()
{
WordFile wordFile = new WordFile();
WordDocument document = wordFile.Import(File.ReadAllBytes("Sample.docx"));
WordDocumentBuilder builder = new WordDocumentBuilder(document);
//Customize the setting of image watermark
ImageWatermarkSettings setting = new ImageWatermarkSettings();
setting.Width = 100;
setting.Height = 50;
setting.Rotation = -45;
using (Stream stream = File.OpenRead("watermark.png"))
{
setting.ImageSource = new Basic.Media.ImageSource(stream, "png");
}
//Create watermark with settings
Watermark imageWatermark = new Watermark(setting);
//Add watermark to Header object
builder.SetWatermark(imageWatermark, document.Sections[0].Headers.Add());
//builder.SetWatermark(imageWatermark, document.Sections[0], HeaderFooterType.Default);
File.WriteAllBytes("AddImageWatermark.docx", wordFile.Export(document));
}
}
}