-
Notifications
You must be signed in to change notification settings - Fork 0
/
PdfGenerator.cs
50 lines (40 loc) · 1.71 KB
/
PdfGenerator.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
using PdfSharp;
using PdfSharp.Pdf;
using PdfSharp.Drawing;
using PdfSharp.Fonts;
using PdfSharp.Snippets.Font;
public class PdfGenerator
{
public static byte[] CreatePdf()
{
using (var memoryStream = new MemoryStream())
{
if (Capabilities.Build.IsCoreBuild)
GlobalFontSettings.FontResolver = new FailsafeFontResolver();
// Create a new PDF document
PdfDocument document = new PdfDocument();
document.Info.Title = "Generated with PDFsharp";
document.Info.Subject = "Just a simple Hello-World program.";
// Create an empty page
PdfPage page = document.AddPage();
// Get an XGraphics object for drawing
XGraphics gfx = XGraphics.FromPdfPage(page);
// Draw two lines with a red default pen.
var width = page.Width;
var height = page.Height;
gfx.DrawLine(XPens.Red, 0, 0, width, height);
gfx.DrawLine(XPens.Red, width, 0, 0, height);
// Draw a circle with a red pen and white brush.
var radius = width / 5;
gfx.DrawEllipse(new XPen(XColors.Red, 1.5), XBrushes.White, new XRect(width / 2 - radius, height / 2 - radius, 2 * radius, 2 * radius));
// Create a font.
XFont font = new XFont("Times New Roman", 20, XFontStyleEx.BoldItalic);
// Draw the text.
gfx.DrawString("Hello, PDFsharp!", font, XBrushes.Black,
new XRect(0, 0, page.Width, page.Height), XStringFormats.Center);
// Save the document to the memory stream
document.Save(memoryStream);
return memoryStream.ToArray();
}
}
}