行或嵌套?创建一个表itextsharp [英] Rowspan or nested? Create a table itextsharp

查看:205
本文介绍了行或嵌套?创建一个表itextsharp的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直在搜索如何合并两个单元格,我发现了两个答案Rowspan和Nested。我不能用这两个函数来创建表格,因为我不知道如何在开始和结束时合并单元格。我一直在尝试很多方法,但是这让我疯狂。



我想要的表格有9列和3行。所以,我希望表格看起来像:

 
|标题1 |标题2 |标题3 |标题4 |标题5 |
| | | H1 | H2 | H3 | H4 | H5 | | |
| D1 | D2 | D3 | D4 | D5 | D6 | D7 | D8 | D9 |

标题1:cs = 1,rs = 2。
标题2:cs = 1,rs = 2。
标题3:cs = 5,rs = 1。
H1:cs = 1,rs = 1。
H2:cs = 1,rs = 1。
H3:cs = 1,rs = 1。
H4:cs = 1,rs = 1。
H5:cs = 1,rs = 1。
标题4:cs = 1,rs = 2。
标题5:cs = 1,rs = 2。



cs:colspan,rs:rowspan。

标题3包含H1, H2,H3,H4,H5。

我觉得这个解决方案很简单,但是我找不到。我希望你能理解哪个问题是因为我现在还不能上传图片。

解决方案

有几个方法可以做你想做什么。由于你的表格结构不是太复杂,也许最简单的方法是用9列创建PdfPTable,并在创建行时使用每个单元格的Rowspan和Colspan属性一次一个地添加单元格。



下面的代码将创建一个与您所描述的完全一致的表:

  private void CreatePdf(){ 
using(FileStream fs = new FileStream(TableTest.pdf,FileMode.Create)){

Document doc = new Document(new iTextSharp.text.Rectangle(800f,800f)) ;
PdfWriter.GetInstance(doc,fs);
doc.Open();

PdfPTable表= new PdfPTable(9);

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

//内部中间行。
table.AddCell(GetCell(H1));
table.AddCell(GetCell(H2));
table.AddCell(GetCell(H3));
table.AddCell(GetCell(H4));
table.AddCell(GetCell(H5));

//底部行。
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));

doc.Add(table);
doc.Close();

$ b 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.Horizo​​ntalAlignment = 1;
cell.Rowspan = rowSpan;
cell.Colspan = colSpan;

return cell;





$ b

正如你所看到的,我已经用辅助方法包装了PdfPCells的构造,允许你设置每个单元格的文本内容和Rowspan和Colspan属性。

如果你还没有这样做,我建议你在iTextSharp表格上看看这个教程: / p>

iTextSharp - 介绍表格


I've been searching about how to merge two cells and I've found two answers Rowspan and Nested. I can't make my table with those two functions because I don't know how to merge cells at the begining and at the end. I've been trying many ways but this is driving me crazy.

The table I want to make has 9 columns and 3 rows. So, I want the table looks like:

| Header 1 | Header 2 |       Header 3         | Header 4 | Header 5 |  
|          |          | H1 | H2 | H3 | H4 | H5 |          |          |  
|    D1    |    D2    | D3 | D4 | D5 | D6 | D7 |    D8    |    D9    |

Header 1: cs=1, rs=2.
Header 2: cs=1, rs=2.
Header 3: cs=5, rs=1.
      H1: cs=1, rs=1.
      H2: cs=1, rs=1.
      H3: cs=1, rs=1.
      H4: cs=1, rs=1.
      H5: cs=1, rs=1.
Header 4: cs=1, rs=2.
Header 5: cs=1, rs=2.

cs:colspan, rs:rowspan.

Header 3 contains H1, H2, H3, H4, H5.

I think the solution is pretty easy but I can't find it. I hope you can understand Which the problem is because I can't upload images yet.

解决方案

There are a couple ways to do what you want to do. Since your table structure isn't overly complex perhaps the easiest approach is to create your PdfPTable with nine columns and add your cells one at a time using each cell's Rowspan and Colspan properties as you create rows.

The code below will create a table laid out exactly as you described:

    private void CreatePdf() {
        using (FileStream fs = new FileStream("TableTest.pdf", FileMode.Create)) {

            Document doc = new Document(new iTextSharp.text.Rectangle(800f, 800f));
            PdfWriter.GetInstance(doc, fs);
            doc.Open();

            PdfPTable table = new PdfPTable(9);

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

            doc.Add(table);
            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;
    }

As you can see I've wrapped the construction of PdfPCells in helper methods that allow you to set each cell's text content and Rowspan and Colspan properties.

If you haven't already done so, I suggest you check out this tutorial on iTextSharp tables:

iTextSharp - Introducing Tables

这篇关于行或嵌套?创建一个表itextsharp的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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