DEV Community

developercheese
developercheese

Posted on

Automatic reading Swiss QR bills

The banking sector is an important part of the Swiss economy. Electronic wire transfers for the settlement of bills had been standardized several decades ago. Most of the payments done until 2022 were based on a standard defined by the Swiss Post.

Old style payment slip

This standard defined what information the paper slips needed to contain and also defined a single line of numbers that contained the most important routing information which could be read electronically. The amount of information was very limited, which meant that a lot of data still needed to be entered manually to register an invoice in any accounting system.

With the introduction of the Swiss QR bill standard in 2020 and the phasing out of the previous standard in 2022, all software that handles invoices either in the way of producing invoices or processing incoming invoices is be able to handle these new type of invoices.

New style payment slip

While the payments slips contain also human readable information about the payment, this information is now included as a machine readable QR code as well.

The content of the QR code the highly standardized to allow processing of incoming invoices fully automatically.

The QR bill structure contains defined rows of data with line breaks. Every QR bill starts with the letters SPC on the first line, followed by the version on the second line.

SPC
0200
1
CH4431999123000889012
S
Robert Schneider AG
Rue du Lac
1268/2/22
2501
Biel
CH
S
Robert Schneider Services Switzerland AG
Rue du Lac
1268/3/1
2501
Biel
CH
...

Because the QR codes included all the information needed, additional OCR or manual entry of data is not required to read, recognize, categorize and process incoming invoices.

Aspose with its two component Aspose.Pdf and Aspose.Barcode provides all the required components for implementation in .NET to process incoming invoices and extract all the required information in just a few lines of code.

In the first step you initialize the incoming PDF document that you might have received by email or which was received by post and scanned into a PDF file.

Initialization

We are also setting up the resolution and PngDevice to allow us to render the PDF page to a image that can then be used for the QR recognition code.

The QR code can be on any page, so we are looping using a foreach to go through the pages.

foreach (Page p in pdfDocument.Pages) // loop through pages
{
   using (MemoryStream imageStream = new MemoryStream())
   {
      pngDevice.Process(p, imageStream); // write pdf page to PNG
      imageStream.Position = 0;
      using (BarCodeReader reader = new BarCodeReader(imageStream, DecodeType.QR))
      {
         foreach (BarCodeResult result in reader.ReadBarCodes())
         {
            code = result.CodeText;
            if (code.StartsWith("SPC"))
            {
               qrcode = code;
               imageStream.Close();
               return qrcode;
            }
            else
            {
               code = "";
            }
         }
      }
      imageStream.Close();
   }
}
Enter fullscreen mode Exit fullscreen mode

The Aspose libraries allow the full reading of the PDF document, conversion of the pages to bitmaps and recognize the contained QR codes with high reliability.

After extracting the content of the QR code, split the resulting string into the individual parts (creditor, accounts, reference numbers, amount, currency etc.) that are contained in the complete content of the QR code.

This process can be either implemented in a user facing application or also in a background service that either monitors email inboxes for incoming invoices or drive folders for scans coming in from a centralized mail office.

The complete structure of the QR bill code is described in the official implementation guidelines from SIX which be can be found at SIX.

With the help of the underlying Aspose libraries, implementing the business logic can be done within one hour. Don't forget to look for blocked files and handle errors nicely when coming across corrupt pdf files or things like this.

Top comments (0)