com.lowagie.text.Cell Java Examples

The following examples show how to use com.lowagie.text.Cell. 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: PatchRtfRow.java    From pentaho-reporting with GNU Lesser General Public License v2.1 6 votes vote down vote up
/**
 * Imports a Row and copies all settings
 *
 * @param row
 *          The Row to import
 */
private void importRow( Row row ) {
  this.cells = new ArrayList<PatchRtfCell>();
  this.width =
      this.document.getDocumentHeader().getPageSetting().getPageWidth()
          - this.document.getDocumentHeader().getPageSetting().getMarginLeft()
          - this.document.getDocumentHeader().getPageSetting().getMarginRight();
  this.width = (int) ( this.width * this.parentTable.getTableWidthPercent() / 100 );

  int cellRight = 0;
  int cellWidth = 0;
  for ( int i = 0; i < row.getColumns(); i++ ) {
    cellWidth = (int) ( this.width * this.parentTable.getProportionalWidths()[i] / 100 );
    cellRight = cellRight + cellWidth;

    Cell cell = (Cell) row.getCell( i );
    PatchRtfCell rtfCell = new PatchRtfCell( this.document, this, cell );
    rtfCell.setCellRight( cellRight );
    rtfCell.setCellWidth( cellWidth );
    this.cells.add( rtfCell );
  }
}
 
Example #2
Source File: RTFPrinter.java    From pentaho-reporting with GNU Lesser General Public License v2.1 6 votes vote down vote up
private void computeCellStyle( final RenderBox content, final Cell cell ) {
  final ElementAlignment verticalAlign = content.getNodeLayoutProperties().getVerticalAlignment();
  if ( ElementAlignment.BOTTOM.equals( verticalAlign ) ) {
    cell.setVerticalAlignment( Element.ALIGN_BOTTOM );
  } else if ( ElementAlignment.MIDDLE.equals( verticalAlign ) ) {
    cell.setVerticalAlignment( Element.ALIGN_MIDDLE );
  } else {
    cell.setVerticalAlignment( Element.ALIGN_TOP );
  }

  final ElementAlignment textAlign =
      (ElementAlignment) content.getStyleSheet().getStyleProperty( ElementStyleKeys.ALIGNMENT );
  if ( ElementAlignment.RIGHT.equals( textAlign ) ) {
    cell.setHorizontalAlignment( Element.ALIGN_RIGHT );
  } else if ( ElementAlignment.JUSTIFY.equals( textAlign ) ) {
    cell.setHorizontalAlignment( Element.ALIGN_JUSTIFIED );
  } else if ( ElementAlignment.CENTER.equals( textAlign ) ) {
    cell.setHorizontalAlignment( Element.ALIGN_CENTER );
  } else {
    cell.setHorizontalAlignment( Element.ALIGN_LEFT );
  }
}
 
Example #3
Source File: RtfRow.java    From itext2 with GNU Lesser General Public License v3.0 6 votes vote down vote up
/**
 * Imports a Row and copies all settings
 * 
 * @param row The Row to import
 */
private void importRow(Row row) {
    this.cells = new ArrayList();
    this.width = this.document.getDocumentHeader().getPageSetting().getPageWidth() - this.document.getDocumentHeader().getPageSetting().getMarginLeft() - this.document.getDocumentHeader().getPageSetting().getMarginRight();
    this.width = (int) (this.width * this.parentTable.getTableWidthPercent() / 100);
    
    int cellRight = 0;
    int cellWidth = 0;
    for(int i = 0; i < row.getColumns(); i++) {
        cellWidth = (int) (this.width * this.parentTable.getProportionalWidths()[i] / 100);
        cellRight = cellRight + cellWidth;
        
        Cell cell = (Cell) row.getCell(i);
        RtfCell rtfCell = new RtfCell(this.document, this, cell);
        rtfCell.setCellRight(cellRight);
        rtfCell.setCellWidth(cellWidth);
        this.cells.add(rtfCell);
    }
}
 
Example #4
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 #5
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 #6
Source File: ElementFactory.java    From gcs with Mozilla Public License 2.0 5 votes vote down vote up
/**
 * Creates a Cell object based on a list of properties.
 * 
 * @param attributes
 * @return a Cell
 */
public static Cell getCell(Properties attributes) {
	Cell cell = new Cell();
	String value;

	cell.setHorizontalAlignment(attributes.getProperty(ElementTags.HORIZONTALALIGN));
	cell.setVerticalAlignment(attributes.getProperty(ElementTags.VERTICALALIGN));

	value = attributes.getProperty(ElementTags.WIDTH);
	if (value != null) {
		cell.setWidth(value);
	}
	value = attributes.getProperty(ElementTags.COLSPAN);
	if (value != null) {
		cell.setColspan(Integer.parseInt(value));
	}
	value = attributes.getProperty(ElementTags.ROWSPAN);
	if (value != null) {
		cell.setRowspan(Integer.parseInt(value));
	}
	value = attributes.getProperty(ElementTags.LEADING);
	if (value != null) {
		cell.setLeading(Float.parseFloat(value + "f"));
	}
	cell.setHeader(Utilities.checkTrueOrFalse(attributes, ElementTags.HEADER));
	if (Utilities.checkTrueOrFalse(attributes, ElementTags.NOWRAP)) {
		cell.setMaxLines(1);
	}
	setRectangleProperties(cell, attributes);
	return cell;
}
 
Example #7
Source File: RTFPrinter.java    From pentaho-reporting with GNU Lesser General Public License v2.1 5 votes vote down vote up
private void updateCellStyle( final Cell cell, final CellBackground background ) {

    final Color backgroundColor = background.getBackgroundColor();
    if ( backgroundColor != null ) {
      cell.setBackgroundColor( backgroundColor );
    }
    final BorderEdge top = background.getTop();
    if ( BorderEdge.EMPTY.equals( top ) == false ) {
      cell.setBorderColorTop( top.getColor() );
      cell.setBorderWidthTop( (float) StrictGeomUtility.toExternalValue( top.getWidth() ) );
    }

    final BorderEdge left = background.getLeft();
    if ( BorderEdge.EMPTY.equals( left ) == false ) {
      cell.setBorderColorLeft( left.getColor() );
      cell.setBorderWidthLeft( (float) StrictGeomUtility.toExternalValue( left.getWidth() ) );
    }

    final BorderEdge bottom = background.getBottom();
    if ( BorderEdge.EMPTY.equals( bottom ) == false ) {
      cell.setBorderColorBottom( bottom.getColor() );
      cell.setBorderWidthBottom( (float) StrictGeomUtility.toExternalValue( bottom.getWidth() ) );
    }

    final BorderEdge right = background.getRight();
    if ( BorderEdge.EMPTY.equals( right ) == false ) {
      cell.setBorderColorRight( right.getColor() );
      cell.setBorderWidthRight( (float) StrictGeomUtility.toExternalValue( right.getWidth() ) );
    }
  }
 
Example #8
Source File: SpreadsheetDataFileWriterPdf.java    From sakai with Educational Community License v2.0 5 votes vote down vote up
private Cell getHeaderCell(String headerTxt) throws BadElementException{
	//set header cells:								
	Cell c = new Cell(new Chunk(headerTxt, getBoldFont()));
	c.setHeader(true);
	c.setBackgroundColor(Color.LIGHT_GRAY);
	
	return c;
}
 
Example #9
Source File: Coversheet.java    From kfs with GNU Affero General Public License v3.0 5 votes vote down vote up
/**
 * Helper method to create a Header Cell from text
 *
 * @returns {@link Cell} with the header flag set
 */
protected Cell getHeaderCell(final String text) throws BadElementException {
    final Font headerFont = FontFactory.getFont(FontFactory.HELVETICA, 12, Font.BOLD);
    final Cell retval = new Cell(new Chunk(text, headerFont));
    retval.setBorder(NO_BORDER);
    retval.setHeader(true);
    return retval;
}
 
Example #10
Source File: Coversheet.java    From kfs with GNU Affero General Public License v3.0 5 votes vote down vote up
/**
 * Helper method to create a Header Cell from text
 *
 * @returns {@link Cell} with the header flag set
 */
protected Cell getBorderlessCell(final String text) throws BadElementException {
    final Font normalFont = FontFactory.getFont(FontFactory.HELVETICA, 12, Font.NORMAL);
    final Cell retval = new Cell(new Chunk(text, normalFont));
    retval.setBorder(NO_BORDER);
    return retval;
}
 
Example #11
Source File: Coversheet.java    From kfs with GNU Affero General Public License v3.0 5 votes vote down vote up
/**
 * Information about the traveler described in the trip for the {@link TravelReimbursementDocument}
 *
 */
protected Cell getTravelerInfo() throws BadElementException {
    final StringBuilder strBuilder = new StringBuilder();
    strBuilder.append(getTravelerName()).append("\n")
        .append(getTravelerPrincipalName()).append("\n")
        .append(getTravelerPhone()).append("\n")
        .append(getTravelerEmail()).append("\n");
    final Cell retval = getBorderlessCell(strBuilder.toString());
    return retval;
}
 
Example #12
Source File: Coversheet.java    From kfs with GNU Affero General Public License v3.0 5 votes vote down vote up
/**
 * Information about the person that initiated the {@link TravelReimbursementDocument}
 */
protected Cell getInitiatorInfo() throws BadElementException {
    final StringBuilder strBuilder = new StringBuilder();
    strBuilder.append(getInitiatorName()).append("\n")
        .append(getInitiatorPrincipalName()).append("\n")
        .append(getInitiatorPhone()).append("\n")
        .append(getInitiatorEmail()).append("\n");
    final Cell retval = getBorderlessCell(strBuilder.toString());
    return retval;
}
 
Example #13
Source File: SpreadsheetDataFileWriterPdf.java    From sakai with Educational Community License v2.0 5 votes vote down vote up
private Cell getHeaderCell(String headerTxt) throws BadElementException{
	//set header cells:								
	Cell c = new Cell(new Chunk(headerTxt, getBoldFont()));
	c.setHeader(true);
	c.setBackgroundColor(Color.LIGHT_GRAY);
	
	return c;
}
 
Example #14
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 #15
Source File: ElementFactory.java    From itext2 with GNU Lesser General Public License v3.0 5 votes vote down vote up
/**
 * Creates a Cell object based on a list of properties.
 * @param attributes
 * @return a Cell
 */
public static Cell getCell(Properties attributes) {
	Cell cell = new Cell();
	String value;

	cell.setHorizontalAlignment(attributes
			.getProperty(ElementTags.HORIZONTALALIGN));
	cell.setVerticalAlignment(attributes
			.getProperty(ElementTags.VERTICALALIGN));

	value = attributes.getProperty(ElementTags.WIDTH);
	if (value != null) {
		cell.setWidth(value);
	}
	value = attributes.getProperty(ElementTags.COLSPAN);
	if (value != null) {
		cell.setColspan(Integer.parseInt(value));
	}
	value = attributes.getProperty(ElementTags.ROWSPAN);
	if (value != null) {
		cell.setRowspan(Integer.parseInt(value));
	}
	value = attributes.getProperty(ElementTags.LEADING);
	if (value != null) {
		cell.setLeading(Float.parseFloat(value + "f"));
	}
	cell.setHeader(Utilities.checkTrueOrFalse(attributes,
			ElementTags.HEADER));
	if (Utilities.checkTrueOrFalse(attributes, ElementTags.NOWRAP)) {
		cell.setMaxLines(1);
	}
	setRectangleProperties(cell, attributes);
	return cell;
}
 
Example #16
Source File: SAXiTextHandler.java    From itext2 with GNU Lesser General Public License v3.0 5 votes vote down vote up
protected void addImage(Image img) throws EmptyStackException {
    // if there is an element on the stack...
    Object current = stack.pop();
    // ...and it's a Chapter or a Section, the Image can be
    // added directly
    if (current instanceof Chapter
            || current instanceof Section
            || current instanceof Cell) {
        ((TextElementArray) current).add(img);
        stack.push(current);
        return;
    }
    // ...if not, we need to to a lot of stuff
    else {
        Stack newStack = new Stack();
        while (!(current instanceof Chapter
                || current instanceof Section || current instanceof Cell)) {
            newStack.push(current);
            if (current instanceof Anchor) {
                img.setAnnotation(new Annotation(0, 0, 0,
                        0, ((Anchor) current).getReference()));
            }
            current = stack.pop();
        }
        ((TextElementArray) current).add(img);
        stack.push(current);
        while (!newStack.empty()) {
            stack.push(newStack.pop());
        }
        return;
    }
}
 
Example #17
Source File: SAXiTextHandler.java    From gcs with Mozilla Public License 2.0 5 votes vote down vote up
protected void addImage(Image img) throws EmptyStackException {
	// if there is an element on the stack...
	Object current = stack.pop();
	// ...and it's a Chapter or a Section, the Image can be
	// added directly
	if (current instanceof Chapter || current instanceof Section || current instanceof Cell) {
		((TextElementArray) current).add(img);
		stack.push(current);
		return;
	}
	// ...if not, we need to to a lot of stuff
	else {
		Stack newStack = new Stack();
		while (!(current instanceof Chapter || current instanceof Section || current instanceof Cell)) {
			newStack.push(current);
			if (current instanceof Anchor) {
				img.setAnnotation(new Annotation(0, 0, 0, 0, ((Anchor) current).getReference()));
			}
			current = stack.pop();
		}
		((TextElementArray) current).add(img);
		stack.push(current);
		while (!newStack.empty()) {
			stack.push(newStack.pop());
		}
		return;
	}
}
 
Example #18
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 #19
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 #20
Source File: RtfCell.java    From itext2 with GNU Lesser General Public License v3.0 2 votes vote down vote up
/**
 * Constructs a RtfCell based on a Cell.
 * 
 * @param doc The RtfDocument this RtfCell belongs to
 * @param row The RtfRow this RtfCell lies in
 * @param cell The Cell to base this RtfCell on
 */
protected RtfCell(RtfDocument doc, RtfRow row, Cell cell) {
    this.document = doc;
    this.parentRow = row;
    importCell(cell);
}
 
Example #21
Source File: PatchRtfCell.java    From pentaho-reporting with GNU Lesser General Public License v2.1 2 votes vote down vote up
/**
 * Constructs a PatchRtfCell based on a Cell.
 *
 * @param doc
 *          The RtfDocument this PatchRtfCell belongs to
 * @param row
 *          The PatchRtfRow this PatchRtfCell lies in
 * @param cell
 *          The Cell to base this PatchRtfCell on
 */
protected PatchRtfCell( RtfDocument doc, PatchRtfRow row, Cell cell ) {
  this.document = doc;
  this.parentRow = row;
  importCell( cell );
}