iTEXT USER wrote:

Hi Bruno, Please find the attached file. The code is available. When you run this and
generate the pdf , you can see the tables are being splitted . I dont want
them to be splitted. Please suggest some alternatives.
http://www.nabble.com/file/p12361961/Test.java Test.java

I'm sorry to say this: but your code is very obscure.
Normally I'd suggest that you start again from scratch
using only methods and techniques explained in the book.

I tried adapting your code to make it work, but as you
can see when you execute the code in attachment,
it doesn't work 100% correctly. Nevertheless, it will
help you find out how to work with ColumnText the way
you should (not the way you did). There's a lot more room
for improvement in other areas of your code (for instance
the way you are using the page events), but this was as
much help I could give you, given the limited time available.

br,
Bruno
package test;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;

import com.lowagie.text.Document;
import com.lowagie.text.DocumentException;
import com.lowagie.text.Element;
import com.lowagie.text.ExceptionConverter;
import com.lowagie.text.Font;
import com.lowagie.text.FontFactory;
import com.lowagie.text.Paragraph;
import com.lowagie.text.Phrase;
import com.lowagie.text.Rectangle;
import com.lowagie.text.pdf.ColumnText;
import com.lowagie.text.pdf.PdfContentByte;
import com.lowagie.text.pdf.PdfPCell;
import com.lowagie.text.pdf.PdfPTable;
import com.lowagie.text.pdf.PdfPageEvent;
import com.lowagie.text.pdf.PdfTemplate;
import com.lowagie.text.pdf.PdfWriter;


/**
 * This class is used to create the EPub Numeric PDF template for all ePub
 * Categories.
 * 
 * @author sakthi
 */
public class Test implements PdfPageEvent{
	
	private static final String HEADER_TEXT = "HEADER TEXT";
	private static final String footerText = "FOOTER TEXT";
	private static final String footerDate = "28 Aug 2007";
	
	private static final Font BOLD_FONT = FontFactory.getFont(
			FontFactory.HELVETICA, 6f, Font.BOLD);
	private static final Font NORMAL_FONT = FontFactory.getFont(
			FontFactory.HELVETICA, 6f, Font.NORMAL);
	
	private Document doc;
	private ColumnText columnText;
	private final float gutter = 0;
	private final int numColumns = 4;
	private float topColumn;
	private PdfContentByte contentByte;
	private PdfTemplate template;
	private float fullWidth;
	private float columnWidth;
	private float allColumns[];
	private int currentColumn;
	private String tempNumber;	
	
	
	/**Creates pdf doc.
	 * 
	 * @return
	 */
	public void createPDF() {
		try {
			//Create document object
			doc = new Document();
			
			PdfWriter writer = PdfWriter.getInstance(doc, new FileOutputStream("test.pdf"));
			//writer.setViewerPreferences(PdfWriter.PageModeUseOutlines);
				
			doc.open();
			writer.setPageEvent(this);				
			
			contentByte = writer.getDirectContent();
			createTemplate();
			currentColumn = 0;
			columnText = new ColumnText(contentByte);
			columnText.setSimpleColumn(allColumns[currentColumn], doc.bottom(),
				      allColumns[currentColumn] + columnWidth+30, topColumn,
				      0, Element.ALIGN_LEFT);
			
			
			for (int i = 1; i < 200; i++) {
				PdfPTable addressTable = new PdfPTable(2);
				addressTable.setTotalWidth(columnWidth);
				
				int teleNumber = i * 1000;
				//Assgining temp number
				tempNumber = Integer.toString(teleNumber);
				PdfPCell teleNumberCell = new PdfPCell(new Paragraph( Integer.toString(teleNumber), BOLD_FONT));
				teleNumberCell.setHorizontalAlignment(Element.ALIGN_LEFT);
				//teleNumberCell.setBorder(Rectangle.NO_BORDER);
				addressTable.addCell(teleNumberCell);
				
				PdfPCell lCityCell = new PdfPCell(new Phrase("City Name", NORMAL_FONT));
				lCityCell.setHorizontalAlignment(Element.ALIGN_RIGHT);
				//lCityCell.setBorder(Rectangle.NO_BORDER);
				addressTable.addCell(lCityCell);

				PdfPCell tradeNameCell = new PdfPCell(new Phrase("Trade Name 1 , Trade Name 2", NORMAL_FONT));
				tradeNameCell.setHorizontalAlignment(Element.ALIGN_LEFT);
				//tradeNameCell.setBorder(0);
				tradeNameCell.setPaddingLeft(12f);
				tradeNameCell.setColspan(2);
				addressTable.addCell(tradeNameCell);

				PdfPCell countryCell = new PdfPCell(new Phrase("Country name", NORMAL_FONT));
				countryCell.setHorizontalAlignment(Element.ALIGN_LEFT);
				//countryCell.setBorder(Rectangle.NO_BORDER);
				countryCell.setPaddingLeft(12f);
				countryCell.setColspan(2);
				addressTable.addCell(countryCell);
				
				PdfPCell emptyCell = new PdfPCell();
				//emptyCell.setBorder(0);
				emptyCell.setPaddingTop(10f);
				emptyCell.setPaddingBottom(10f);
				emptyCell.setColspan(2);
				addressTable.addCell(emptyCell);
				
				addColumns(addressTable, teleNumber);
			} //End of for loop

			doc.close();
			System.out.println("created document");

		} catch (DocumentException de) {
			de.printStackTrace();
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		}		
	}
	
	
	private void addColumns(PdfPTable table, int teleNumber) throws DocumentException {
		float y = columnText.getYLine();
		columnText.addElement(table);
		int goFlag = columnText.go(true);
		columnText.setYLine(y);
		if (ColumnText.hasMoreText(goFlag)) {
			currentColumn++;
			if (currentColumn == 4) {
				doc.newPage();
				currentColumn = 0;
			}
			columnText = new ColumnText(contentByte);
			columnText.setSimpleColumn(allColumns[currentColumn],
					doc.bottom(),
					allColumns[currentColumn] + columnWidth + 30, topColumn,
					0, Element.ALIGN_LEFT);
		}
		columnText.addElement(table);
		columnText.go();
		/*while ((goFlag & ColumnText.NO_MORE_TEXT) == 0) {

			if (goFlag == ColumnText.NO_MORE_COLUMN) {

				currentY = topColumn;

				columnText.setSimpleColumn(allColumns[currentColumn], doc
						.bottom(),
						allColumns[currentColumn] + columnWidth + 30, currentY,
						0, Element.ALIGN_LEFT);
				currentColumn++;
			}
			goFlag = columnText.go();
			if (currentColumn == 4) {
				if (goFlag == ColumnText.NO_MORE_COLUMN) {
					doc.newPage();
					for (int k = 1; k < numColumns; ++k) {
						float x = allColumns[k] - gutter / 2;
						contentByte.moveTo(x, topColumn);
						contentByte.lineTo(x, doc.bottom());
					}
					contentByte.stroke();
					contentByte.addTemplate(template, 0, 0);
					currentColumn = 0;
				}
			}
		}*/
	}	
	
	private void createTemplate() {

		template = contentByte.createTemplate(600, 800);
		template.rectangle(doc.left(), doc.bottom(), doc.right() - doc.left(),
				doc.top() - doc.bottom() - 35);
		template.stroke();

		fullWidth = doc.right() - doc.left();

		columnWidth = (fullWidth - (numColumns - 1) * gutter) / numColumns;
				
		allColumns = new float[numColumns]; // left
		for (int k = 0; k < numColumns; ++k) {
			allColumns[k] = doc.left() + (columnWidth + gutter) * k;
			
		}
		// Added column borders
		topColumn = doc.top() - 35;

		for (int k = 1; k < numColumns; ++k) {
			float x = allColumns[k] - gutter / 2;
			contentByte.moveTo(x, topColumn);
			contentByte.lineTo(x, doc.bottom());
		}
		contentByte.stroke();
		contentByte.addTemplate(template, 0, 0);
	}
	
			
		// For agency number
	public void onOpenDocument(PdfWriter writer, Document document) {

	}

	public void onStartPage(PdfWriter writer, Document document) {

	}

	public void onCloseDocument(PdfWriter writer, Document document) {

	}

	public void onParagraph(PdfWriter writer, Document document,
			float paragraphPosition) {
	}

	public void onParagraphEnd(PdfWriter writer, Document document,
			float paragraphPosition) {

	}

	public void onChapter(PdfWriter writer, Document document,
			float paragraphPosition, Paragraph title) {

	}

	public void onChapterEnd(PdfWriter writer, Document document,
			float paragraphPosition) {

	}

	public void onSection(PdfWriter writer, Document document,
			float paragraphPosition, int depth, Paragraph title) {

	}

	public void onSectionEnd(PdfWriter writer, Document document,
			float paragraphPosition) {

	}

	public void onGenericTag(PdfWriter writer, Document document,
			Rectangle rect, String text) {

	}

	/**
	 * Adds headers and footers after wrinting the contents on document
	 * 
	 */
	public void onEndPage(PdfWriter writer, Document document) {
		try {

			for (int k = 1; k < numColumns; ++k) {
				float x = allColumns[k] - gutter / 2;
				contentByte.moveTo(x, topColumn);
				contentByte.lineTo(x, doc.bottom());
			}
			contentByte.stroke();
			contentByte.addTemplate(template, 0, 0);
			
			Rectangle page = document.getPageSize();
			PdfPTable header = new PdfPTable(3);
				
			PdfPCell logoCell = new PdfPCell(new Phrase("Logo cell", NORMAL_FONT));
			logoCell.setBorder(Rectangle.NO_BORDER);
			
			PdfPCell headerTextCell = new PdfPCell(new Paragraph(HEADER_TEXT, BOLD_FONT));
			headerTextCell.setBorder(Rectangle.NO_BORDER);
			headerTextCell.setHorizontalAlignment(Element.ALIGN_CENTER);
			
			PdfPTable pageTable = new PdfPTable(1);
			String pageNumber = Integer.toString(document.getPageNumber());
			
			PdfPCell pageNoCell = new PdfPCell(new Phrase(pageNumber, NORMAL_FONT));
			pageNoCell.setBorder(Rectangle.NO_BORDER);
			
			PdfPCell teleNumberCell = new PdfPCell(new Phrase(tempNumber, NORMAL_FONT));
			teleNumberCell.setBorder(Rectangle.NO_BORDER);
			teleNumberCell.setPaddingTop(15f);
			
			
			
			// Footer
			PdfPTable footer = new PdfPTable(2);
			// Name of document
			PdfPCell footerTextCell = new PdfPCell(new Paragraph(footerText, NORMAL_FONT));
			footerTextCell.setBorder(Rectangle.NO_BORDER);
			// Date created
			PdfPCell footerDateCell = new PdfPCell(new Paragraph(footerDate, NORMAL_FONT));
			footerDateCell.setBorder(Rectangle.NO_BORDER);
			
			if (document.getPageNumber() % 2 == 0) {
				logoCell.setHorizontalAlignment(Element.ALIGN_RIGHT);
				
				pageNoCell.setHorizontalAlignment(Element.ALIGN_LEFT);
				teleNumberCell.setHorizontalAlignment(Element.ALIGN_LEFT);
				
				pageTable.addCell(pageNoCell);
				if(teleNumberCell != null){
				pageTable.addCell(teleNumberCell);
				} 
				PdfPCell pageTableCell = new PdfPCell(pageTable);
				pageTableCell.setBorder(Rectangle.NO_BORDER);
				pageTableCell.setHorizontalAlignment(Element.ALIGN_LEFT);
				
				header.addCell(pageTableCell);
				header.addCell(headerTextCell);
				header.addCell(logoCell);
				footerTextCell.setHorizontalAlignment(Element.ALIGN_RIGHT);
				footerDateCell.setHorizontalAlignment(Element.ALIGN_LEFT);
				footer.addCell(footerDateCell);
				footer.addCell(footerTextCell);
			} else {
				logoCell.setHorizontalAlignment(Element.ALIGN_LEFT);
				
				pageNoCell.setHorizontalAlignment(Element.ALIGN_RIGHT);
				teleNumberCell.setHorizontalAlignment(Element.ALIGN_RIGHT);
				pageTable.addCell(pageNoCell);
				if(teleNumberCell != null){
				pageTable.addCell(teleNumberCell);
				} 
				PdfPCell pageTableCell = new PdfPCell(pageTable);
				pageTableCell.setBorder(Rectangle.NO_BORDER);
				pageTableCell.setHorizontalAlignment(Element.ALIGN_RIGHT);
				
				header.addCell(logoCell);
				header.addCell(headerTextCell);
				header.addCell(pageTableCell);
				
				footerTextCell.setHorizontalAlignment(Element.ALIGN_LEFT);
				footerDateCell.setHorizontalAlignment(Element.ALIGN_RIGHT);
				footer.addCell(footerTextCell);
				footer.addCell(footerDateCell);
			}
			header.setTotalWidth(page.width() - document.leftMargin()
					- document.rightMargin());
			header.writeSelectedRows(0, -1, document.leftMargin(), page
					.height()- document.topMargin() + header.getTotalHeight()-30,
					writer.getDirectContent());

			footer.setTotalWidth(page.width() - document.leftMargin()
					- document.rightMargin());
			footer.writeSelectedRows(0, -1, document.leftMargin(), document
					.bottomMargin(), writer.getDirectContent());
		} catch (Exception e) {
			throw new ExceptionConverter(e);
		}
	}
	
	public static void main(String args[]){
		new Test().createPDF();
	}
}
-------------------------------------------------------------------------
This SF.net email is sponsored by: Splunk Inc.
Still grepping through log files to find problems?  Stop.
Now Search log events and configuration files using AJAX and a browser.
Download your FREE copy of Splunk now >>  http://get.splunk.com/
_______________________________________________
iText-questions mailing list
iText-questions@lists.sourceforge.net
https://lists.sourceforge.net/lists/listinfo/itext-questions
Buy the iText book: http://itext.ugent.be/itext-in-action/

Reply via email to