Remove Margin Left and Right of a table iText java

10,638

By default the table drawn by a PdfPTable object only fills 80% of the width of the page content area width. You can change this ratio using the PdfPTable method

/**
 * Sets the width percentage that the table will occupy in the page.
 *
 * @param widthPercentage the width percentage that the table will occupy in
 * the page
 */
public void setWidthPercentage(final float widthPercentage)

to avoid those extra margins.

Furthermore, a PdfPTable instance added to a Document respects the document margin values. To use (nearly) the whole page as page content area for a table to fill, you have to reduce the document margins to (nearly) 0 using the constructor with 5 arguments

/**
 * Constructs a new <CODE>Document</CODE>-object.
 *
 * @param pageSize the pageSize
 * @param marginLeft the margin on the left
 * @param marginRight the margin on the right
 * @param marginTop the margin on the top
 * @param marginBottom the margin on the bottom
 */
public Document(Rectangle pageSize, float marginLeft, float marginRight, float marginTop, float marginBottom)

or the setter

/**
 * Sets the margins.
 *
 * @param marginLeft the margin on the left
 * @param marginRight the margin on the right
 * @param marginTop the margin on the top
 * @param marginBottom the margin on the bottom
 * @return a <CODE>boolean</CODE>
 */
public boolean setMargins(float marginLeft, float marginRight, float marginTop, float marginBottom)
Share:
10,638
Dwhitz
Author by

Dwhitz

Apparently, this user prefers to keep an air of mystery about them.

Updated on June 09, 2022

Comments

  • Dwhitz
    Dwhitz about 2 years

    I have a problem with the fixed margin on left and right of a table.

    enter image description here

    I want remove that margin and use all the sheet without margin or padding. How i can do ?

    I've just tryed this but doesn't work for me:

    cell.setPaddingLeft(0);
    cell.setBorderWidthLeft(0);
    cell.setLeft(0);
    

    This works for me, but the borders of the cell , don't follow the text (Look at the table in bottom)

    cell.setPaddingLeft(-50);
    

    This is a part of my code :

    Font fontStandard = FontFactory.getFont("Arial", 8);
    int w[] = { 50, 50 };
    PdfPTable tableInner = new PdfPTable(5);
    tableInner.setWidths(w);
    
    cell = new PdfPCell(new Phrase("PADDING -50", fontStandard));
    cell.setHorizontalAlignment(PdfPCell.ALIGN_LEFT);
    /******/
    //cell.setPaddingLeft(0);
    //cell.setBorderWidthLeft(0); 
    //cell.setLeft(0); 
    cell.setPaddingLeft(-50);
    /******/
    tableInner.addCell(cell);
    document.add(tableInner);
    

    This is what i want. enter image description here