Java Code Examples for com.lowagie.text.pdf.PdfPTable#setHeaderRows()

The following examples show how to use com.lowagie.text.pdf.PdfPTable#setHeaderRows() . 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: CellEventsTest.java    From itext2 with GNU Lesser General Public License v3.0 5 votes vote down vote up
/**
 * General example using cell events.
 * 
 */
@Test
public void main() throws Exception {
	// step1
	Document document = new Document(PageSize.A4, 50, 50, 50, 50);
	// step2
	PdfWriter writer = PdfWriter.getInstance(document, PdfTestBase.getOutputStream("CellEvents.pdf"));
	// step3
	document.open();
	// step4
	CellEventsTest event = new CellEventsTest();
	Image im = Image.getInstance(PdfTestBase.RESOURCES_DIR + "otsoe.jpg");
	im.setRotationDegrees(30);
	PdfPTable table = new PdfPTable(4);
	table.addCell("text 1");
	PdfPCell cell = new PdfPCell(im, true);
	cell.setCellEvent(event);
	table.addCell(cell);
	table.addCell("text 3");
	im.setRotationDegrees(0);
	table.addCell(im);
	table.setTotalWidth(300);
	PdfContentByte cb = writer.getDirectContent();
	table.writeSelectedRows(0, -1, 50, 600, cb);
	table.setHeaderRows(3);
	document.add(table);

	// step5
	document.close();
}
 
Example 2
Source File: GridUtils.java    From dhis2-core with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
private static void toPdfInternal( Grid grid, Document document, float spacing )
{
    if ( grid == null || grid.getVisibleWidth() == 0 )
    {
        return;
    }

    PdfPTable table = new PdfPTable( grid.getVisibleWidth() );

    table.setHeaderRows( 1 );
    table.setWidthPercentage( 100F );
    table.setKeepTogether( false );
    table.setSpacingAfter( spacing );

    table.addCell( resetPaddings( getTitleCell( grid.getTitle(), grid.getVisibleWidth() ), 0, 30, 0, 0 ) );

    if ( StringUtils.isNotEmpty( grid.getSubtitle() ) )
    {
        table.addCell( getSubtitleCell( grid.getSubtitle(), grid.getVisibleWidth() ) );
        table.addCell( getEmptyCell( grid.getVisibleWidth(), 30 ) );
    }

    for ( GridHeader header : grid.getVisibleHeaders() )
    {
        table.addCell( getItalicCell( header.getColumn() ) );
    }

    table.addCell( getEmptyCell( grid.getVisibleWidth(), 10 ) );

    for ( List<Object> row : grid.getVisibleRows() )
    {
        for ( Object col : row )
        {
            table.addCell( getTextCell( col ) );
        }
    }

    addTableToDocument( document, table );
}
 
Example 3
Source File: PdfRuntimeDependenciesReport.java    From javamelody with Apache License 2.0 5 votes vote down vote up
private void writeHeader() throws DocumentException {
	final List<String> headers = new ArrayList<String>();
	headers.add("Beans");
	headers.addAll(calledBeans);
	final int[] relativeWidths = new int[headers.size()];
	Arrays.fill(relativeWidths, 0, headers.size(), 1);
	relativeWidths[0] = 4;

	final PdfPTable table = new PdfPTable(headers.size());
	table.setWidthPercentage(100);
	table.setWidths(relativeWidths);
	table.setHeaderRows(1);
	final PdfPCell defaultCell = table.getDefaultCell();
	defaultCell.setGrayFill(0.9f);
	defaultCell.setHorizontalAlignment(Element.ALIGN_CENTER);
	defaultCell.setVerticalAlignment(Element.ALIGN_MIDDLE);
	defaultCell.setPaddingLeft(0);
	defaultCell.setPaddingRight(0);
	for (final String header : headers) {
		table.addCell(new Phrase(header, boldCellFont));
		// pas la première entête de colonne
		defaultCell.setRotation(90);
	}
	defaultCell.setRotation(0);
	defaultCell.setPaddingLeft(2);
	defaultCell.setPaddingRight(2);
	currentTable = table;
}
 
Example 4
Source File: PDFPrinter.java    From unitime with Apache License 2.0 5 votes vote down vote up
@Override
public void printHeader(String... fields) {
	iTable = new PdfPTable(fields.length - iHiddenColumns.size());
	iMaxWidth = new float[fields.length];
	iTable.setHeaderRows(1);
	iTable.setWidthPercentage(100);

	for (int idx = 0; idx < fields.length; idx++) {
		if (iHiddenColumns.contains(idx)) continue;
		String f = fields[idx];
		
		PdfPCell cell = new PdfPCell();
		cell.setBorder(Rectangle.BOTTOM);
		cell.setVerticalAlignment(Element.ALIGN_TOP);
		cell.setHorizontalAlignment(Element.ALIGN_LEFT);
		
		Font font = PdfFont.getFont(true);
		Paragraph ch = new Paragraph(f, font);
		ch.setLeading(0f, 1f);
		cell.addElement(ch);		
		iTable.addCell(cell);
		
		float width = 0; 
		if (f.indexOf('\n')>=0) {
			for (StringTokenizer s = new StringTokenizer(f,"\n"); s.hasMoreTokens();)
				width = Math.max(width,font.getBaseFont().getWidthPoint(s.nextToken(), font.getSize()));
		} else 
			width = Math.max(width,font.getBaseFont().getWidthPoint(f, font.getSize()));
		iMaxWidth[idx] = width;
	}
}
 
Example 5
Source File: AddBigTableTest.java    From itext2 with GNU Lesser General Public License v3.0 4 votes vote down vote up
/**
 * Big PdfPTable with document.add().
 * 
 */
@Test
public void main() throws Exception {

	// step1
	Document document = new Document(PageSize.A4.rotate(), 10, 10, 10, 10);
	// step2
	PdfWriter.getInstance(document, PdfTestBase.getOutputStream("AddBigTable.pdf"));
	// step3
	document.open();
	// step4
	String[] bogusData = { "M0065920", "SL", "FR86000P", "PCGOLD", "119000", "96 06", "2001-08-13", "4350",
			"6011648299", "FLFLMTGP", "153", "119000.00" };
	int NumColumns = 12;

	PdfPTable datatable = new PdfPTable(NumColumns);
	int headerwidths[] = { 9, 4, 8, 10, 8, 11, 9, 7, 9, 10, 4, 10 }; // percentage
	datatable.setWidths(headerwidths);
	datatable.setWidthPercentage(100); // percentage
	datatable.getDefaultCell().setPadding(3);
	datatable.getDefaultCell().setBorderWidth(2);
	datatable.getDefaultCell().setHorizontalAlignment(Element.ALIGN_CENTER);
	datatable.addCell("Clock #");
	datatable.addCell("Trans Type");
	datatable.addCell("Cusip");
	datatable.addCell("Long Name");
	datatable.addCell("Quantity");
	datatable.addCell("Fraction Price");
	datatable.addCell("Settle Date");
	datatable.addCell("Portfolio");
	datatable.addCell("ADP Number");
	datatable.addCell("Account ID");
	datatable.addCell("Reg Rep ID");
	datatable.addCell("Amt To Go ");

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

	datatable.getDefaultCell().setBorderWidth(1);
	for (int i = 1; i < 750; i++) {
		if (i % 2 == 1) {
			datatable.getDefaultCell().setGrayFill(0.9f);
		}
		for (int x = 0; x < NumColumns; x++) {
			datatable.addCell(bogusData[x]);
		}
		if (i % 2 == 1) {
			datatable.getDefaultCell().setGrayFill(1);
		}
	}
	document.add(datatable);

	// step5
	document.close();
}