Hi,
 
Here is a standalone class using V5.0.1 which shows the problem. Shows
the same sort of behavior as in V1.4.5.
 
package testtables;
 
import java.io.ByteArrayOutputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import com.itextpdf.text.Document;
import com.itextpdf.text.DocumentException;
import com.itextpdf.text.Font;
import com.itextpdf.text.PageSize;
import com.itextpdf.text.Paragraph;
import com.itextpdf.text.Rectangle;
import com.itextpdf.text.pdf.PdfPCell;
import com.itextpdf.text.pdf.PdfPTable;
import com.itextpdf.text.pdf.PdfWriter;
 
/**
 iText version 5.0.1
 Build a table, place it in a document having a single page. 
 Size the page/doc according to the size of the table. 
 If the table has wrapping in cells, see if that causes multiple pages.
*/
public final class TestTables {
  
  public static void main(String... aArgs) throws FileNotFoundException,
DocumentException{
    System.out.println("Start.");
    TestTables test = new TestTables();
    System.out.println("Done.");
  }
  
  public TestTables() throws FileNotFoundException, DocumentException{
    buildDocWithTable();
  }
  
  // PRIVATE 
  private PdfPTable fPdfPTable;
  
  private static final int FONT_SIZE = 10;
  private static final  float SPACING = 10f;
  private static final float CELL_PADDING = 2.0f;
  
  private void buildDocWithTable() throws FileNotFoundException,
DocumentException {
    Document document = new Document(PageSize.LETTER); //this size gets
overwritten below
    PdfWriter writer = PdfWriter.getInstance(document, new
FileOutputStream("C:\\Temp4\\test.pdf"));
    document.addTitle("Preview Table");
    document.addSubject("Preview Table");
    fPdfPTable = buildTableContent(); //has side effect regarding
height/width
    //resize according to table size
    document.setPageSize(computePageSizeFrom(fPdfPTable));
    document.open(); 
    document.add(fPdfPTable);
    document.close();
    System.out.println("Page Size from document: "  +
document.getPageSize());
  }
  
  private Rectangle computePageSizeFrom(PdfPTable aTable) {
    float width = PageSize.LETTER.getWidth();
    float height = aTable.getTotalHeight() + 72.0f; 
    
    //cross-check: calc total height in a different way
    System.out.println("Letter height: " + height + " num rows: " +
aTable.getRows().size()); 
    System.out.println("Num rows from table: " +
aTable.getRows().size()); 
    float totalRowHeight = 0; 
    for(int idx = 0; idx < aTable.getRows().size(); ++idx){
      totalRowHeight = totalRowHeight + aTable.getRowHeight(idx);
    }
    System.out.println("Sum of row heights: " + totalRowHeight); //74
for 5 rows
    
    Rectangle result = new Rectangle(width, height);
    System.out.println("Returning page size, height: " + height);
    return result;
  }
 
  private PdfPTable buildTableContent() {
    PdfPTable result = new PdfPTable(new float[]{100.0f, 50.0f});
    result.setWidthPercentage(100.0f);
    result.setHeaderRows(1); // column headers only
    result.setSpacingAfter(SPACING);
    addHeaderCell(result, "Uno due tre quattro cinque sei sette otto
nove dieci ondeci dodeci tredici quattordici quindici sedici");
    addHeaderCell(result, "Two Buckly my scarpa. Il mie chiave.");
 
    addCellContent(result, "A");
    //OK:
    //addCellContent(result, "Basld;f jas as;ldkjf as;ldf ;laskjdf
asdf;lkj asdlfj ;laksjdf;lkjas ;lfkjas asdl as;lkdj ;laskjdf l;kasdj
;lkajsd f;las");
    //Not OK - 2 pages
    addCellContent(result, "Basld;f jas as;ldkjf as;ldf ;laskjdf
asdf;lkj asdlfj ;laksjdf;lkjas ;lfkjas asdl as;lkdj ;laskjdf l;kasdj
;lkajsd f;las asdkfjlas daslkdf l;kasjdfl; asdlfjal;skjdf l;kasjdl
a;slkjd");
 
    result.setTotalWidth(PageSize.LETTER.getWidth());
    result.calculateHeightsFast();
    System.out.println("Finished buildTableContent");
    return result;
  }
  
  private void addHeaderCell(PdfPTable aTable, String aText) {
    PdfPCell headerCell = getRenderedCellTextMode(aText);
    aTable.addCell(headerCell);
  }
  
  private PdfPCell getRenderedCellTextMode(String aText) {
    Font font = new Font(Font.getFamily("Courier"), FONT_SIZE);
    Paragraph para = new Paragraph(aText, font);
    PdfPCell result = new PdfPCell(para);
    result.setPadding(0.0f);
    result.setPaddingBottom(CELL_PADDING);
    result.setPaddingLeft(CELL_PADDING);
    result.setPaddingRight(CELL_PADDING);
    return result;
  }
  
  private void addCellContent(PdfPTable aTable, String aText) {
    PdfPCell cell = getRenderedCellTextMode(aText);
    aTable.addCell(cell);
  }
}
 
 


________________________________

From: Alexis Pigeon [mailto:pigeon.ale...@gmail.com] 
Sent: August 3, 2011 4:05 AM
To: Post all your questions about iText here
Subject: Re: [iText-questions] Computing page size from Table - iText
v1.4.5


Hi John,


On 2 August 2011 18:13, O'Hanley, John <John.O'han...@cra-arc.gc.ca
<mailto:john.o%27han...@cra-arc.gc.ca> > wrote:


        
        Hi, 
        


First of all, you mention you use iText 1.4.5. This version is almost 5
years old (september 2006), and no free support is provided on this list
to such an old version. The codebase underwent a major refactoring since
then, a huge amount of funcitonnalities were added, some were removed.
You might as well be on your own here...



        I am creating a pdf containing a table. I need to size the pdf
such that:
        
         - the pdf uses a single page only, regardless of the height of
the table
         - no extraneous white space appears at the bottom
         
        I want the pdf to contain the table, the whole table, and
nothing but the table, on *a single page*. (I'm actually creating an
image of the resulting pdf, but that's likely irrelevant in this
context.)
         
        It usually works. However, when there is *line-wrapping* of wide
text in a cell(s), then then it sometimes succeeds, and sometimes fails.
It seems to fail after the amount of wrapping exceeds some unknown
amount. It fails in the sense that the pdf is spread over 2 pages, not
1.


What do you mean by "amount of wrapping"?



        
        The pertinent sections of my Shakespearean-yet-oh-so-faulty code
look like this:


This is not a standalone running piece of code. Should anyone be eager
to help you with such an old release, he or she wouldn't really be able,
since there is no way to quicly reproduce the problem you noticed.

[...]



        Do you know where I'm going wrong? I've whacked away at it in
the obvious ways, but the problem persists..
        
        Any help appreciated, 


I don't know if this class already existed in version 1.4.5, but you
could give a try using ColumnText.

Cheers,
alexis

------------------------------------------------------------------------------
BlackBerry&reg; DevCon Americas, Oct. 18-20, San Francisco, CA
The must-attend event for mobile developers. Connect with experts. 
Get tools for creating Super Apps. See the latest technologies.
Sessions, hands-on labs, demos & much more. Register early & save!
http://p.sf.net/sfu/rim-blackberry-1
_______________________________________________
iText-questions mailing list
iText-questions@lists.sourceforge.net
https://lists.sourceforge.net/lists/listinfo/itext-questions

iText(R) is a registered trademark of 1T3XT BVBA.
Many questions posted to this list can (and will) be answered with a reference 
to the iText book: http://www.itextpdf.com/book/
Please check the keywords list before you ask for examples: 
http://itextpdf.com/themes/keywords.php

Reply via email to