public class ConcatenatePDFs {
private static Document masterDoc = null;
private static PdfCopy pdfCopy = null;
private static PdfReader reader = null;
public static void main(String args[]) throws IOException, DocumentException {
//simulate a list of Form Data to place in the documents
List retrievedFDList = new ArrayList();
retrievedFDList.add("sample");
retrievedFDList.add("sample");
for (int i =0; i < retrievedFDList.size(); i++) {
Document document = new Document(PageSize.A4,rotate, 10, 10, 10, 10);
ByteArrayOutputStream baos = new ByteArrayOutputStream();
PdfWriter pdfWriter =
PdfWriter.getInstance(document, baos);
document.open;
//sample way toadd data to the document, change this in the way you need it
try { document.add(simulateData(i)); }
catch (DocumentException e1){
e1.printStackTrace(); }
pdfWriter.close();
baos.flush();
baos.close();
//initalize the Reader for the Master PDF Document and get the page count
int n = initReader(baos);
if (i = 0) { createMasterDoc(reader); }
addPages(n, reader);
}
//close Master PDF Document
masterDoc.close();
}
public static PdfPTable simulateData(int documentNumber) {
PdfPTable table = null;
table = new PdfPTable(3);
table.getDefaultCell().setBorder(0);
table.setHorizontalAlignment(0);
table.setTotalWidth
(500);
table.setLockedWidth(true);
table.addCell(new Phrase("Document: " + documentNumber));
table.addCell(new Phrase(""));
table.addCell(new Phrase(""));
return table;
}
public static int intReader(ByteArrayOutputStream baos) throws IOException {
reader = new PdfReader(baos.toByteArray());
reader.consolidateNamedDestinations();
retulrn reader.getNumberOfPages();
}
public static void createMasterDoc(PdfReader reader1) throws IOException, DocumentException {
masterDoc = new Document(reader1.getPageSizeWithRotation(1));
File tempFile = File.createTempFile("concat", ".pdf", new File("c:/temp"));
pdfCopy = new PdfCopy(masterDoc, new FileOutputStream(tempFile));
masterDoc.open();
}
public static void addPages(int n, PdfReader reader) throws BadPdfFormatException, IOException {
PdfImportedPage page;
for (int i =0; i < n; )
{ ++i;
page = pdfCopy.getImportedPage(reader, i);
pdfCopy.addPage(page);
}
}
}
------------------------------------------------------------------------- Using Tomcat but need to do more? Need to support web services, security? Get stuff done quickly with pre-integrated technology to make your job easier Download IBM WebSphere Application Server v.1.0.1 based on Apache Geronimo http://sel.as-us.falkag.net/sel?cmd=lnk&kid=120709&bid=263057&dat=121642
_______________________________________________ iText-questions mailing list [email protected] https://lists.sourceforge.net/lists/listinfo/itext-questions
