-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathSample05.cs
202 lines (165 loc) · 6.69 KB
/
Sample05.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
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
using System.Collections.Generic;
using System.Diagnostics;
using System.Drawing;
using iTin.Core.ComponentModel;
using iTin.Core.Models.Design.Enums;
using iTin.Logging.ComponentModel;
using iTin.Utilities.Pdf.Design.Image;
using iTin.Utilities.Pdf.Design.Styles;
using iTin.Utilities.Pdf.Design.Table;
using iPdfWriter.ComponentModel;
using iPdfWriter.Operations.Actions;
using iPdfWriter.Operations.Replace;
using iPdfWriter.Operations.Replace.Replacement.Text;
using iPdfWriter.SystemTag;
namespace iPdfWriter.ConsoleApp.Code
{
using ComponentModel.Helpers;
/// <summary>
/// Shows the use of <b>System Tags</b> such as page number of a document and the use of merge of several entries to compose a complete document.
/// </summary>
internal static class Sample05
{
public static void Generate(ILogger logger, YesNo useTestMode = YesNo.No)
{
#region Initialize timer
var sw = new Stopwatch();
sw.Start();
#endregion
#region page-1
var page1 = new PdfInput
{
AutoUpdateChanges = true,
Input = "~/Resources/Sample-05/file-sample-1.pdf"
};
// Report title
page1
.Replace(new ReplaceText(
new WithTextObject
{
Text = "#TITLE#",
NewText = "Lorem ipsum",
UseTestMode = useTestMode,
Offset = PointF.Empty,
Style = StylesHelper.Sample05.TextStylesTable["ReportTitle"],
ReplaceOptions = ReplaceTextOptions.AccordingToMargins
}))
// bar-chart image
.Replace(new ReplaceText(
new WithImageObject
{
Text = "#BAR-CHART#",
UseTestMode = useTestMode,
Offset = PointF.Empty,
Style = PdfImageStyle.Center,
ReplaceOptions = ReplaceTextOptions.AccordingToMargins,
Image = PdfImage.FromFile("~/Resources/Sample-05/Images/bar-chart.png")
}));
#endregion
#region page-2
var page2 = new PdfInput
{
AutoUpdateChanges = true,
Input = "~/Resources/Sample-05/file-sample-2.pdf"
};
// html table
page2.Replace(new ReplaceText(
new WithTableObject
{
Text = "#DATA-TABLE#",
UseTestMode = useTestMode,
Offset = PointF.Empty,
Style = PdfTableStyle.Default,
ReplaceOptions = ReplaceTextOptions.FromPositionToRightMargin,
Table = PdfTable.CreateFromHtml(HtmlDataHelper.GenerateHtmlDatatable())
}));
#endregion
#region page-3
var page3 = new PdfInput
{
AutoUpdateChanges = true,
Input = "~/Resources/Sample-05/file-sample-3.pdf"
};
#endregion
#region page-4
var page4 = new PdfInput
{
AutoUpdateChanges = true,
Input = "~/Resources/Sample-05/file-Sample-4.pdf"
};
// Inserts image
page4.Replace(new ReplaceText(
new WithImageObject
{
Text = "#IMAGE1#",
UseTestMode = useTestMode,
Offset = PointF.Empty,
Style = PdfImageStyle.Default,
ReplaceOptions = ReplaceTextOptions.AccordingToMargins,
Image = PdfImage.FromFile("~/Resources/Sample-05/Images/image-1.jpg")
}));
#endregion
#region merge
// Defines system tags to replace > page number
var systemTags = new SystemTagsCollection
{
new PageNumberSystemTag
{
UseTestMode = useTestMode,
Offset = PointF.Empty,
Style = StylesHelper.Sample05.TextStylesTable["PageNumber"],
ReplaceOptions = ReplaceTextOptions.FromPositionToRightMargin
}
};
// Defines global text replacements to replace > header text
var globalReplacements = new GlobalReplacementsCollection
{
new WithTextObject
{
Text = "#HEADER-TEXT#",
NewText = "Report Name - Lorem ipsum dolor",
Style = StylesHelper.Sample05.TextStylesTable["Header"],
ReplaceOptions = ReplaceTextOptions.FromLeftMarginToNextElement,
UseTestMode = useTestMode,
Offset = PointF.Empty
}
};
// Defines merge configuration, includes tags, global replacements and allow compress the merged output
var files = new PdfObject(new PdfObjectConfig { Tags = systemTags, GlobalReplacements = globalReplacements })
{
Items = new List<PdfInput>
{
new() { Index = 0, Input = page1 },
new() { Index = 1, Input = page2 },
new() { Index = 2, Input = page3 },
new() { Index = 3, Input = page4 }
}
};
var mergeResult = files.TryMergeInputs();
if (!mergeResult.Success)
{
logger.Info(" > Error creating output merge result");
logger.Info($" > Error: {mergeResult.Errors.AsMessages().ToStringBuilder()}");
return;
}
#endregion
#region save
// Saves merged result to disk
var saveResult = mergeResult.Result.Action(new SaveToFile { OutputPath = "~/Output/Sample05/Sample-05" });
var ts = sw.Elapsed;
sw.Stop();
if (!saveResult.Success)
{
logger.Info(" > Error while saving to disk");
logger.Info($" > Error: {saveResult.Errors.AsMessages().ToStringBuilder()}");
logger.Info($" > Elapsed time: {ts.Hours:00}:{ts.Minutes:00}:{ts.Seconds:00}.{ts.Milliseconds / 10:00}");
return;
}
logger.Info(" > Saved to disk correctly");
logger.Info(" > Path: ~/Output/Sample05/Sample-05.pdf");
logger.Info($" > Elapsed time: {ts.Hours:00}:{ts.Minutes:00}:{ts.Seconds:00}.{ts.Milliseconds / 10:00}");
#endregion
}
}
}