2

Below is my sample application where I am getting an error on line Add2CellRow(table, "Age", 1, "Gender", 2);

I think the rowspan value is creating the issue as the next line is not building.

Can someone help on the same?

The code is for Arabic Culture so need to set

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;
}
6
  • 2
    There's lots of row and column spans which makes my head hurt but I was finally able to reproduce your issue. When the table is operating in LTR mode everything works correctly, however when switching over to RTL mode I get the System.IndexOutOfRangeException exception from iTextSharp 5.5.5.0 (the only one I have locally).
    – Chris Haas
    Jun 16, 2015 at 14:07
  • 1
    I am getting the same problem Chris. In RTL too if you remove the rowsapn and create the PDF it is working fine.
    – Nikita
    Jun 16, 2015 at 14:32
  • 1
    That looks like something that should be posted to JIRA rather than to StackOverflow. Jun 16, 2015 at 15:53
  • @Bruno Not a member. Cant login.
    – Nikita
    Jun 17, 2015 at 8:57
  • @Nikita It's for customers only. Ask your project manager to find out if you're a customer. If you're using iText in a commercial context (which you seem to be doing), you should be. In that case, up to three of your colleagues have a login. Jun 17, 2015 at 9:02

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Browse other questions tagged or ask your own question.