Java Code Examples for com.lowagie.text.pdf.PdfPCell#setBorder()

The following examples show how to use com.lowagie.text.pdf.PdfPCell#setBorder() . 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 6 votes vote down vote up
private void insertTable_HiddenValue( PdfPTable mainTable, Rectangle rectangle, PdfWriter writer, String fieldName,
    String value )
    throws IOException, DocumentException
{
    boolean hasBorder = false;

    // Add Organization ID/Period textfield
    // Create A table to add for each group AT HERE
    PdfPTable table = new PdfPTable( 1 ); // Code 1

    addCell_WithTextField( table, rectangle, writer, PdfDataEntryFormUtil.getPdfPCell( hasBorder ), fieldName, value );

    // Add to the main table
    PdfPCell cell_withInnerTable = new PdfPCell( table );
    // cell_withInnerTable.setPadding(0);
    cell_withInnerTable.setBorder( Rectangle.NO_BORDER );
    mainTable.addCell( cell_withInnerTable );
}
 
Example 2
Source File: DefaultPdfDataEntryFormService.java    From dhis2-core with BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
private void insertTable_TextRow( PdfWriter writer, PdfPTable mainTable, String text, Font font )
{
    boolean hasBorder = false;

    // Add Organization ID/Period textfield
    // Create A table to add for each group AT HERE
    PdfPTable table = new PdfPTable( 1 );
    table.setHorizontalAlignment( Element.ALIGN_LEFT );

    addCell_Text( table, PdfDataEntryFormUtil.getPdfPCell( hasBorder ), text, Element.ALIGN_LEFT, font );

    // Add to the main table
    PdfPCell cell_withInnerTable = new PdfPCell( table );

    cell_withInnerTable.setBorder( Rectangle.NO_BORDER );

    mainTable.addCell( cell_withInnerTable );
}
 
Example 3
Source File: PurchaseOrderQuotePdf.java    From kfs with GNU Affero General Public License v3.0 6 votes vote down vote up
/**
 * A helper method to create a PdfPCell. We can specify the content, font, horizontal alignment, border (borderless, no
 * bottom border, no right border, no top border, etc.
 *
 * @param content              The text content to be displayed in the cell.
 * @param borderless           boolean true if the cell should be borderless.
 * @param noBottom             boolean true if the cell should have borderWidthBottom = 0.
 * @param noRight              boolean true if the cell should have borderWidthRight = 0.
 * @param noTop                boolean true if the cell should have borderWidthTop = 0.
 * @param horizontalAlignment  The desired horizontal alignment for the cell.
 * @param font                 The font type to be used in the cell.
 * @return                     An instance of PdfPCell which content and attributes were set by the input parameters.
 */
private PdfPCell createCell(String content, boolean borderless, boolean noBottom, boolean noRight, boolean noTop, int horizontalAlignment, Font font) {
    PdfPCell tableCell = new PdfPCell(new Paragraph(content, font));
    if (borderless) {
        tableCell.setBorder(0);
    }
    if (noBottom) {
        tableCell.setBorderWidthBottom(0);
    }
    if (noTop) {
        tableCell.setBorderWidthTop(0);
    }
    if (noRight) {
        tableCell.setBorderWidthRight(0);
    }
    tableCell.setHorizontalAlignment(horizontalAlignment);
    return tableCell;
}
 
Example 4
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 5
Source File: PDFUtils.java    From dhis2-core with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
/**
 * Creates a cell.
 *
 * @param text            The text to include in the cell.
 * @param colspan         The column span of the cell.
 * @param font            The font of the cell text.
 * @param horizontalAlign The vertical alignment of the text in the cell.
 * @return A PdfCell.
 */
public static PdfPCell getCell( String text, int colspan, Font font, int horizontalAlign )
{
    Paragraph paragraph = new Paragraph( text, font );

    PdfPCell cell = new PdfPCell( paragraph );

    cell.setColspan( colspan );
    cell.setBorder( 0 );
    cell.setMinimumHeight( 15 );
    cell.setHorizontalAlignment( horizontalAlign );

    return cell;
}
 
Example 6
Source File: PDFUtils.java    From dhis2-core with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
/**
 * Creates an empty cell.
 *
 * @param colspan The column span of the cell.
 * @param height  The height of the column.
 * @return A PdfCell.
 */
public static PdfPCell getEmptyCell( int colSpan, int height )
{
    PdfPCell cell = new PdfPCell();

    cell.setColspan( colSpan );
    cell.setBorder( 0 );
    cell.setMinimumHeight( height );

    return cell;
}
 
Example 7
Source File: PdfMBeansReport.java    From javamelody with Apache License 2.0 5 votes vote down vote up
private static PdfPTable createAttributesTable() {
	final PdfPTable table = new PdfPTable(3);
	table.setWidthPercentage(100);
	final PdfPCell defaultCell = table.getDefaultCell();
	defaultCell.setPaddingLeft(2);
	defaultCell.setPaddingRight(2);
	defaultCell.setVerticalAlignment(Element.ALIGN_TOP);
	defaultCell.setBorder(0);
	return table;
}
 
Example 8
Source File: PdfWebTable.java    From unitime with Apache License 2.0 5 votes vote down vote up
private float addText(PdfPCell cell, String text, boolean bold, boolean italic, boolean underline, Color color,
		boolean borderTop, boolean borderBottom, boolean borderLeft, boolean borderRight, Color borderColor, Color bgColor ) {
	
   	cell.setBorderWidth(1);
   	
	if (borderTop) {
		cell.setBorder(PdfPCell.TOP);
		if (borderColor==null)
			cell.setBorderColorTop(Color.BLACK);
		else
			cell.setBorderColorTop(borderColor);
	}
	
	if (borderBottom) {
		cell.setBorder(PdfPCell.BOTTOM);
		if (borderColor==null)
			cell.setBorderColorBottom(Color.BLACK);
		else
			cell.setBorderColorBottom(borderColor);
	}

	if (borderLeft) {
		cell.setBorder(PdfPCell.LEFT);
		if (borderColor==null)
			cell.setBorderColorLeft(Color.BLACK);
		else
			cell.setBorderColorLeft(borderColor);
	}

	if (borderRight) {
		cell.setBorder(PdfPCell.RIGHT);
		if (borderColor==null)
			cell.setBorderColorRight(Color.BLACK);
		else
			cell.setBorderColorRight(borderColor);
	}

	return addText(cell, text, bold, italic, underline, color, bgColor);
}
 
Example 9
Source File: PdfWebTable.java    From unitime with Apache License 2.0 5 votes vote down vote up
private float addText(PdfPCell cell, String text, boolean bold, boolean italic, boolean underline, Color color,
		boolean border, Color borderColor, Color bgColor) {
	
	if (border) {
    	cell.setBorderWidth(1);
		cell.setBorder(PdfPCell.RIGHT | PdfPCell.LEFT | PdfPCell.TOP | PdfPCell.BOTTOM );
		if (borderColor==null)
			cell.setBorderColor(Color.BLACK);
		else
			cell.setBorderColor(borderColor);
	}
	
	return addText(cell, text, bold, italic, underline, color, bgColor);
}
 
Example 10
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 11
Source File: SimpleCell.java    From gcs with Mozilla Public License 2.0 4 votes vote down vote up
/**
 * Creates a PdfPCell with these attributes.
 * 
 * @param rowAttributes
 * @return a PdfPCell based on these attributes.
 */
public PdfPCell createPdfPCell(SimpleCell rowAttributes) {
	PdfPCell cell = new PdfPCell();
	cell.setBorder(NO_BORDER);
	SimpleCell tmp = new SimpleCell(CELL);
	tmp.setSpacing_left(spacing_left);
	tmp.setSpacing_right(spacing_right);
	tmp.setSpacing_top(spacing_top);
	tmp.setSpacing_bottom(spacing_bottom);
	tmp.cloneNonPositionParameters(rowAttributes);
	tmp.softCloneNonPositionParameters(this);
	cell.setCellEvent(tmp);
	cell.setHorizontalAlignment(rowAttributes.horizontalAlignment);
	cell.setVerticalAlignment(rowAttributes.verticalAlignment);
	cell.setUseAscender(rowAttributes.useAscender);
	cell.setUseBorderPadding(rowAttributes.useBorderPadding);
	cell.setUseDescender(rowAttributes.useDescender);
	cell.setColspan(colspan);
	if (horizontalAlignment != Element.ALIGN_UNDEFINED) {
		cell.setHorizontalAlignment(horizontalAlignment);
	}
	if (verticalAlignment != Element.ALIGN_UNDEFINED) {
		cell.setVerticalAlignment(verticalAlignment);
	}
	if (useAscender) {
		cell.setUseAscender(useAscender);
	}
	if (useBorderPadding) {
		cell.setUseBorderPadding(useBorderPadding);
	}
	if (useDescender) {
		cell.setUseDescender(useDescender);
	}
	float p;
	float sp_left = spacing_left;
	if (Float.isNaN(sp_left)) {
		sp_left = 0f;
	}
	float sp_right = spacing_right;
	if (Float.isNaN(sp_right)) {
		sp_right = 0f;
	}
	float sp_top = spacing_top;
	if (Float.isNaN(sp_top)) {
		sp_top = 0f;
	}
	float sp_bottom = spacing_bottom;
	if (Float.isNaN(sp_bottom)) {
		sp_bottom = 0f;
	}
	p = padding_left;
	if (Float.isNaN(p)) {
		p = 0f;
	}
	cell.setPaddingLeft(p + sp_left);
	p = padding_right;
	if (Float.isNaN(p)) {
		p = 0f;
	}
	cell.setPaddingRight(p + sp_right);
	p = padding_top;
	if (Float.isNaN(p)) {
		p = 0f;
	}
	cell.setPaddingTop(p + sp_top);
	p = padding_bottom;
	if (Float.isNaN(p)) {
		p = 0f;
	}
	cell.setPaddingBottom(p + sp_bottom);
	Element element;
	for (Iterator i = content.iterator(); i.hasNext();) {
		element = (Element) i.next();
		cell.addElement(element);
	}
	return cell;
}
 
Example 12
Source File: SimpleCell.java    From itext2 with GNU Lesser General Public License v3.0 4 votes vote down vote up
/**
 * Creates a PdfPCell with these attributes.
 * @param rowAttributes
 * @return a PdfPCell based on these attributes.
 */
public PdfPCell createPdfPCell(SimpleCell rowAttributes) {
	PdfPCell cell = new PdfPCell();
	cell.setBorder(NO_BORDER);
	SimpleCell tmp = new SimpleCell(CELL);
	tmp.setSpacing_left(spacing_left);
	tmp.setSpacing_right(spacing_right);
	tmp.setSpacing_top(spacing_top);
	tmp.setSpacing_bottom(spacing_bottom);
	tmp.cloneNonPositionParameters(rowAttributes);
	tmp.softCloneNonPositionParameters(this);
	cell.setCellEvent(tmp);
	cell.setHorizontalAlignment(rowAttributes.horizontalAlignment);
	cell.setVerticalAlignment(rowAttributes.verticalAlignment);
	cell.setUseAscender(rowAttributes.useAscender);
	cell.setUseBorderPadding(rowAttributes.useBorderPadding);
	cell.setUseDescender(rowAttributes.useDescender);
	cell.setColspan(colspan);
	if (horizontalAlignment != Element.ALIGN_UNDEFINED)
		cell.setHorizontalAlignment(horizontalAlignment);
	if (verticalAlignment != Element.ALIGN_UNDEFINED)
		cell.setVerticalAlignment(verticalAlignment);
	if (useAscender)
		cell.setUseAscender(useAscender);
	if (useBorderPadding)
		cell.setUseBorderPadding(useBorderPadding);
	if (useDescender)
		cell.setUseDescender(useDescender);
	float p;
	float sp_left = spacing_left;
	if (Float.isNaN(sp_left)) sp_left = 0f;
	float sp_right = spacing_right;
	if (Float.isNaN(sp_right)) sp_right = 0f;
	float sp_top = spacing_top;
	if (Float.isNaN(sp_top)) sp_top = 0f;
	float sp_bottom = spacing_bottom;
	if (Float.isNaN(sp_bottom)) sp_bottom = 0f;
	p = padding_left;
	if (Float.isNaN(p)) p = 0f; 
	cell.setPaddingLeft(p + sp_left);
	p = padding_right;
	if (Float.isNaN(p)) p = 0f; 
	cell.setPaddingRight(p + sp_right);
	p = padding_top;
	if (Float.isNaN(p)) p = 0f; 
	cell.setPaddingTop(p + sp_top);
	p = padding_bottom;
	if (Float.isNaN(p)) p = 0f; 
	cell.setPaddingBottom(p + sp_bottom);
	Element element;
	for (Iterator i = content.iterator(); i.hasNext(); ) {
		element = (Element)i.next();
		cell.addElement(element);
	}
	return cell;
}
 
Example 13
Source File: ExampleEAN128Test.java    From itext2 with GNU Lesser General Public License v3.0 4 votes vote down vote up
/**
 * Example Barcode EAN128.
 */
@Test
public void main() throws Exception {

	// step 1
	Document document = new Document();
	// step 2
	PdfWriter writer = PdfWriter.getInstance(document, PdfTestBase.getOutputStream("ean128.pdf"));
	// step 3
	document.open();
	// step 4
	PdfContentByte cb = writer.getDirectContent();
	PdfPTable pageTot = new PdfPTable(1);
	pageTot.getDefaultCell().setPadding(0f);
	pageTot.getDefaultCell().setBorder(Rectangle.NO_BORDER);
	pageTot.getDefaultCell().setHorizontalAlignment(Element.ALIGN_LEFT);
	pageTot.setWidthPercentage(100f);
	// Data for the barcode : it is composed of 3 blocks whith AI 402, 90
	// and 421
	// The blocks whith the type 402 and 90 are of variable size so you must
	// put a FNC1
	// to delimitate the block
	String code402 = "24132399420058289" + Barcode128.FNC1;
	String code90 = "3700000050" + Barcode128.FNC1;
	String code421 = "422356";
	String data = code402 + code90 + code421;

	PdfPTable cell = new PdfPTable(1);
	cell.getDefaultCell().setBorder(Rectangle.NO_BORDER);
	cell.getDefaultCell().setPadding(0f);

	PdfPCell info = new PdfPCell(new Phrase("Barcode EAN 128"));
	info.setBorder(Rectangle.NO_BORDER);
	pageTot.addCell(info);

	Barcode128 shipBarCode = new Barcode128();
	shipBarCode.setX(0.75f);
	shipBarCode.setN(1.5f);
	shipBarCode.setChecksumText(true);
	shipBarCode.setGenerateChecksum(true);
	shipBarCode.setSize(10f);
	shipBarCode.setTextAlignment(Element.ALIGN_CENTER);
	shipBarCode.setBaseline(10f);
	shipBarCode.setCode(data);
	shipBarCode.setBarHeight(50f);

	Image imgShipBarCode = shipBarCode.createImageWithBarcode(cb, Color.black, Color.blue);
	PdfPCell shipment = new PdfPCell(new Phrase(new Chunk(imgShipBarCode, 0, 0)));
	shipment.setFixedHeight(shipBarCode.getBarcodeSize().getHeight() + 16f);
	shipment.setPaddingTop(5f);
	shipment.setPaddingBottom(10f);
	shipment.setBorder(Rectangle.BOX);
	shipment.setVerticalAlignment(Element.ALIGN_TOP);
	shipment.setHorizontalAlignment(Element.ALIGN_CENTER);
	cell.addCell(shipment);

	pageTot.addCell(cell);

	document.add(pageTot);

	// step 5
	document.close();
}
 
Example 14
Source File: PdfDataEntryFormUtil.java    From dhis2-core with BSD 3-Clause "New" or "Revised" License 4 votes vote down vote up
public static PdfPCell getPdfPCell( float minHeight, int cellContentType, boolean hasBorder )
{
    PdfPCell cell = new PdfPCell();
    cell.setMinimumHeight( minHeight );
    
    if( hasBorder )
    {
        cell.setBorderWidth( 0.1f );
        cell.setBorderColor( COLOR_CELLBORDER );            
    }
    else
    {
        cell.setBorder( Rectangle.NO_BORDER );
    }
    
    cell.setPadding( 2.0f );

    switch ( cellContentType )
    {
        case CELL_COLUMN_TYPE_ENTRYFIELD:
            cell.setHorizontalAlignment( Element.ALIGN_CENTER );
            cell.setVerticalAlignment( Element.ALIGN_MIDDLE );

            break;

        case CELL_COLUMN_TYPE_HEADER:
            cell.setHorizontalAlignment( Element.ALIGN_CENTER );
            cell.setVerticalAlignment( Element.ALIGN_MIDDLE );

            break;

        case CELL_COLUMN_TYPE_LABEL:
            cell.setHorizontalAlignment( Element.ALIGN_RIGHT );
            cell.setVerticalAlignment( Element.ALIGN_TOP );

        default:
            break;
    }

    return cell;
}
 
Example 15
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 );
}