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

时间:2016-03-24 12:16:08

标签: c# .net c#-4.0 itextsharp itext

iTextSharp中,如何设置两个单元格之间的空间(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' s border right 重叠)。但我需要在2个单元格之间留一点空间

2 个答案:

答案 0 :(得分:1)

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

答案 1 :(得分:1)

我通过为表中的列设置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);