How to generate Swiss QR Code in C# and VBA

Swiss QR Code in C-Sharp and VBAToday, we are looking into the field of business computer science. Since end of April, SIX has released the specification for the new Swiss QR Code according to ISO 20022. The Swiss QR Code is an integral part of the new Swiss deposit slip, the so-called QR invoice, which shall be implemented by all companies by mid-2018 at the latest. The Swiss QR code is placed in the so-called “number part with QR code” of the QR invoice.

In the following, we will discuss how the Swiss QR code can be generated using C # or VBA. To do this, we use the free QRCoder library, which I developed almost 4 years ago and to which recently added the Swiss QR Code. One more thing before we start: Only the implementation of the QR code is shown, but not the creation of a complete QR invoice or a complete QR invoice form.

Swiss QR Code in C#

In order to be able to generate the QR codes, a reference to the QRCoder must be added first. The easiest way to do this is via NuGet package manager. (Alternatively, the source code can also be downloaded from the Github repository and compiled by yourself.)

The following command is required for the NuGet console installation:

Install-Package QRCoder

Alternatively, the package can also be installed through the graphical package manager in Visual Studio. The following steps are necessary: Rightlick “References” in the Project Explorer –> Manage NuGet packages… –> “Browse”-tab –> Search for “QRCoder” –> install

If the reference to the QRCoder is set, its functionality can be checked using the following short code sample:

QRCodeGenerator qrGenerator = new QRCodeGenerator();
//Bring "Sample-Text" into QR Data format
QRCodeData qrCodeData = qrGenerator.CreateQrCode("Sample-Text", QRCodeGenerator.ECCLevel.M);
//Create RAW-QR-code data
QRCode qrCode = new QRCode(qrCodeData);
//Create image out of raw QR code data
Bitmap qrCodeImage = qrCode.GetGraphic(20);

The image in qrCodeImage can now be saved or shown be assigning it to a PictureBox. If the basic QR code creation works, we can turn to the specific features of Swiss QR Code. It is important to understand that a Swiss QR Code is technically a normal QR code according to ISO/IEC 18004. The peculiarities are found only within the text, which is encoded and in the graphic output instead.

In order to generate the correct text, the QRCoder offers the so-called payload generator. This class helps to create the appropriate text for many different QR code formats. So also for the Swiss QR code. In the following we create the payload (= text encoded in the QR code) for a Swiss QR code, which transfers the sum of CHF 100.25 to the recipient John Doe from the parliament building. (A complete reference of the Swiss QR Code Payload Generator can be found here.)

//Create contact data
SwissQrCode.Contact contactGeneral = new SwissQrCode.Contact("John Doe", "3003", "Bern", "CH", "Parlamentsgebäude", "1");
//Create IBAN
SwissQrCode.Iban iban = new SwissQrCode.Iban("CH2609000000857666015", PayloadGenerator.SwissQrCode.Iban.IbanType.Iban);
//Create reference
SwissQrCode.Reference reference = new SwissQrCode.Reference(SwissQrCode.Reference.ReferenceType.QRR, "990005000000000320071012303", SwissQrCode.Reference.ReferenceTextType.QrReference);
//Set currency and amount to transfer
SwissQrCode.Currency currency = SwissQrCode.Currency.CHF;
decimal amount = 100.25m;

//Create Swiss QR code payload
SwissQrCode generator = new SwissQrCode(iban, currency, contactGeneral, reference, null, amount, null, null);
string payload = generator.ToString();

Now we have already made the largest part of the Swiss QR code generation. If we insert the string variable “payload” into our input example, we get an almost complete Swiss QR code:

//[...] Code for payload generation

QRCodeGenerator qrGenerator = new QRCodeGenerator();
//Bring the Swiss QR payload into QR Data format
QRCodeData qrCodeData = qrGenerator.CreateQrCode(payload, QRCodeGenerator.ECCLevel.M);
//Create RAW-QR-code data
QRCode qrCode = new QRCode(qrCodeData);
//Create image out of raw QR code data
Bitmap qrCodeImage = qrCode.GetGraphic(20);

At this point only one last step to get the valid Swiss QR code is missing – the small graphic with the Swiss cross in the middle of the QR code. The graphic itself can be obtained from the ISO 20022 pages:

https://www.paymentstandards.ch/dam/downloads/swiss-cross.zip

QRCoder already has a function to put images on QR codes. For our example, we put the cross-graphic named “CH-Kreuz_7mm.png” in the execution directory of our program. In order to provide the QR code with the cross, we change the following line in our example code:

//Old:
//Bitmap qrCodeImage = qrCode.GetGraphic(20);

//New:
Bitmap qrCodeImage = qrCode.GetGraphic(20, Color.Black, Color.White, (Bitmap)Bitmap.FromFile(Application.StartupPath + "\CH-Kreuz_7mm.png"), 14, 1);

The parameter “14” indicates that the cross should cover the QR code to 14%. According to specification, the cross must be 7mm in the printed Swiss QR code. Since the QR code itself is 46mm (or 49.2mm with white borders), 7mm is 14% coverage. The other parameters can be looked up in the QRCoder wiki.

Finally, once again the complete code example:

//Create contact data
SwissQrCode.Contact contactGeneral = new SwissQrCode.Contact("John Doe", "3003", "Bern", "CH", "Parlamentsgebäude", "1");
//Create IBAN
SwissQrCode.Iban iban = new SwissQrCode.Iban("CH2609000000857666015", PayloadGenerator.SwissQrCode.Iban.IbanType.Iban);
//Create reference
SwissQrCode.Reference reference = new SwissQrCode.Reference(SwissQrCode.Reference.ReferenceType.QRR, "990005000000000320071012303", SwissQrCode.Reference.ReferenceTextType.QrReference);
//Set currency and amount to transfer
SwissQrCode.Currency currency = SwissQrCode.Currency.CHF;
decimal amount = 100.25m;

//Create Swiss QR code payload
SwissQrCode generator = new SwissQrCode(iban, currency, contactGeneral, reference, null, amount, null, null);
string payload = generator.ToString();

QRCodeGenerator qrGenerator = new QRCodeGenerator();
//Bring the Swiss QR payload into QR Data format
QRCodeData qrCodeData = qrGenerator.CreateQrCode(payload, QRCodeGenerator.ECCLevel.M);
//Create RAW-QR-code data
QRCode qrCode = new QRCode(qrCodeData);
//Create Swiss QR code image out of raw QR code data
Bitmap qrCodeImage = qrCode.GetGraphic(20, Color.Black, Color.White, (Bitmap)Bitmap.FromFile(Application.StartupPath + "\CH-Kreuz_7mm.png"), 14, 1);

For questions or problems concerning the example, I am available in the comments.

Need help with implementation? Are you looking for an individual solution? Do you need round, transparent or specially designed QR codes? Then feel free to contact me.

I have already implemented several QR code solutions for customers in various industries. So why not for you?

Create Swiss QR Code in VBA

Since the QRCoder is written in C#.NET and it has no dependencies to additional libraries, the use of  it in VBA applications is possible in principle. However, a few changes are necessary:

  • Download the source from Github
  • Change project type to “COM”
  • Make the DLL “COM visible”
  • Implement a “COM-Interface” in the QRCoder because the methods are not VBA-compatible due to their method signatures with default values
  • Register the DLL (typically +TLB) via RegASM on the system

All these steps are definitely feasible – the QRCoder is already in use in some VBA projects. Should you need any help with the conversion/implementation in VBA, please feel free to contact me regarding a commission. (Smaller questions/assistance is usually also free.)

5 Comments

  1. Ed Dansays:

    Hello,

    Since do you have the code Swiss QR Code in VBA.

    Thanks for your answer

    Best regards

  2. This is really great! thanks for sharing! Im about to implement in our CH branch, i will let you know how it works out!

  3. Schmidhausersays:

    Hello
    I developped with an xBase tool (Xbase ++ from Alaska software) an invoicing system which I need to adapt for SWISS QR invoice.

    I’m however not a professional developper.

    My Xbase Tools can manage ActiveX components.

    Have you an ActiveX compatible QR library or any similar products that I may use to generate a Swiss QR Code in my software?

    Thanks for your answer

    Kind regards

    • Hi Schmidhauser,

      right now I don’t have a ready-to-run ActiveX solution, but since the library is built in C#.NET and the source code can be taken from GitHub, you can slightly rewrite it and compile it as “COM accessible” dll, which then can be interfaced. (Just Google for “How to make .NET dll COM-Visible”.) If you need support or want a ready-to-run solution with documentation, feel free to send me an email, so that I can provide you with a quote.

      Kind regards

Leave a comment

Please be polite. We appreciate that. Your email address will not be published and required fields are marked