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

The following examples show how to use com.lowagie.text.pdf.PdfPTable#setWidths() . 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: DefaultPdfDataEntryFormService.java    From dhis2-core with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
private void insertTable_OrgAndPeriod( PdfPTable mainTable, PdfWriter writer, List<Period> periods )
    throws IOException, DocumentException
{
    boolean hasBorder = false;
    float width = 220.0f;

    // Input TextBox size
    Rectangle rectangle = new Rectangle( width, PdfDataEntryFormUtil.CONTENT_HEIGHT_DEFAULT );

    // Add Organization ID/Period textfield
    // Create A table to add for each group AT HERE
    PdfPTable table = new PdfPTable( 2 ); // Code 1
    table.setWidths( new int[]{ 1, 3 } );
    table.setHorizontalAlignment( Element.ALIGN_LEFT );


    addCell_Text( table, PdfDataEntryFormUtil.getPdfPCell( hasBorder ), "Organization unit identifier", Element.ALIGN_RIGHT );
    addCell_WithTextField( table, rectangle, writer, PdfDataEntryFormUtil.getPdfPCell( hasBorder ), PdfDataEntryFormUtil.LABELCODE_ORGID,
        PdfFieldCell.TYPE_TEXT_ORGUNIT );

    String[] periodsTitle = getPeriodTitles( periods, format );
    String[] periodsValue = getPeriodValues( periods );

    addCell_Text( table, PdfDataEntryFormUtil.getPdfPCell( hasBorder ), "Period", Element.ALIGN_RIGHT );
    addCell_WithDropDownListField( table, rectangle, writer, PdfDataEntryFormUtil.getPdfPCell( hasBorder ), PdfDataEntryFormUtil.LABELCODE_PERIODID, periodsTitle, periodsValue );

    // Add to the main table
    PdfPCell cell_withInnerTable = new PdfPCell( table );
    // cell_withInnerTable.setPadding(0);
    cell_withInnerTable.setBorder( Rectangle.NO_BORDER );

    cell_withInnerTable.setHorizontalAlignment( Element.ALIGN_LEFT );

    mainTable.addCell( cell_withInnerTable );
}
 
Example 2
Source File: PdfJavaInformationsReport.java    From javamelody with Apache License 2.0 5 votes vote down vote up
private static PdfPTable createJavaInformationsTable() throws DocumentException {
	final PdfPTable table = new PdfPTable(2);
	table.setHorizontalAlignment(Element.ALIGN_LEFT);
	table.setWidthPercentage(100);
	table.setWidths(new int[] { 2, 8 });
	table.getDefaultCell().setBorder(0);
	return 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: 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 5
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 6
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 7
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();
}
 
Example 8
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();
}
 
Example 9
Source File: DefaultPdfDataEntryFormService.java    From dhis2-core with BSD 3-Clause "New" or "Revised" License 4 votes vote down vote up
private void insertTable_DataSetSections( PdfPTable mainTable, PdfWriter writer, Rectangle rectangle,
    Collection<DataElement> dataElements, String sectionName )
    throws IOException, DocumentException
{
    boolean hasBorder = true;

    // Add Section Name and Section Spacing
    insertTable_TextRow( writer, mainTable, TEXT_BLANK );

    if ( sectionName != null && !sectionName.isEmpty() )
    {
        insertTable_TextRow( writer, mainTable, sectionName,
            pdfFormFontSettings.getFont( PdfFormFontSettings.FONTTYPE_SECTIONHEADER ) );
    }

    // Create A Table To Add For Each Section
    PdfPTable table = new PdfPTable( 2 );

    table.setWidths( new int[]{ 2, 1 } );
    table.setWidthPercentage( 100.0f );
    table.setHorizontalAlignment( Element.ALIGN_LEFT );


    // For each DataElement and Category Combo of the dataElement, create
    // row.
    for ( DataElement dataElement : dataElements )
    {
        for ( CategoryOptionCombo categoryOptionCombo : dataElement.getSortedCategoryOptionCombos() )
        {
            String categoryOptionComboDisplayName = "";

            // Hide Default category option combo name
            if ( !categoryOptionCombo.isDefault() )
            {
                categoryOptionComboDisplayName = categoryOptionCombo.getDisplayName();
            }

            addCell_Text( table, PdfDataEntryFormUtil.getPdfPCell( hasBorder ), dataElement.getFormNameFallback() + " " +
                categoryOptionComboDisplayName, Element.ALIGN_RIGHT );

            String strFieldLabel = PdfDataEntryFormUtil.LABELCODE_DATAENTRYTEXTFIELD + dataElement.getUid() + "_"
                + categoryOptionCombo.getUid();

            ValueType valueType = dataElement.getValueType();

            // Yes Only case - render as check-box
            if ( ValueType.TRUE_ONLY == valueType )
            {
                addCell_WithCheckBox( table, writer, PdfDataEntryFormUtil.getPdfPCell( hasBorder ), strFieldLabel );
            }
            else if ( ValueType.BOOLEAN == valueType )
            {
                // Create Yes - true, No - false, Select..
                String[] optionList = new String[]{ "[No Value]", "Yes", "No" };
                String[] valueList = new String[]{ "", "true", "false" };

                // addCell_WithRadioButton(table, writer, strFieldLabel);
                addCell_WithDropDownListField( table, rectangle, writer, PdfDataEntryFormUtil.getPdfPCell( hasBorder ), strFieldLabel, optionList, valueList );
            }
            else if ( valueType.isNumeric() )
            {
                Rectangle rectNum = new Rectangle( TEXTBOXWIDTH_NUMBERTYPE, PdfDataEntryFormUtil.CONTENT_HEIGHT_DEFAULT );

                addCell_WithTextField( table, rectNum, writer, PdfDataEntryFormUtil.getPdfPCell( hasBorder ), strFieldLabel, PdfFieldCell.TYPE_TEXT_NUMBER );
            }
            else
            {
                addCell_WithTextField( table, rectangle, writer, PdfDataEntryFormUtil.getPdfPCell( hasBorder ), strFieldLabel );
            }
        }
    }

    PdfPCell cell_withInnerTable = new PdfPCell( table );
    cell_withInnerTable.setBorder( Rectangle.NO_BORDER );

    mainTable.addCell( cell_withInnerTable );
}