【发布时间】:2017-10-03 11:33:02
【问题描述】:
嗨,我使用 ItextSharp C# 得到一个空 PDF,只有在 Header 部分之前是空的,我知道当列的 colspan 没有填满表格时会出现这个问题,但是我的 tblData 有 (2) 列和单元格每个都有 1 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();
}
标题部分很好,但数据部分是空的,我不知道我做错了什么。
结果:
【问题讨论】: