Hey,

I seem to be having another issue using PdfPTables.  As always I might just
be doing something wrong, we'll see.

I'm building a list with PdfPCells which are added to a table as long as the
table has enough space.  The max space of the table is the max size of the
whole document.  So basically the document is the table.

The table itself is pretty straightforward.  It's a table with predetermined
width and one column.
When everything is built, the table needs to be increased in height until a
certain height is reachted.  What I do is count the height of all parts and
when that's done add an amount of whitespace to the padding of the footer.
But when I use a paragraph with spacingAfter less than -1.1f the heightcount
appears to be wrong.  During debug it seems fine, the table has the expected
length but when opening the document it appears to have been too large and
is placed on 2 pages.  If needed I'll attach the generated pdf, both the
correct one as the incorrect one.  But due to the large size of header and
footer pics I'm not doing that right away (2.7mb each)
I'll post some code but be aware that there is a high dependency on internal
business logic.

//This method gets invoked at least 15 times / document
public void createTextBody(String key,String titel,String suffix) throws
IOException, DocumentException {
        titel = StringUtils.unEscapeHtml(titel);               
        Font titelFont = new Font(vectoraLHFont,8,Font.BOLD);
        Font main = new Font(vectoraLHFont,6,Font.NORMAL);
        Font bodyFont = new Font(vectoraLHFont,6,Font.ITALIC);
        Font mainBold = new Font(vectoraLHFont,6,Font.BOLD);
        PdfPCell cell = new PdfPCell();

        Phrase titelPhrase = new Phrase(new Chunk(titel,titelFont));
        Paragraph titelPara = new Paragraph(titelPhrase);
//        titelPara.setSpacingAfter(-1f);
        cell.addElement(titelPara);

        for(int i=1; true ; i++){
            String regel = "lijn"+i+"."+suffix;
            String lijn = moduleItem.getITextField(regel);
            if(lijn != null){
                lijn = StringUtils.unEscapeHtml(lijn);          
                Paragraph mainTxt = new Paragraph(new Phrase(new
Chunk(lijn,main)));
                mainTxt.setSpacingAfter(-1.1f);  //increase this to -1.2f
and it happens.
//                mainTxt.setSpacingBefore(defaultSpacing);
                cell.addElement(mainTxt);
            }else {
                break;
            }
        }
        String body = moduleItem.getITextField("body."+suffix);
        body = StringUtils.unEscapeHtml(body);                 
        Paragraph italicBodyPart = new Paragraph(new Phrase(new
Chunk(body,bodyFont)));
//        italicBodyPart.setSpacingBefore(defaultSpacing);
        cell.addElement(italicBodyPart);

        String foot = moduleItem.getITextField("footer."+suffix);
        foot = StringUtils.unEscapeHtml(foot);                  
        final String code = "Code";
        final String op = "op";

        int codeInt = foot.indexOf(code);
        int opInt = foot.indexOf(op);
        String codeTxt = foot.substring(codeInt+code.length()+1,opInt-1);
        String opTxt = foot.substring(opInt+code.length()-1);
        String beforeCode = foot.substring(0,codeInt+code.length()+1);

        
        Phrase footerPhrase = new Phrase(new Chunk(beforeCode,main));
        footerPhrase.add(new Chunk(codeTxt,mainBold));
        footerPhrase.add(new Chunk(" "+op+" ",main));
        footerPhrase.add(new Chunk(opTxt,mainBold));

        Paragraph footer = new Paragraph(footerPhrase);

        footer.setSpacingBefore(Convert.mm2pt(0.8f));
        cell.addElement(footer);
        applyDefaultCellProps(cell);
        List<PdfPCell> list = elementen.get(key);
        list.add(cell);
    }

//constructs the document depending on the size of the list (put into a map)
constructed above.
public Map<String,List<PdfPCell>> createDocument(Map<String,List<PdfPCell>>
valueMap) throws DocumentException, FileNotFoundException {


        float totalSom = 0f;
        float gekniptOp = HEIGHT; //default volledige pagina

        PdfPTable mainTable = createNewTable();
        PdfPCell headerImageCell = createHeader(images[0]);

        PdfPCell footerImageCell = createFooter(images[1]);
        mainTable.addCell(headerImageCell);                             

        float cellSom = 0f;

        float footerHeight = 0f;
        PdfPTable tempTable = createNewTable();
        tempTable.addCell(footerImageCell);
        footerHeight = tempTable.getTotalHeight();
//        Font titelFont = new Font(vectoraLHFont,11,Font.BOLD,Color.WHITE);
        Font titelFont = new
Font(vectoraLHFont,11,Font.BOLD,BaseColor.WHITE);
        Font subTitFont = new Font(vectoraLHFont,9,Font.NORMAL,mainColour);
        PdfPCell headerCell = new PdfPCell();
        Paragraph headerPar = new Paragraph(new Phrase(new
Chunk(mainHeader,titelFont)));
        
        headerCell.addElement(headerPar);
        headerCell.setBackgroundColor(mainColour);
        applyHeaderCellProps(headerCell);

        mainTable.addCell(headerCell);                                     
//image + header in maintable, hoogte is gekend
        totalSom += mainTable.getTotalHeight();

        Map<String,List<PdfPCell>> forNext = new TreeMap<String,
List<PdfPCell>>();

        boolean overBounds = false;

        for(String subHeader:valueMap.keySet()){

            PdfPCell subCell = new PdfPCell();
            subCell.setCellEvent(new CellLine(colour));
            subCell.addElement(new Phrase(new
Chunk(subHeader.toUpperCase(),subTitFont)));  //in caps
            applySubCellProps(subCell);
            PdfPTable subCellDummyTable = createNewTable();
            subCellDummyTable.addCell(subCell);
            subCellDummyTable.addCell(valueMap.get(subHeader).get(0)); //we
rekenen de subheader samen met z'n eerste child

            float subCellSize = subCellDummyTable.getTotalHeight();
            float tempTotal = totalSom + subCellSize;

            if(tempTotal + cellSom + footerHeight <= HEIGHT && !overBounds)
{ //eerste size check @ subheader niveau
                mainTable.addCell(subCell);
                totalSom = tempTotal;                          //enkel
wanneer de eerste cel met de header geplaatst is is de totalsom verhoogd 
                
                Iterator<PdfPCell> cellIter =
valueMap.get(subHeader).iterator();
                for(int i = 0;cellIter.hasNext();i++){
                    PdfPCell bodyCell = cellIter.next();
                    PdfPTable dummyTable = createNewTable();
                    dummyTable.addCell(bodyCell);
                    if(i != 0) {
                        cellSom += dummyTable.getTotalHeight();
                    }

                    if(totalSom + cellSom + footerHeight <= HEIGHT){       
//tweede size check @ cell niveau

                        float nextSize = 0f;  //om te weten te komen wanneer
de laatste lijn niet mag getekend worden
                        if(cellIter.hasNext())
                        {
                            PdfPTable dummy = createNewTable();
                            List<PdfPCell> list = valueMap.get(subHeader);
                           
dummy.addCell(list.get(list.indexOf(bodyCell)+1)); //de volgende cel
                            nextSize = dummy.getTotalHeight();
                        }
                        //Dus wanneer we niet met de laatste cel uit de rij
zitten of de laatst getekende cel van de PDF
                        if(cellIter.hasNext() && totalSom + cellSom +
footerHeight + nextSize <= HEIGHT){
                            bodyCell.setCellEvent(new
CellLine(Color.BLACK));
                        }
                        mainTable.addCell(bodyCell);
                        cellIter.remove();                   //processed =
uit de lijst
                    }else {
                        cellSom -= dummyTable.getTotalHeight(); //cel te
veel
                        forNext.put(subHeader,valueMap.get(subHeader));
                        break;
                    }
                }

            }else {
                overBounds = true;
                forNext.put(subHeader,valueMap.get(subHeader));
            }


        }
        totalSom += cellSom;
                                                 The result is !always!
calculated correctly, also when the doc is spanned over 2 pages
        for(float f:pieceLengtes){
            
            float result = f - (totalSom + footerHeight);  
            if(result >= 0){
                gekniptOp = f;  //cuts the page to the size of the table.
                footerImageCell.setPaddingTop(result);
                mainTable.addCell(footerImageCell);
                break;
            }
        }

//                    validateLength(mainTable);

        Document document = new Document(new Rectangle(WIDTH, gekniptOp));
        ByteArrayOutputStream baos = new ByteArrayOutputStream();
        PdfWriter writer = PdfWriter.getInstance(document, baos);
        document.setMargins(0f,0f,0f,0f);   //eerst, daarna doc openen
        document.open();

        document.add(mainTable);
        document.close();
        writer.close();
        resultStreamList.add(baos);

        if(forNext.isEmpty()) return null;

        return forNext;
    }


I hope I've given enough info.  I can tell you with the business logic here
that's not a very easy thing to do.
-- 
View this message in context: 
http://itext-general.2136553.n4.nabble.com/Paragraph-negative-spacing-weirdness-in-PdfPTables-tp2245863p2245863.html
Sent from the iText - General mailing list archive at Nabble.com.

------------------------------------------------------------------------------
ThinkGeek and WIRED's GeekDad team up for the Ultimate 
GeekDad Father's Day Giveaway. ONE MASSIVE PRIZE to the 
lucky parental unit.  See the prize list and enter to win: 
http://p.sf.net/sfu/thinkgeek-promo
_______________________________________________
iText-questions mailing list
iText-questions@lists.sourceforge.net
https://lists.sourceforge.net/lists/listinfo/itext-questions

Buy the iText book: http://www.itextpdf.com/book/
Check the site with examples before you ask questions: 
http://www.1t3xt.info/examples/
You can also search the keywords list: http://1t3xt.info/tutorials/keywords/

Reply via email to