gpt4 book ai didi

c# - iTextSharp绝对定位(GridView)

转载 作者:行者123 更新时间:2023-12-03 04:16:25 24 4
gpt4 key购买 nike

我在使用 iTextSharp 时遇到重叠表格的问题。

我有多个表(来自 gridviews),我想使用 iTextSharp 将其写入 pdf。

我希望每个 table 之间只有 10px 的间隙(垂直方向),并且 table 的高度总是不同的。

有人有一篇文章可以帮助我解决这个问题吗?或者有什么建议吗?绝对定位对我不起作用。

最佳答案

您可以将每个表格放入 iTextSharp.text.Paragraph 中,并使用 Paragraph 对象的 SpacingAfter 属性来创建间隙。

喜欢这个测试方法:

private static void DemoTableSpacing() {
using (FileStream fs = new FileStream("SpacingTest.pdf", FileMode.Create)) {

Document doc = new Document();
PdfWriter.GetInstance(doc, fs);
doc.Open();

Paragraph paragraphTable1 = new Paragraph();
paragraphTable1.SpacingAfter = 15f;

PdfPTable table = new PdfPTable(3);
PdfPCell cell = new PdfPCell(new Phrase("This is table 1"));
cell.Colspan = 3;
cell.HorizontalAlignment = 1;
table.AddCell(cell);
table.AddCell("Col 1 Row 1");
table.AddCell("Col 2 Row 1");
table.AddCell("Col 3 Row 1");
//table.AddCell("Col 1 Row 2");
//table.AddCell("Col 2 Row 2");
//table.AddCell("Col 3 Row 2");
paragraphTable1.Add(table);
doc.Add(paragraphTable1);

Paragraph paragraphTable2 = new Paragraph();
paragraphTable2.SpacingAfter = 10f;

table = new PdfPTable(3);
cell = new PdfPCell(new Phrase("This is table 2"));
cell.Colspan = 3;
cell.HorizontalAlignment = 1;
table.AddCell(cell);
table.AddCell("Col 1 Row 1");
table.AddCell("Col 2 Row 1");
table.AddCell("Col 3 Row 1");
table.AddCell("Col 1 Row 2");
table.AddCell("Col 2 Row 2");
table.AddCell("Col 3 Row 2");
paragraphTable2.Add(table);
doc.Add(paragraphTable2);
doc.Close();
}
}

这应该显示您可以做什么。尝试在第一个表中添加和删除行;您会看到两个表格之间的空间始终存在并且不会改变。

关于c# - iTextSharp绝对定位(GridView),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3825550/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com