如何让我的 PdfPTable 显示边框和背景颜色 (iTextSharp)?

How can I get my PdfPTable's border and background color to display (iTextSharp)?

如您所见,我有一个 table 运动边框和背景颜色(第 1 部分):

...但是第 3 节缺少这些着装上的改进,我不知道为什么。

这是第 1 部分的相关代码(应该显示):

PdfPTable tblHeadings = new PdfPTable(3);
tblHeadings.WidthPercentage = 100;
tblHeadings.SpacingBefore = 10f;
float[] headingRowWidths = new float[] { 550f, 50f, 400f };
tblHeadings.SetWidths(headingRowWidths);
tblHeadings.HorizontalAlignment = Element.ALIGN_LEFT;

Phrase phrasesec1Heading = new Phrase("Section 1: Payment Information", timesRoman9BoldFont);
PdfPCell cellSec1Heading GetCellForBorderedTable(phrasesec1Heading, Element.ALIGN_LEFT, ucscgold);
tblHeadings.AddCell(cellSec1Heading);
. . .
doc.Add(tblHeadings);

...这里是第 3 节(没有显示它应该显示的内容):

// Section 3
PdfPTable tblSection3 = new PdfPTable(1);
tblSection3.WidthPercentage = 100;
tblSection3.SpacingBefore = 10f;
//tblSection3.HorizontalAlignment = Element.ALIGN_LEFT;

Chunk sec3PayeeStatus = new Chunk("Section 3: Payee Status", timesRoman9BoldFont);
Chunk requiredFields = new Chunk("    * Required Fields", timesRoman9BoldRedFont);
Paragraph parSection3 = new Paragraph();
parSection3.Add(sec3PayeeStatus);
parSection3.Add(requiredFields);
//Phrase phrasesec1Heading = new Phrase("Section 1: Payment Information", timesRoman9BoldFont);
PdfPCell cellSec3Heading = GetCellForBorderedTable(parSection3, Element.ALIGN_LEFT, ucscgold);
tblSection3.AddCell(cellSec3Heading);

doc.Add(parSection3);                        

我错过或忘记了什么? table 创建和设置的区别在于 cells/columns 的数量以及相关声明(浮点数组)和 属性 (setWidths())。添加到 PdfPCell 的代码不同之处在于 Phrase 用于第 1 节(按我的意愿显示),段落用于第 3 节(不显示),但我试过了:

// try using a Phrase instead of a Paragraph
Chunk sec3PayeeStatus = new Chunk("Section 3 PayeeStatus", 
    timesRoman9BoldFont);
Chunk requiredFields = new Chunk("    * Require Fields", 
    timesRoman9BoldRedFont);
Phrase phraseSection3 = new Phrase();
phraseSection3.Add(sec3PayeeStatus);
phraseSection3.Add(requiredFields);
PdfPCell cellSec3Heading GetCellForBorderedTable(phraseSection3, 
    Element.ALIGN_LEFT, ucscgold);
tblSection3.AddCell(cellSec3Heading);
doc.Add(phraseSection3);

...但这并没有改善,实际上更糟,因为 "SpacingBefore" 似乎没有 "take"

最后(到目前为止),我尝试使用短语而不是块,如下所示:

Phrase sec3PayeeStatus = new Phrase("Section 3: Payee Status", timesRoman9BoldFont);
Phrase requiredFields = new Phrase("    * Required Fields", timesRoman9BoldRedFont);
Paragraph parSection3 = new Paragraph();
parSection3.Add(sec3PayeeStatus);
parSection3.Add(requiredFields);
PdfPCell cellSec3Heading = GetCellForBorderedTable(parSection3, Element.ALIGN_LEFT, ucscgold);
tblSection3.AddCell(cellSec3Heading);
doc.Add(parSection3);

...但这与第一次尝试没有更好、更坏或不同(至少我得到了垂直 space 回来,但是,将第 2 部分和第 3 部分分开)。

如何显示单元格边框和背景颜色?

更新

这是 GetCellForBorderedTable():

private static PdfPCell GetCellForBorderedTable(Phrase phrase, int align, BaseColor color)
{
    PdfPCell cell = new PdfPCell(phrase);
    cell.HorizontalAlignment = align;
    cell.PaddingBottom = 2f;
    cell.PaddingTop = 0f;
    cell.BackgroundColor = color;
    cell.VerticalAlignment = PdfPCell.ALIGN_CENTER;
    return cell;
}

问题是(对我来说不是 "visible",直到我在代码比较实用程序(即 KDiff3)中比较代码)我将段落而不是 table 添加到文档中:

doc.Add(parSection3);

现在我已将其更改为:

doc.Add(tblSection3);

...有效。