Reputation: 3571
In iTextSharp, how to set the space between two cells(PdfPCell)
Code:
var doc = new Document();
PdfWriter.GetInstance(doc, new FileStream("C:/Doc1.pdf", FileMode.Create));
doc.Open();
PdfPTable table = new PdfPTable(1);
PdfPCell cell1 = new PdfPCell(new Phrase("Cell1"));
cell1.Colspan = 1;
table2.AddCell(cell1);
PdfPCell cell2 = new PdfPCell(new Phrase("Cell2"));
cell2.Colspan = 1;
table2.AddCell(cell2);
doc.Add(table);
System.Diagnostics.Process.Start("C:/Doc1.pdf");
Here, two cells are created(cell2's left border overlapped with cell1's border right). But I need a little space between 2 cells.
Upvotes: 2
Views: 5720
Reputation: 3571
I achieved by setting width
for the columns in the table as following.
table.SetWidths(new float[] { 1f, 0.1f, 1f });
PdfPCell cell1 = new PdfPCell(new Phrase("Cell1"));
table.AddCell(cell1);
//dummy cell created to have an empty space with width `0.1f` which was declared //above.
PdfPCell cell2 = new PdfPCell(new Phrase(""));
table.AddCell(cell2);
PdfPCell cell3 = new PdfPCell(new Phrase("Cell3"));
table.AddCell(cell3);
Upvotes: 1
Reputation: 2084
Play around a bit with cellpadding. Like this:
var doc = new Document();
PdfWriter.GetInstance(doc, new FileStream("C:/Doc1.pdf", FileMode.Create));
doc.Open();
PdfPTable table = new PdfPTable(1);
PdfPCell cell1 = new PdfPCell(new Phrase("Cell1"));
cell1.Colspan = 1;
cell.PaddingRight = 20f; //Here you can set padding (Top, Bottom, Right, Left)
table2.AddCell(cell1);
PdfPCell cell2 = new PdfPCell(new Phrase("Cell2"));
cell2.Colspan = 1;
table2.AddCell(cell2);
doc.Add(table);
System.Diagnostics.Process.Start("C:/Doc1.pdf");
Upvotes: 1