【问题标题】:ItextSharp C# Empty PDFItextSharp C# 空 PDF
【发布时间】: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();

}

标题部分很好,但数据部分是空的,我不知道我做错了什么。

结果:

【问题讨论】:

    标签: c# itext


    【解决方案1】:

    请将单元格的高度更改为更高的值。

    例如;

    Awb.FixedHeight = 10f;
    

    将其更改为以下内容:

    Awb.FixedHeight = 20f;
    

    这里 '_standardFont' 的字体大小为 9,单元格不够大,无法容纳文本,这就是单元格为空的原因。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2011-11-28
      • 2015-05-29
      • 2016-11-07
      • 2015-05-19
      • 1970-01-01
      • 1970-01-01
      • 2016-05-28
      • 1970-01-01
      相关资源
      最近更新 更多