如何在iTextsharp中为表赋予绝对位置 [英] How to give an absolute position to a table in iTextsharp

查看:293
本文介绍了如何在iTextsharp中为表赋予绝对位置的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用iTextsharp创建表.我想要在页面中特定位置的表,并且正在使用 table.WriteSelectedRows(0,-1,300,300,pcb); 方法,但无法正常工作.这是我的代码.

I am using a iTextsharp to create a table.I want a table at particular position in the page and I am using a table.WriteSelectedRows(0, -1, 300, 300, pcb); method but its not working. Here is my code.

           using (FileStream fs = new FileStream("TableTest.pdf", FileMode.Create))
           {
            Document doc = new Document(PageSize.A4);
            PdfWriter writer =  PdfWriter.GetInstance(doc, fs);
            doc.Open();
            PdfContentByte pcb = writer.DirectContent;
            PdfPTable table = new PdfPTable(9);
            table.TotalWidth = 595f;

            // Hdeaer row.
            table.AddCell(GetCell("Header 1", 1, 2));
            table.AddCell(GetCell("Header 2", 1, 2));
            table.AddCell(GetCell("Header 3", 5, 1));
            table.AddCell(GetCell("Header 4", 1, 2));
            table.AddCell(GetCell("Header 5", 1, 2));

            // Inner middle row.
            table.AddCell(GetCell("H1"));
            table.AddCell(GetCell("H2"));
            table.AddCell(GetCell("H3"));
            table.AddCell(GetCell("H4"));
            table.AddCell(GetCell("H5"));

            // Bottom row.
            table.AddCell(GetCell("D1"));
            table.AddCell(GetCell("D2"));
            table.AddCell(GetCell("D3"));
            table.AddCell(GetCell("D4"));
            table.AddCell(GetCell("D5"));
            table.AddCell(GetCell("D6"));
            table.AddCell(GetCell("D7"));
            table.AddCell(GetCell("D8"));
            table.AddCell(GetCell("D9"));
            table.WriteSelectedRows(0, -1, 300, 300, pcb);
            doc.Close();
   }
   private PdfPCell GetCell(string text)
    {
        return GetCell(text, 1, 1);
    }
    private PdfPCell GetCell(string text, int colSpan, int rowSpan)
    {
        PdfPCell cell = new PdfPCell(new Phrase(text));
        cell.HorizontalAlignment = 1;
        cell.Rowspan = rowSpan;
        cell.Colspan = colSpan;

        return cell;
    }

推荐答案

在向表中添加任何内容之前,请尝试编写表.页面上什么也没有显示是正常的.在代码中向下移动writeSelectedRows():

You try to write the table before you've added any content to the table. It is normal that nothing is shown on the page. Move writeSelectedRows() down in your code:

PdfPTable table = new PdfPTable(9);
table.TotalWidth = 595;

// there isn't any content in the table: there's nothing to write yet

// Header row.
table.AddCell(GetCell("Header 1", 1, 2));
table.AddCell(GetCell("Header 2", 1, 2));
table.AddCell(GetCell("Header 3", 5, 1));
table.AddCell(GetCell("Header 4", 1, 2));
table.AddCell(GetCell("Header 5", 1, 2));

// Inner middle row.
table.AddCell(GetCell("H1"));
table.AddCell(GetCell("H2"));
table.AddCell(GetCell("H3"));
table.AddCell(GetCell("H4"));
table.AddCell(GetCell("H5"));

// Now we've added 2 rows: two rows will be shown:

table.WriteSelectedRows(0, -1, 300, 300, pcb);

请注意,我更改了表中的列数,因为您仅在第一行添加了5行,在第二行添加了5行.请注意,不完整的行不会被显示,因此这可能是您的代码未达到预期效果的另一个原因.

Note that I've changed the number of columns in the table, because you're only adding 5 rows for the first row, and five rows for the second row. Note that incomplete rows aren't rendered, so that may be another reason why your code doesn't do what you expect.

最后,您应该删除:

doc.Add(table);

这会将表格添加到页面中光标的当前位置.那可能不是您想要的.

This adds the table at the current position of the cursor in the page. That's probably not where you want it.

这篇关于如何在iTextsharp中为表赋予绝对位置的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆