如何在两个单元格之间设置 space

How to set space between two cells

iTextSharp, how to set the space between two cells(PdfPCell)

代码:

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");

这里创建了两个单元格(cell2 的左边框cell1 的右边框 重叠)。但是我需要在 2 个单元格之间有一点 space

尝试一下 cellpadding。像这样:

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");

我通过为 table 中的列设置 width 来实现,如下所示。

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);