Hi,
I feel that I am so close to the solutions, but I need some help. Basically, I am trying to print labels from Java. If some tries to ship 10 pieces, I have to print 10 labels with the same information except � for each label I need to pring �piece x of y�. So I created a template pdf file that represents a single lable. The steps are as fillowing -
- Use a label template PDF with logos, text and form fields. The template represents a single label format.
- Fill form fields in the template from database. For example fill in Shipper and Receiver address. All the labels I want to print will have the same shipper and receiver information. So, I need to add the same template several times in a single pdf document as the next step.
- Generate the pdf which will have multiple labels/templates. The number of templates/labels to add depends on number of pieces going to be shipped.
The problems is for each label/template I need to dynamically say �Piece x of y�, �Piece 2 of 5� and so on. The form/template (step 1) has everything except Piece x field. After adding the templates (step 3), how do I access the fields that represent piece x in each label and update it?
I also followed the �Page number� example, but I am not sure how to change piece x in all the templates without finding the exact location to write.
I feel I am only a step away from the solution. Please help. See the code below.
Thanks in advance
Sam
Code:
public static void main(String[] args) {
// creation of the document with a certain size and certain margins
try {
//Read template for a Label
PdfReader reader = new PdfReader("c:\\downloads\\iText\\DBA.pdf");
Rectangle psize = reader.getPageSize(1);
Document document = new Document(psize, 50, 50, 50, 50);
document.addAuthor("DBA");
document.addTitle("Cyber Labels");
document.addKeywords("Java Architecture by I-Solutions");
float width = psize.width();
float height = psize.height();
// Step 2 update the form fields with database � the same label that will be printed several times
PdfStamper stamp = new PdfStamper(reader, new FileOutputStream("c:\\temp\\import_page2.pdf"));
AcroFields form = stamp.getAcroFields();
form.setField("To","Sam Shah");
form.setField("PieceY","5");
form.setField("Piecex","1");
stamp.setFormFlattening(true);
stamp.close();
//step 3 � create a new pdf doc that will contain x number of labels
PdfWriter writer = PdfWriter.getInstance(document, new FileOutputStream("c:\\temp\\import_page.pdf"));
document.open();
PdfContentByte cb = writer.getDirectContent();
PdfReader reader2 = new PdfReader("c:\\temp\\import_page2.pdf");
PdfImportedPage page1 = writer.getImportedPage(reader2, 1);
cb.addTemplate(page1, .5f, 0, 0, .5f, 0, height / 2);
cb.addTemplate(page1, .5f, 0, 0, .5f, width / 2, height / 2);
cb.addTemplate(page1, .5f, 0, 0, .5f, 0, 0);
cb.addTemplate(page1, .5f, 0, 0, .5f, width / 2, 0);
cb.setRGBColorStroke(255, 0, 0);
cb.moveTo(0, height / 2);
cb.lineTo(width, height / 2);
cb.stroke();
cb.moveTo(width / 2, height);
cb.lineTo(width / 2, 0);
document.newPage();
cb.addTemplate(page1, .5f, 0, 0, .5f, 0, height / 2);
cb.stroke();
document.close();
System.out.println("Finished.");
}
catch (Exception de) {
de.printStackTrace();
}
}
}
Do you Yahoo!?
Yahoo! SiteBuilder - Free, easy-to-use web site design software
