Java Code Examples for com.lowagie.text.Table#addCell()

The following examples show how to use com.lowagie.text.Table#addCell() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: MyFirstTableTest.java    From itext2 with GNU Lesser General Public License v3.0 7 votes vote down vote up
/**
 * A very simple Table example.
 * 
 */
@Test
public void main() throws Exception {
	// step 1: creation of a document-object
	Document document = new Document();
	// step 2:
	// we create a writer that listens to the document
	// and directs a PDF-stream to a file
	PdfWriter.getInstance(document, PdfTestBase.getOutputStream("MyFirstTable.pdf"));
	// step 3: we open the document
	document.open();
	// step 4: we create a table and add it to the document
	Table table = new Table(2, 2); // 2 rows, 2 columns
	table.addCell("0.0");
	table.addCell("0.1");
	table.addCell("1.0");
	table.addCell("1.1");
	document.add(table);
	document.add(new Paragraph("converted to PdfPTable:"));
	table.setConvert2pdfptable(true);
	document.add(table);

	// step 5: we close the document
	document.close();
}
 
Example 2
Source File: Coversheet.java    From kfs with GNU Affero General Public License v3.0 6 votes vote down vote up
/**
 * Get the PDF Table containing trip information like trip id, date, and destination
 *
 * @returns {@link Table} used for a PDF
 */
protected Table getTripInfo() throws BadElementException {
    final Table retval = new Table(3);
    retval.setWidth(100f);
    retval.setBorder(NO_BORDER);
    retval.addCell(getHeaderCell("Trip/Event ID"));

    final Cell dateHeaderCell = getHeaderCell("Date");

    retval.addCell(dateHeaderCell);
    retval.addCell(getHeaderCell("Destination/Event Name"));
    retval.endHeaders();
    retval.addCell(getBorderlessCell(getTripId()));

    final Cell dateCell = getBorderlessCell(getDate());

    retval.addCell(dateCell);
    retval.addCell(getBorderlessCell(getDestination()));
    return retval;
}
 
Example 3
Source File: Coversheet.java    From kfs with GNU Affero General Public License v3.0 6 votes vote down vote up
/**
 * Get the PDF Table with personal information about the initiator and traveler
 *
 * @returns {@link Table} used for a PDF
 */
protected Table getPersonalInfo() throws BadElementException {
    final Table retval = new Table(2);
    retval.setWidth(100f);
    retval.setBorder(NO_BORDER);
    retval.addCell(getHeaderCell("Traveler"));

    final Cell initiatorHeaderCell = getHeaderCell("Request Submitted By");

    retval.addCell(initiatorHeaderCell);
    retval.endHeaders();
    retval.addCell(getTravelerInfo());

    final Cell initiatorCell = getInitiatorInfo();

    retval.addCell(initiatorCell);
    return retval;
}
 
Example 4
Source File: Coversheet.java    From kfs with GNU Affero General Public License v3.0 6 votes vote down vote up
public Table getExpenses() throws BadElementException {
    final Table retval = new Table(3);
    retval.setWidth(100f);
    retval.setBorder(NO_BORDER);
    retval.addCell(getHeaderCell("Expenses"));
    retval.addCell(getHeaderCell("Amount"));
    retval.addCell(getHeaderCell("Receipt Required?"));
    retval.endHeaders();

    for (final Map<String, String> expense : expenses) {
        retval.addCell(getBorderlessCell(expense.get("expenseType")));
        retval.addCell(getBorderlessCell(expense.get("amount")));
        retval.addCell(getBorderlessCell(expense.get("receipt")));
    }
    return retval;
}
 
Example 5
Source File: TableWithImageTest.java    From itext2 with GNU Lesser General Public License v3.0 5 votes vote down vote up
/**
 * A very simple PdfPTable example.
 * 
 */
@Test
public void main() throws Exception {
	// step 1: creation of a document-object
	Document document = new Document();
	// step 2:
	// we create a writer that listens to the document
	// and directs a PDF-stream to a file
	PdfWriter.getInstance(document, PdfTestBase.getOutputStream("imageTable.pdf"));
	// step 3: we open the document
	document.open();
	// step 4: we create a table and add it to the document
	Table table = new Table(2, 2); // 2 rows, 2 columns
	table.addCell(new Cell(Image.getInstance(PdfTestBase.RESOURCES_DIR + "otsoe.jpg")));
	table.addCell(new Cell(Image.getInstance(PdfTestBase.RESOURCES_DIR + "iText.gif")));
	Cell c1 = new Cell();
	c1.add(Image.getInstance(PdfTestBase.RESOURCES_DIR + "iText.gif"));
	table.addCell(c1);
	Cell c2 = new Cell();
	c2.add(Image.getInstance(PdfTestBase.RESOURCES_DIR + "otsoe.jpg"));
	table.addCell(c2);
	document.add(table);
	document.add(new Paragraph("converted to PdfPTable:"));
	table.setConvert2pdfptable(true);
	document.add(table);

	// step 5: we close the document
	document.close();
}
 
Example 6
Source File: LargeCellTest.java    From itext2 with GNU Lesser General Public License v3.0 5 votes vote down vote up
/**
 * Demonstrates the features of the old Table class.
 */
@Test
public void main() throws Exception {
	// step 1: creation of a document-object
	Document document = new Document(PageSize.A6);
	// step 2: creation of the writer-object
	PdfWriter.getInstance(document, PdfTestBase.getOutputStream("largecell.pdf"));
	// step 3: we open the document
	document.open();
	// step 4: we create a table and add it to the document
	Table table = new Table(3);
	table.setCellsFitPage(true);
	String text = "long long long long long long long long long long long long long long long long long long long long long long long long long long long long long long long long long long long long long long long long long long long long long long long long long long long long long long long long long long long long long long long long long long long long long long long long long long long long long long long long long long long long long long long long long long long long long long long long long long long long long long long long long long long long long long long long long long long long long long long long long long long long long long long long long long long long long long long long long long long long long long long long long long long long long long long long long long long long long long long long long long text";
	table.addCell("text");
	table.addCell("text");
	table.addCell("text");
	table.addCell(text);
	table.addCell(text + text);
	table.addCell(text);
	table.addCell("text");
	table.addCell("text");
	table.addCell("text");
	document.add(table);

	// step 5: we close the document
	document.close();
}
 
Example 7
Source File: MPdfWriter.java    From javamelody with Apache License 2.0 5 votes vote down vote up
/**
 * Effectue le rendu des headers.
 *
 * @param table
 *           MBasicTable
 * @param datatable
 *           Table
 * @throws BadElementException
 *            e
 */
protected void renderHeaders(final MBasicTable table, final Table datatable)
		throws BadElementException {
	final int columnCount = table.getColumnCount();
	final TableColumnModel columnModel = table.getColumnModel();
	// size of columns
	float totalWidth = 0;
	for (int i = 0; i < columnCount; i++) {
		totalWidth += columnModel.getColumn(i).getWidth();
	}
	final float[] headerwidths = new float[columnCount];
	for (int i = 0; i < columnCount; i++) {
		headerwidths[i] = 100f * columnModel.getColumn(i).getWidth() / totalWidth;
	}
	datatable.setWidths(headerwidths);
	datatable.setWidth(100f);

	// table header
	final Font font = FontFactory.getFont(FontFactory.HELVETICA, 12, Font.BOLD);
	datatable.getDefaultCell().setBorderWidth(2);
	datatable.getDefaultCell().setHorizontalAlignment(Element.ALIGN_CENTER);
	// datatable.setDefaultCellGrayFill(0.75f);

	String text;
	Object value;
	for (int i = 0; i < columnCount; i++) {
		value = columnModel.getColumn(i).getHeaderValue();
		text = value != null ? value.toString() : "";
		datatable.addCell(new Phrase(text, font));
	}
	// end of the table header
	datatable.endHeaders();
}
 
Example 8
Source File: MPdfWriter.java    From javamelody with Apache License 2.0 5 votes vote down vote up
/**
 * Effectue le rendu de la liste.
 *
 * @param table
 *           MBasicTable
 * @param datatable
 *           Table
 * @throws BadElementException
 *            e
 */
protected void renderList(final MBasicTable table, final Table datatable)
		throws BadElementException {
	final int columnCount = table.getColumnCount();
	final int rowCount = table.getRowCount();
	// data rows
	final Font font = FontFactory.getFont(FontFactory.HELVETICA, 10, Font.NORMAL);
	datatable.getDefaultCell().setBorderWidth(1);
	datatable.getDefaultCell().setHorizontalAlignment(Element.ALIGN_LEFT);
	// datatable.setDefaultCellGrayFill(0);
	Object value;
	String text;
	int horizontalAlignment;
	for (int k = 0; k < rowCount; k++) {
		for (int i = 0; i < columnCount; i++) {
			value = getValueAt(table, k, i);
			if (value instanceof Number || value instanceof Date) {
				horizontalAlignment = Element.ALIGN_RIGHT;
			} else if (value instanceof Boolean) {
				horizontalAlignment = Element.ALIGN_CENTER;
			} else {
				horizontalAlignment = Element.ALIGN_LEFT;
			}
			datatable.getDefaultCell().setHorizontalAlignment(horizontalAlignment);
			text = getTextAt(table, k, i);
			datatable.addCell(new Phrase(8, text != null ? text : "", font));
		}
	}
}
 
Example 9
Source File: RepeatingTableTest.java    From itext2 with GNU Lesser General Public License v3.0 4 votes vote down vote up
/**
 * Shows how a table is split if it doesn't fit the page.
 */
@Test
public void main() throws Exception {
	// creation of the document with a certain size and certain margins
	Document document = new Document(PageSize.A4.rotate(), 50, 50, 50, 50);

	// creation of the different writers
	PdfWriter.getInstance(document, PdfTestBase.getOutputStream("repeatingtable.pdf"));

	// we add some meta information to the document
	document.addAuthor("Alan Soukup");
	document.addSubject("This is the result of a Test.");

	document.open();

	Table datatable = new Table(10);

	int headerwidths[] = { 10, 24, 12, 12, 7, 7, 7, 7, 7, 7 };
	datatable.setWidths(headerwidths);
	datatable.setWidth(100);
	datatable.setPadding(3);

	// the first cell spans 10 columns
	Cell cell = new Cell(new Phrase("Administration -System Users Report", FontFactory.getFont(
			FontFactory.HELVETICA, 24, Font.BOLD)));
	cell.setHorizontalAlignment(Element.ALIGN_CENTER);
	cell.setLeading(30);
	cell.setColspan(10);
	cell.setBorder(Rectangle.NO_BORDER);
	cell.setBackgroundColor(new Color(0xC0, 0xC0, 0xC0));
	datatable.addCell(cell);

	// These cells span 2 rows
	datatable.getDefaultCell().setBorderWidth(2);
	datatable.getDefaultCell().setHorizontalAlignment(1);
	datatable.addCell("User Id");
	datatable.addCell("Name\nAddress");
	datatable.addCell("Company");
	datatable.addCell("Department");
	datatable.addCell("Admin");
	datatable.addCell("Data");
	datatable.addCell("Expl");
	datatable.addCell("Prod");
	datatable.addCell("Proj");
	datatable.addCell("Online");

	// this is the end of the table header
	datatable.endHeaders();

	datatable.getDefaultCell().setBorderWidth(1);

	for (int i = 1; i < 30; i++) {

		datatable.getDefaultCell().setHorizontalAlignment(Element.ALIGN_LEFT);

		datatable.addCell("myUserId");
		datatable
				.addCell("Somebody with a very, very, very, very, very, very, very, very, very, very, very, very, very, very, very, very, very, very, very, very, very, very, very, very, very, very, very, very, very, very, very long long name");
		datatable.addCell("No Name Company");
		datatable.addCell("D" + i);

		datatable.getDefaultCell().setHorizontalAlignment(Element.ALIGN_CENTER);
		datatable.addCell("No");
		datatable.addCell("Yes");
		datatable.addCell("No");
		datatable.addCell("Yes");
		datatable.addCell("No");
		datatable.addCell("Yes");

	}
	document.add(new Paragraph("com.lowagie.text.Table - Cells split"));
	document.add(datatable);
	document.newPage();
	document.add(new Paragraph("com.lowagie.text.pdf.PdfPTable - Cells split\n\n"));
	datatable.setConvert2pdfptable(true);
	document.add(datatable);
	document.newPage();
	document.add(new Paragraph("com.lowagie.text.Table - Cells kept together"));
	datatable.setConvert2pdfptable(false);
	datatable.setCellsFitPage(true);
	document.add(datatable);
	document.newPage();
	document.add(new Paragraph("com.lowagie.text.pdf.PdfPTable - Cells kept together\n\n"));
	datatable.setConvert2pdfptable(true);
	document.add(datatable);

	// we close the document
	document.close();
}
 
Example 10
Source File: RtfTOCandCellbordersTest.java    From itext2 with GNU Lesser General Public License v3.0 4 votes vote down vote up
/**
 * Creates an RTF document with a TOC and Table with special Cellborders.
 * 
 * 
 */
@Test
public void main() throws Exception {
	Document document = new Document();
	RtfWriter2 writer2 = RtfWriter2.getInstance(document, PdfTestBase.getOutputStream("toc.rtf"));

	writer2.setAutogenerateTOCEntries(true);

	document.open();

	Paragraph para = new Paragraph();
	para.add(new RtfTableOfContents("RIGHT CLICK AND HERE AND SELECT \"UPDATE FIELD\" TO UPDATE."));
	document.add(para);

	Paragraph par = new Paragraph("This is some sample content.");
	Chapter chap1 = new Chapter("Chapter 1", 1);
	chap1.add(par);
	Chapter chap2 = new Chapter("Chapter 2", 2);
	chap2.add(par);
	document.add(chap1);
	document.add(chap2);

	for (int i = 0; i < 300; i++) {
		if (i == 158) {
			document.add(new RtfTOCEntry("This is line 158."));
		}
		document.add(new Paragraph("Line " + i));
	}

	document.add(new RtfTOCEntry("Cell border demonstration"));

	Table table = new Table(3);

	RtfCell cellDotted = new RtfCell("Dotted border");
	cellDotted.setBorders(new RtfBorderGroup(Rectangle.BOX, RtfBorder.BORDER_DOTTED, 1, new Color(0, 0, 0)));
	RtfCell cellEmbossed = new RtfCell("Embossed border");
	cellEmbossed.setBorders(new RtfBorderGroup(Rectangle.BOX, RtfBorder.BORDER_EMBOSS, 1, new Color(0, 0, 0)));
	RtfCell cellNoBorder = new RtfCell("No border");
	cellNoBorder.setBorders(new RtfBorderGroup());

	table.addCell(cellDotted);
	table.addCell(cellEmbossed);
	table.addCell(cellNoBorder);

	document.add(table);
	document.close();
}
 
Example 11
Source File: ExtendedHeaderFooterTest.java    From itext2 with GNU Lesser General Public License v3.0 4 votes vote down vote up
/**
 * Extended headers / footers example
 * 
 * 
 */
@Test
public void main() throws Exception {
	Document document = new Document();
	RtfWriter2.getInstance(document, PdfTestBase.getOutputStream("ExtendedHeaderFooter.rtf"));

	// Create the Paragraphs that will be used in the header.
	Paragraph date = new Paragraph("01.01.2010");
	date.setAlignment(Paragraph.ALIGN_RIGHT);
	Paragraph address = new Paragraph("TheFirm\nTheRoad 24, TheCity\n" + "+00 99 11 22 33 44");

	// Create the RtfHeaderFooter with an array containing the Paragraphs to
	// add
	RtfHeaderFooter header = new RtfHeaderFooter(new Element[] { date, address });

	// Set the header
	document.setHeader(header);

	// Create the table that will be used as the footer
	Table footer = new Table(2);
	footer.setBorder(0);
	footer.getDefaultCell().setBorder(0);
	footer.setWidth(100);
	footer.addCell(new Cell("(c) Mark Hall"));
	Paragraph pageNumber = new Paragraph("Page ");

	// The RtfPageNumber is an RTF specific element that adds a page number
	// field
	pageNumber.add(new RtfPageNumber());
	pageNumber.setAlignment(Paragraph.ALIGN_RIGHT);
	footer.addCell(new Cell(pageNumber));

	// Create the RtfHeaderFooter and set it as the footer to use
	document.setFooter(new RtfHeaderFooter(footer));

	document.open();

	document.add(new Paragraph("This document has headers and footers created"
			+ " using the RtfHeaderFooter class."));

	document.close();

}
 
Example 12
Source File: ExtendedTableCellTest.java    From itext2 with GNU Lesser General Public License v3.0 4 votes vote down vote up
/**
 * Extended borders for Table Cells.
 * 
 * 
 */
@Test
public void main() throws Exception {
	Document document = new Document();
	RtfWriter2.getInstance(document, PdfTestBase.getOutputStream("ExtendedTableCell.rtf"));

	document.open();

	Table table = new Table(3);

	// Create a simple RtfCell with a dotted border.
	RtfCell cellDotted = new RtfCell("Dotted border");
	cellDotted.setBorders(new RtfBorderGroup(Rectangle.BOX, RtfBorder.BORDER_DOTTED, 1, new Color(0, 0, 0)));

	// Create a simple RtfCell with an embossed border.
	RtfCell cellEmbossed = new RtfCell("Embossed border");
	cellEmbossed.setBorders(new RtfBorderGroup(Rectangle.BOX, RtfBorder.BORDER_EMBOSS, 1, new Color(0, 0, 0)));

	// Create a simple RtfCell with no border.
	RtfCell cellNoBorder = new RtfCell("No border");
	cellNoBorder.setBorders(new RtfBorderGroup());
	cellNoBorder.setRowspan(2);

	// Create a simple RtfCell that only has a border
	// on the bottom side.
	RtfCell bottomBorder = new RtfCell("Bottom border");
	bottomBorder.setBorders(new RtfBorderGroup(Rectangle.BOTTOM, RtfBorder.BORDER_SINGLE, 2, new Color(255, 0, 0)));

	// Create a simple RtfCell that has different borders
	// on the left and bottom sides.
	RtfCell mixedBorder = new RtfCell("Mixed border");
	RtfBorderGroup mixedBorders = new RtfBorderGroup();
	mixedBorders.addBorder(Rectangle.RIGHT, RtfBorder.BORDER_DOUBLE_WAVY, 2, Color.GREEN);
	mixedBorders.addBorder(Rectangle.BOTTOM, RtfBorder.BORDER_DOT_DASH, 1, Color.BLUE);
	mixedBorder.setBorders(mixedBorders);

	// Add the cells to the table
	table.addCell(cellDotted);
	table.addCell(cellEmbossed);
	table.addCell(cellNoBorder);
	table.addCell(bottomBorder);
	table.addCell(mixedBorder);

	document.add(table);

	document.close();
}