Skip to content

Commit a4c878c

Browse files
committed
Extend PDFBox 3 example
1 parent 0f221a7 commit a4c878c

File tree

3 files changed

+26
-10
lines changed

3 files changed

+26
-10
lines changed

examples/pdfbox3/.gitignore

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1,3 @@
1-
/qrbill.pdf
1+
/invoice-0.pdf
2+
/invoice-1.pdf
3+
/invoice-2.pdf

examples/pdfbox3/src/main/java/net/codecrete/qrbill/examples/QRBillExample.java

Lines changed: 23 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,11 @@
66
//
77
package net.codecrete.qrbill.examples;
88

9+
import net.codecrete.qrbill.canvas.PDFCanvas;
910
import net.codecrete.qrbill.generator.*;
1011

1112
import java.io.IOException;
13+
import java.net.URISyntaxException;
1214
import java.nio.file.Files;
1315
import java.nio.file.Path;
1416
import java.nio.file.Paths;
@@ -22,7 +24,7 @@
2224
*/
2325
public class QRBillExample {
2426

25-
public static void main(String[] args) {
27+
public static void main(String[] args) throws IOException, URISyntaxException {
2628

2729
// Setup bill
2830
Bill bill = new Bill();
@@ -57,18 +59,30 @@ public static void main(String[] args) {
5759
format.setLanguage(Language.DE);
5860
bill.setFormat(format);
5961

60-
// Generate QR bill
62+
// Generate new PDF with QR bill
6163
byte[] svg = QRBill.generate(bill);
64+
Path path0 = Paths.get("invoice-0.pdf");
65+
Files.write(path0, svg);
66+
System.out.println("QR bill saved at " + path0.toAbsolutePath());
6267

63-
// Save QR bill
64-
Path path = Paths.get("qrbill.pdf");
65-
try {
66-
Files.write(path, svg);
67-
} catch (IOException e) {
68-
throw new RuntimeException(e);
68+
// Load existing PDF
69+
Path invoiceWithoutQRBill = Paths.get(QRBillExample.class.getResource("/invoice.pdf").toURI());
70+
71+
// Add QR bill to last page
72+
try (PDFCanvas canvas = new PDFCanvas(invoiceWithoutQRBill, PDFCanvas.LAST_PAGE)) {
73+
QRBill.draw(bill, canvas);
74+
Path path = Paths.get("invoice-1.pdf");
75+
canvas.saveAs(path);
76+
System.out.println("Invoice 1 saved at " + path.toAbsolutePath());
6977
}
7078

71-
System.out.println("QR bill saved at " + path.toAbsolutePath());
79+
// Append QR bill to a new page
80+
try (PDFCanvas canvas = new PDFCanvas(invoiceWithoutQRBill, PDFCanvas.NEW_PAGE_AT_END)) {
81+
QRBill.draw(bill, canvas);
82+
Path path = Paths.get("invoice-2.pdf");
83+
canvas.saveAs(path);
84+
System.out.println("Invoice 2 saved at " + path.toAbsolutePath());
85+
}
7286

7387
System.out.println("Generated with version " + QRBill.getLibraryVersion());
7488
}
44 KB
Binary file not shown.

0 commit comments

Comments
 (0)