Skip to content

Commit d146153

Browse files
committed
Update code snippet
1 parent 228d75f commit d146153

File tree

1 file changed

+25
-55
lines changed

1 file changed

+25
-55
lines changed

README.md

Lines changed: 25 additions & 55 deletions
Original file line numberDiff line numberDiff line change
@@ -24,64 +24,34 @@ See open issues.
2424

2525
## Usage
2626

27+
The following code snippet creates a simple PDF-file with the text 'Hello World!'.
28+
The code is written for a .NET 6 console app with top level statements.
29+
2730
```csharp
28-
// See the "Example" Project for a MigraDoc example
29-
static void Main(string[] args)
30-
{
31-
GlobalFontSettings.FontResolver = new FontResolver();
32-
33-
var document = new PdfDocument();
34-
var page = document.AddPage();
35-
var gfx = XGraphics.FromPdfPage(page);
36-
var font = new XFont("OpenSans", 20, XFontStyle.Bold);
37-
38-
gfx.DrawString("Hello World!", font, XBrushes.Black, new XRect(20, 20, page.Width, page.Height), XStringFormats.Center);
39-
40-
document.Save("test.pdf");
41-
}
42-
43-
// This implementation is obviously not very good --> Though it should be enough for everyone to implement their own.
44-
public class FontResolver : IFontResolver
45-
{
46-
public byte[] GetFont(string faceName)
47-
{
48-
using(var ms = new MemoryStream())
49-
{
50-
using(var fs = File.Open(faceName, FileMode.Open))
51-
{
52-
fs.CopyTo(ms);
53-
ms.Position = 0;
54-
return ms.ToArray();
55-
}
56-
}
57-
}
58-
public FontResolverInfo ResolveTypeface(string familyName, bool isBold, bool isItalic)
59-
{
60-
if (familyName.Equals("OpenSans", StringComparison.CurrentCultureIgnoreCase))
61-
{
62-
if (isBold && isItalic)
63-
{
64-
return new FontResolverInfo("OpenSans-BoldItalic.ttf");
65-
}
66-
else if (isBold)
67-
{
68-
return new FontResolverInfo("OpenSans-Bold.ttf");
69-
}
70-
else if (isItalic)
71-
{
72-
return new FontResolverInfo("OpenSans-Italic.ttf");
73-
}
74-
else
75-
{
76-
return new FontResolverInfo("OpenSans-Regular.ttf");
77-
}
78-
}
79-
return null;
80-
}
81-
}
82-
}
31+
using PdfSharpCore.Drawing;
32+
using PdfSharpCore.Fonts;
33+
using PdfSharpCore.Pdf;
34+
using PdfSharpCore.Utils;
35+
36+
GlobalFontSettings.FontResolver = new FontResolver();
37+
38+
var document = new PdfDocument();
39+
var page = document.AddPage();
40+
41+
var gfx = XGraphics.FromPdfPage(page);
42+
var font = new XFont("Arial", 20, XFontStyle.Bold);
43+
44+
var textColor = XBrushes.Black;
45+
var layout = new XRect(20, 20, page.Width, page.Height);
46+
var format = XStringFormats.Center;
47+
48+
gfx.DrawString("Hello World!", font, textColor, layout, format);
49+
50+
document.Save("helloworld.pdf");
8351
```
8452

53+
See the [example project](#example-project) for MigraDoc usage.
54+
8555
## License
8656

8757
Distributed under the MIT License. See [`LICENSE.md`](LICENSE.md) for more information.

0 commit comments

Comments
 (0)