-
Notifications
You must be signed in to change notification settings - Fork 262
Expand file tree
/
Copy pathProgram.cs
More file actions
151 lines (109 loc) · 5.72 KB
/
Program.cs
File metadata and controls
151 lines (109 loc) · 5.72 KB
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
using PdfSharpCore.Drawing;
using PdfSharpCore.Drawing.Layout;
using PdfSharpCore.Drawing.Layout.enums;
using PdfSharpCore.Pdf;
namespace SampleApp
{
public class Program
{
private static string GetOutFilePath(string name)
{
string OutputDirName = @".";
return System.IO.Path.Combine(OutputDirName, name);
}
private static void SaveDocument(PdfSharpCore.Pdf.PdfDocument document, string name)
{
string outFilePath = GetOutFilePath(name);
string? dir = System.IO.Path.GetDirectoryName(outFilePath);
if (dir != null && !System.IO.Directory.Exists(dir))
{
System.IO.Directory.CreateDirectory(dir);
}
document.Save(outFilePath);
}
public static void Main(string[] args)
{
System.Console.WriteLine("Starting...");
const string outName = "test1.pdf";
PdfDocument? document = new PdfDocument();
PdfPage? pageNewRenderer = document.AddPage();
XGraphics? renderer = XGraphics.FromPdfPage(pageNewRenderer);
renderer.DrawString(
"Testy Test Test"
, new XFont("Arial", 12)
, XBrushes.Black
, new XPoint(12, 12)
);
XTextFormatter? formatter = new XTextFormatter(renderer);
var font = new XFont("Arial", 12);
var brush = XBrushes.Black;
formatter.AllowVerticalOverflow = true;
var originalLayout = new XRect(0, 30, 120, 120);
var text = "More and more text boxes to show alignment capabilities"; // " with addipional gline";
var anotherText =
"Text to determine the size of the box I would like to place the text I'm goint to test";
var rect = formatter.GetLayout(
anotherText,
font,
brush,
originalLayout);
rect.Location = new XPoint(50, 50);
formatter.AllowVerticalOverflow = false;
// Prepare brush to draw the box that demostrates the text fits and aligns correctly
var translucentBrush = new XSolidBrush(XColor.FromArgb(20, 0, 0, 0));
// Draw the string with default alignments
formatter.DrawString(
text,
font,
brush,
rect
);
// For checking purposes
renderer.DrawRectangle(translucentBrush, rect);
rect.Location = new XPoint(300, 50);
// Draw the string with custom alignments
formatter.DrawString(text, font, brush, rect, new TextFormatAlignment()
{
Horizontal = XParagraphAlignment.Center,
Vertical = XVerticalAlignment.Middle
});
// For checking purposes
renderer.DrawRectangle(translucentBrush, rect);
////////////////
// Use a new formatter to get rid of any cached values from above
formatter = new XTextFormatter(renderer);
formatter.AllowVerticalOverflow = true;
formatter.Alignment = XParagraphAlignment.Justify;
formatter.VerticalAlignment = XVerticalAlignment.Top;
// Test justified text without line breaks
text = @"Lorem ipsum dolor sit amet, consectetur adipiscing elit. Sed non sapien leo. Sed lacinia velit ex, id auctor tellus varius a. Cras scelerisque in risus vitae hendrerit. Duis venenatis felis in lacinia vestibulum. Proin mauris ex, efficitur nec tincidunt in, imperdiet eget risus. Nulla porttitor mollis pellentesque.";
// Define the area to draw the text
rect = new XRect(0, 0, 310, 200);
// Get the actual layout rectangle
rect = formatter.GetLayout(text, font, brush, rect);
// Move it to the desired location
rect.Location = new XPoint(40, 150);
// Draw the string with justify alignment
formatter.DrawString(text, font, brush, rect);
// For checking purposes
renderer.DrawRectangle(translucentBrush, rect);
////////////////
// Test justified text with line breaks
// First paragraph should be laid out identically to the one above
text = @"Lorem ipsum dolor sit amet, consectetur adipiscing elit. Sed non sapien leo. Sed lacinia velit ex, id auctor tellus varius a. Cras scelerisque in risus vitae hendrerit. Duis venenatis felis in lacinia vestibulum. Proin mauris ex, efficitur nec tincidunt in, imperdiet eget risus. Nulla porttitor mollis pellentesque.
Maecenas mollis sollicitudin felis at imperdiet. Duis dignissim purus quis interdum mattis. Nam sit amet quam quis enim hendrerit tincidunt. Aliquam euismod metus justo, non lobortis risus vehicula in. Pellentesque tempus, leo at placerat interdum, diam lectus gravida purus, id placerat justo quam nec mauris.";
// Define the area to draw the text
rect = new XRect(0, 0, 310, 200);
// Get the actual layout rectangle
rect = formatter.GetLayout(text, font, brush, rect);
// Move it to the desired location
rect.Location = new XPoint(40, 280);
// Draw the string with justify alignment
formatter.DrawString(text, font, brush, rect);
// For checking purposes
renderer.DrawRectangle(translucentBrush, rect);
SaveDocument(document, outName);
System.Console.WriteLine("Done!");
} // End Sub Main
} // End Class Program
} // End Namespace SampleApp