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

The following examples show how to use com.lowagie.text.pdf.PdfPTable#setTotalWidth() . 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: SplitTableTest.java    From itext2 with GNU Lesser General Public License v3.0 8 votes vote down vote up
/**
 * Break a large table up into several smaller tables for memory management
 * purposes.
 * 
 */
@Test
public void main() throws Exception {
	// step1
	Document document = new Document(PageSize.A4, 10, 10, 10, 10);
	// step2
	PdfWriter writer = PdfWriter.getInstance(document, PdfTestBase.getOutputStream("SplitTable.pdf"));
	// step3
	document.open();
	// step4

	PdfContentByte cb = writer.getDirectContent();
	PdfPTable table = new PdfPTable(10);
	for (int k = 1; k <= 100; ++k) {
		table.addCell("The number " + k);
	}
	table.setTotalWidth(800);
	table.writeSelectedRows(0, 5, 0, -1, 50, 650, cb);
	document.newPage();
	table.writeSelectedRows(5, -1, 0, -1, 50, 650, cb);
	document.close();

	// step5
	document.close();
}
 
Example 2
Source File: EndPageTest.java    From itext2 with GNU Lesser General Public License v3.0 6 votes vote down vote up
/**
 * @see com.lowagie.text.pdf.PdfPageEventHelper#onEndPage(com.lowagie.text.pdf.PdfWriter,
 *      com.lowagie.text.Document)
 */
public void onEndPage(PdfWriter writer, Document document) {
	try {
		Rectangle page = document.getPageSize();
		PdfPTable head = new PdfPTable(3);
		for (int k = 1; k <= 6; ++k)
			head.addCell("head " + k);
		head.setTotalWidth(page.getWidth() - document.leftMargin()
				- document.rightMargin());
		head.writeSelectedRows(
				0,
				-1,
				document.leftMargin(),
				page.getHeight() - document.topMargin()
						+ head.getTotalHeight(), writer.getDirectContent());
		PdfPTable foot = new PdfPTable(3);
		for (int k = 1; k <= 6; ++k)
			foot.addCell("foot " + k);
		foot.setTotalWidth(page.getWidth() - document.leftMargin()
				- document.rightMargin());
		foot.writeSelectedRows(0, -1, document.leftMargin(),
				document.bottomMargin(), writer.getDirectContent());
	} catch (Exception e) {
		throw new ExceptionConverter(e);
	}
}
 
Example 3
Source File: IncTable.java    From gcs with Mozilla Public License 2.0 5 votes vote down vote up
public PdfPTable buildTable() {
	if (rows.isEmpty()) {
		return new PdfPTable(1);
	}
	int ncol = 0;
	ArrayList c0 = (ArrayList) rows.get(0);
	for (int k = 0; k < c0.size(); ++k) {
		ncol += ((PdfPCell) c0.get(k)).getColspan();
	}
	PdfPTable table = new PdfPTable(ncol);
	String width = (String) props.get("width");
	if (width == null) {
		table.setWidthPercentage(100);
	} else {
		if (width.endsWith("%")) {
			table.setWidthPercentage(Float.parseFloat(width.substring(0, width.length() - 1)));
		} else {
			table.setTotalWidth(Float.parseFloat(width));
			table.setLockedWidth(true);
		}
	}
	for (int row = 0; row < rows.size(); ++row) {
		ArrayList col = (ArrayList) rows.get(row);
		for (int k = 0; k < col.size(); ++k) {
			table.addCell((PdfPCell) col.get(k));
		}
	}
	return table;
}
 
Example 4
Source File: IncTable.java    From itext2 with GNU Lesser General Public License v3.0 5 votes vote down vote up
public PdfPTable buildTable() {
    if (rows.isEmpty())
        return new PdfPTable(1);
    int ncol = 0;
    ArrayList c0 = (ArrayList)rows.get(0);
    for (int k = 0; k < c0.size(); ++k) {
        ncol += ((PdfPCell)c0.get(k)).getColspan();
    }
    PdfPTable table = new PdfPTable(ncol);
    String width = (String)props.get("width");
    if (width == null)
        table.setWidthPercentage(100);
    else {
        if (width.endsWith("%"))
            table.setWidthPercentage(Float.parseFloat(width.substring(0, width.length() - 1)));
        else {
            table.setTotalWidth(Float.parseFloat(width));
            table.setLockedWidth(true);
        }
    }
    for (int row = 0; row < rows.size(); ++row) {
        ArrayList col = (ArrayList)rows.get(row);
        for (int k = 0; k < col.size(); ++k) {
            table.addCell((PdfPCell)col.get(k));
        }
    }
    return table;
}
 
Example 5
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 6
Source File: Conversion.java    From docx4j-export-FO with Apache License 2.0 5 votes vote down vote up
/**
 * @see com.lowagie.text.pdf.PdfPageEventHelper#onEndPage(com.lowagie.text.pdf.PdfWriter, com.lowagie.text.Document)
 */
public void onEndPage(PdfWriter writer, Document document) {
    try {
        Rectangle page = document.getPageSize();

        if (headerFooterPolicy.getHeader(writer.getPageNumber())!=null) {
         Hdr hdr = headerFooterPolicy.getHeader(writer.getPageNumber()).getJaxbElement();
         PdfPTable head = new PdfPTable(1); // num cols	            
         // TODO - no cell borders
         traverseBlockLevelContent( hdr.getEGBlockLevelElts(), head);
         head.setTotalWidth(page.getWidth() - document.leftMargin() - document.rightMargin());
         head.writeSelectedRows(0, -1, document.leftMargin(), page.getHeight() - document.topMargin() + head.getTotalHeight(),
             writer.getDirectContent());
        }
        
        if (headerFooterPolicy.getFooter(writer.getPageNumber())!=null) {
         Ftr ftr = headerFooterPolicy.getFooter(writer.getPageNumber()).getJaxbElement();	            
         PdfPTable foot = new PdfPTable(1);
         traverseBlockLevelContent( ftr.getEGBlockLevelElts(), foot);
         foot.setTotalWidth(page.getWidth() - document.leftMargin() - document.rightMargin());
         foot.writeSelectedRows(0, -1, document.leftMargin(), document.bottomMargin(),
             writer.getDirectContent());
        }
    }
    catch (Exception e) {
        throw new ExceptionConverter(e);
    }
}
 
Example 7
Source File: DefaultPdfDataEntryFormService.java    From dhis2-core with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
private PdfPTable getProgramStageMainTable()
{
    PdfPTable mainTable = new PdfPTable( 1 ); // Code 1

    mainTable.setTotalWidth( 800f );
    mainTable.setLockedWidth( true );
    mainTable.setHorizontalAlignment( Element.ALIGN_LEFT );

    return mainTable;
}
 
Example 8
Source File: SimpleTable.java    From gcs with Mozilla Public License 2.0 4 votes vote down vote up
/**
 * Creates a PdfPTable object based on this TableAttributes object.
 * 
 * @return a com.lowagie.text.pdf.PdfPTable object
 * @throws DocumentException
 */
public PdfPTable createPdfPTable() throws DocumentException {
	if (content.isEmpty()) {
		throw new BadElementException("Trying to create a table without rows.");
	}
	SimpleCell row = (SimpleCell) content.get(0);
	SimpleCell cell;
	int columns = 0;
	for (Iterator i = row.getContent().iterator(); i.hasNext();) {
		cell = (SimpleCell) i.next();
		columns += cell.getColspan();
	}
	float[] widths = new float[columns];
	float[] widthpercentages = new float[columns];
	PdfPTable table = new PdfPTable(columns);
	table.setTableEvent(this);
	table.setHorizontalAlignment(alignment);
	int pos;
	for (Iterator rows = content.iterator(); rows.hasNext();) {
		row = (SimpleCell) rows.next();
		pos = 0;
		for (Iterator cells = row.getContent().iterator(); cells.hasNext();) {
			cell = (SimpleCell) cells.next();
			if (Float.isNaN(cell.getSpacing_left())) {
				cell.setSpacing_left(cellspacing / 2f);
			}
			if (Float.isNaN(cell.getSpacing_right())) {
				cell.setSpacing_right(cellspacing / 2f);
			}
			if (Float.isNaN(cell.getSpacing_top())) {
				cell.setSpacing_top(cellspacing / 2f);
			}
			if (Float.isNaN(cell.getSpacing_bottom())) {
				cell.setSpacing_bottom(cellspacing / 2f);
			}
			cell.setPadding(cellpadding);
			table.addCell(cell.createPdfPCell(row));
			if (cell.getColspan() == 1) {
				if (cell.getWidth() > 0) {
					widths[pos] = cell.getWidth();
				}
				if (cell.getWidthpercentage() > 0) {
					widthpercentages[pos] = cell.getWidthpercentage();
				}
			}
			pos += cell.getColspan();
		}
	}
	float sumWidths = 0f;
	for (int i = 0; i < columns; i++) {
		if (widths[i] == 0) {
			sumWidths = 0;
			break;
		}
		sumWidths += widths[i];
	}
	if (sumWidths > 0) {
		table.setTotalWidth(sumWidths);
		table.setWidths(widths);
	} else {
		for (int i = 0; i < columns; i++) {
			if (widthpercentages[i] == 0) {
				sumWidths = 0;
				break;
			}
			sumWidths += widthpercentages[i];
		}
		if (sumWidths > 0) {
			table.setWidths(widthpercentages);
		}
	}
	if (width > 0) {
		table.setTotalWidth(width);
	}
	if (widthpercentage > 0) {
		table.setWidthPercentage(widthpercentage);
	}
	return table;
}
 
Example 9
Source File: SimpleTable.java    From itext2 with GNU Lesser General Public License v3.0 4 votes vote down vote up
/**
 * Creates a PdfPTable object based on this TableAttributes object.
 * @return a com.lowagie.text.pdf.PdfPTable object
 * @throws DocumentException
 */
public PdfPTable createPdfPTable() throws DocumentException {
	if (content.isEmpty()) throw new BadElementException("Trying to create a table without rows.");
	SimpleCell row = (SimpleCell)content.get(0);
	SimpleCell cell;
	int columns = 0;
	for (Iterator i = row.getContent().iterator(); i.hasNext(); ) {
		cell = (SimpleCell)i.next();
		columns += cell.getColspan();
	}
	float[] widths = new float[columns];
	float[] widthpercentages = new float[columns];
	PdfPTable table = new PdfPTable(columns);
	table.setTableEvent(this);
	table.setHorizontalAlignment(alignment);
	int pos;
	for (Iterator rows = content.iterator(); rows.hasNext(); ) {
		row = (SimpleCell)rows.next();
		pos = 0;
		for (Iterator cells = row.getContent().iterator(); cells.hasNext(); ) {
			cell = (SimpleCell)cells.next();
			if (Float.isNaN(cell.getSpacing_left()))	{
				cell.setSpacing_left(cellspacing / 2f);
			}
			if (Float.isNaN(cell.getSpacing_right()))	{
				cell.setSpacing_right(cellspacing / 2f);
			}
			if (Float.isNaN(cell.getSpacing_top()))	{
				cell.setSpacing_top(cellspacing / 2f);
			}
			if (Float.isNaN(cell.getSpacing_bottom()))	{
				cell.setSpacing_bottom(cellspacing / 2f);
			}
			cell.setPadding(cellpadding);
			table.addCell(cell.createPdfPCell(row));
			if (cell.getColspan() == 1) {
				if (cell.getWidth() > 0) widths[pos] = cell.getWidth();
				if (cell.getWidthpercentage() > 0) widthpercentages[pos] = cell.getWidthpercentage();
			}
			pos += cell.getColspan();
		}
	}
	float sumWidths = 0f;
	for(int i = 0; i < columns; i++) {
		if (widths[i] == 0) {
			sumWidths = 0;
			break;
		}
		sumWidths += widths[i];
	}
	if (sumWidths > 0) {
		table.setTotalWidth(sumWidths);
		table.setWidths(widths);
	}
	else {
		for(int i = 0; i < columns; i++) {
			if (widthpercentages[i] == 0) {
				sumWidths = 0;
				break;
			}
			sumWidths += widthpercentages[i];
		}
		if (sumWidths > 0) {
			table.setWidths(widthpercentages);
		}
	}
	if (width > 0) {
		table.setTotalWidth(width);
	}
	if (widthpercentage > 0) {
		table.setWidthPercentage(widthpercentage);
	}
	return table;
}
 
Example 10
Source File: StudentCardTest.java    From itext2 with GNU Lesser General Public License v3.0 4 votes vote down vote up
/**
 * Generates a StudentCard
 */
@Test
public void main() throws Exception {

	// step 1: creation of a document-object
	Rectangle rect = new Rectangle(243, 153);
	rect.setBackgroundColor(new Color(0xFF, 0xFF, 0xCC));
	Document document = new Document(rect, 10, 10, 10, 10);

	// step 2:
	PdfWriter writer = PdfWriter.getInstance(document, PdfTestBase.getOutputStream("studentcard.pdf"));

	// step 3: we open the document
	document.open();

	// step 4:
	Font font = FontFactory.getFont(FontFactory.HELVETICA, 14, Font.BOLD, Color.BLUE);
	Paragraph p = new Paragraph("Ghent University", font);
	p.setAlignment(Element.ALIGN_CENTER);
	document.add(p);
	PdfContentByte cb = writer.getDirectContent();
	Font f = FontFactory.getFont(FontFactory.HELVETICA, 8);
	PdfPTable outertable = new PdfPTable(3);
	outertable.setTotalWidth(200);
	outertable.getDefaultCell().setBorder(Rectangle.NO_BORDER);
	float[] outer = { 60, 25, 15 };
	outertable.setWidths(outer);
	PdfPTable innertable = new PdfPTable(2);
	float[] inner = { 35, 65 };
	innertable.setWidths(inner);
	innertable.addCell(new Paragraph("name:", f));
	innertable.addCell(new Paragraph("Bruno Lowagie", f));
	innertable.addCell(new Paragraph("date of birth:", f));
	innertable.addCell(new Paragraph("June 10th, 1970", f));
	innertable.addCell(new Paragraph("Study Program:", f));
	innertable.addCell(new Paragraph("master in civil engineering", f));
	innertable.addCell(new Paragraph("option:", f));
	innertable.addCell(new Paragraph("architecture", f));
	outertable.addCell(innertable);
	outertable.getDefaultCell().setBackgroundColor(new Color(0xFF, 0xDE, 0xAD));
	outertable.addCell(Image.getInstance(PdfTestBase.RESOURCES_DIR + "bruno.jpg"));
	BarcodeEAN codeEAN = new BarcodeEAN();
	codeEAN.setCodeType(Barcode.EAN13);
	codeEAN.setCode("8010012529736");
	Image imageEAN = codeEAN.createImageWithBarcode(cb, null, null);
	imageEAN.setRotationDegrees(90);
	outertable.getDefaultCell().setBackgroundColor(Color.WHITE);
	outertable.addCell(imageEAN);
	outertable.writeSelectedRows(0, -1, 20, 100, writer.getDirectContent());

	// step 5: we close the document
	document.close();
}
 
Example 11
Source File: CellWidthsTest.java    From itext2 with GNU Lesser General Public License v3.0 4 votes vote down vote up
/**
 * Width manipulations of cells.
 * 
 */
@Test
public void main() throws Exception {
	// step1
	Document document = new Document(PageSize.A4, 36, 36, 36, 36);
	// step2
	PdfWriter.getInstance(document, PdfTestBase.getOutputStream("CellWidths.pdf"));
	// step3
	document.open();
	// step4
	float[] widths = { 0.1f, 0.1f, 0.05f, 0.75f };
	PdfPTable table = new PdfPTable(widths);
	table.addCell("10%");
	table.addCell("10%");
	table.addCell("5%");
	table.addCell("75%");
	table.addCell("aa");
	table.addCell("aa");
	table.addCell("a");
	table.addCell("aaaaaaaaaaaaaaa");
	table.addCell("bb");
	table.addCell("bb");
	table.addCell("b");
	table.addCell("bbbbbbbbbbbbbbb");
	table.addCell("cc");
	table.addCell("cc");
	table.addCell("c");
	table.addCell("ccccccccccccccc");
	document.add(table);
	document.add(new Paragraph("We change the percentages:\n\n"));
	widths[0] = 20f;
	widths[1] = 20f;
	widths[2] = 10f;
	widths[3] = 50f;
	table.setWidths(widths);
	document.add(table);
	widths[0] = 40f;
	widths[1] = 40f;
	widths[2] = 20f;
	widths[3] = 300f;
	Rectangle r = new Rectangle(PageSize.A4.getRight(72), PageSize.A4.getTop(72));
	table.setWidthPercentage(widths, r);
	document.add(new Paragraph("We change the percentage using absolute widths:\n\n"));
	document.add(table);
	document.add(new Paragraph("We use a locked width:\n\n"));
	table.setTotalWidth(300);
	table.setLockedWidth(true);
	document.add(table);

	// step5
	document.close();
}