使用iText

时间:2015-06-16 13:22:05

标签: itext

下面是我的示例应用程序,我在网上收到错误 Add2CellRow(table," Age",1," Gender",2);

我认为rowspan值正在创建问题,因为下一行没有构建。

有人可以帮忙吗?

代码适用于阿拉伯文化,因此需要设置

Thread.CurrentThread.CurrentCulture = new CultureInfo("ar-SA");
Thread.CurrentThread.CurrentUICulture = new CultureInfo("ar-SA");

private static void Add3CellRow(PdfPTable table, string cell1Value, string cell2Value, int rowspan, string imgPath) 
{

    PdfPCell new1Cell = new PdfPCell(new Phrase(cell1Value, tableFont));
    new1Cell.VerticalAlignment = Element.ALIGN_MIDDLE;
    new1Cell.MinimumHeight = 25f;
    new1Cell.Padding = 6f;
    table.AddCell(new1Cell);

    PdfPCell new2Cell = new PdfPCell(new Phrase(cell2Value, tableFont));
    new2Cell.VerticalAlignment = Element.ALIGN_MIDDLE;
    new2Cell.MinimumHeight = 25f;
    new2Cell.Padding = 6f;
    new2Cell.Colspan = 2;
    table.AddCell(new2Cell);

    PdfPCell new3Cell = null;

    if (!String.IsNullOrWhiteSpace(imgPath))
    {
        Image img = Image.GetInstance(imgPath);
        img.ScaleToFit(150, 150);
        new3Cell = new PdfPCell(img);
    }
    else
    {
        new3Cell = new PdfPCell(new Phrase(String.Empty));
    }

    new3Cell.VerticalAlignment = Element.ALIGN_MIDDLE;
    new3Cell.HorizontalAlignment = Element.ALIGN_CENTER;
    new3Cell.MinimumHeight = 25f;
    new3Cell.Rowspan = rowspan;
    new3Cell.Colspan = 2;
    new3Cell.Padding = 6f;
    table.AddCell(new3Cell);
}             

private static void Add2CellRow(PdfPTable table, string cell1Value, int colspanCell1, string cell2Value, int colspanCell2 = 2)
{
    PdfPCell new1Cell = new PdfPCell(new Phrase(cell1Value, tableFont));
    new1Cell.VerticalAlignment = Element.ALIGN_MIDDLE;
    new1Cell.MinimumHeight = 25f;
    new1Cell.Colspan = colspanCell1;
    table.AddCell(new1Cell);

    PdfPCell new2Cell = new PdfPCell(new Phrase(cell2Value, tableFont));
    new2Cell.VerticalAlignment = Element.ALIGN_MIDDLE;
    new2Cell.Colspan = colspanCell2;
    new2Cell.MinimumHeight = 25f;
    new2Cell.Padding = 6f;
    table.AddCell(new2Cell);
}

public static MemoryStream Generate()
{                        
    var doc = new Document(PageSize.A4, 10, 10, 90, 110);

    MemoryStream memoryStream = new MemoryStream();
    PdfWriter writer = PdfWriter.GetInstance(doc, memoryStream);            

    doc.Open();

    PdfPTable table = new PdfPTable(5);

    table.RunDirection = PdfWriter.RUN_DIRECTION_RTL;


    table.WidthPercentage = 89.5f;
    table.DefaultCell.VerticalAlignment = Element.ALIGN_MIDDLE;
    table.DefaultCell.Padding = 6f;
    table.DefaultCell.UseAscender = true;
    table.DefaultCell.UseDescender = true;

    Add3CellRow(table,"Name","Harshit Verma",4,"");            
    Add2CellRow(table, "Age", 1, "Gender", 2);
    Add2CellRow(table, "Age", 1, "Gender", 2);
    Add2CellRow(table, "Age", 1, "Gender", 2);

    table.SplitLate = false;

    Paragraph p = new Paragraph("");
    doc.Add(p);
    table.SpacingBefore = 30f;
    doc.Add(table);

    writer.CloseStream = false;
    doc.Close();
    memoryStream.Position = 0;

    return memoryStream;
}

0 个答案:

没有答案