-
Notifications
You must be signed in to change notification settings - Fork 3
/
TextHelper.cs
152 lines (130 loc) · 6.29 KB
/
TextHelper.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
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
using iDiTect.Pdf.Basic.Model;
using iDiTect.Pdf.Basic.Primitives;
using iDiTect.Pdf.ColorSpaces;
using iDiTect.Pdf.Editing;
using iDiTect.Pdf.Editing.Flow;
using iDiTect.Pdf.IO;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Media;
namespace iDiTect.Pdf.Demo
{
public static class TextHelper
{
public static void AddText()
{
//This example is using page level builder, all the text will be add in current pdf page
PdfDocument document = new PdfDocument();
PdfPage page = document.Pages.AddPage();
//Create page level builder
PageContentBuilder builder = new PageContentBuilder(page);
//Add text using builder's DrawText directly
builder.TextState.FontSize = 18;
builder.GraphicState.FillColor = RgbColors.Black;
//Set the position for text
builder.Position.Translate(50, 50);
builder.DrawText("Insert text directly in builder");
//Add text using Block object
//Set the position for first block
builder.Position.Translate(50, 100);
//Create the first block with different font style
Block block = new Block();
block.HorizontalAlignment = Editing.Flow.HorizontalAlignment.Left;
block.TextState.FontSize = 16;
block.TextState.SetFont(new FontFamily("Times New Roman"));
block.InsertText("This is a Normal style sentence with target font.");
block.TextState.SetFont(new FontFamily("Times New Roman"), FontStyles.Italic, FontWeights.Bold);
block.InsertText(" This is another sentence in Italic and Bold style with target font.");
//Add first sentence to page, if you don't know how much height need to show the text, just using
//PositiveInfinity property, our builder will measure the height depend on the given width automatically
builder.DrawBlock(block, new Size(page.Size.Width - 50 * 2, double.PositiveInfinity));
//Get the actual size used by the first block, it works after block drawn
Size usedSize = block.DesiredSize;
//Set the position for the second block
builder.Position.Translate(50, 100 + usedSize.Height);
//Crete the second block with different text color
Block block2 = new Block();
block2.GraphicState.FillColor = RgbColors.Black;
block2.InsertText("Black Text,");
block2.GraphicState.FillColor = new RgbColor(255, 0, 0);
block2.InsertText(" Colorful Text.");
//Set wanted line width to measure the size used by the second block
Size needSize2 = block2.Measure(page.Size.Width - 50 * 2);
//Add second sentence to page
builder.DrawBlock(block2, needSize2);
using (FileStream fs = File.OpenWrite("InsertText.pdf"))
{
PdfFile pdfFile = new PdfFile();
pdfFile.Export(document, fs);
}
}
public static void AddText2()
{
//This example is using document level builder(flow-like), if the text inserted is out of one page,
//the builder will insert the left text on a second page automatically
PdfDocument document = new PdfDocument();
//Create document level builder
using(PdfDocumentBuilder builder = new PdfDocumentBuilder(document))
{
//Set page size and margins
builder.SectionState.PageSize = PaperTypeConverter.ToSize(PaperTypes.A4);
builder.SectionState.PageMargins = new Padding(20);
//Set text alignment
builder.ParagraphState.HorizontalAlignment = Editing.Flow.HorizontalAlignment.Center;
//Set font style
builder.CharacterState.SetFont(new FontFamily("Times New Roman"));
builder.CharacterState.FontSize = 40;
builder.InsertParagraph();
builder.InsertText("Document Title");
builder.InsertLineBreak();
//Add several paragraphs to page
builder.ParagraphState.HorizontalAlignment = Editing.Flow.HorizontalAlignment.Left;
builder.CharacterState.FontSize = 20;
for (int i = 0; i < 20; i++)
{
builder.InsertParagraph();
string text = "";
for (int j = 1; j < 11; j++)
{
text += "This is sentence " + j.ToString() + ". ";
}
builder.InsertText(text);
builder.InsertLineBreak();
}
}
using (FileStream fs = File.Create("InsertText2.pdf"))
{
PdfFile pdfFile = new PdfFile();
pdfFile.Export(document, fs);
}
}
public static void AddTextWatermark()
{
PdfFile pdfFile = new PdfFile();
PdfDocument document = pdfFile.Import(File.ReadAllBytes("sample.pdf"));
//Get first page of pdf
PdfPage page = document.Pages[0];
PageContentBuilder builder = new PageContentBuilder(page);
//Create a block with watermark text
Block block = new Block();
//Set text color and font size
block.GraphicState.FillColor = new RgbColor(100, 255, 0, 0);
block.TextState.FontSize = 50;
//Set a text format.
block.HorizontalAlignment = Editing.Flow.HorizontalAlignment.Center;
block.VerticalAlignment = Editing.Flow.VerticalAlignment.Center;
block.InsertText("watermark");
Size needSize = block.Measure();
//Set watermark text position and rotation
builder.Position.Translate((page.Size.Width - needSize.Width) / 2, (page.Size.Height - needSize.Height) / 2);
builder.Position.Rotate(-45);
builder.DrawBlock(block);
File.WriteAllBytes("TextWatermark.pdf", pdfFile.Export(document));
}
}
}