com.lowagie.text.SimpleTable Java Examples

The following examples show how to use com.lowagie.text.SimpleTable. 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: TablePdfPTableTest.java    From itext2 with GNU Lesser General Public License v3.0 4 votes vote down vote up
/**
 * Example that is used to test the TableAttributes class.
 * 
 * @param args
 *            no arguments needed
 */
@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("tableattributes.pdf"));
	RtfWriter2.getInstance(document, PdfTestBase.getOutputStream("tableattributes.rtf"));
	// open the document
	document.open();
	// add content
	SimpleTable table = new SimpleTable();
	table.setCellpadding(5);
	table.setCellspacing(8);
	SimpleCell row = new SimpleCell(SimpleCell.ROW);
	row.setBackgroundColor(Color.yellow);
	SimpleCell cell = new SimpleCell(SimpleCell.CELL);
	cell.setWidth(100f);
	cell.add(new Paragraph("rownumber"));
	row.add(cell);
	cell = new SimpleCell(SimpleCell.CELL);
	cell.setWidth(50f);
	cell.add(new Paragraph("A"));
	row.add(cell);
	cell = new SimpleCell(SimpleCell.CELL);
	cell.setWidth(50f);
	cell.add(new Paragraph("B"));
	row.add(cell);
	cell = new SimpleCell(SimpleCell.CELL);
	cell.setWidth(50f);
	cell.add(new Paragraph("C"));
	row.add(cell);
	table.addElement(row);
	for (int i = 0; i < 100; i++) {
		row = new SimpleCell(SimpleCell.ROW);
		switch (i % 3) {
		case 0:
			row.setBackgroundColor(Color.red);
			break;
		case 1:
			row.setBackgroundColor(Color.green);
			break;
		case 2:
			row.setBackgroundColor(Color.blue);
			break;
		}
		if (i % 2 == 1) {
			row.setBorderWidth(3f);
		}
		cell = new SimpleCell(SimpleCell.CELL);
		cell.add(new Paragraph("Row " + (i + 1)));
		cell.setSpacing_left(20f);
		row.add(cell);
		if (i % 5 == 4) {
			cell = new SimpleCell(SimpleCell.CELL);
			cell.setColspan(3);
			cell.setBorderColor(Color.orange);
			cell.setBorderWidth(5f);
			cell.add(new Paragraph("Hello!"));
			cell.setHorizontalAlignment(Element.ALIGN_CENTER);
			row.add(cell);
		} else {
			cell = new SimpleCell(SimpleCell.CELL);
			cell.add(new Paragraph("A"));
			row.add(cell);
			cell = new SimpleCell(SimpleCell.CELL);
			cell.add(new Paragraph("B"));
			cell.setBackgroundColor(Color.gray);
			row.add(cell);
			cell = new SimpleCell(SimpleCell.CELL);
			cell.add(new Paragraph("C"));
			row.add(cell);
		}
		table.addElement(row);
	}
	document.add(table);

	// we close the document
	document.close();
}
 
Example #2
Source File: PatchRtfDocument.java    From pentaho-reporting with GNU Lesser General Public License v2.1 4 votes vote down vote up
public RtfBasicElement[] mapElement( Element element ) throws DocumentException {
  ArrayList<RtfBasicElement> rtfElements = new ArrayList<RtfBasicElement>();
  if ( element instanceof RtfBasicElement ) {
    RtfBasicElement rtfElement = (RtfBasicElement) element;
    rtfElement.setRtfDocument( rtfDoc );
    return new RtfBasicElement[] { rtfElement };
  }
  switch ( element.type() ) {
    case Element.CHUNK:
      Chunk chunk = (Chunk) element;
      if ( chunk.hasAttributes() ) {
        if ( chunk.getAttributes().containsKey( Chunk.IMAGE ) ) {
          rtfElements.add( new RtfImage( rtfDoc, chunk.getImage() ) );
        } else if ( chunk.getAttributes().containsKey( Chunk.NEWPAGE ) ) {
          rtfElements.add( new RtfNewPage( rtfDoc ) );
        } else if ( chunk.getAttributes().containsKey( Chunk.TAB ) ) {
          Float tabPos = (Float) ( (Object[]) chunk.getAttributes().get( Chunk.TAB ) )[1];
          RtfTab tab = new RtfTab( tabPos.floatValue(), RtfTab.TAB_LEFT_ALIGN );
          tab.setRtfDocument( rtfDoc );
          rtfElements.add( tab );
          rtfElements.add( new RtfChunk( rtfDoc, new Chunk( "\t" ) ) );
        } else {
          rtfElements.add( new RtfChunk( rtfDoc, (Chunk) element ) );
        }
      } else {
        rtfElements.add( new RtfChunk( rtfDoc, (Chunk) element ) );
      }
      break;
    case Element.PHRASE:
      rtfElements.add( new RtfPhrase( rtfDoc, (Phrase) element ) );
      break;
    case Element.PARAGRAPH:
      rtfElements.add( new RtfParagraph( rtfDoc, (Paragraph) element ) );
      break;
    case Element.ANCHOR:
      rtfElements.add( new RtfAnchor( rtfDoc, (Anchor) element ) );
      break;
    case Element.ANNOTATION:
      rtfElements.add( new RtfAnnotation( rtfDoc, (Annotation) element ) );
      break;
    case Element.IMGRAW:
    case Element.IMGTEMPLATE:
    case Element.JPEG:
      rtfElements.add( new RtfImage( rtfDoc, (Image) element ) );
      break;
    case Element.AUTHOR:
    case Element.SUBJECT:
    case Element.KEYWORDS:
    case Element.TITLE:
    case Element.PRODUCER:
    case Element.CREATIONDATE:
      rtfElements.add( new RtfInfoElement( rtfDoc, (Meta) element ) );
      break;
    case Element.LIST:
      rtfElements.add( new RtfList( rtfDoc, (List) element ) ); // TODO: Testing
      break;
    case Element.LISTITEM:
      rtfElements.add( new RtfListItem( rtfDoc, (ListItem) element ) ); // TODO: Testing
      break;
    case Element.SECTION:
      rtfElements.add( new RtfSection( rtfDoc, (Section) element ) );
      break;
    case Element.CHAPTER:
      rtfElements.add( new RtfChapter( rtfDoc, (Chapter) element ) );
      break;
    case Element.TABLE:
      if ( element instanceof Table ) {
        rtfElements.add( new PatchRtfTable( rtfDoc, (Table) element ) );
      } else {
        rtfElements.add( new PatchRtfTable( rtfDoc, ( (SimpleTable) element ).createTable() ) );
      }
      break;
    case Element.PTABLE:
      if ( element instanceof PdfPTable ) {
        rtfElements.add( new PatchRtfTable( rtfDoc, (PdfPTable) element ) );
      } else {
        rtfElements.add( new PatchRtfTable( rtfDoc, ( (SimpleTable) element ).createTable() ) );
      }
      break;
  }

  return rtfElements.toArray( new RtfBasicElement[rtfElements.size()] );
}