-
Notifications
You must be signed in to change notification settings - Fork 36
Expand file tree
/
Copy pathProgram.cs
More file actions
126 lines (109 loc) · 3.79 KB
/
Program.cs
File metadata and controls
126 lines (109 loc) · 3.79 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
//
// Swiss QR Bill Generator for .NET
// Copyright (c) 2020 Robert Hegner
// Copyright (c) 2020 Manuel Bleichenbacher
// Licensed under MIT License
// https://opensource.org/licenses/MIT
//
using Codecrete.SwissQRBill.Generator;
using PdfSharp;
using PdfSharp.Drawing;
using PdfSharp.Fonts;
using PdfSharp.Pdf;
using PdfSharp.Pdf.IO;
using System.IO;
using System.Reflection;
namespace PDFsharp
{
/// <summary>
/// Console application demonstrating the use of SwissQRBill.NET with PDFsharp
/// </summary>
class Program
{
/// <summary>
/// Creates two PDF documents with a QR bill.
/// <para>
/// The first document consist of static text and the QR bill. It is
/// entirely created by this program.
/// </para>
/// <para>
/// The second document mainly consist of content from a template. The content is copied,
/// and the QR bill is added on the last page.
/// </para>
/// </summary>
static void Main()
{
GlobalFontSettings.UseWindowsFontsUnderWindows = true;
// create new PDF file with QR bill
using (var doc = new PdfDocument())
{
var page = doc.AddPage();
page.Size = PageSize.A4;
// static text on page
using (var graphic = XGraphics.FromPdfPage(page, XGraphicsUnit.Millimeter))
{
graphic.DrawString("Test Invoice", new XFont("Arial", 20.0), XBrushes.Black, 20.0, 150.0);
}
// add QR bill
using (var canvas = new PdfSharpCanvas(page, "Arial"))
{
QRBill.Draw(Bill, canvas);
}
// save document
doc.Save("Output1.pdf");
}
// append QR bill to existing PDF file (on last page)
using (var templateDoc = PdfReader.Open(OpenInvoice("invoice.pdf"), PdfDocumentOpenMode.Import))
using (var doc = new PdfDocument())
{
// copy all pages
foreach (var page in templateDoc.Pages)
{
doc.AddPage(page);
}
var lastPage = doc.Pages[doc.Pages.Count - 1];
using (var canvas = new PdfSharpCanvas(lastPage, "Arial"))
{
QRBill.Draw(Bill, canvas);
}
doc.Save("Output2.pdf");
}
}
static readonly Bill Bill = new Bill
{
// creditor data
Account = "CH9600781622484102000",
Creditor = new Address
{
Name = "Seemannschaft Rapperswil",
PostalCode = "8640",
Town = "Rapperswil",
CountryCode = "CH"
},
// payment data
Amount = null,
Currency = "CHF",
// more payment data
UnstructuredMessage = "Spende",
// format
Format = new BillFormat
{
Language = Language.DE,
SeparatorType = SeparatorType.DashedLineWithScissors,
OutputSize = OutputSize.A4PortraitSheet,
GraphicsFormat = GraphicsFormat.PDF,
FontFamily = "Arial"
}
};
private static Stream OpenInvoice(string filename)
{
Assembly assembly = Assembly.GetExecutingAssembly();
Stream resourceStream = assembly.GetManifestResourceStream(typeof(Program), $"Templates.{filename}");
if (resourceStream == null)
{
throw new FileNotFoundException($"Resource not found: Templates/{filename}");
}
return resourceStream;
}
}
}