The problem here is that PdfPTable.setWidthPercentage(float[]
columnWidth, Rectangle pageSize) is misleading.
The page size isn't taking the margins into account. The working code
should be: 

table.setWidthPercentage(columnWidth, new Rectangle(document.right() -
document.left(), 10 /* only the width matters */));

If you really want to be safe use:

table.setTotalWidth(float[] columnWidth);
table.setLockedWidth(true);


Best Regards,
Paulo Soares

> -----Original Message-----
> From: Mike Steele [mailto:[EMAIL PROTECTED] 
> Sent: Monday, June 21, 2004 8:07 PM
> To: Paulo Soares
> Subject: RE: [iText-questions] PdfPTable and Horizontal Points
> 
> Paulo,
> 
> I'm attempting to use PdfPTable and PdfPCell to create mailing label
> templates.  The vertical point spacing is always correct.  
> However, the
> horizontal point sizes almost always need to be modified by 
> some number.
> This is NOT on printout, but on the PDF itself.  If it try to 
> specify a
> one column table with a 4" width (288.0 pts) it comes out only about
> 2.5" wide.  But if I multiply the points by a modifier, it 
> works.  If I
> have a 3 column table (2 label columns with a middle column 
> for spacing)
> of 234.0, 54.0, 234.0, then in order for the PDF to generate columns
> that fit those measurements, I have to modify each horizontal 
> point size
> by 1.1685.
> 
> Here is the code used.  NOTE:  This is ColdFusion script, NOT 
> pure Java.
> The getLabelObj() returns a Label Object that contains the modifier as
> an attribute.  All horizontal measurements returned by a 
> get() statment
> are modified by that attribute.
> 
>       <cfscript>
>               function createDocument() {
>                       var pdfFile =
> createObject("java","java.io.FileOutputStream").init( 
> getLabelFile() );
>                       var document =
> createObject("java","com.lowagie.text.Document").init( getLabelPage(),
> getLabelObj().getPageLeftMarginPoints(),
> getLabelObj().getPageRightMarginPoints(),
> getLabelObj().getPageTopMarginPoints(),
> getLabelObj().getPageBottomMarginPoints() );
> 
>                       writeOutput( "Left: " &
> getLabelObj().getPageLeftMarginPoints() & "<br>" );
>                       writeOutput( "Right: " &
> getLabelObj().getPageRightMarginPoints() & "<br>" );
>                       writeOutput( "Top: " &
> getLabelObj().getPageTopMarginPoints() & "<br>" );
>                       writeOutput( "Bottom: " &
> getLabelObj().getPageBottomMarginPoints() & "<br>" );
>                       
>       
> createObject("java","com.lowagie.text.pdf.PdfWriter").getInsta
> nce(docume
> nt,pdfFile);
> 
>                       document.open();
> 
>                       document.add( createTable() );
>                       
>                       document.close();
>               }
>               
>               function createTable() {
>                       var row_counter = 0;
>                       var i = 0;
>                       var j = 0;
>                                       
>                       rowPaddingCell = createCell( " ",
> getLabelObj().getLabelVPaddingPoints() );
> 
>                       columnArray = arrayNew( 1 );
>                       for ( i = 1; i lte
> getLabelObj().getPageColumns(); i=i+1 ) {
>                               arrayAppend( columnArray,
> getLabelObj().getLabelWidthPoints() );
>                               
>                               if ( isAddPadding( i ) ) {
>                                       arrayAppend( columnArray,
> getLabelObj().getLabelHPaddingPoints() );
>                               }
>                       }
>                       
>                       table = createObject("java",
> "com.lowagie.text.pdf.PdfPTable").init( arrayLen( columnArray ) );
>                       table.setWidthPercentage( columnArray,
> getLabelPage() );
>                                               
>                       for ( i=1; i lte getLabelQuery().recordCount;
> i=i+1 ) {
>                               table.addCell( createLabelCell( i ) );
>                               
>                               if ( isAddPadding( ( i ) ) ) {
>                                       table.addCell( createCell( "",
> getLabelObj().getLabelHeightPoints() ) );
>                               }
>                               
>                               // NEW ROW //
>                               if ( (NOT i mod
> getlabelObj().getPageColumns()) AND i lt 
> getLabelQuery().recordCount ) {
>                                       row_counter = row_counter + 1;
>                                       
>                                       if ( row_counter mod
> getlabelObj().getPageRows() ) {
>                                               for ( j = 1; j lte
> getLabelObj().getPageColumns(); j=j+1 ) {
>                                                       table.addCell(
> rowPaddingCell );
>                       
>                                                       if (
> isAddPadding( j ) ) {
>       
> table.addCell( rowPaddingCell );
>                                                       }
>                                               }
>                                       }
>                               }
> 
>                       }
> 
>                       // FINISH THE LAST ROW //
>                       if ( getLabelQuery().RecordCount mod
> getlabelObj().getPageColumns() ) {
>                               for (x = (getLabelQuery().RecordCount
> mod getlabelObj().getPageColumns())+1; x LTE
> getlabelObj().getPageColumns(); x=x+1) {
>                                       table.addCell( rowPaddingCell );
> 
>                                       if ( isAddPadding( x ) ) {
>                                               table.addCell(
> rowPaddingCell );
>                                       }
>                               }
>                       }
>                       
>                       return table;                   
> 
>               }
>               
>               function createLabelCell( col ) {
>                       var stringValue = "";
> 
>                       stringValue = stringValue &
> variables.labelQuery["can_firstname"][col];
>                       stringValue = stringValue & " ";
>                       stringValue = stringValue &
> variables.labelQuery["can_lastname"][col];
>                       stringValue = stringValue & chr(13);
>                       stringValue = stringValue & chr(10);
>                       
>                       stringValue = stringValue &
> variables.labelQuery["can_address1"][col];
>                       if ( len( variables.labelQuery["can_apt"][col] )
> ) {
>                               stringValue = stringValue & ", Apt " &
> variables.labelQuery["can_apt"][col];
>                       }
>                       stringValue = stringValue & chr(13) & chr(10);
>                       if ( len(
> variables.labelQuery["can_address2"][col] ) ) {
>                               stringValue= stringValue &
> variables.labelQuery["can_address2"][col] & chr(13) & chr(10);
>                       }
>                       stringValue = stringValue &
> variables.labelQuery["can_city"][col] & ", "  &
> variables.labelQuery["can_state"][col] & "  " &
> variables.labelQuery["can_zip"][col];
>                       
>                       return createCell( stringValue,
> getLabelObj().getLabelHeightPoints() );
>               }
> 
>               function createCell( stringValue, fixedHeight ) {
>                       cell = createObject("java",
> "com.lowagie.text.pdf.PdfPCell").init( createPhrase( stringValue,
> "Helvetica", getLabelObj().getFontSize(), "plain", 
> createObject("java",
> "java.awt.Color").init( JavaCast("int", 0), JavaCast("int", 0),
> JavaCast("int", 0) ), 1.0) );
>                       cell.setFixedHeight( fixedHeight );
>                       cell.setBorderWidth( 0.0 );
>                       return cell;
>               }
>       
>               function createPhrase( stringValue, family, size, style,
> color, lineheight ) {
>                       font = createObject("java",
> "com.lowagie.text.Font").init();
>                       font.setFamily( family );
>                       font.setSize( size );
>                       font.setStyle( style );
>                       font.setColor( color );
>                               
>                       return createObject("java",
> "com.lowagie.text.Phrase").init( font.leading( lineheight ), JavaCast(
> "String", stringValue), font);
>               }
>               
>               function isAddPadding( col ) {
>                       if ( ( col mod getLabelObj().getPageColumns() )
> AND getLabelObj().getLabelHPaddingPoints() ) {
>                               return true;
>                       } else {
>                               return false;
>                       }
>               }
>       </cfscript>
> 
> -----Original Message-----
> From: Paulo Soares [mailto:[EMAIL PROTECTED]
> Sent: Sunday, June 20, 2004 5:14 AM
> To: Mike Steele; [EMAIL PROTECTED]
> Subject: Re: [iText-questions] PdfPTable and Horizontal Points
> 
> 
> Show us your code.
> 
> Best Regards,
> Paulo Soares
> 
> ----- Original Message ----- 
> From: "Mike Steele" <[EMAIL PROTECTED]>
> To: <[EMAIL PROTECTED]>
> Sent: Friday, June 18, 2004 18:46
> Subject: [iText-questions] PdfPTable and Horizontal Points
> 
> 
> Has anyone else noticed that in order to get PdfPCell widths in points
> to come out accurately that you have to modify the points by 1.18?
> 
> 
> -------------------------------------------------------
> This SF.Net email is sponsored by The 2004 JavaOne(SM) Conference
> Learn from the experts at JavaOne(SM), Sun's Worldwide Java Developer
> Conference, June 28 - July 1 at the Moscone Center in San 
> Francisco, CA
> REGISTER AND SAVE! http://java.sun.com/javaone/sf Priority 
> Code NWMGYKND
> _______________________________________________
> iText-questions mailing list
> [EMAIL PROTECTED]
> https://lists.sourceforge.net/lists/listinfo/itext-questions
> 


-------------------------------------------------------
This SF.Net email sponsored by Black Hat Briefings & Training.
Attend Black Hat Briefings & Training, Las Vegas July 24-29 -
digital self defense, top technical experts, no vendor pitches,
unmatched networking opportunities. Visit www.blackhat.com
_______________________________________________
iText-questions mailing list
[EMAIL PROTECTED]
https://lists.sourceforge.net/lists/listinfo/itext-questions

Reply via email to