You don't need (and shouldn't use) fitsPage to set the tabe width. Use
table.setTotalWidth(document.right() - document.left()) and place the table
at position document.left().
Best Regards,
Paulo Soares
----- Original Message -----
From: "Rolando Cuevas" <[EMAIL PROTECTED]>
To: <[EMAIL PROTECTED]>
Sent: Wednesday, July 23, 2003 18:48
Subject: Re: [iText-questions] tables within a header
>
>
> >From: Yuan Chang <[EMAIL PROTECTED]>
> >Reply-To: [EMAIL PROTECTED]
> >To: [EMAIL PROTECTED]
> >Subject: [iText-questions] tables within a header
> >Date: Wed, 23 Jul 2003 11:41:32 +0100
> >
> >Could anyone give me an example of how i could have a table within the
> >header on my pdf page?
> >
>
> Included in this mail is sample code for using pdfptables in headers.
> The
> sample prints a table header having three columns. First with company
> name,
> middle with date and right with pag number. It could be used as an
> starting
> point.
> Classes should probably be better organized, but this is only an
> example.
>
> The code sets document margins according to the table, beware that this
> is
> not always accurate. getTotalHeight() does not always returns the same
> value
> after document.add() and writer.fitsPage().
> at the eventhelper and pagenumchunk are in the same class. By the way
> does
> anyone knows a way to get the height of a pdfptable ? The way I use is
> first
> writer.fitsPage(oTable) and then table.getTotalHeight(). This has to be
> before adding the table to the document, since its height determines doc
>
> margins.
>
> If you want to use the pagenumchunk do not comment out the line
> this.setGenericTag("test")
> Even though the generic tag is not used in the code, without it the
> printed
> page number is always zero.
> The itext version I use is 1.00.
>
> Hope this helps,
> Rolando
>
> package prueba;
>
> import java.awt.Color;
> import java.io.*;
>
> import com.lowagie.text.*;
> import com.lowagie.text.pdf.*;
>
> public class headerWithTable {
>
> //----------------------------------------------
> public class PageNumChunk extends Chunk {
> private PdfWriter m_oPdfWriter;
> /** Creates a new instance of PageNumChunk */
> public PageNumChunk(PdfWriter oPdfWriter ) {
> m_oPdfWriter = oPdfWriter;
> this.content = new StringBuffer("");
> this.font = new Font();
> this.setGenericTag("test"); // has to be set otherwise page
> number
> is allways zero
> }
>
> /**
> * Returns the content of this <CODE>Chunk</CODE>.
> *
> * @return a <CODE>String</CODE>
> */
>
> public String content() {
> content.delete(0, content.length());
> content.append(m_oPdfWriter.getPageNumber());
>
> System.out.println(" \n------------>
> m_oPdfWriter.getPageNumber()
> "+m_oPdfWriter.getPageNumber());
> System.out.println(" ------------> Chunk pageNum
> "+content.toString()+"\n");
> return content.toString();
> }
>
>
> }
>
> //-----------------------------------------------
> public class TPdfEvent extends PdfPageEventHelper {
> PdfPTable m_oHeader=null;
>
> float m_fLeftX;
> float m_fRightX;
>
> float m_fHeaderTop;
> float m_fHeaderX;
>
> boolean m_bActive=false;
> boolean m_bMarginsSet = false;
>
> /** Creates a new instance of TPdfEvent */
> public TPdfEvent() {
> }
>
> public void setActive(boolean bActive) {
>
> m_bActive= bActive;
> System.out.println(" m_bActive = "+
> String.valueOf(m_bActive));
>
> }
>
>
> public void setLeftRightXCoords(float fLeftX, float fRightX) {
> m_fLeftX = fLeftX;
> m_fRightX = fRightX;
> m_bMarginsSet = true;
>
> }
> public void setHeader(PdfPTable oHeader, float fHeaderTop) {
> if(m_bMarginsSet) {
> System.out.println(" setHeader() begin");
> m_oHeader = oHeader;
> m_fHeaderTop = fHeaderTop;
> System.out.println(" m_fHeaderTop =
> "+m_fHeaderTop);
>
> float fHeaderWidth = (m_fRightX - m_fLeftX) *
> m_oHeader.getWidthPercentage() / 100;
> m_fHeaderX = 0;
> switch (m_oHeader.getHorizontalAlignment()) {
> case Element.ALIGN_LEFT:
> m_fHeaderX= m_fLeftX;
> break;
> case Element.ALIGN_RIGHT:
> m_fHeaderX= m_fRightX - fHeaderWidth;
> break;
> default:
> m_fHeaderX= (m_fRightX + m_fLeftX -
> fHeaderWidth) /
> 2;
> }
> System.out.println(" orig width % =
> "+m_oHeader.getWidthPercentage());
> System.out.println(" right margin = "+m_fRightX);
> System.out.println(" left margin = "+m_fLeftX);
> System.out.println(" fHeaderWidth = "+fHeaderWidth);
> System.out.println(" m_fHeaderX = "+m_fHeaderX);
>
> m_oHeader.setTotalWidth(fHeaderWidth);
> System.out.println(" setHeader() end");
> }
> else {
> throw new RuntimeException("Prior to setHeader()
> setMargins() has to be called.");
> }
> }
>
>
>
> public void onStartPage(PdfWriter oWriter, Document oDocument) {
> System.out.println(" Call to Event onStartPage. Page num
> =
> "+ oDocument.getPageNumber());
> if(m_bActive && m_oHeader!=null) {
> oWriter.fitsPage(m_oHeader);
> System.out.println(" ---------- onStartPage
> -------Header
> total height = "+m_oHeader.getTotalHeight());
> m_oHeader.writeSelectedRows(0, -1, m_fHeaderX,
> m_fHeaderTop,
> oWriter.getDirectContent());
> }
>
> }
>
>
> }
> //-----------------------------------------------
> //
> //-----------------------------------------------
> static Phrase m_oStEmptyPhrase=new Phrase(" ");
> static Font stFontTitle =
> FontFactory.getFont(FontFactory.HELVETICA,
> FontFactory.defaultEncoding, FontFactory.defaultEmbedding,
> 14.f, Font.BOLD, java.awt.Color.black);
> static Font stFontHeading =
> FontFactory.getFont(FontFactory.HELVETICA,
> FontFactory.defaultEncoding, FontFactory.defaultEmbedding,
> 10.f, Font.BOLD, java.awt.Color.black);
> static Font stFontBold =
> FontFactory.getFont(FontFactory.HELVETICA,
> FontFactory.defaultEncoding, FontFactory.defaultEmbedding,
> Font.UNDEFINED, Font.BOLD, java.awt.Color.black);
> static Font stFontOverview =
> FontFactory.getFont(FontFactory.HELVETICA,
> FontFactory.defaultEncoding, FontFactory.defaultEmbedding,
> 8.f, Font.BOLD, java.awt.Color.black);
>
> PdfPTable m_oHeader;
> PdfWriter m_oPdfWriter;
> Rectangle m_oPageRectangle;
> float m_fHeaderBegin=72f, m_fMarginTop=36f, m_fMarginBottom=36f,
>
> m_fMarginLeft=36f, m_fMarginRight=36f;
>
>
> public headerWithTable() {
> m_oPageRectangle = PageSize.A4;
> // m_oPageRectangle = m_oPageRectangle.rotate(); // landscape
> }
> public Rectangle getPageRectangle(){
> return m_oPageRectangle;
> }
> public void setWriter(PdfWriter oWriter){
> m_oPdfWriter = oWriter;
> }
>
>
> static PdfPCell getEmptyCell(int iColSpan, float fBorderWidth, float
>
> fCellPadding, Color borderColor, int iBorderType, Color backColor) {
> PdfPCell oCell= new PdfPCell(m_oStEmptyPhrase);
> oCell.setColspan(iColSpan);
> oCell.setPadding(fCellPadding);
> if(backColor != null){
> oCell.setBackgroundColor(backColor);
> }
> oCell.setBorderWidth(fBorderWidth);
> if(fBorderWidth>0) {
> oCell.setBorder(iBorderType);
> if(borderColor !=null){
> oCell.setBorderColor(borderColor);
> }
>
> }
> oCell.setHorizontalAlignment(Element.ALIGN_LEFT);
> oCell.setVerticalAlignment(Element.ALIGN_MIDDLE);
> return oCell;
>
> }
>
> void addContent2Phrase(Phrase oPhrase, String sContent, Font oFont){
> Chunk oChunk = new Chunk(sContent);
> if(oFont != null){
> oChunk.setFont(oFont);
> }
> oPhrase.add(oChunk);
> }
>
> void addPageNumChunk(Phrase oPhrase, PdfWriter oWriter, Font oFont){
> PageNumChunk oPageNum= new PageNumChunk(oWriter);
> if(oFont != null){
> oPageNum.setFont(oFont);
> }
> oPhrase.add(oPageNum);
> }
>
> void setRow1(){
> PdfPCell oCell = getEmptyCell(3, 0.f, 1.f, null, Rectangle.BOX,
>
> null);
> Phrase oPhrase = new Phrase();
> addContent2Phrase(oPhrase, "ACM Inc", stFontTitle);
> oCell.setPhrase(oPhrase);
> m_oHeader.addCell(oCell);
>
> }
> void setRow2(){
> PdfPCell oCell = getEmptyCell(1, 0.f, 1.f, null,
> Rectangle.NO_BORDER, null);
> Phrase oPhrase = new Phrase();
> addContent2Phrase(oPhrase, "Summary ", stFontHeading);
> oCell.setHorizontalAlignment(Element.ALIGN_LEFT);
> oCell.setPhrase(oPhrase);
> m_oHeader.addCell(oCell);
>
> oCell = getEmptyCell(1, 0.f, 1.f, null, Rectangle.NO_BORDER,
> null);
> oPhrase = new Phrase();
> addContent2Phrase(oPhrase, "23/07/2003", stFontHeading);
> oCell.setHorizontalAlignment(Element.ALIGN_CENTER);
> oCell.setPhrase(oPhrase);
> m_oHeader.addCell(oCell);
>
> oCell = getEmptyCell(1, 0.f, 1.f, null, Rectangle.NO_BORDER,
> null);
> oPhrase = new Phrase();
> addContent2Phrase(oPhrase, "Page ", stFontHeading);
> addPageNumChunk(oPhrase, m_oPdfWriter , stFontHeading);
> oCell.setHorizontalAlignment(Element.ALIGN_RIGHT);
> oCell.setPhrase(oPhrase);
> m_oHeader.addCell(oCell);
>
>
> }
> void setRow3(){
> PdfPCell oCell = getEmptyCell(3, 0.5f, 1.f,
> java.awt.Color.black,
> Rectangle.TOP, null);
> Phrase oPhrase = new Phrase();
> //addContent2Phrase(oPhrase, "Test line", stFontOverview);
> oCell.setPhrase(oPhrase);
> m_oHeader.addCell(oCell);
> }
>
>
> public PdfPTable getRandomContentTable(){
> PdfPTable oTable=new PdfPTable(1);
> oTable.setWidthPercentage(100);
>
> for( int i=0; i<10; i++){
> PdfPCell oCell = getEmptyCell(1, 0.5f, 1.f,
> java.awt.Color.black,
> Rectangle.BOX, null);
> Phrase oPhrase = new Phrase();
> addContent2Phrase(oPhrase, String.valueOf(Math.random()),
> stFontOverview);
> oCell.setPhrase(oPhrase);
> oTable.addCell(oCell);
> }
> System.out.println(" m_oPdfWriter.getPageNumber()");
>
> return oTable;
> }
> public PdfPTable getLine(){
> PdfPTable oTable=new PdfPTable(1);
> oTable.setWidthPercentage(100);
> PdfPCell oCell = getEmptyCell(1, 1.5f, 1.f, java.awt.Color.red,
>
> Rectangle.TOP, null);
> Phrase oPhrase = new Phrase();
> addContent2Phrase(oPhrase, "Test line", stFontOverview);
> oCell.setPhrase(oPhrase);
>
> oTable.addCell(oCell);
> return oTable;
> }
>
> public PdfPTable getTable() throws Exception{
> float aRelColWidths[] = {25f, 25f, 25f};
> m_oHeader = new PdfPTable(3);
> m_oHeader.setWidthPercentage(100);
> m_oHeader.setWidths(aRelColWidths);
> setRow1();
> setRow2();
> setRow3();
> // setRow4();
> return m_oHeader;
> }
> public void setHeaderWithPageEvent(PdfWriter oWriter, PdfPTable
> oHeader){
>
> TPdfEvent oPdfEvent = new TPdfEvent();
> oPdfEvent.setLeftRightXCoords( m_fMarginLeft,
> m_oPageRectangle.right(m_fMarginRight));
> if( oHeader!= null)
> oPdfEvent.setHeader(oHeader, m_fHeaderBegin);
> oPdfEvent.setActive(true);
> oWriter.setPageEvent(oPdfEvent);
> }
>
>
> private void setDocumentMarginsDynHeight(PdfWriter oWriter, Document
>
> oDocument ) {
> System.out.println(" setDocumentMargins() begin");
> System.out.println(" \n All measures are in points.");
> // m_oHeader to be used as header
> //
>
> float fTop=0f, fPageTop, fPageBottom, fHeaderSpaceAfter;
>
> fPageTop = m_oPageRectangle.top();
> fPageBottom = m_oPageRectangle.bottom();
> fHeaderSpaceAfter = 36f;
>
>
> System.out.println(" Page Y coordinates:\n\t top =
> "+fPageTop+"\t
> bottom = "+fPageBottom);
> System.out.println(" Original Margins:\n\t top =
> "+m_fMarginTop+"\t
> bottom = "+m_fMarginBottom);
> System.out.println(" \t left = "+m_fMarginLeft+"\t right =
> "+m_fMarginRight);
> System.out.println(" \t original pageHeight = "+(((fPageTop
> -m_fMarginTop) -(fPageBottom +m_fMarginBottom))));
>
>
> if(m_oHeader != null) // getting new top margin
> {
> oWriter.fitsPage(m_oHeader);
> float fHeaderHeight = m_oHeader.getTotalHeight();
> System.out.println(" \t dyn header height =
> "+fHeaderHeight);
> m_fHeaderBegin = fPageTop - m_fMarginTop ;
>
> fTop = m_fMarginTop + fHeaderHeight +
> fHeaderSpaceAfter; // new top margin
> System.out.println(" Has Header\n\t HeaderHeight=
> "+fHeaderHeight);
> System.out.println(" \t HeaderSpace = "+fHeaderSpaceAfter);
> System.out.println(" \t NEW top = "+fTop);
> System.out.println(" \t beginY= "+m_fHeaderBegin);
> }
>
>
> // if new pageheight less than Min, throw a runtime exception
> oDocument.setMargins(m_fMarginLeft, m_fMarginRight, fTop,
> m_fMarginBottom);
> System.out.println(" New Margins:\n\t top = "+fTop+"\t bottom =
>
> "+m_fMarginBottom);
> System.out.println(" \t left = "+m_fMarginLeft+"\t right =
> "+m_fMarginRight);
> System.out.println(" \t New pageHeight = "+((fPageTop -fTop) -
> (fPageBottom +m_fMarginBottom)));
>
>
> System.out.println(" setDocumentMargins() end");
> }
>
> public static void main(String[] args) {
> headerWithTable oHeightTest= new headerWithTable();
> headerWithTable oContent= new headerWithTable();
>
> Document document = new
> Document(oHeightTest.getPageRectangle()); //
> A4 landscape
>
> try {
> PdfWriter writer = PdfWriter.getInstance(document, new
> FileOutputStream("c://table_height_test.pdf"));
> oHeightTest.setWriter(writer);
>
> PdfPTable table = oHeightTest.getRandomContentTable();
> PdfPTable header = oHeightTest.getTable();
>
>
> oHeightTest.setDocumentMarginsDynHeight(writer, document);
> oHeightTest.setHeaderWithPageEvent(writer,header);
>
> System.out.println(" main ------------------------- total
> height = " + table.getTotalHeight());
> writer.fitsPage(table);
> System.out.println(" after writer.fitspage() - total height
> = "
> + table.getTotalHeight());
> document.open();
>
> document.add(table);
> document.newPage();
>
> table = oHeightTest.getRandomContentTable();
> document.add(table);
> document.close();
> System.out.println(" after doc.add() ---------- total height
> = "
> + table.getTotalHeight());
> System.out.println("Finished.");
> }
> catch (Exception de) {
> de.printStackTrace();
> }
> }
>
>
>
> }
>
> _________________________________________________________________
> Charla con tus amigos en l�nea mediante MSN Messenger:
> http://messenger.yupimsn.com/
>
>
>
> -------------------------------------------------------
> This SF.Net email sponsored by: Free pre-built ASP.NET sites including
> Data Reports, E-commerce, Portals, and Forums are available now.
> Download today and enter to win an XBOX or Visual Studio .NET.
> http://aspnet.click-url.com/go/psa00100003ave/direct;at.aspnet_072303_01
> /01
> _______________________________________________
> iText-questions mailing list
> [EMAIL PROTECTED]
> https://lists.sourceforge.net/lists/listinfo/itext-questions
>
-------------------------------------------------------
This SF.Net email sponsored by: Free pre-built ASP.NET sites including
Data Reports, E-commerce, Portals, and Forums are available now.
Download today and enter to win an XBOX or Visual Studio .NET.
http://aspnet.click-url.com/go/psa00100003ave/direct;at.aspnet_072303_01/01
_______________________________________________
iText-questions mailing list
[EMAIL PROTECTED]
https://lists.sourceforge.net/lists/listinfo/itext-questions