Subhrajyoti Moitra wrote:
Hi,
I have a table with a custom image background. I am using PdfpTableEvent to set this background. The header cells of this table uses some other color, different from the table background.
To set this background color, I am using PdfPCellEvent.

PdfPCellEvent docHistTableHeaderCellEvent=new PdfPCellEvent(){
public void cellLayout(PdfPCell cell, Rectangle rect,PdfContentByte[] canvases) { PdfContentByte backCb=canvases[PdfPTable.BACKGROUNDCANVAS];
                    backCb.setRGBColorFill(0xFF, 0x00, 0x00);
backCb.rectangle(rect.getLeft(), rect.getBottom(),20, 10);
                    backCb.fill();
                    backCb.resetRGBColorFill();
                }
            };

This seems to be setting the red color rectangles, in a fixed location (top of the table) and not adapting to the table headers positions. Please advice. Any pointers to similar issues will be highly appreciated. Thanks a lot in advance!

Attached is an example with cell events in the header.
I don't see any problem, do you?
--
This answer is provided by 1T3XT BVBA
http://www.1t3xt.com/ - http://www.1t3xt.info
import java.awt.Color;
import java.io.FileOutputStream;
import java.io.IOException;

import com.lowagie.text.Document;
import com.lowagie.text.DocumentException;
import com.lowagie.text.Element;
import com.lowagie.text.Font;
import com.lowagie.text.FontFactory;
import com.lowagie.text.PageSize;
import com.lowagie.text.Phrase;
import com.lowagie.text.Rectangle;
import com.lowagie.text.pdf.PdfContentByte;
import com.lowagie.text.pdf.PdfPCell;
import com.lowagie.text.pdf.PdfPCellEvent;
import com.lowagie.text.pdf.PdfPTable;
import com.lowagie.text.pdf.PdfWriter;


public class HeaderCellEvents {

        class Ellipse implements PdfPCellEvent {
                /**
                 * @see 
com.lowagie.text.pdf.PdfPCellEvent#cellLayout(com.lowagie.text.pdf.PdfPCell,
                 *      com.lowagie.text.Rectangle,
                 *      com.lowagie.text.pdf.PdfContentByte[])
                 */
                public void cellLayout(PdfPCell cell, Rectangle rect,
                                PdfContentByte[] canvas) {
                        PdfContentByte cb = canvas[PdfPTable.BACKGROUNDCANVAS];
                        cb.setRGBColorFill(0xFF, 0x00, 0x00);
                        cb.ellipse(rect.getLeft(), rect.getBottom(), 
rect.getRight(), rect.getTop());
                        cb.fill();
                        cb.resetRGBColorFill();
                }
        }
        
        public static void main(String[] args) {
                // step 1: creation of a document-object
                Document document = new Document(PageSize.A4.rotate());
                try {
                        // step 2:
                        // we create a writer
                        PdfWriter.getInstance(
                        // that listens to the document
                                        document,
                                        // and directs a PDF-stream to a file
                                        new 
FileOutputStream("headercellevents.pdf"));
                        // step 3: we open the document
                        document.open();
                        // step 4: we add a table to the document
                        PdfPTable datatable = new PdfPTable(10);
                        int headerwidths[] = { 10, 24, 12, 12, 7, 7, 7, 7, 7, 7 
};
                        datatable.setWidths(headerwidths);
                        datatable.setWidthPercentage(100);
                        datatable.getDefaultCell().setPadding(5);

                        // The header starts with a cell that spans 10 columns
                        PdfPCell cell = new PdfPCell(new Phrase(
                                        "Administration - System Users Report", 
FontFactory
                                                        
.getFont(FontFactory.HELVETICA, 24, Font.BOLD)));
                        cell.setHorizontalAlignment(Element.ALIGN_CENTER);
                        cell.setBorderWidth(2);
                        cell.setColspan(10);
                        cell.setBackgroundColor(new Color(0xC0, 0xC0, 0xC0));
                        cell.setUseDescender(true);
                        cell.setCellEvent(new HeaderCellEvents().new Ellipse());
                        datatable.addCell(cell);
                        // We need 4 cells with rowspan 2
                        datatable.getDefaultCell().setBorderWidth(2);
                        datatable.getDefaultCell().setHorizontalAlignment(
                                        Element.ALIGN_CENTER);
                        datatable.addCell("User Id");
                        datatable.addCell("Name\nAddress");
                        datatable.addCell("Company");
                        datatable.addCell("Department");
                        // we use a nested table to fake this
                        PdfPTable permissions = new PdfPTable(6);
                        permissions.getDefaultCell().setBorderWidth(2);
                        permissions.getDefaultCell().setHorizontalAlignment(
                                        Element.ALIGN_CENTER);
                        permissions.getDefaultCell().setColspan(6);
                        permissions.addCell("Permissions");
                        permissions.getDefaultCell().setColspan(1);
                        permissions.addCell("Admin");
                        permissions.addCell("Data");
                        permissions.addCell("Expl");
                        permissions.addCell("Prod");
                        permissions.addCell("Proj");
                        permissions.addCell("Online");
                        PdfPCell permission = new PdfPCell(permissions);
                        permission.setColspan(6);
                        datatable.addCell(permission);
                        // this is the end of the table header
                        // as far as PdfPTable is concerned there are 2 rows in 
the header
                        datatable.setHeaderRows(2);

                        // we add the data to the table
                        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);
                                for (int j = 0; j < 6; j++)
                                        datatable.addCell(Math.random() > .5 ? 
"Yes" : "No");
                        }
                        datatable.setSplitLate(false);
                        document.add(datatable);
                } catch (DocumentException de) {
                        System.err.println(de.getMessage());
                } catch (IOException ioe) {
                        System.err.println(ioe.getMessage());
                }

                // step 5: we close the document
                document.close();
        }
}
------------------------------------------------------------------------------
Apps built with the Adobe(R) Flex(R) framework and Flex Builder(TM) are
powering Web 2.0 with engaging, cross-platform capabilities. Quickly and
easily build your RIAs with Flex Builder, the Eclipse(TM)based development
software that enables intelligent coding and step-through debugging.
Download the free 60 day trial. http://p.sf.net/sfu/www-adobe-com
_______________________________________________
iText-questions mailing list
[email protected]
https://lists.sourceforge.net/lists/listinfo/itext-questions

Buy the iText book: http://www.1t3xt.com/docs/book.php

Reply via email to