Hello,

I�m creating pdfs with header data coming from databases. Until now I have used a fixed header height and tableevents, this way headers can be tables with images and text. It works fine. However this is not enough for all cases. Header data sometimes is mixed with content.

To obtain the table height I first call writer.fitspage(oTable) and then oTable.getTotalHeight(), having this value I set document margins.
However totalheight value is different when called after document.add(oTable). The difference between the values is noticeable in the generated pdf file.



Is there any other way of getting a pdfptable height? It seems that the value obtained after document.add(table) is correct, but how can I get it? I need the height value before adding the table because document margins depend on the header's table height.



I include in this message the code that will recreate the situation. Sorry if you find the code lenghty but the problem with totalheight does not always show up.
The first red line should be closer to the upper row with yellow background. I am using itext 1.00.
By the way value obtain during pageEvents after fitspage() is different from value obtained after fitspage() in main().


Thanks in advance,
Rolando.



---------------------------------------------------
---------------------------------------------------
import java.awt.Color;
import java.io.*;

import com.lowagie.text.*;
import com.lowagie.text.pdf.*;

public class ptableHeightTest {
   //-----------------------------------------------
   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;

       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;
Rectangle m_oPageRectangle;
float m_fHeaderBegin=72f, m_fMarginTop=36f, m_fMarginBottom=36f, m_fMarginLeft=36f, m_fMarginRight=36f;



public ptableHeightTest() { m_oPageRectangle = PageSize.A4; m_oPageRectangle = m_oPageRectangle.rotate(); } public Rectangle getPageRectangle(){ return m_oPageRectangle; }

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 setRow1(){
PdfPCell oCell = getEmptyCell(14, 0.f, 1.f, null, Rectangle.BOX, null);
Phrase oPhrase = new Phrase();
addContent2Phrase(oPhrase, "ACM - Pro Audio / Outbound: 9456/2", stFontTitle);
oCell.setPhrase(oPhrase);
m_oHeader.addCell(oCell);


}
void setRow2(){
PdfPCell oCell = getEmptyCell(14, 0.f, 1.f, null, Rectangle.NO_BORDER, null);
Phrase oPhrase = new Phrase();
addContent2Phrase(oPhrase, "Infocomm ", stFontHeading);
oCell.setPhrase(oPhrase);
m_oHeader.addCell(oCell);
}
void setRow3(){
//<ROW empty="Y" emptyWithHR="N" />
PdfPCell oCell = getEmptyCell(14, 0.f, 1.f, null, Rectangle.NO_BORDER, null);
Phrase oPhrase = new Phrase();
oCell.setPhrase(oPhrase);
m_oHeader.addCell(oCell);
}
void setRow4(){
PdfPCell oCell = getEmptyCell(2, 0.f, 1.f, null, Rectangle.NO_BORDER, null);
Phrase oPhrase = new Phrase();
//A.Executive:
addContent2Phrase(oPhrase, "A.Executive: ", stFontBold);
oCell.setPhrase(oPhrase);
m_oHeader.addCell(oCell);


//Sherri Anderson
oCell = getEmptyCell(4, 0.f, 1.f, null, Rectangle.NO_BORDER, null);
oPhrase = new Phrase();
addContent2Phrase(oPhrase, "Sherri Anderson ", null);
oCell.setPhrase(oPhrase);
m_oHeader.addCell(oCell);
//Booth #:
oCell = getEmptyCell(2, 0.f, 1.f, null, Rectangle.NO_BORDER, null);
oPhrase = new Phrase();
addContent2Phrase(oPhrase, "Booth #: ", stFontBold);
oCell.setPhrase(oPhrase);
m_oHeader.addCell(oCell);


//Booth Size:
oCell = getEmptyCell(3, 0.f, 1.f, null, Rectangle.NO_BORDER, null);
oPhrase = new Phrase();
addContent2Phrase(oPhrase, "Booth Size: ", stFontBold);
addContent2Phrase(oPhrase,"30 x 30", null);
oCell.setPhrase(oPhrase);
m_oHeader.addCell(oCell);



//Job #: 9186
oCell = getEmptyCell(3, 0.f, 1.f, null, Rectangle.NO_BORDER, null);
oPhrase = new Phrase();
addContent2Phrase(oPhrase, "Job #: ", stFontBold);
addContent2Phrase(oPhrase,"9186", null);
oCell.setPhrase(oPhrase);
m_oHeader.addCell(oCell);


}
void setRow5(){
PdfPCell oCell = getEmptyCell(2, 0.f, 1.f, null, Rectangle.NO_BORDER, null);
Phrase oPhrase = new Phrase();


       addContent2Phrase(oPhrase, "Prepared By: ", stFontBold);
       oCell.setPhrase(oPhrase);
       m_oHeader.addCell(oCell);


oCell = getEmptyCell(4, 0.f, 1.f, null, Rectangle.NO_BORDER, null);
oPhrase = new Phrase();
addContent2Phrase(oPhrase, "Dan Smith ", null);
oCell.setPhrase(oPhrase);
m_oHeader.addCell(oCell);


oCell = getEmptyCell(2, 0.f, 1.f, null, Rectangle.NO_BORDER, null);
oPhrase = new Phrase();
addContent2Phrase(oPhrase, "Shipped via:", stFontBold);
addContent2Phrase(oPhrase,"DBA-Air Freight or Ground", null);
oCell.setPhrase(oPhrase);
m_oHeader.addCell(oCell);


//To: Advance Warehouse
oCell = getEmptyCell(3, 0.f, 1.f, null, Rectangle.NO_BORDER, null);
oPhrase = new Phrase();
addContent2Phrase(oPhrase, "To: ", stFontBold);
addContent2Phrase(oPhrase,"Advance Warehouse", null);
oCell.setPhrase(oPhrase);
m_oHeader.addCell(oCell);



//On: 05/19/2003
oCell = getEmptyCell(3, 0.f, 1.f, null, Rectangle.NO_BORDER, null);
oPhrase = new Phrase();
addContent2Phrase(oPhrase, "On: ", stFontBold);
addContent2Phrase(oPhrase," / / ", null);
oCell.setPhrase(oPhrase);
m_oHeader.addCell(oCell);


}
void setRow6(){
//Total Pcs: 20 Total Wgt: 11,820.00 Date: 07/15/2003
PdfPCell oCell = getEmptyCell(2, 0.f, 1.f, null, Rectangle.NO_BORDER, null);
Phrase oPhrase = new Phrase();


       addContent2Phrase(oPhrase, "Total Pcs: ", stFontBold);
       oCell.setPhrase(oPhrase);
       m_oHeader.addCell(oCell);


oCell = getEmptyCell(4, 0.f, 1.f, null, Rectangle.NO_BORDER, null);
oPhrase = new Phrase();
addContent2Phrase(oPhrase, "20 ", null);
oCell.setPhrase(oPhrase);
m_oHeader.addCell(oCell);


oCell = getEmptyCell(2, 0.f, 1.f, null, Rectangle.NO_BORDER, null);
oPhrase = new Phrase();
addContent2Phrase(oPhrase, "Total Wgt: ", stFontBold);
addContent2Phrase(oPhrase,"11,820.00", null);
oCell.setPhrase(oPhrase);
m_oHeader.addCell(oCell);



//Date: 07/15/2003
oCell = getEmptyCell(6, 0.f, 1.f, null, Rectangle.NO_BORDER, null);
oPhrase = new Phrase();
addContent2Phrase(oPhrase, "Date: ", stFontBold);
addContent2Phrase(oPhrase," / / ", null);
oCell.setPhrase(oPhrase);
m_oHeader.addCell(oCell);


}
void setRow7(){
PdfPCell oCell = getEmptyCell(14, 0.f, 1.f, null, Rectangle.NO_BORDER, null);
Phrase oPhrase = new Phrase();
oCell.setPhrase(oPhrase);
m_oHeader.addCell(oCell);
}
void setRow8(){
PdfPCell oCell = getEmptyCell(2, 1.f, 1.f, java.awt.Color.black, Rectangle.BOX, java.awt.Color.yellow);
Phrase oPhrase = new Phrase();


       addContent2Phrase(oPhrase, "Storage ", stFontOverview);
       oCell.setPhrase(oPhrase);
       m_oHeader.addCell(oCell);

oCell = getEmptyCell(3, 1.f, 1.f, java.awt.Color.black, Rectangle.BOX, java.awt.Color.yellow);
oPhrase = new Phrase();
addContent2Phrase(oPhrase, "Size ", stFontOverview);
oCell.setPhrase(oPhrase);
m_oHeader.addCell(oCell);


       oCell = getEmptyCell(5, 0.f, 1.f, null,  Rectangle.BOX, null);
       m_oHeader.addCell(oCell);

oCell = getEmptyCell(4, 1.f, 1.f, java.awt.Color.black, Rectangle.BOX, java.awt.Color.yellow);
oPhrase = new Phrase();
addContent2Phrase(oPhrase, "Source Storage", stFontOverview);
oCell.setPhrase(oPhrase);
m_oHeader.addCell(oCell);
}


void setRow9(){

PdfPCell oCell = getEmptyCell(1, 1.f, 1.f, java.awt.Color.black, Rectangle.BOX, java.awt.Color.yellow);
Phrase oPhrase = new Phrase();


       addContent2Phrase(oPhrase, "Unit Type", stFontOverview);
       oCell.setPhrase(oPhrase);
       m_oHeader.addCell(oCell);


oCell = getEmptyCell(1, 1.f, 1.f, java.awt.Color.black, Rectangle.BOX, java.awt.Color.yellow);
oPhrase = new Phrase();
addContent2Phrase(oPhrase, "Unit Name", stFontOverview);
oCell.setPhrase(oPhrase);
m_oHeader.addCell(oCell);



oCell = getEmptyCell(1, 1.f, 1.f, java.awt.Color.black, Rectangle.BOX, java.awt.Color.yellow);
oPhrase = new Phrase();
addContent2Phrase(oPhrase, "H", stFontOverview);
oCell.setPhrase(oPhrase);
m_oHeader.addCell(oCell);



oCell = getEmptyCell(1, 1.f, 1.f, java.awt.Color.black, Rectangle.BOX, java.awt.Color.yellow);
oPhrase = new Phrase();
addContent2Phrase(oPhrase, "W", stFontOverview);
oCell.setPhrase(oPhrase);
m_oHeader.addCell(oCell);


oCell = getEmptyCell(1, 1.f, 1.f, java.awt.Color.black, Rectangle.BOX, java.awt.Color.yellow);
oPhrase = new Phrase();
addContent2Phrase(oPhrase, "D", stFontOverview);
oCell.setPhrase(oPhrase);
m_oHeader.addCell(oCell);



oCell = getEmptyCell(1, 1.f, 1.f, java.awt.Color.black, Rectangle.BOX, java.awt.Color.yellow);
oPhrase = new Phrase();
addContent2Phrase(oPhrase, "Lbs", stFontOverview);
oCell.setPhrase(oPhrase);
m_oHeader.addCell(oCell);



oCell = getEmptyCell(1, 1.f, 1.f, java.awt.Color.black, Rectangle.BOX, java.awt.Color.yellow);
oPhrase = new Phrase();
addContent2Phrase(oPhrase, "Container - Item Name", stFontOverview);
oCell.setPhrase(oPhrase);
m_oHeader.addCell(oCell);



oCell = getEmptyCell(1, 1.f, 1.f, java.awt.Color.black, Rectangle.BOX, java.awt.Color.yellow);
oPhrase = new Phrase();
addContent2Phrase(oPhrase, "Inventory #", stFontOverview);
oCell.setPhrase(oPhrase);
m_oHeader.addCell(oCell);




oCell =getEmptyCell(1, 1.f, 1.f, java.awt.Color.black, Rectangle.BOX, java.awt.Color.yellow);
oPhrase = new Phrase();
addContent2Phrase(oPhrase, "Qty", stFontOverview);
oCell.setPhrase(oPhrase);
m_oHeader.addCell(oCell);


oCell = getEmptyCell(1, 1.f, 1.f, java.awt.Color.black, Rectangle.BOX, java.awt.Color.yellow);
oPhrase = new Phrase();
addContent2Phrase(oPhrase, "Description", stFontOverview);
oCell.setPhrase(oPhrase);
m_oHeader.addCell(oCell);



oCell = getEmptyCell(1, 1.f, 1.f, java.awt.Color.black, Rectangle.BOX, java.awt.Color.yellow);
oPhrase = new Phrase();
addContent2Phrase(oPhrase, "Unit Type", stFontOverview);
oCell.setPhrase(oPhrase);
m_oHeader.addCell(oCell);




oCell = getEmptyCell(1, 1.f, 1.f, java.awt.Color.black, Rectangle.BOX, java.awt.Color.yellow);
oPhrase = new Phrase();
addContent2Phrase(oPhrase, "Unit Name", stFontOverview);
oCell.setPhrase(oPhrase);
m_oHeader.addCell(oCell);



oCell = getEmptyCell(1, 1.f, 1.f, java.awt.Color.black, Rectangle.BOX, java.awt.Color.yellow);
oPhrase = new Phrase();
addContent2Phrase(oPhrase, "Company", stFontOverview);
oCell.setPhrase(oPhrase);
m_oHeader.addCell(oCell);



oCell = getEmptyCell(1, 1.f, 1.f, java.awt.Color.black, Rectangle.BOX, java.awt.Color.yellow);
oPhrase = new Phrase();
addContent2Phrase(oPhrase, "Division", stFontOverview);
oCell.setPhrase(oPhrase);
m_oHeader.addCell(oCell);
}


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[] = {1.8f,1.6f,0.8f,0.8f,0.8f,0.8f,4f,2f,1f,2.7f,1.8f,1.6f,3.9f,3.8f};
m_oHeader = new PdfPTable(14);
m_oHeader.setWidthPercentage(100);
m_oHeader.setWidths(aRelColWidths);
setRow1();
setRow2();
setRow3();
setRow4();
setRow5();
setRow6();
setRow7();
setRow8();
setRow9();
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 Measure Unit: Points.");


float fTop=0f, fPageTop, fPageBottom, fHeaderSpaceAfter;

       fPageTop        = m_oPageRectangle.top();
       fPageBottom     = m_oPageRectangle.bottom();
       //fHeaderSpaceAfter = 18f;
       fHeaderSpaceAfter = 0f;

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(" ------------------------------- 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) {
       ptableHeightTest oHeightTest= new ptableHeightTest();
       ptableHeightTest  oContent= new ptableHeightTest();

Document document = new Document(oHeightTest.getPageRectangle()); // A4 landscape

try {
PdfWriter writer = PdfWriter.getInstance(document, new FileOutputStream("c://table_height_test.pdf"));
//PdfWriter writer = PdfWriter.getInstance(document, new FileOutputStream(File2Dir.FILEDIR+"/table_height_test.pdf"));


           PdfPTable table = oContent.getTable();
           PdfPTable header = oHeightTest.getTable();
           PdfPTable line  = oHeightTest.getLine();

           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(line);
document.add(table);
//line = oHeightTest.getLine();
document.add(line);
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 is sponsored by: VM Ware
With VMware you can run multiple operating systems on a single machine.
WITHOUT REBOOTING! Mix Linux / Windows / Novell virtual machines at the
same time. Free trial click here: http://www.vmware.com/wl/offer/345/0
_______________________________________________
iText-questions mailing list
[EMAIL PROTECTED]
https://lists.sourceforge.net/lists/listinfo/itext-questions

Reply via email to