0
votes

I am trying to create a pdf with fixed width using itext. The pdf width is fixed to 85.0f.The pdf has a border of say 'border' width For this I create a table with width 85.0f-(border*2) and add data to it.Then I find its height and width and create a Rectangle with width 85.0f and height as heightOftable+(border*2) However the pdf I get has a thin spacing between the border and the table. I want to remove that spacing. I have already tried setting padding to zero etc.However that thin spacing is still there. My code is:

public class Template2100_2 {

     public static final String DEST = "D:\\uploads\\abc.pdf";
     public static void main(String[] args) throws DocumentException, IOException {
         Template2100_2 t=new Template2100_2();
         Font font = new Font(Font.FontFamily.TIMES_ROMAN, 7f, Font.NORMAL);

         t.createPdfOrdinary("The Commerce Ministry has received several references from various stakeholders seeking clarification", "E-mail ID\n"
                + " 1854213265\n"
                + " Ph: 12547869",1,font);
    }
    public void createPdfOrdinary(String chunk1Text, String chunk2Text,Integer border,Font font) throws DocumentException, IOException
    {


        Document document = new Document(PageSize.A4, 0, 0, 0, 0);
        PdfPTable table = new PdfPTable(1);
        table.setTotalWidth(85f-(border*2));
        table.setLockedWidth(true);
        //table.setWidthPercentage(100);

        table.setSpacingBefore(0f);
        table.setSpacingAfter(0f);


        PdfPCell cell;
        cell = new PdfPCell(new Phrase(chunk1Text));
        cell.setBorder(0);
        cell.setPadding(0);
        cell.setColspan(1);
        cell.isUseBorderPadding();
        cell.setPaddingBottom(1f);
        cell.setPaddingRight(-1f);
        cell.setBackgroundColor(BaseColor.PINK);
        cell.setHorizontalAlignment(Element.ALIGN_CENTER);
        table.addCell(cell);

        cell = new PdfPCell(new Phrase(chunk2Text));
        cell.setBorder(0);
        cell.setPadding(0);
        cell.setPaddingBottom(1f);
        cell.setHorizontalAlignment(Element.ALIGN_CENTER);
        cell.setRowspan(1);
        cell.setBackgroundColor(BaseColor.PINK);
        cell.isUseBorderPadding();



        table.addCell(cell);
        FileOutputStream fos=new FileOutputStream(DEST);
        PdfWriter writer = PdfWriter.getInstance(document, fos);
        document.open();
        document.add(table);
        Float height=table.getTotalHeight();
        System.out.println(table.getTotalHeight()+"::"+table.getTotalWidth());
        document.close();
        fos.close();
        writer.close();
        Rectangle pagesize = new Rectangle(85f, height+(border*2));
        pagesize.setBorder(Rectangle.BOX);
        pagesize.setBorderWidth(border);
        document = new Document(pagesize, border, border,border, border);
        fos=new FileOutputStream(DEST);
        writer = PdfWriter.getInstance(document, fos);
        document.open();
        document.add(table);

        document.close();
        fos.close();
        writer.close();
    }
}

Can anybody suggest where the thin spacing between the border and table is coming from and how can i remove it.Thanks.

2
There's too much code in your sample. Remove the parts that aren't relevant to your question. Someone willing to answer your question might decide that you're asking too much time for him or her to disentangle what is relevant from what is irrelevant.Bruno Lowagie

2 Answers

1
votes

Your code is too complex. The people who will have to maintain the code after you have written it, won't like that.

If this is the result you want:

enter image description here

Then this is the code you need:

public void createPdf(String dest) throws IOException, DocumentException {
    float width = 85;
    float border = 1;
    PdfPTable table = new PdfPTable(1);
    table.setTotalWidth(width - 2 * border);
    table.setLockedWidth(true);
    table.addCell("Some text in some cell.");
    table.addCell("In some very narrow table");
    Rectangle rect = new Rectangle(width, table.getTotalHeight() + 2 * border);
    Document document = new Document(rect, border, border, border, border);
    PdfWriter.getInstance(document, new FileOutputStream(dest));
    document.open();
    document.add(table);
    document.close();
}

There is no need to create the same document twice. Just create the document using the correct dimensions from the start.

-1
votes

Now I am able to remove that spacing using:

pagesize.setUseVariableBorders(true);