Hi JP.
You can use the following Java code to insert a different barcode on each page. In this code, I have inserted the same barcode on each page but you can provide a different barcode based on the pageId in the InsertBarcodeIntoFooter method.
public static void main(String[] args)
{
try {
<?xml:namespace prefix = "o" ns = "urn:schemas-microsoft-com:office:office" />
com.aspose.words.License wLic = new
com.aspose.words.License();
wLic.setLicense("Aspose.Total.Java.lic");
// Create a blank documenet.
Document doc = new Document();
DocumentBuilder builder = new
DocumentBuilder(doc);
// The number of pages the document
should have.
int numPages = 4;
// The document starts with one
section, insert the barcode into this existing section.
InsertBarcodeIntoFooter(builder,
doc.getFirstSection(), 1, HeaderFooterType.FOOTER_PRIMARY);
int i = 1;
while (i < numPages)
{
// Clone the first
section and add it into the end of the document.
Section cloneSection =
(Section)doc.getFirstSection().deepClone(false);
cloneSection.getPageSetup().setSectionStart(SectionStart.NEW_PAGE);
doc.appendChild(cloneSection);
// Insert the barcode
and other information into the footer of the section.
InsertBarcodeIntoFooter(builder,
cloneSection, i, HeaderFooterType.FOOTER_PRIMARY);
i += 1;
}
// Save the document as a PDF to
disk. You can also save this directly to a stream.
doc.save("Document
out.docx");
System.out.println("Done");
} catch (Exception e) {
// TODO Auto-generated
catch block
e.printStackTrace();
}
}
private static void
InsertBarcodeIntoFooter(DocumentBuilder builder, Section section, int pageId, int footerType) throws Exception
{
// Move to the
footer type in the specific section.
builder.moveToSection(section.getDocument().indexOf(section));
builder.moveToHeaderFooter(footerType);
// Insert the barcode,
then move to the next line and insert the ID along with the page number.
// Use pageId if
you need to insert a different barcode on each page. 0 = First page, 1 =
Second page etc.
builder.insertImage(ImageIO.read(new File("C:\\Barcode1.png")));
builder.writeln();
builder.write("1234567890");
builder.insertField("PAGE");
// Create a right
aligned tab at the right margin.
double tabPos =
section.getPageSetup().getPageWidth() - section.getPageSetup().getRightMargin()
- section.getPageSetup().getLeftMargin();
builder.getCurrentParagraph().getParagraphFormat().getTabStops().add(new TabStop(tabPos,
TabAlignment.RIGHT, TabLeader.NONE));
// Move to the
right hand side of the page and insert the page and page total.
builder.write(ControlChar.TAB);
builder.insertField("PAGE");
builder.write(" of ");
builder.insertField("NUMPAGES");
}
Best Regards,