Good morning,
I’m using ASPOSE WORDS 19.10 for JAVA and I’ve to put in every page of every processed documents a unique QR code as a floating image next to bottom right corner of the page. I’ve already taken a look at https://docs.aspose.com/display/wordsjava/Working+with+Images but the “How to Insert Barcode on each Page of a Document” example doesn’t work…
The only way I found (thanks to stackoverflow) to do that is to create, for every document section and for every document pages, an IF statement like this:
for(int j = 0; j < doc.getSections().getCount(); j++){
builder.moveToSection(j);
builder.moveToHeaderFooter(HeaderFooterType.HEADER_PRIMARY);
insertQRCodeImage(builder, qrCodeIdentifier, numPages);
builder.moveToHeaderFooter(HeaderFooterType.HEADER_FIRST);
insertQRCodeImage(builder, qrCodeIdentifier, numPages);
builder.moveToHeaderFooter(HeaderFooterType.HEADER_EVEN);
insertQRCodeImage(builder, qrCodeIdentifier, numPages);
}
private static void insertQRCodeImage(DocumentBuilder builder, String qrCodeIdentifier, int numPages) throws Exception {
Field field = null;
for (int i = 1; i <= numPages; i++) {
field = builder.insertField("IF ");
builder.moveTo(field.getSeparator());
builder.insertField("PAGE");
builder.write(" = " + i);
builder.write(" \" ");
PageSetup ps = builder.getPageSetup();
Shape image = builder.insertImage(LiscorAsposeExpression.createBarcode(qrCodeIdentifier, i, numPages));
//put the qr code next to bottom right corner of the page
image.setRelativeHorizontalPosition(RelativeHorizontalPosition.PAGE);
image.setLeft(ps.getPageWidth() - image.getSizeInPoints().getX() -2);
image.setRelativeVerticalPosition(RelativeVerticalPosition.PAGE);
image.setTop(ps.getPageHeight() - image.getSizeInPoints().getY() - 2);
image.setWrapType(WrapType.NONE);
image.setBehindText(false);
builder.write(" \" ");
}
field.update();
}
In this way I’m sure that the generated QR code will be put in every document page. I tried with a little complex document (with 14 pages, 3 sections and different headers) but the performarce are pretty bad… more than 2 minutes against 3 seconds without QR code generation.
I saw that the only set of statements:
field = builder.insertField("IF ");
builder.moveTo(field.getSeparator());
builder.insertField("PAGE");
builder.write(" = " + i);
builder.write(" \" ");
took about 1.5 second, and the statement
field.update();
took about 1 second
So… is there a smarter way to do that?
Thanks