Hi,
below is the test code, you could run this code, the yellow background cells are the one where cell event is fired for drawing the lines but they do not extend to other page while the cell in which they are extend, green background..
thanks,
amit.
public class TestClass
{
private static int _signLength = 30;
public static void main (String args[])
{
createDocumentation();
}
{
private static int _signLength = 30;
public static void main (String args[])
{
createDocumentation();
}
private static void createDocumentation( )
{
final Document document = new Document( PageSize.A6 );
/// A4 or other sizes
try
{
final PdfWriter writer = PdfWriter.getInstance( document, new FileOutputStream( "Test.pdf ") );
document.open();
final PdfPTable table = new PdfPTable( 1 );
table.getDefaultCell().setPadding( 0 );
table.getDefaultCell().setBorder( 0 );
table.setSplitRows( true );
table.setSplitLate( false );
table.setWidthPercentage( 98 );
Map tree= prepareNodeData();
table.setHorizontalAlignment( Element.ALIGN_LEFT );
final PdfPTable table1 = generatePDFDocument( writer, tree, document );
table.addCell( table1 );
document.add( table );
table.setSplitRows( true );
document.newPage();
writer.setPageEmpty( false );
document.newPage();
}
catch ( Exception e )
{
e.printStackTrace( );
}
document.close();
}
{
final Document document = new Document( PageSize.A6 );
/// A4 or other sizes
try
{
final PdfWriter writer = PdfWriter.getInstance( document, new FileOutputStream( "Test.pdf ") );
document.open();
final PdfPTable table = new PdfPTable( 1 );
table.getDefaultCell().setPadding( 0 );
table.getDefaultCell().setBorder( 0 );
table.setSplitRows( true );
table.setSplitLate( false );
table.setWidthPercentage( 98 );
Map tree= prepareNodeData();
table.setHorizontalAlignment( Element.ALIGN_LEFT );
final PdfPTable table1 = generatePDFDocument( writer, tree, document );
table.addCell( table1 );
document.add( table );
table.setSplitRows( true );
document.newPage();
writer.setPageEmpty( false );
document.newPage();
}
catch ( Exception e )
{
e.printStackTrace( );
}
document.close();
}
private static PdfPTable generatePDFDocument( final PdfWriter writer, final Map tree, Document document ) throws Exception
{
return getPDFStructure( writer, tree, "0", 350, 0, document, 0 );
}
{
return getPDFStructure( writer, tree, "0", 350, 0, document, 0 );
}
private static PdfPTable getPDFStructure( final PdfWriter writer, final Map tree, final String nodeID,int descLength,
final int depth, Document document, int childCount ) throws IOException, DocumentException
{
int signLength = 30;
final Node node = (Node)tree.get( nodeID );
if ( descLength < 0 )
{
descLength = 100;
}
final int depth, Document document, int childCount ) throws IOException, DocumentException
{
int signLength = 30;
final Node node = (Node)tree.get( nodeID );
if ( descLength < 0 )
{
descLength = 100;
}
final float width[] = {signLength, descLength
};
final PdfPTable table = getPDFTable( width );
table.getDefaultCell().setNoWrap( false );
table.setTotalWidth( signLength + descLength );
table.setLockedWidth( true );
};
final PdfPTable table = getPDFTable( width );
table.getDefaultCell().setNoWrap( false );
table.setTotalWidth( signLength + descLength );
table.setLockedWidth( true );
table.getDefaultCell().setHorizontalAlignment( Element.ALIGN_LEFT );
table.setHorizontalAlignment( Element.ALIGN_LEFT );
PdfPTable tableSign = getSignTable( node );
PdfPCell cell = new PdfPCell( tableSign );
cell.setBackgroundColor( Color.GREEN );
table.addCell( cell );
PdfPTable innerTable = getIconTable( node,writer, tree, descLength - signLength, depth, document, childCount );
PdfPCell cell1 = new PdfPCell( innerTable );
table.addCell( cell1 );
return table;
}
PdfPTable tableSign = getSignTable( node );
PdfPCell cell = new PdfPCell( tableSign );
cell.setBackgroundColor( Color.GREEN );
table.addCell( cell );
PdfPTable innerTable = getIconTable( node,writer, tree, descLength - signLength, depth, document, childCount );
PdfPCell cell1 = new PdfPCell( innerTable );
table.addCell( cell1 );
return table;
}
private static PdfPTable getPDFTable( final
float[] width )
{
final PdfPTable table = new PdfPTable( width );
table.getDefaultCell().setPadding( 0 );
table.getDefaultCell().setBorder( 0 );
float[] width )
{
final PdfPTable table = new PdfPTable( width );
table.getDefaultCell().setPadding( 0 );
table.getDefaultCell().setBorder( 0 );
table.getDefaultCell().setHorizontalAlignment( Element.ALIGN_LEFT );
table.setExtendLastRow( true );
table.setSplitRows( true );
table.setSplitLate( false );
return table;
}
table.setExtendLastRow( true );
table.setSplitRows( true );
table.setSplitLate( false );
return table;
}
private static PdfPTable getIconTable( final Node node, final PdfWriter writer, final Map tree, final int
descLength, int depth, Document document, int
childCount ) throws IOException, DocumentException
{
final float[] outerWidth = {10 + 22 + 100 + 16 };
final PdfPTable outerTable = getPDFTable( outerWidth );
outerTable.setHorizontalAlignment( Element.ALIGN_LEFT );
final float[] width = {10, 22, 100, 16};
final PdfPTable table = getPDFTable( width );
final PdfPTable table = getPDFTable( width );
table.setSplitRows( false );
table.setSplitLate( true );
table.setHorizontalAlignment( Element.ALIGN_LEFT );
table.setTotalWidth( 10 + 22 + 100 + 16 );
table.setLockedWidth( true );
final PdfPCell cellLine = new PdfPCell( new Phrase ("--"));// cell with line
final PdfPCell cellIcon = new PdfPCell( new Phrase ("|X|"));// cell with ObjectIcon;
final PdfPCell cellText = new PdfPCell( new Phrase ("test " + node.getId() ));// cell with ObjectText;
final PdfPCell cellEndIcon = new PdfPCell( new Phrase ("-E-"));// cell with end icon;
final PdfPCell cellIcon = new PdfPCell( new Phrase ("|X|"));// cell with ObjectIcon;
final PdfPCell cellText = new PdfPCell( new Phrase ("test " + node.getId() ));// cell with ObjectText;
final PdfPCell cellEndIcon = new PdfPCell( new Phrase ("-E-"));// cell with end icon;
table.addCell( cellLine );
table.addCell( cellIcon );
table.addCell( cellText );
table.addCell( cellEndIcon );
PdfPCell cell1 = new PdfPCell( table );
outerTable.addCell( cell1 );
table.addCell( cellIcon );
table.addCell( cellText );
table.addCell( cellEndIcon );
PdfPCell cell1 = new PdfPCell( table );
outerTable.addCell( cell1 );
if ( !node.getChilds().isEmpty() )
{
depth++;
final Iterator it = node.getChilds().iterator();
while ( it.hasNext() )
{
final String childNodeID =(String) it.next();
//System.out.println( " creating for nodeID" + childNodeID );
PdfPCell cell = new PdfPCell( getPDFStru cture( writer, tree, childNodeID,
descLength - _signLength, depth, document, childCount ) );
outerTable.addCell( cell );
childCount++;
}
}
return outerTable;
}
{
depth++;
final Iterator it = node.getChilds().iterator();
while ( it.hasNext() )
{
final String childNodeID =(String) it.next();
//System.out.println( " creating for nodeID" + childNodeID );
PdfPCell cell = new PdfPCell( getPDFStru cture( writer, tree, childNodeID,
descLength - _signLength, depth, document, childCount ) );
outerTable.addCell( cell );
childCount++;
}
}
return outerTable;
}
private static PdfPTable getSignTable( final Node node ) throws IOException, BadElementException
{
final float width[] = {10, 20};
final PdfPTable table = new PdfPTable( width );
table.getDefaultCell().setBackgroundColor( Color.RED );
table.getDefaultCell().setBorder( Rectangle.NO_BORDER );
table.setSplitRows( false );
table.setSplitLate( true );
table.setExtendLastRow( true );
PdfEvents event = new PdfEvents();
table.setTableEvent( event );
table.setTotalWidth( _signLength );
table.setLockedWidth( true );
//final Image imgMinusStr = Image.getInstance( new URL( Constants.PDF_NODE_SIGN ) ));
final PdfPCell cellImgMinus = new PdfPCell( new Phrase ("|_"));
table.setSplitRows( false );
table.setSplitLate( true );
table.setExtendLastRow( true );
PdfEvents event = new PdfEvents();
table.setTableEvent( event );
table.setTotalWidth( _signLength );
table.setLockedWidth( true );
//final Image imgMinusStr = Image.getInstance( new URL( Constants.PDF_NODE_SIGN ) ));
final PdfPCell cellImgMinus = new PdfPCell( new Phrase ("|_"));
if ( node.draw() )
{
final PdfEvents events = new PdfEvents();
cellImgMinus.setCellEvent( events );
cellImgMinus.setBackgroundColor( Color.YELLOW );
cellImgMinus.setNoWrap( false );
{
final PdfEvents events = new PdfEvents();
cellImgMinus.setCellEvent( events );
cellImgMinus.setBackgroundColor( Color.YELLOW );
cellImgMinus.setNoWrap( false );
}
PdfPCell eCell = new PdfPCell( new Phrase( " "));
eCell.setBorder( 1 );
eCell.setBackgroundColor( Color.CYAN );
table.addCell( eCell );
table.addCell( cellImgMinus );
return table;
}
PdfPCell eCell = new PdfPCell( new Phrase( " "));
eCell.setBorder( 1 );
eCell.setBackgroundColor( Color.CYAN );
table.addCell( eCell );
table.addCell( cellImgMinus );
return table;
}
static class PdfEvents implements PdfPCellEvent, PdfPTableEvent
{
public void cellLayout( final PdfPCell cell,
final Rectangle position, final PdfContentByte[]
canvases )
{
final PdfContentByte cb =
canvases[PdfPTable.TEXTCANVAS];
cb.moveTo( position.left() + (
position.right() - position.left()) / 2,
position.bottom() );
cb.lineTo( position.left() + (
position.right() - position.left()) / 2,
position.top() );
cb.stroke();
}
{
public void cellLayout( final PdfPCell cell,
final Rectangle position, final PdfContentByte[]
canvases )
{
final PdfContentByte cb =
canvases[PdfPTable.TEXTCANVAS];
cb.moveTo( position.left() + (
position.right() - position.left()) / 2,
position.bottom() );
cb.lineTo( position.left() + (
position.right() - position.left()) / 2,
position.top() );
cb.stroke();
}
public void tableLayout( PdfPTable table,
float[][] width, float[] height, int headerRows, int
rowStart, PdfContentByte[] canvases )
{
float widths[] = width[0];
float x1 = widths[0];
float x2 = widths[widths.length - 1];
float y1 = height[0];
float y2 = height[height.length - 1];
PdfContentByte canvas =
canvases[PdfPTable.LINECANVAS];
canvas.setRGBColorStroke( 0x00, 0x00,
0xFF );
canvas.rectangle( x1, y1, x2 - x1, y2 -
y1 );
canvas.stroke();
canvas.resetRGBColorStroke();
}
}
static class Node
{
private List _childs= new ArrayList();
private String _id ;
private boolean _draw =true;
float[][] width, float[] height, int headerRows, int
rowStart, PdfContentByte[] canvases )
{
float widths[] = width[0];
float x1 = widths[0];
float x2 = widths[widths.length - 1];
float y1 = height[0];
float y2 = height[height.length - 1];
PdfContentByte canvas =
canvases[PdfPTable.LINECANVAS];
canvas.setRGBColorStroke( 0x00, 0x00,
0xFF );
canvas.rectangle( x1, y1, x2 - x1, y2 -
y1 );
canvas.stroke();
canvas.resetRGBColorStroke();
}
}
static class Node
{
private List _childs= new ArrayList();
private String _id ;
private boolean _draw =true;
public Node(String id)
{
_id = id;
}
public String getId()
{
return _id;
}
public Object getNodeProperty( String key )
{
return null;
}
public List getChilds()
{
return _childs;
}
public void addChild (Node node)
{
_childs.add( node.getId() );
}
{
_id = id;
}
public String getId()
{
return _id;
}
public Object getNodeProperty( String key )
{
return null;
}
public List getChilds()
{
return _childs;
}
public void addChild (Node node)
{
_childs.add( node.getId() );
}
public boolean draw()
{
return _draw;
}
{
return _draw;
}
public void setDraw( boolean draw)
{
_draw = draw ;
}
{
_draw = draw ;
}
}
private static Map prepareNodeData()
{
Map tree = new Hashtable();
Node rootNode = new Node("0");
tree.put( rootNode.getId(), rootNode );
prepareNodeList(rootNode,5,"", tree);
Node secondNode = (Node)tree.get("1");
prepareNodeList(secondNode,5,"2", tree);
Node thirdNode = (Node)tree.get("21");
prepareNodeList(thirdNode,8,"3", tree);
((Node)tree.get("24")).setDraw( false );
((Node)tree.get("37")).setDraw( false );
((Node)tree.get("4")).setDraw( false );
return tree;
{
Map tree = new Hashtable();
Node rootNode = new Node("0");
tree.put( rootNode.getId(), rootNode );
prepareNodeList(rootNode,5,"", tree);
Node secondNode = (Node)tree.get("1");
prepareNodeList(secondNode,5,"2", tree);
Node thirdNode = (Node)tree.get("21");
prepareNodeList(thirdNode,8,"3", tree);
((Node)tree.get("24")).setDraw( false );
((Node)tree.get("37")).setDraw( false );
((Node)tree.get("4")).setDraw( false );
return tree;
}
private static List prepareNodeList( Node parent, int size, String start, Map tree )
{
List list = new ArrayList(size);
for( int i=1; i < size; i++ )
{
Node node = new Node(start+i);
list.add(node);
parent.addChild(node);
tree.put( node.getId(), node);
}
return list;
}
{
List list = new ArrayList(size);
for( int i=1; i < size; i++ )
{
Node node = new Node(start+i);
list.add(node);
parent.addChild(node);
tree.put( node.getId(), node);
}
return list;
}
}
Paulo Soares <[EMAIL PROTECTED]> wrote:
I don't want to see your production code. I want a simple example that I
can run here (with main()).
> -----Original Message-----
> From: Amit Gijare [mailto:[EMAIL PROTECTED]
> Sent: Wednesday, April 27, 2005 12:27 PM
> To: Paulo Soares; [email protected]
> Subject: RE: [iText-questions] Cell events and page breaks,
>
> I have this following example, if you try to go
> over pages with depth of childs (in horizontal as well
> vertical direction) increasing, then at page breaks
> outer table refuse to extend even thought the cell
> extends to next page the cellevent does not..
>
> code sample below....
>
>
> private void createDocumentation ( final
> HttpServletResponse response ,final
> HttpServletRequest request )
> {
> final Document document = generateDocument();
> /// A4 or other sizes
> try
> {
> final PdfWriter writer =
> PdfWriter.getInstance(document,
> response.getOutputStream() );
> document.open();
> final PdfPTable table = new PdfPTable(1);
> table.getDefaultCell().setPadding(0);
> table.getDefaultCell().setBorder(0);
> table.setSplitRows( true );
> table.setSplitLate( false );
> table.setWidthPercentage(98);
>
> table.setHorizontalAlignment(Element.ALIGN_LEFT);
> final PdfPTable table1 =
> generatePDFDocument(writer, tree, document);
> table.addCell( table1 );
> document.add(table);
> table.setSplitRows(true);
>
> response.setContentType("application/pdf");
> document.newPage();
> writer.setPageEmpty(false);
> document.newPage();
> }
> catch ( Exception e )
> {
> ExceptionUtil.printStackTrace(e);
> }
> document.close();
> }
>
> private PdfPTable generatePDFDocument(final
> PdfWriter writer, final ObjectTree tree, Document
> document) throws Exception
> {
> String nodeID = tree.getRootNodeID();
> return getPDFStructure(writer, tree, nodeID,
> 350, 0, document, 0 );
> }
>
> private PdfPTable getPDFStructure(final PdfWriter
> writer,final ObjectTree tree, final String nodeID,
> int descLength,
> final int depth, Document document, int childCount )
> throws IOException, DocumentException
> {
> int signLength = 30;
> final Node node = tree.getNode(nodeID);
> if( descLength < 0)
> {
> descLength =100;
> }
>
> final float width[]= { signLength, descLength
> } ;
> final PdfPTable table = getPDFTable( width );
> table.getDefaultCell().setNoWrap( false );
> table.setTotalWidth( signLength+ descLength );> table.setLockedWidth(true);
>
> table.getDefaultCell().setHorizontalAlignment(Element.ALIGN_LEFT);
>
> table.setHorizontalAlignment(Element.ALIGN_LEFT);
> PdfPTable tableSign = getSignTable( node);
> PdfPCell cell = new PdfPCell(tableSign );
> cell.setBackgroundColor( Color.GREEN );
> table.addCell(cell);
> PdfPTable innerTable = getIconTable( node,
> writer, tree, descLength - signLength, depth,
> document, childCount );
> PdfPCell cell1 = new PdfPCell(innerTable );
> table.addCell( cell1);
> return table;
> }
>
> private static PdfPTable getPDFTable( final
> float[] width )
> {
> final PdfPTable table = new PdfPTable( width
> );
> table.getDefaultCell().setPadding(0);
> table.getDefaultCell().setBorder(0);
>
> table.getDefaultCell().setHorizontalAlignment(Element.ALIGN_LEFT);
> table.setExtendLastRow(true);
> table.setSplitRows( true );
> table.setSplitLate( false );
> return table;
> }
>
>
> private PdfPTable getIconTable( final Node node,
> final PdfWriter writer, final ObjectTree tree,
> final int
> descLength, int depth, Document document, int
> childCount ) throws IOException, DocumentException
> {
> final float[] outerWidth= { 10 + 22 + 100 + 16
> } ;
> final PdfPTable outerTable = getPDFTable(
> outerWidth );
>
> outerTable.setHorizontalAlignment(Element.ALIGN_LEFT);
>
> final float[] width= { 10, 22, 100, 16 } ;
> final PdfPTable table = getPDFTable( width );
>
>
> table.setSplitRows( false );
> table.setSplitLate( true );
>
>
> table.setHorizontalAlignment(Element.ALIGN_LEFT);
> table.setTotalWidth( 10 + 22 + 100 + 16 );
> table.setLockedWidth( true );
>
> final PdfPCell cellLin e // cell with line
> final PdfPCell cellIcon = // cell with
> ObjectIcon;
> final PdfPCell cellText = // cell with Object
> Text;
> final PdfPCell cellEndIcon = // cell with end
> icon ;
>
> table.addCell( cellLine );
> table.addCell(cellIcon );
> table.addCell( cellText );
> table.addCell( cellEndIcon );
> PdfPCell cell1 = new PdfPCell(table);
> outerTable.addCell( cell1 );
>
> if( node.getChildCount() > 0 )
> {
> depth++;
> final Iterator it =
> node.getChilds().iterator();
> while(it.hasNext())
> {
> final String childNodeID =
> (String)it.next();
> //System.out.println( " creating for
> nodeID " + childNodeID );
> PdfPCell cell = new
> PdfPCell(getPDFStructure(writer, tree, childNodeID,
> descLength - signLength, depth, document, childCount)
> );
> outerTable.addCell( cell );
> childCount++;
> }
> }
> return outerTable;
> }
>
>
> private PdfPTable getSignTable( final Node node )
> throws IOException, BadElementException
> {
> final float width[]= {10,20} ;
> final PdfPTable table = new PdfPTable( width
> );
> table.getDefaultCell().setBackgroundColor(
> Color.RED );
>
> table.getDefaultCell().setBorder(Rectangle.NO_BORDER);
> table.setSplitRows( false );
> table.setSplitLate( true );
> table.setExtendLastRow( true );
> PdfEvents event = new PdfEvents();
> table.setTableEvent(event);
> table.setTotalWidth( signLength);
> table.setLockedWidth(true);
> final Image imgMinusStr = Image.getInstance (
> new URL( Constants.PDF_NODE_SIGN) ));
> final PdfPCell cellImgMinus = new PdfPCell(
> imgMinusStr );
>
> if ( Boolean.valueOf(
> (String)node.getNodeProperty("DRAW") ).booleanValue())
> {
> final PdfEvents events = new PdfEvents();
> cellImgMinus.setCellEvent( events );
> cellImgMinus.setBackgroundColor(
> Color.YELLOW );
> cellImgMinus.setNoWrap(false);
>
> }
> PdfPCell eCell = getImageCell();
> eCell.setBorder(1);
> eCell.setBackgroundColor( Color.CYAN );
> table.addCell( eCell );
> table.addCell( cellImgMinus );
> return table;
> }
>
> class PdfEvents implements
> PdfPCellEvent,PdfPTableEvent
> {
> public void cellLayout(final PdfPCell cell,
> final Rectangle position,final PdfContentByte[]
> canvases)
> {
> final PdfContentByte cb =
> canvases[PdfPTable.TEXTCANVAS];
> cb.moveTo(position.left() + (
> position.right() - position.left())/2 ,
> position.bottom());
> cb.lineTo(position.left() + (
> position.right() - position.left())/2 ,
> position.to p() );
> cb.stroke();
> }
>
> public void tableLayout( PdfPTable table,
> float[][] width, float[] height, int headerRows, int
> rowStart, PdfContentByte[] canvases )
> {
> float widths[] = width[0];
> float x1 = widths[0];
> float x2 = widths[widths.length - 1];
> float y1 = height[0];
> float y2 = height[height.length - 1];
> PdfContentByte canvas =
> canvases[PdfPTable.LINECANVAS];
> canvas.setRGBColorStroke(0x00, 0x00,
> 0xFF);
> canvas.rectangle(x1, y1, x2 - x1, y2 -
> y1);
> canvas.stroke();
> canvas.resetRGBColorStroke();
> }
> }
>
>
>
>
>
>
> --- Paulo Soares <[EMAIL PROTECTED]>wrote:
> > The cell event is from the time when cells didn't
> > split, it's possible
> > that there are some inconsistencies. Post a small
> > example with the
> > problem .
> >
> > > -----Original Message-----
> > > From: [EMAIL PROTECTED]
> > >
> > [mailto:[EMAIL PROTECTED]
> > On
> > > Behalf Of Amit Gijare
> > > Sent: Wednesday, April 27, 2005 10:18 AM
> > > To: [email protected]
> > > Subject: [iText-questions] Cell events and page
> > breaks,
> > >
> > > hi,
> > >
> > > are cell events influenced by page breaks, I
> > have
> > > an cell event which draws line in a cell, works
> > fine
> > > if its a single page, but not when it extends over
> > > multiple pages. I tried changing the page layout
> > from
> > > A4 to A2 and it works fine, or is that splitting
> > of
> > > rows influences cell events.
& gt; > >
> > > thanks,
> > > amit.
> > >
> > > __________________________________________________
> > > Do You Yahoo!?
> > > Tired of spam? Yahoo! Mail has the best spam
> > protection around
> > > http://mail.yahoo.com
> > >
> > >
> > >
> >
> -------------------------------------------------------
> > > SF.Net email is sponsored by: Tell us your
> > software development plans!
> > > Take this survey and enter to win a one-year sub
> > to SourceForge.net
> > > Plus IDC's 2005 look-ahead and a copy of this
> > survey
> > > Click here to start!
> > http://www.idcswdc.com/cgi-bin/survey?id=105hix
> > > _______________________________________________
> > > iText-questions mailing list
> > > [email protected]
> > >
> >
> https://lists.sourceforge.net/lists/listinfo/itext-questions
> > >
> >
> >
> >
> -------------------------------------------------------
> > SF.Net email is sponsored by: Tell us your software
> > development plans!
> > Take this survey and enter to win a one-year sub to
> > SourceForge.net
> > Plus IDC's 2005 look-ahead and a copy of this survey
> > Click here to start!
> > http://www.idcswdc.com/cgi-bin/survey?id5hix
> > _______________________________________________
> > iText-questions mailing list
> > [email protected]
> >
> https://lists.sourceforge.net/lists/listinfo/itext-questions
> >
>
> __________________________________________________
> Do You Yahoo!?
> Tired of spam? Yahoo! Mail has the best spam protection around
> http://mail.yahoo.com
>
__________________________________________________
Do You Yahoo!?
Tired of spam? Yahoo! Mail has the best spam protection around
http://mail.yahoo.com
