Add to existing PDF - manuelbl/SwissQRBill GitHub Wiki
In the process of generating the invoices for your clients, it might be useful to first generate the detailed invoice and add to add the QR bill (payment part and receipt) in a separate step. The library supports this flow: it can add the QR bill to an existing PDF.
The QR bill is add at the bottom of a page, either:
- on the last page
- on a new page at the end of the document
- to a specific page
Java
A full example can be found at https://github.com/manuelbl/SwissQRBill/tree/master/examples/append_to_pdf.
At the core, it's:
Bill bill = ...; // bill data
Path invoiceWithoutQRBill = ...; // path to existing PDF
Path invoiceWithQRBill = ...; // path to new PDF
try (PDFCanvas canvas = new PDFCanvas(invoiceWithoutQRBill, PDFCanvas.LAST_PAGE)) {
QRBill.draw(bill, canvas);
canvas.saveAs(invoiceWithQRBill);
}
.NET
As there is no good library for generating PDF file that is free even for commercial use, this library implements its own simple PDF generator for generating QR bills as PDF files. However, it does not support appending to an existing PDF file.
So the use case is achieved by using iText 7 Community. Note that iText requires a license for commercial use.
A full example can be found at https://github.com/manuelbl/SwissQRBill.NET/tree/master/Examples/iText. It consists of the example code as well as a Canvas implementation for iText 7.
The core of the example is:
Bill bill = ...; // bill data
string invoiceWithoutQRBill = ...; // path to existing PDF
string invoiceWithQRBill = ...; // path to new PDF
using (FileStream fs = new FileStream(invoiceWithQRBill, FileMode.Create))
using (IText7Canvas canvas = new IText7Canvas(OpenPdfInvoice(invoiceWithoutQRBill), fs, IText7Canvas.LastPage))
{
QRBill.Draw(bill, canvas);
}