您好,我在使用ItexSharp C#时遇到了一个问题,生成的PDF文件在页眉区域之前是空白的。我知道这个问题通常发生在表格中列的colspan没有填满的情况下。但是我的tblData表格有两列,每一列都只有一个colspan。抱歉我的英语不太好。
static void Main(string[] args)
{
DataTable dt = new DataTable();
dt = GetData();
Document doc = new Document(PageSize.LETTER);
PdfWriter writer = PdfWriter.GetInstance(doc, new FileStream(@"D:\prueba.pdf", FileMode.Create));
doc.Open();
iTextSharp.text.Font _standardFont = new iTextSharp.text.Font(iTextSharp.text.Font.FontFamily.HELVETICA, 9, iTextSharp.text.Font.NORMAL, BaseColor.BLACK);
iTextSharp.text.Font CompanyFont = new iTextSharp.text.Font(iTextSharp.text.Font.FontFamily.HELVETICA, 10, iTextSharp.text.Font.NORMAL, BaseColor.BLACK);
iTextSharp.text.Font HeaderGrowerFont = new iTextSharp.text.Font(iTextSharp.text.Font.FontFamily.HELVETICA, 10, iTextSharp.text.Font.NORMAL, BaseColor.BLACK);
//Header ---------------------------------------------------------------
PdfPTable tblHeader = new PdfPTable(3);
tblHeader.WidthPercentage = 100;
PdfPCell Date = new PdfPCell(new Phrase("Date: 09/28/17", _standardFont));
Date.BorderWidth = 0;
Date.HorizontalAlignment = Element.ALIGN_LEFT;
Date.Colspan = 1;
Date.FixedHeight = 40f;
PdfPCell CompanyName = new PdfPCell(new Phrase("T H E Q U E E N ' S F L O W E R S", CompanyFont));
CompanyName.BorderWidth = 0;
CompanyName.HorizontalAlignment = Element.ALIGN_CENTER;
CompanyName.Colspan = 1;
CompanyName.FixedHeight = 40f;
PdfPCell PageNumber = new PdfPCell(new Phrase("Page 1", _standardFont));
PageNumber.HorizontalAlignment = Element.ALIGN_RIGHT;
PageNumber.BorderWidth = 0;
PageNumber.Colspan = 1;
PageNumber.FixedHeight = 40f;
PdfPCell Time = new PdfPCell(new Phrase("Time: 06 53 32", _standardFont));
Time.BorderWidth = 0;
Time.Colspan = 1;
Time.FixedHeight = 40f;
PdfPCell Text = new PdfPCell(new Phrase("LARRIGLO", _standardFont));
Text.BorderWidth = 0;
Text.Colspan = 2;
Text.HorizontalAlignment = Element.ALIGN_RIGHT;
Text.FixedHeight = 40f;
PdfPCell HeaderGrower = new PdfPCell(new Phrase("STARTING POSITION BY GROWER", HeaderGrowerFont));
HeaderGrower.BorderWidth = 0;
HeaderGrower.Colspan = 3;
HeaderGrower.HorizontalAlignment = Element.ALIGN_CENTER;
HeaderGrower.FixedHeight = 40f;
HeaderGrower.BorderWidthBottom = 0.75f;
tblHeader.AddCell(Date);
tblHeader.AddCell(CompanyName);
tblHeader.AddCell(PageNumber);
tblHeader.AddCell(Time);
tblHeader.AddCell(Text);
tblHeader.AddCell(HeaderGrower);
doc.Add(tblHeader);
//Data ---------------------------------------------------------------
PdfPTable tblData = new PdfPTable(2);
tblData.WidthPercentage = 100;
PdfPCell WhitheSpace = new PdfPCell(new Phrase("", _standardFont));
WhitheSpace.BorderWidth = 0;
WhitheSpace.Colspan = 1;
WhitheSpace.FixedHeight = 10f;
PdfPCell Awb = new PdfPCell(new Phrase("AWB: 2716 AAO: 554 DATE: 09/28/2017", _standardFont));
Awb.BorderWidth = 0;
Awb.Colspan = 1;
Awb.FixedHeight = 10f;
tblData.AddCell(WhitheSpace);
tblData.AddCell(Awb);
doc.Add(tblData);
doc.Close();
writer.Close();
}
![Empty PDF](https://istack.dev59.com/3skxS.webp)