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.