We help IT Professionals succeed at work.

How do I concatenate documents using Itext

dan4good
dan4good asked
on
I want to take a series of independently produced ITEXT document objects, each having their own margins and repeatable headers and footers,  add them to a single writerwith bookmarks, then produce a single pdf file.

I have been searching the internet but I can't find what I am looking for. There are several solutions that write each document to file system first and then concatenate or merge them together (such as http://www.roseindia.net/java/itext/ConcatenateMoreThanTwoPdfFiles.shtml and http://java-x.blogspot.com/2006/11/merge-pdf-files-with-itext.html are just two examples). THIS WONT WORK FOR ME. Performance is NOT ACCEPTABLE when you do this in our environment since each write to the hard drive MUSTbe encrypted for security reasons. I am assuming I want to use ByteArrayOutputStream to render the individual docments, but not sure how to aggregate them or finally flush them to a physical file when I am finally ready. The following snippent inNO WAY representsworking source code since it requires functions that don't exist in ITEXT at present, but I am including it because it might help illustrate what I am lokking for to some of you experts better than this explanation does.
package in_action.chapter01;
import java.io.FileOutputStream;
import java.util.ArrayList;
import java.util.List;

import com.lowagie.text.Document;
import com.lowagie.text.pdf.PdfCopy;
import com.lowagie.text.pdf.PdfImportedPage;


public class MyItextWishClass {

	public static void assembleIntoOnePDF() {

		try {
			List <Document> documentList = new ArrayList<Document>();
			/**
			 * each of the following 4 lines represents the calls needed to  retrieve the independently produced ITEXT Document Objects
			 * a sample document object follows this snippet.
			 */
			documentList.add(CoverPage.getDocument());
			documentList.add(ReportOne.getDocument());
			documentList.add(ReportTwo.getDocument());
			documentList.add(ReportThree.getDocument());


			String outFile; // represents final physical location of aggregated PDF. 
			Document document = null;
			PdfCopy writer = null;
			PdfImportedPage page;
			int idx = 0;
			while (idx < documentList.size()) {						
				document = documentList.get(idx);

				writer = new PdfCopy(document, new FileOutputStream(outFile));
				document.open();						
				// step 4: we somehow add content of each document to writer


				idx++;
			}
			// step 5: we close the document
			document.close();
		}
		catch(Exception e) {
			e.printStackTrace();
		}
	}
}

///////!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!////
Sample report generating logic to follow......

package com.mm.isip.reportLayer.reports.traditionalproduct;

import java.awt.Color;
import java.io.ByteArrayOutputStream;
import java.io.IOException;

import com.lowagie.text.Cell;
import com.lowagie.text.Chunk;
import com.lowagie.text.Document;
import com.lowagie.text.DocumentException;
import com.lowagie.text.Element;
import com.lowagie.text.Image;
import com.lowagie.text.Phrase;
import com.lowagie.text.pdf.PdfPCell;
import com.lowagie.text.pdf.PdfPTable;
import com.lowagie.text.pdf.PdfWriter;

import com.mm.isip.print.PrintContext;
import com.mm.isip.print.PrintEngine;

public class CoverPage implements DocumentPrintComponent {
	private static final String LINEBREAK = "\n";
	private static final float topBORDER = 5f;
	private static final float leftBORDER = 5f;
	private static final float rightBORDER = 15.75f;
	private static final Color borderCOLOR = Color.blue;
	/**
	 * iText does have it's bizarre little tweaks, just like any other software. iText WILL ALWAYS GIVE
	 * you a default margin of .5 in. or so So if you want to cut that, you need to make -36. 
	 */
	private static final float leftMARGIN = -36f;
	private static final float rightMARGIN = -36f;
	private static final float topMARGIN = 18f;
	private static final float bottomMARGIN = 18f;
	
	/**
	 * add a 1/4 inch of additional padding for page content. 
	 */
	private static final float leftPADDING = 18f;
	
public static Document print(PrintContext cxt) {
	Document document = new Document();
	PdfWriter writer = null;		
	//TODO "Totalpages" needs to be retrieved programatically
	String totalpages = "37";
	String insuredDemographicData = "";
	String insured = PreparedFor.getValue(cxt);	
	String crnnumber = CRNCodeByProduct.getValue(cxt);
	String presentedby = PreparedByAddress.getValue(cxt);
	String preparedOnDate = FooterPreparedOnDate.getValue(cxt);	
		
	String productName = "";
	String pension = Pension.getValue(cxt);
	String planName = MarketingNameForCoverPage.getValue(cxt);
	String coverageDescription = CoverageLISR.getValue(cxt);
	String revisedIllustration = RevisedIllustration.getValue(cxt);
	insuredDemographicData = Gender.getValue(cxt)+ ", Age " + ClientAge.getValue(cxt); 
		
	String pagenumber = "1 of " + totalpages;

	try {
		PrintEngine pe = cxt.getPrintEngine();
		ByteArrayOutputStream byteArrayPDFContainer = new ByteArrayOutputStream();
		writer = PdfWriter.getInstance(document, byteArrayPDFContainer);
			
		document.open();

		PdfPTable layout = new PdfPTable(1);
		Phrase blank = new Phrase("\n");

		layout.getDefaultCell().setBorder(Cell.NO_BORDER);
		layout.getDefaultCell().setPadding(0f);
			
		layout.setTotalWidth(100f);
			
		PdfPCell contentcell = null;			
		PdfPCell leftcell = null;
		PdfPCell rightcell = null;

		PdfPTable table = new PdfPTable(2);			
		table.setTotalWidth(document.right() - document.left());	
		table.getDefaultCell().setPadding(0f);
		table.getDefaultCell().setBorder(Cell.NO_BORDER);
			
		leftcell = new PdfPCell(new Phrase("LIFE INSURANCE", pe.getHelv10PointBoldFont() ));
		leftcell.setHorizontalAlignment(Element.ALIGN_CENTER);				
		leftcell.setVerticalAlignment(Element.ALIGN_MIDDLE);
		leftcell.setBackgroundColor(borderCOLOR);
		leftcell.setBorderColor(borderCOLOR);
		leftcell.setBorderWidthLeft(leftBORDER);
		leftcell.setFixedHeight(35);

		rightcell = new PdfPCell(blank);
		rightcell.setBorderWidthTop(topBORDER);
		rightcell.setBorderWidthBottom(0f);
		rightcell.setBorderWidthLeft(0f);
		rightcell.setBorderWidthRight(rightBORDER);
		rightcell.setBorderColor(borderCOLOR);

		table.addCell(leftcell);			
		table.addCell(rightcell);

		layout.addCell(table);

		Phrase phrase = new Phrase("", pe.get18PointFont());
		contentcell = setCellDisplayProperties(70, phrase);
		layout.addCell(contentcell);

		contentcell.setFixedHeight(20);
			
		Chunk chunks[] = new Chunk[2];
		Chunk chunk = new Chunk();
			
		if (pension == null) {
			pension = "";
		}
			
		if (coverageDescription == null) {
			coverageDescription = "";
		}
		productName = pension + planName + coverageDescription;
			
		chunk = new Chunk (productName, pe.get24PointBoldFont());
		chunks[0] = chunk;
						
		if (revisedIllustration == null) {
			revisedIllustration = "";
		}
		chunk = new Chunk (revisedIllustration + LINEBREAK + "Basic Life Insurance Illustration", pe.get22PointBoldFont());
		chunks[1] = chunk;
					
		phrase = PrintEngine.createCompositePhrase(chunks);
		contentcell = setCellDisplayProperties(70, phrase);
		contentcell.setPaddingLeft(leftPADDING);
		layout.addCell(contentcell);

		contentcell.setFixedHeight(10);
		phrase = new Phrase("", pe.get18PointFont());
		contentcell = setCellDisplayProperties(50, phrase);
//		contentcell.setVerticalAlignment(Element.ALIGN_TOP);			
		layout.addCell(contentcell);

		contentcell.setFixedHeight(20);
			
		chunk = new Chunk ("Prepared for:", pe.get18PointBoldFont());
		chunks[0] = chunk;
		chunk = new Chunk (LINEBREAK + insured, pe.get18PointFont());
		chunks[1] = chunk;
			
		phrase = PrintEngine.createCompositePhrase(chunks);
		contentcell = setCellDisplayProperties(50, phrase);
		contentcell.setPaddingLeft(leftPADDING);
		layout.addCell(contentcell);
			
		phrase = new Phrase("", pe.get18PointFont());
		contentcell = setCellDisplayProperties(20, phrase);
		layout.addCell(contentcell);

		contentcell.setFixedHeight(40);
		phrase = new Phrase(insuredDemographicData, pe.get18PointFont());
		contentcell = setCellDisplayProperties(20, phrase);
		contentcell.setPaddingLeft(leftPADDING);
		layout.addCell(contentcell);
			
		phrase = new Phrase("", pe.get18PointFont());
		contentcell = setCellDisplayProperties(20, phrase);
		layout.addCell(contentcell);

		chunk = new Chunk("Presented by:", pe.get18PointBoldFont());
		chunks[0] = chunk;
		chunk = new Chunk (LINEBREAK + presentedby, pe.get18PointFont());
		chunks[1] = chunk;			
		phrase = PrintEngine.createCompositePhrase(chunks);
		contentcell = setCellDisplayProperties(180, phrase);
		contentcell.setPaddingLeft(leftPADDING);
		layout.addCell(contentcell);	

		phrase = new Phrase(preparedOnDate, pe.get18PointFont());
		contentcell = setCellDisplayProperties(20, phrase);
		contentcell.setPaddingLeft(leftPADDING);
		layout.addCell(contentcell);
			
		phrase = new Phrase("", pe.get18PointFont());
		contentcell = setCellDisplayProperties(50, phrase);
		layout.addCell(contentcell);
			
		table = new PdfPTable(2); 
		leftcell = new PdfPCell(new Phrase("CRN: " + crnnumber, pe.get12PointBoldFont()));
		rightcell = new PdfPCell(new Phrase("Page " + pagenumber + " ", pe.get12PointBoldFont()));
		rightcell.setHorizontalAlignment(Element.ALIGN_RIGHT);
			
		leftcell.setBorderColor(borderCOLOR);
		rightcell.setBorderColor(borderCOLOR);
			
		leftcell.setBorderWidthLeft(leftBORDER);			
		leftcell.setBorderWidthTop(0f);
		leftcell.setBorderWidthBottom(0f);
		rightcell.setBorderWidthTop(0f);
		rightcell.setBorderWidthBottom(0f);
		rightcell.setBorderWidthRight(rightBORDER);
			/**
			 * 
			 */
		leftcell.setPaddingLeft(leftPADDING);
		rightcell.setPaddingRight(rightBORDER);
		table.addCell(leftcell);
		table.addCell(rightcell);
		
		layout.addCell(table);
		PdfPTable footer = new PdfPTable(1);
		footer.getDefaultCell().setPadding(0f);
		footer.getDefaultCell().setHorizontalAlignment(Element.ALIGN_RIGHT);
		footer.getDefaultCell().setBorderWidthLeft(0f);
		footer.getDefaultCell().setBorderWidthRight(0f);
		footer.getDefaultCell().setBorderWidthBottom(0f);
		footer.getDefaultCell().setBorderWidthTop(0f);
		footer.getDefaultCell().setBorderColor(Color.GREEN);
			
		Image image = Image.getInstance("src/main/resources/images/report_image.jpg");
			
		contentcell.setImage(image);
		contentcell.setFixedHeight(70);
			
		contentcell.setPaddingBottom(0f);
		contentcell.setBorderWidthBottom(1f);
		contentcell.setBorderColorBottom(Color.CYAN);
			
		contentcell.setBorderWidthTop(0f);	
			
		contentcell.setPaddingLeft(leftBORDER);			
		contentcell.setBorderWidthLeft(leftBORDER);
			
		contentcell.setPaddingRight(0.0f);
		contentcell.setBorderWidthRight(rightBORDER);
		footer.addCell(contentcell);
		layout.addCell(footer);	
			
		PdfPCell disclaimer = null;
		String verbiage = "bla bla bla.";
		disclaimer = new PdfPCell(new Phrase( verbiage, pe.get10PointFont()));
		disclaimer.setBorder(Cell.NO_BORDER);
		layout.addCell(disclaimer);							

		document.add(layout);
			
	} catch (DocumentException e) {
		e.printStackTrace();
	} catch (IOException e) {
		e.printStackTrace();
	}
	return document;
}


	private static PdfPCell setCellDisplayProperties(float fixedHeight, Phrase phrase) {
		PdfPCell cell2Display = new PdfPCell(phrase);
		cell2Display.setFixedHeight(fixedHeight);
		cell2Display.setBorderColor(borderCOLOR);
		cell2Display.setBorderWidthLeft(leftBORDER);
		cell2Display.setBorderWidthRight(rightBORDER);		
		cell2Display.setBorderWidthBottom(0f);
		cell2Display.setBorderWidthTop(0f);
		return cell2Display;
	}	
}

Open in new window

package in_action.chapter01;
import java.io.FileOutputStream;
import java.util.ArrayList;
import java.util.List;

import com.lowagie.text.Document;
import com.lowagie.text.pdf.PdfCopy;
import com.lowagie.text.pdf.PdfImportedPage;


public class MyItextWishClass {

	public static void assembleIntoOnePDF() {

		try {
			List <Document> documentList = new ArrayList<Document>();
			/**
			 * each of the following 4 lines represents the calls needed to  retrieve the independently produced ITEXT Document Objects. Each document object 
implements the DocumentPrintComponent interface which has a single public method public void print(PrintContext)
			 * a sample document object follows this snippet.
			 */
			documentList.add(coverPage.getDocument());
			documentList.add(reportOne.getDocument());
			documentList.add(reportTwo.getDocument());
			documentList.add(reportThree.getDocument());


			String outFile; // represents final physical location of aggregated PDF. 
			Document document = null;
			PdfCopy writer = null;
			PdfImportedPage page;
			int idx = 0;
			while (idx < documentList.size()) {						
				document = documentList.get(idx);

				writer = new PdfCopy(document, new FileOutputStream(outFile));
				document.open();						
				// step 4: we somehow add content of each document to writer


				idx++;
			}
			// step 5: we close the document
			document.close();
		}
		catch(Exception e) {
			e.printStackTrace();
		}
	}
}

///////!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!////
Sample report generating logic to follow......

package com.mm.isip.reportLayer.reports.traditionalproduct;

import java.awt.Color;
import java.io.ByteArrayOutputStream;
import java.io.IOException;

import com.lowagie.text.Cell;
import com.lowagie.text.Chunk;
import com.lowagie.text.Document;
import com.lowagie.text.DocumentException;
import com.lowagie.text.Element;
import com.lowagie.text.Image;
import com.lowagie.text.Phrase;
import com.lowagie.text.pdf.PdfPCell;
import com.lowagie.text.pdf.PdfPTable;
import com.lowagie.text.pdf.PdfWriter;

import com.mm.isip.print.PrintContext;
import com.mm.isip.print.PrintEngine;

public class CoverPage implements DocumentPrintComponent {
	private static final String LINEBREAK = "\n";
	private static final float topBORDER = 5f;
	private static final float leftBORDER = 5f;
	private static final float rightBORDER = 15.75f;
	private static final Color borderCOLOR = Color.blue;
	/**
	 * iText does have it's bizarre little tweaks, just like any other software. iText WILL ALWAYS GIVE
	 * you a default margin of .5 in. or so So if you want to cut that, you need to make -36. 
	 */
	private static final float leftMARGIN = -36f;
	private static final float rightMARGIN = -36f;
	private static final float topMARGIN = 18f;
	private static final float bottomMARGIN = 18f;
	
	/**
	 * add a 1/4 inch of additional padding for page content. 
	 */
	private static final float leftPADDING = 18f;
	
public static Document print(PrintContext cxt) {
	Document document = new Document();
	PdfWriter writer = null;		
	//TODO "Totalpages" needs to be retrieved programatically
	String totalpages = "37";
	String insuredDemographicData = "";
	String insured = PreparedFor.getValue(cxt);	
	String crnnumber = CRNCodeByProduct.getValue(cxt);
	String presentedby = PreparedByAddress.getValue(cxt);
	String preparedOnDate = FooterPreparedOnDate.getValue(cxt);	
		
	String productName = "";
	String pension = Pension.getValue(cxt);
	String planName = MarketingNameForCoverPage.getValue(cxt);
	String coverageDescription = CoverageLISR.getValue(cxt);
	String revisedIllustration = RevisedIllustration.getValue(cxt);
	insuredDemographicData = Gender.getValue(cxt)+ ", Age " + ClientAge.getValue(cxt); 
		
	String pagenumber = "1 of " + totalpages;

	try {
		PrintEngine pe = cxt.getPrintEngine();
		ByteArrayOutputStream byteArrayPDFContainer = new ByteArrayOutputStream();
		writer = PdfWriter.getInstance(document, byteArrayPDFContainer);
			
		document.open();

		PdfPTable layout = new PdfPTable(1);
		Phrase blank = new Phrase("\n");

		layout.getDefaultCell().setBorder(Cell.NO_BORDER);
		layout.getDefaultCell().setPadding(0f);
			
		layout.setTotalWidth(100f);
			
		PdfPCell contentcell = null;			
		PdfPCell leftcell = null;
		PdfPCell rightcell = null;

		PdfPTable table = new PdfPTable(2);			
		table.setTotalWidth(document.right() - document.left());	
		table.getDefaultCell().setPadding(0f);
		table.getDefaultCell().setBorder(Cell.NO_BORDER);
			
		leftcell = new PdfPCell(new Phrase("LIFE INSURANCE", pe.getHelv10PointBoldFont() ));
		leftcell.setHorizontalAlignment(Element.ALIGN_CENTER);				
		leftcell.setVerticalAlignment(Element.ALIGN_MIDDLE);
		leftcell.setBackgroundColor(borderCOLOR);
		leftcell.setBorderColor(borderCOLOR);
		leftcell.setBorderWidthLeft(leftBORDER);
		leftcell.setFixedHeight(35);

		rightcell = new PdfPCell(blank);
		rightcell.setBorderWidthTop(topBORDER);
		rightcell.setBorderWidthBottom(0f);
		rightcell.setBorderWidthLeft(0f);
		rightcell.setBorderWidthRight(rightBORDER);
		rightcell.setBorderColor(borderCOLOR);

		table.addCell(leftcell);			
		table.addCell(rightcell);

		layout.addCell(table);

		Phrase phrase = new Phrase("", pe.get18PointFont());
		contentcell = setCellDisplayProperties(70, phrase);
		layout.addCell(contentcell);

		contentcell.setFixedHeight(20);
			
		Chunk chunks[] = new Chunk[2];
		Chunk chunk = new Chunk();
			
		if (pension == null) {
			pension = "";
		}
			
		if (coverageDescription == null) {
			coverageDescription = "";
		}
		productName = pension + planName + coverageDescription;
			
		chunk = new Chunk (productName, pe.get24PointBoldFont());
		chunks[0] = chunk;
						
		if (revisedIllustration == null) {
			revisedIllustration = "";
		}
		chunk = new Chunk (revisedIllustration + LINEBREAK + "Basic Life Insurance Illustration", pe.get22PointBoldFont());
		chunks[1] = chunk;
					
		phrase = PrintEngine.createCompositePhrase(chunks);
		contentcell = setCellDisplayProperties(70, phrase);
		contentcell.setPaddingLeft(leftPADDING);
		layout.addCell(contentcell);

		contentcell.setFixedHeight(10);
		phrase = new Phrase("", pe.get18PointFont());
		contentcell = setCellDisplayProperties(50, phrase);
//		contentcell.setVerticalAlignment(Element.ALIGN_TOP);			
		layout.addCell(contentcell);

		contentcell.setFixedHeight(20);
			
		chunk = new Chunk ("Prepared for:", pe.get18PointBoldFont());
		chunks[0] = chunk;
		chunk = new Chunk (LINEBREAK + insured, pe.get18PointFont());
		chunks[1] = chunk;
			
		phrase = PrintEngine.createCompositePhrase(chunks);
		contentcell = setCellDisplayProperties(50, phrase);
		contentcell.setPaddingLeft(leftPADDING);
		layout.addCell(contentcell);
			
		phrase = new Phrase("", pe.get18PointFont());
		contentcell = setCellDisplayProperties(20, phrase);
		layout.addCell(contentcell);

		contentcell.setFixedHeight(40);
		phrase = new Phrase(insuredDemographicData, pe.get18PointFont());
		contentcell = setCellDisplayProperties(20, phrase);
		contentcell.setPaddingLeft(leftPADDING);
		layout.addCell(contentcell);
			
		phrase = new Phrase("", pe.get18PointFont());
		contentcell = setCellDisplayProperties(20, phrase);
		layout.addCell(contentcell);

		chunk = new Chunk("Presented by:", pe.get18PointBoldFont());
		chunks[0] = chunk;
		chunk = new Chunk (LINEBREAK + presentedby, pe.get18PointFont());
		chunks[1] = chunk;			
		phrase = PrintEngine.createCompositePhrase(chunks);
		contentcell = setCellDisplayProperties(180, phrase);
		contentcell.setPaddingLeft(leftPADDING);
		layout.addCell(contentcell);	

		phrase = new Phrase(preparedOnDate, pe.get18PointFont());
		contentcell = setCellDisplayProperties(20, phrase);
		contentcell.setPaddingLeft(leftPADDING);
		layout.addCell(contentcell);
			
		phrase = new Phrase("", pe.get18PointFont());
		contentcell = setCellDisplayProperties(50, phrase);
		layout.addCell(contentcell);
			
		table = new PdfPTable(2); 
		leftcell = new PdfPCell(new Phrase("CRN: " + crnnumber, pe.get12PointBoldFont()));
		rightcell = new PdfPCell(new Phrase("Page " + pagenumber + " ", pe.get12PointBoldFont()));
		rightcell.setHorizontalAlignment(Element.ALIGN_RIGHT);
			
		leftcell.setBorderColor(borderCOLOR);
		rightcell.setBorderColor(borderCOLOR);
			
		leftcell.setBorderWidthLeft(leftBORDER);			
		leftcell.setBorderWidthTop(0f);
		leftcell.setBorderWidthBottom(0f);
		rightcell.setBorderWidthTop(0f);
		rightcell.setBorderWidthBottom(0f);
		rightcell.setBorderWidthRight(rightBORDER);
			/**
			 * 
			 */
		leftcell.setPaddingLeft(leftPADDING);
		rightcell.setPaddingRight(rightBORDER);
		table.addCell(leftcell);
		table.addCell(rightcell);
		
		layout.addCell(table);
		PdfPTable footer = new PdfPTable(1);
		footer.getDefaultCell().setPadding(0f);
		footer.getDefaultCell().setHorizontalAlignment(Element.ALIGN_RIGHT);
		footer.getDefaultCell().setBorderWidthLeft(0f);
		footer.getDefaultCell().setBorderWidthRight(0f);
		footer.getDefaultCell().setBorderWidthBottom(0f);
		footer.getDefaultCell().setBorderWidthTop(0f);
		footer.getDefaultCell().setBorderColor(Color.GREEN);
			
		Image image = Image.getInstance("src/main/resources/images/report_image.jpg");
			
		contentcell.setImage(image);
		contentcell.setFixedHeight(70);
			
		contentcell.setPaddingBottom(0f);
		contentcell.setBorderWidthBottom(1f);
		contentcell.setBorderColorBottom(Color.CYAN);
			
		contentcell.setBorderWidthTop(0f);	
			
		contentcell.setPaddingLeft(leftBORDER);			
		contentcell.setBorderWidthLeft(leftBORDER);
			
		contentcell.setPaddingRight(0.0f);
		contentcell.setBorderWidthRight(rightBORDER);
		footer.addCell(contentcell);
		layout.addCell(footer);	
			
		PdfPCell disclaimer = null;
		String verbiage = "bla bla bla.";
		disclaimer = new PdfPCell(new Phrase( verbiage, pe.get10PointFont()));
		disclaimer.setBorder(Cell.NO_BORDER);
		layout.addCell(disclaimer);							

		document.add(layout);
			
	} catch (DocumentException e) {
		e.printStackTrace();
	} catch (IOException e) {
		e.printStackTrace();
	}
	return document;
}


	private static PdfPCell setCellDisplayProperties(float fixedHeight, Phrase phrase) {
		PdfPCell cell2Display = new PdfPCell(phrase);
		cell2Display.setFixedHeight(fixedHeight);
		cell2Display.setBorderColor(borderCOLOR);
		cell2Display.setBorderWidthLeft(leftBORDER);
		cell2Display.setBorderWidthRight(rightBORDER);		
		cell2Display.setBorderWidthBottom(0f);
		cell2Display.setBorderWidthTop(0f);
		return cell2Display;
	}	
}

Open in new window

Comment
Watch Question

this you are trying to do is one of the most complicated thing i heared on my 15 years of programming, once i need some help with a iText code, I wrote to the Author

Bruno Lowagie
itext-questions@lists.sourceforge.net <itext-questions@lists.sourceforge.net>

and he give me a code example.
Probably I've misunderstood something, but...
When you create each one of the documents if you add it's elements to a certain document (the one you want to produce) finally you will have it. The difference wil be that you have to change the margins, headers and footers each time you start producing new document.
Commented:
Thanks for trying...Got it here it is.  (see TraditionalIllustration.java)

with each report class formed similar to as follows:(see TraditionalProductCoverPage.java)

There are two keys to doing this. First using the ByteArrayOutputStream  it not only helps speed the whole process up, but even more important, it provides a means to instantiate a reader used in the
private method combineIntoOneFile(List<byte[]> illustrationOutputStreams) by taking advantage of the
ability of the ByteArrayOutputStream to perform a .toByteArray() conversion.

So in the public method public static void assembleIntoOnePDF(Context cxt) I invoke and aggregate the individual streams, while in  combineIntoOneFile(List<byte[]> illustrationOutputStreams) I copy each content into a single writer and flush to the OS filesystem.


List <byte[]> illustrationOutputStream = new ArrayList<byte[]>();

byte[] reportdata = new byte[2];
/**
 * the print(cxt) method of any report generation class always returns a ByteArrayOutputStream
 * that contains the actual contents of a report (albeit minus the pdf's document wrapper)
 * take each ByteArrayOutputStream and use it's toByteArray() method to prepare content
 * for being used with a PdfReader within static method
 *  IllustrationDocument.combineIntoOneFile(illustrationOutputStream).
 */
reportdata = coverpage.print(cxt).toByteArray();      
illustrationOutputStream.add(reportdata);

reportdata = illustrationsummary.print(cxt).toByteArray();      
illustrationOutputStream.add(reportdata);

reportdata = tabularvalues.print(cxt).toByteArray();      
illustrationOutputStream.add(reportdata);

TraditionalIllustration.combineIntoOneFile(illustrationOutputStream);

the bookmarks and page counting  are not 100% there yet, but like everything else within our illustration documents have some pretty unique and difficult special requirements,  I am stll working on them.

This solution seems to work pretty well for me. Every Illustration has a minimum of 8 reports up to a max
of 19. Each report has its own layout (some tabular, some narrative, some both). Almost every report has their own variation to a standard header (except the coverpage), and all have a unique set of footers (except the coverpage) that varies in size. Every tabular report has its own display grouping of data rows. The benefit too having each class implement  it's own print() method is I can encapsulate all this complexity on a report by report basis, then aggregate it and assemble into a single pdf.

Thanks all for you help.
TraditionalIllustration.java
TraditionalProductCoverPage.java