3

I'm using iText 5.3.4 with Eclipse Kepler. I have a bill table, normally it would not have more than one page, but for testing pourposes i've been checking it's behaviour and I'm facing a weird problem, table width changes when page changes:

table width changes!

My table definition:

PdfPTable tableFactura = new PdfPTable(4);
tableFactura.setWidthPercentage(80f);
tableFactura.setWidths(new int[]{55, 15, 15, 15});
tableFactura.setKeepTogether(false);
tableFactura.setHeaderRows(1);

If i define different width the problem still persist, but with another dimensions.

The table is filled in a for-loop with PdfCell and Paragraph inside, nothing complex:

// ONE of the CELLS of each line
cell = new PdfPCell(new Paragraph(factura.get("descripcion_linea" + numeroLinea), tinyNormal));
cell.setHorizontalAlignment(Element.ALIGN_LEFT);
cell.setPaddingLeft(10f);
cell.setPaddingTop(5f);
cell.setBorder(Rectangle.LEFT);
tableFactura.addCell(cell);

My document is standard, Document document = new Document();, i tried including also document.setPageSize(PageSize.A4); but nothing changes. As you can see in the image, i've also tried without content in cells.

Any idea why is this happening?

UPDATE1: as suggested by @Bruno here is Link To File.

UPDATE2: here is a piece of code "simulating" what I am doing, but issue is NOT happening

Jordi Castilla
  • 26,609
  • 8
  • 70
  • 109
  • This looks like the print margin for double sided print (Mirror Margin), when the margins are flipped depending on the page being even or odd. – cheffe Mar 31 '15 at 14:24
  • if you look a the image, you can see issue is happening in both sides... – Jordi Castilla Mar 31 '15 at 14:25
  • 1
    What happens if you add a third page? Does the width grow more or does it have the same size as the second page? – Ian2thedv Mar 31 '15 at 14:27
  • Without a [SSCCE](http://sscce.org), you should not expect an answer, even if you open a bounty on it. We just can't reproduce the problem. – Bruno Lowagie Mar 31 '15 at 14:31
  • @Ian2thedv if there are more pages size stays same as 2nd page... – Jordi Castilla Mar 31 '15 at 14:56
  • @BrunoLowagie i've tried to reproduce outside the main program, but error does not appear, that's why I started bounty, because I know is not problem of `iText` is something I'm doing wrong, but I can't figure out what... I expect somebody has found same issue and knows what can be wrong in my background.... – Jordi Castilla Mar 31 '15 at 14:57
  • Maybe if you share your PDF so that we can take a look inside. For instance: we don't know if the page got smaller or if the table got bigger (I assume the latter). It's also odd to me that the table doesn't have a bottom border. Also what happens if you remove the `setKeepTogether()`. I think we should delete this method from iText. That could very well be the culprit. Why is it there? It's not documented anywhere is it? – Bruno Lowagie Mar 31 '15 at 15:02
  • @BrunoLowagie updated question with pdf file. Also nothing changes when `setKeepTogether(false)` line is deleted... I thought it was useful when a table gets splitted in 2 pages to mantain a continuity, it does not work for that? You suggest to not use it? – Jordi Castilla Mar 31 '15 at 15:14
  • 1
    We no longer use it. – Bruno Lowagie Mar 31 '15 at 15:19
  • @JordiCastilla is it possible for you to post all your code so that i can run it on my local pc? – kucing_terbang Apr 01 '15 at 07:40
  • @kucing_terbang if you read all comments you will find: *i've tried to reproduce outside the main program, but error does not appear, that's why I started bounty, because I know is not problem of iText* – Jordi Castilla Apr 01 '15 at 07:44
  • @JordiCastilla oh yes i have read all the comments but I'm just wondering if there is some thing in your code that you didn't post like probably you somehow change the width? – kucing_terbang Apr 01 '15 at 07:48
  • [check this... might help](http://stackoverflow.com/questions/22094190/pdfptable-cell-width) – Fahim Parkar Apr 01 '15 at 09:01

2 Answers2

1

I never used iText, but I think the problem is at below lines.

tableFactura.setWidthPercentage(80f);
tableFactura.setWidths(new int[]{55, 15, 15, 15});

Can you try two cases below and let me know how it goes?

Case 1 : Setting width 100%

tableFactura.setWidthPercentage(100f);
tableFactura.setWidths(new int[]{55, 15, 15, 15});

Case 2 : Setting widths as per 80%

tableFactura.setWidthPercentage(80f);
tableFactura.setWidths(new int[]{50, 10, 10, 10});

If not, check this this question might help.

Community
  • 1
  • 1
Fahim Parkar
  • 30,974
  • 45
  • 160
  • 276
  • Can you just go with case 1 and see both pages? still both pages giving this error? – Fahim Parkar Apr 01 '15 at 08:03
  • different size, but same error. *For if the flys I tested it again*, size of first page table is bigger than expected and second page goes out of margins... – Jordi Castilla Apr 01 '15 at 08:03
  • fakim, [your link](http://stackoverflow.com/questions/22094190/pdfptable-cell-width) gives me the clue to fix the problem... B-D if you're agree I will modify your answer with the fix and so I can give to you bounty – Jordi Castilla Apr 01 '15 at 10:57
  • @JordiCastilla : what edit you make? Update answer and put what change made it right... – Fahim Parkar Apr 02 '15 at 09:51
  • i could not update your question with all my answer, edits would be rejected by SO rules, so, I put your comment at the end of your answer, give to you the bounty and answered my own question with all important info I used to solve it... Imagine, the little edit I've done (with explaining comment about why this change) has one downvote... so is the best way i found to explain what happened and why you deserve bounty ;) – Jordi Castilla Apr 02 '15 at 09:56
1

Well, I finally solved it. Thanks to @BrunoLowagie and @Fahim Parkar.

Will make a quick explanation of all my modifications according to all suggestions:

First of all and according @Bruno comments, first I deleted unused method PdfPTable::setKeepTogether(boolean);.

Second, and according to this question, posted by @Fahim (thanks!). We can force table's size by PdfPTable::setLockedWitdth(boolean) before setting the TotalWidth.

PdfPTable tableFactura = new PdfPTable(4);
tableFactura.setLockedWidth(true);
tableFactura.setTotalWidth(425f);
tableFactura.setWidths(new int[]{55, 15, 15, 15});

This allows table to have same size in all pages... but not all done, to avoid weird behaviour in the last row and in page breaks (with single or multiple pages) I must include:

tableFactura.setSkipLastFooter(true);

Third: Now table limits are correct, just need to correct page breaks, for that I defined footer rows.

tableFactura.setHeaderRows(2);
tableFactura.setFooterRows(1);

NOTE: according PdfPTable::setFooterRows():

Sets the number of rows to be used for the footer. The number of footer rows are subtracted from the header rows. So in this example row 1 will be footer and row 2 will be header.

Fourth: simply adjusting margins, borders and paddings to my needs I get the exact result I desired.

TA DA! :)

Community
  • 1
  • 1
Jordi Castilla
  • 26,609
  • 8
  • 70
  • 109