PdfPTable我们可以根据值用IF语句一个接一个地添加多个表吗



我对PDFtable和多个表有问题。它把前两张桌子画得很好,但不能超过两张。我需要它来处理尽可能多的桌子。以前有人有这个要求吗?

string firstN="";
string firstage="";
string firstNV= someValue;
int firstAgeV=110;
string firstNHobbie = someHobby;
string secondN="";
string secondage="";
string secondNV= someValue2
int secondAgeV=130;
string secondNHobbie = someHobby2;

//If value is not nothing i.e. has a value
if(firstN != "")
 PdfPTable mTablePerson1= new PdfPTable(5);
 mTablePerson1.WidthPercentage = 110f;
 mTablePerson1.AddCell("Name");
 mTablePerson1.AddCell("Age");
 mTablePerson1.AddCell(firstNV);
 mTablePerson1.AddCell(firstAgeV);
//if selected hobbies
PdfPTable mTablePerson1Hobbies = new PdfPTable(5);
mTablePerson1Hobbies.WidthPercentage = 110f;
mTablePerson1Hobbies.AddCell("Hobby");
mTablePerson1Hobbies.AddCell("firstNHobbie");
document.Add(mTablePerson1);
document.Add(mTablePerson1Hobbies);
}else if(secondN != "")
 PdfPTable mTablePerson2 = new PdfPTable(5);
 mTablePerson2.WidthPercentage = 110f;
 mTablePerson2.AddCell("Name");
 mTablePerson2.AddCell("Age");
 mTablePerson2.AddCell(secondNV);
 mTablePerson2.AddCell(secondAgeV);
//if selected hobbies
PdfPTable mTablePerson2Hobbies = new PdfPTable(5);
mTablePerson1Hobbies.WidthPercentage = 110f;
mTablePerson1Hobbies.AddCell("Hobby");
mTablePerson1Hobbies.AddCell("secondNHobbie");
document.Add(mTablePerson2);
document.Add(mTablePerson2Hobbies);
}

出于某种原因,此设计隐藏了第二个人的表,即mTablePerson2和mTablePerson2Hobbies。如果满足firstN和secondN条件,如何添加所有表?

默认情况下,iTextSharp只渲染完整的行。如果一行不完整,则该行将不可见。就好像那一行不存在一样。

iTextSharp还会忽略没有行的表。如果表中没有行,则不会显示任何表。

这就是导致您出现问题的原因:

// You create a table with 5 columns
PdfPTable mTablePerson2 = new PdfPTable(5);
// You start the first row:
mTablePerson2.AddCell("Name");     // column 1
mTablePerson2.AddCell("Age");      // column 2
mTablePerson2.AddCell(secondNV);   // column 3
mTablePerson2.AddCell(secondAgeV); // column 4
// You add the table to the document:
document.Add(mTablePerson2);

由于您只向具有5列的行添加了4个单元格,因此该行将被删除。由于该表中只有一行,并且该行已被删除,因此不会显示任何表。

new PdfPTable(5)更改为new PdfPTable(4),或者添加一个额外的单元格,或者使用该方法自动完成行,请参见PdfTable:最后一个单元格不可见

我解释了mTablePerson2的问题,但同样的答案也适用于mTablePerson2Hobbies。在这种情况下,您创建了一个包含5列的表,但只添加了3个单元格。

此外,你有这样的东西:

if (firstN != "") {
    // do something
}
else if(secondN != "") {
    // do something else
}

您抱怨如果满足firstNsecondN标准,则不会发生// do something else。这是一个基本逻辑问题。

如果firstN不是"",则无论secondN的值如何,都将执行// do something,而不是// do something else

你想要这样的东西:

if (firstN != "") {
    // do something
}
if(secondN != "") {
    // do something else
}

你应该放下else

更新:

假设c1c2是布尔值,并且您有:

if (c1)
   do something
else if (c2)
   do something else

下表显示了将要发生的情况:

| conditions | do something | do something else |
|------------|--------------|-------------------|
| c1 = false |     ---      |      ---          |
| c2 = false |              |                   |
|------------|--------------|-------------------|
| c1 = true  |     YES      |      ---          |
| c2 = false |              |                   |
|------------|--------------|-------------------|
| c1 = false |     ---      |      YES          |
| c2 = true  |              |                   |
|------------|--------------|-------------------|
| c1 = true  |     YES      |      ---          |
| c2 = true  |              |                   |

只有一组条件会"做其他事情":c1需要是falsec2需要是true。如果c1true,那么您的程序甚至不会查看c2的值。

你想让表格看起来像这样:

| conditions | do something | do something else |
|------------|--------------|-------------------|
| c1 = false |     ---      |      ---          |
| c2 = false |              |                   |
|------------|--------------|-------------------|
| c1 = true  |     YES      |      ---          |
| c2 = false |              |                   |
|------------|--------------|-------------------|
| c1 = false |     ---      |      YES          |
| c2 = true  |              |                   |
|------------|--------------|-------------------|
| c1 = true  |     YES      |      YES          |
| c2 = true  |              |                   |

要实现这一点,您需要:

if (c1)
    do something
if (c2)
    do something else

更新2:

在你的评论中,你声称你想要这样的东西:

| conditions | do something | do something else |
|------------|--------------|-------------------|
| c1 = false |     ---      |      ---          |
| c2 = false |              |                   |
|------------|--------------|-------------------|
| c1 = true  |     YES      |      ---          |
| c2 = false |              |                   |
|------------|--------------|-------------------|
| c1 = false |     YES      |      YES          |
| c2 = true  |              |                   |
|------------|--------------|-------------------|
| c1 = true  |     YES      |      YES          |
| c2 = true  |              |                   |

在这种情况下,我会创建两种方法:

void doSomething() {
    // code to draw one table
}
void doSomethingElse() {
    // code to draw another table
}

现在你可以这样实现你想要的:

if (c1) {
    doSomething();
}
else if (c2) {
    doSomething();
    doSomethingElse();
}

或者你可以这样做:

if (c1 || c2) {
    doSomething();
}
if (c2) {
    doSomethingElse();
}

如果你在比利时上学,这就是你16岁时在数学课上学到的。

相关内容

最新更新