forked from ststeiger/PdfSharpCore
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathProgram.cs
More file actions
105 lines (76 loc) · 3.21 KB
/
Program.cs
File metadata and controls
105 lines (76 loc) · 3.21 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
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);
SaveDocument(document, outName);
System.Console.WriteLine("Done!");
} // End Sub Main
} // End Class Program
} // End Namespace SampleApp