Hello,
>Are you resetting the page variable with the page of the
>new document each time? It is possible that the value stored
>in the page object is not getting updated and therefore
>you see the same value returned all the time.
>
I have no idea what you mean above "resetting the page variable" or the second part. The imported documents I need scanning already exists in as files i.e. they are not generated in memory therefore I reckon they are updated or?
Please find below the full code containing the issue. Note there are two existing files that are referenced: header.pdf and footer.
Thanks in advance,
Best regards,
Giovanni
/**
* Example for generating one othe letters as shown in the SRS
*/
public class ExampleLetter {
/**
* @param args
*/
public static void main(String[] args) {
// create a PDF document
Document document = new Document();
// add one blank page to the document
document.getPages().add(new Page(PageSize.LETTER));
// create an even odd template and add elements to it
Template template = new Template();
// build the page
template.getElements().add(getHeader());
template.getElements().add(getFooter());
// add the template to the document
document.setTemplate(template);
document.draw("letter.pdf");
}
/**
* Returns the header part
*
* @return the header part
*/
private static PageElement getHeader() {
PdfDocument pdfDocument = new PdfDocument("header.pdf");
showDimensions(pdfDocument);
int pageNumber = 0;
int xOffset = 100;
int yOffset = 0;
PdfPage pdfPage = pdfDocument.getPages().getPdfPage(pageNumber);
ImportedPageArea headerArea = new ImportedPageArea(pdfPage, pageNumber, xOffset, yOffset);
headerArea.getContents().setClipLeft(50);
headerArea.getContents().setClipTop(50);
headerArea.getContents().setClipRight(50);
headerArea.getContents().setClipBottom(50);
return headerArea;
}
/**
* Returns the footer {@link PageElement}
*
* @return the footer {@link PageElement}
*/
private static PageElement getFooter() {
// fill placeholders on template footer
MergeDocument mergeDocument = new MergeDocument("footer.pdf", new MergeOptions(true));
mergeDocument.getForm().getFields().getFormField("ph0001").setValue("Juan Perez");
mergeDocument.getForm().getFields().getFormField("ph0002").setValue("Head of Customer Acquisition");
mergeDocument.getForm().getFields().getFormField("ph0003").setValue("Dayana Gonzales");
mergeDocument.getForm().getFields().getFormField("ph0004").setValue("Head of Customer Satisfaction");
// output the populated template to a PdfDocument
ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
mergeDocument.draw(outputStream);
PdfDocument pdfDocument = new PdfDocument(new ByteArrayInputStream(outputStream.toByteArray()));
showDimensions(pdfDocument);
// create an imported area from the in-memory pdfDocument
int pageNumber = 0;
int xOffset = 0;
int yOffset = 400;
PdfPage pdfPage = pdfDocument.getPages().getPdfPage(pageNumber);
ImportedPageArea importedArea = new ImportedPageArea(pdfPage, pageNumber, xOffset, yOffset);
importedArea.getContents().setClipLeft(50);
importedArea.getContents().setClipTop(50);
importedArea.getContents().setClipRight(100);
importedArea.getContents().setClipBottom(100);
return importedArea;
}
private static void showDimensions(PdfDocument pdfDocument) {
showDimensions(new MergeDocument(pdfDocument));
}
private static void showDimensions(MergeDocument mergeDocument) {
Page page = mergeDocument.getPages().getPage(0);
System.out.println(page.getDimensions().getHeight());
System.out.println(page.getDimensions().getWidth());
System.out.println(page.getDimensions().getTopMargin());
}
}