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

The following examples show how to use com.lowagie.text.pdf.PdfPCell#setBackgroundColor() . 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: IncCell.java    From gcs with Mozilla Public License 2.0 5 votes vote down vote up
/** Creates a new instance of IncCell */
public IncCell(String tag, ChainedProperties props) {
    cell = new PdfPCell((Phrase)null);
    String value = props.getProperty("colspan");
    if (value != null)
        cell.setColspan(Integer.parseInt(value));
    value = props.getProperty("align");
    if (tag.equals("th"))
        cell.setHorizontalAlignment(Element.ALIGN_CENTER);
    if (value != null) {
        if ("center".equalsIgnoreCase(value))
            cell.setHorizontalAlignment(Element.ALIGN_CENTER);
        else if ("right".equalsIgnoreCase(value))
            cell.setHorizontalAlignment(Element.ALIGN_RIGHT);
        else if ("left".equalsIgnoreCase(value))
            cell.setHorizontalAlignment(Element.ALIGN_LEFT);
        else if ("justify".equalsIgnoreCase(value))
            cell.setHorizontalAlignment(Element.ALIGN_JUSTIFIED);
    }
    value = props.getProperty("valign");
    cell.setVerticalAlignment(Element.ALIGN_MIDDLE);
    if (value != null) {
        if ("top".equalsIgnoreCase(value))
            cell.setVerticalAlignment(Element.ALIGN_TOP);
        else if ("bottom".equalsIgnoreCase(value))
            cell.setVerticalAlignment(Element.ALIGN_BOTTOM);
    }
    value = props.getProperty("border");
    float border = 0;
    if (value != null)
        border = Float.parseFloat(value);
    cell.setBorderWidth(border);
    value = props.getProperty("cellpadding");
    if (value != null)
        cell.setPadding(Float.parseFloat(value));
    cell.setUseDescender(true);
    value = props.getProperty("bgcolor");
    cell.setBackgroundColor(Markup.decodeColor(value));
}
 
Example 2
Source File: IncCell.java    From itext2 with GNU Lesser General Public License v3.0 5 votes vote down vote up
/** Creates a new instance of IncCell */
public IncCell(String tag, ChainedProperties props) {
    cell = new PdfPCell((Phrase)null);
    String value = props.getProperty("colspan");
    if (value != null)
        cell.setColspan(Integer.parseInt(value));
    value = props.getProperty("align");
    if (tag.equals("th"))
        cell.setHorizontalAlignment(Element.ALIGN_CENTER);
    if (value != null) {
        if ("center".equalsIgnoreCase(value))
            cell.setHorizontalAlignment(Element.ALIGN_CENTER);
        else if ("right".equalsIgnoreCase(value))
            cell.setHorizontalAlignment(Element.ALIGN_RIGHT);
        else if ("left".equalsIgnoreCase(value))
            cell.setHorizontalAlignment(Element.ALIGN_LEFT);
        else if ("justify".equalsIgnoreCase(value))
            cell.setHorizontalAlignment(Element.ALIGN_JUSTIFIED);
    }
    value = props.getProperty("valign");
    cell.setVerticalAlignment(Element.ALIGN_MIDDLE);
    if (value != null) {
        if ("top".equalsIgnoreCase(value))
            cell.setVerticalAlignment(Element.ALIGN_TOP);
        else if ("bottom".equalsIgnoreCase(value))
            cell.setVerticalAlignment(Element.ALIGN_BOTTOM);
    }
    value = props.getProperty("border");
    float border = 0;
    if (value != null)
        border = Float.parseFloat(value);
    cell.setBorderWidth(border);
    value = props.getProperty("cellpadding");
    if (value != null)
        cell.setPadding(Float.parseFloat(value));
    cell.setUseDescender(true);
    value = props.getProperty("bgcolor");
    cell.setBackgroundColor(Markup.decodeColor(value));
}
 
Example 3
Source File: PdfExportService.java    From fredbet with Creative Commons Attribution Share Alike 4.0 International 5 votes vote down vote up
private void addRowToTable(PdfPTable table, List<String> columns, boolean isHeader) {
    final Font font = fontCreator.createFont();

    for (String column : columns) {
        PdfPCell cell = new PdfPCell(new Phrase(column, font));
        cell.setHorizontalAlignment(Element.ALIGN_CENTER);
        if (isHeader) {
            cell.setBackgroundColor(Color.LIGHT_GRAY);
        }

        table.addCell(cell);
    }
}
 
Example 4
Source File: PdfTimetableGridTable.java    From unitime with Apache License 2.0 5 votes vote down vote up
private void addLegendRow(String color, String text) {
	PdfPCell c = createCellNoBorder();
	c.setBorderWidth(1);
	c.setBackgroundColor(getColor(color));
	iPdfTable.addCell(c);
	c = createCellNoBorder();
	addText(c, "  "+text);
	c.setHorizontalAlignment(Element.ALIGN_LEFT);
	iPdfTable.addCell(c);
}
 
Example 5
Source File: PdfExamGridTable.java    From unitime with Apache License 2.0 5 votes vote down vote up
private void addLegendRow(String color, String text) {
    PdfPCell c = createCellNoBorder();
    c.setBorderWidth(1);
    c.setBackgroundColor(getColor(color));
    iPdfTable.addCell(c);
    c = createCellNoBorder();
    addText(c, "  "+text);
    c.setHorizontalAlignment(Element.ALIGN_LEFT);
    iPdfTable.addCell(c);
}
 
Example 6
Source File: PdfInstructionalOfferingTableBuilder.java    From unitime with Apache License 2.0 5 votes vote down vote up
public PdfPCell createCell() {
	PdfPCell cell = new PdfPCell();
	cell.setBorderColor(sBorderColor);
	cell.setPadding(3);
	cell.setBorderWidth(0);
	cell.setVerticalAlignment(Element.ALIGN_TOP);
	cell.setHorizontalAlignment(Element.ALIGN_CENTER);
	cell.setBackgroundColor(iBgColor);
	return cell;
}
 
Example 7
Source File: TemplateImagesTest.java    From itext2 with GNU Lesser General Public License v3.0 4 votes vote down vote up
/**
    * PdfTemplates can be wrapped in an Image.
    */
@Test
   public  void main() throws Exception {
       
           
       // step 1: creation of a document-object
       Rectangle rect = new Rectangle(PageSize.A4);
       rect.setBackgroundColor(new Color(238, 221, 88));
       Document document = new Document(rect, 50, 50, 50, 50);
	// step 2: we create a writer that listens to the document
	PdfWriter writer = PdfWriter.getInstance(document, PdfTestBase.getOutputStream("templateImages.pdf"));
	// step 3: we open the document
	document.open();
	// step 4:
	PdfTemplate template = writer.getDirectContent().createTemplate(20, 20);
	BaseFont bf = BaseFont.createFont(BaseFont.HELVETICA, BaseFont.WINANSI,
			BaseFont.NOT_EMBEDDED);
	String text = "Vertical";
	float size = 16;
	float width = bf.getWidthPoint(text, size);
	template.beginText();
	template.setRGBColorFillF(1, 1, 1);
	template.setFontAndSize(bf, size);
	template.setTextMatrix(0, 2);
	template.showText(text);
	template.endText();
	template.setWidth(width);
	template.setHeight(size + 2);
	template.sanityCheck();
	Image img = Image.getInstance(template);
	img.setRotationDegrees(90);
	Chunk ck = new Chunk(img, 0, 0);
	PdfPTable table = new PdfPTable(3);
	table.setWidthPercentage(100);
	table.getDefaultCell().setHorizontalAlignment(Element.ALIGN_CENTER);
	table.getDefaultCell().setVerticalAlignment(Element.ALIGN_MIDDLE);
	PdfPCell cell = new PdfPCell(img);
	cell.setPadding(4);
	cell.setBackgroundColor(new Color(0, 0, 255));
	cell.setHorizontalAlignment(Element.ALIGN_CENTER);
	table.addCell("I see a template on my right");
	table.addCell(cell);
	table.addCell("I see a template on my left");
	table.addCell(cell);
	table.addCell("I see a template everywhere");
	table.addCell(cell);
	table.addCell("I see a template on my right");
	table.addCell(cell);
	table.addCell("I see a template on my left");

	Paragraph p1 = new Paragraph("This is a template ");
	p1.add(ck);
	p1.add(" just here.");
	p1.setLeading(img.getScaledHeight() * 1.1f);
	document.add(p1);
	document.add(table);
	Paragraph p2 = new Paragraph("More templates ");
	p2.setLeading(img.getScaledHeight() * 1.1f);
	p2.setAlignment(Element.ALIGN_JUSTIFIED);
	img.scalePercent(70);
	for (int k = 0; k < 20; ++k)
		p2.add(ck);
	document.add(p2);
	// step 5: we close the document
	document.close();

   }
 
Example 8
Source File: ImageChunksTest.java    From itext2 with GNU Lesser General Public License v3.0 4 votes vote down vote up
/**
 * Images wrapped in a Chunk.
 */
@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("imageChunks.pdf"));
	// step 3: we open the document
	document.open();
	// step 4: we create a table and add it to the document
	Image img = Image.getInstance(PdfTestBase.RESOURCES_DIR + "pngnow.png");
	img.scalePercent(70);
	Chunk ck = new Chunk(img, 0, -5);
	PdfPTable table = new PdfPTable(3);
	PdfPCell cell = new PdfPCell();
	cell.addElement(new Chunk(img, 5, -5));
	cell.setBackgroundColor(new Color(0xC0, 0xC0, 0xC0));
	cell.setHorizontalAlignment(Element.ALIGN_CENTER);
	table.addCell("I see an image\non my right");
	table.addCell(cell);
	table.addCell("I see an image\non my left");
	table.addCell(cell);
	table.addCell("I see images\neverywhere");
	table.addCell(cell);
	table.addCell("I see an image\non my right");
	table.addCell(cell);
	table.addCell("I see an image\non my left");

	Phrase p1 = new Phrase("This is an image ");
	p1.add(ck);
	p1.add(" just here.");
	document.add(p1);
	document.add(p1);
	document.add(p1);
	document.add(p1);
	document.add(p1);
	document.add(p1);
	document.add(p1);
	document.add(table);

	// step 5: we close the document
	document.close();
}
 
Example 9
Source File: VerticalTextInCellsTest.java    From itext2 with GNU Lesser General Public License v3.0 4 votes vote down vote up
/**
 * Example with vertical text in Cells.
 */
@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("VerticalText.pdf"));
	// step3
	document.open();
	// step4

	// make a PdfTemplate with the vertical text
	PdfTemplate template = writer.getDirectContent().createTemplate(20, 20);
	BaseFont bf = BaseFont.createFont("Helvetica", "winansi", false);
	String text = "Vertical";
	float size = 16;
	float width = bf.getWidthPoint(text, size);
	template.beginText();
	template.setRGBColorFillF(1, 1, 1);
	template.setFontAndSize(bf, size);
	template.setTextMatrix(0, 2);
	template.showText(text);
	template.endText();
	template.setWidth(width);
	template.setHeight(size + 2);
	// make an Image object from the template
	Image img = Image.getInstance(template);
	img.setRotationDegrees(90);
	PdfPTable table = new PdfPTable(3);
	table.setWidthPercentage(100);
	table.getDefaultCell().setHorizontalAlignment(Element.ALIGN_CENTER);
	table.getDefaultCell().setVerticalAlignment(Element.ALIGN_MIDDLE);
	PdfPCell cell = new PdfPCell(img);
	cell.setPadding(4);
	cell.setBackgroundColor(new Color(0, 0, 255));
	cell.setHorizontalAlignment(Element.ALIGN_CENTER);
	table.addCell("I see a template on my right");
	table.addCell(cell);
	table.addCell("I see a template on my left");
	table.addCell(cell);
	table.addCell("I see a template everywhere");
	table.addCell(cell);
	table.addCell("I see a template on my right");
	table.addCell(cell);
	table.addCell("I see a template on my left");
	document.add(table);

	// step5
	document.close();
}