iTextSharp绝对定位(GridView)

Kuk*_*koy 2 .net c# itextsharp

我在使用iTextSharp重叠表时遇到问题.

我有多个表(来自gridviews),我想用iTextSharp写入pdf.

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

有没有人有一篇我可以阅读的文章来帮助我解决这个问题?还是有什么建议?绝对定位对我不起作用.

Jay*_*ggs 6

您可以将每个表放入一个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();
    }
}
Run Code Online (Sandbox Code Playgroud)

这应该表明你可以做什么.尝试在第一个表中添加和删除行; 你会看到两个表之间的空间总是在那里并且不会改变.