Spire.PDF is a professional PDF library applied to creating, writing, editing, handling and reading PDF files without any external dependencies. Get free and professional technical support for Spire.PDF for .NET, Java, Android, C++, Python.

Thu Jan 30, 2020 9:35 pm

Hi,
Im trying to populate my table with information from an array.
I used "How to Draw PDF Table in C#" post to help me populate my own table, i did the exact same things as this post, but i am getting exceptions
saying "End column index is less than start column index." I wonder what i did wrong.

Here are parts of my quote

String[] data = new String[ls2.Count];
data = ls2.ToArray();


String[][] datasource = new String[data.Length][];
for (int i = 0; i < data.Length; i++)
{
datasource[i] = data[i].Split(';');
}
// Console.WriteLine(ls[linenumber][0]);

PdfTrueTypeFont font1 = new PdfTrueTypeFont(new Font("Arial", 16f, FontStyle.Bold));
PdfStringFormat format1 = new PdfStringFormat(PdfTextAlignment.Center);
float y = 10;
y = y + font1.MeasureString("Country List", format1).Height;
y = y + 5;
PdfTable table = new PdfTable();

table.Style.CellPadding = 2;

table.Style.HeaderStyle.StringFormat = new PdfStringFormat(PdfTextAlignment.Center);
table.Style.HeaderSource = PdfHeaderSource.Rows;
table.Style.HeaderRowCount = 1;
table.Style.ShowHeader = true;
table.Style.HeaderStyle.BackgroundBrush = PdfBrushes.CadetBlue;
//table.DataSource = datasource;

foreach (PdfColumn col in table.Columns)
col.StringFormat = new PdfStringFormat(PdfTextAlignment.Center, PdfVerticalAlignment.Middle);
Console.WriteLine($"{string.Join('!', data)}");
// table.Draw(page, new PointF(0, 12));
table.Draw(page, new PointF(0f, y));


// Console.WriteLine("done");
// Console.WriteLine($"{string.Join('!', data)}");


doc2.SaveToFile(pdfOutputPath);

my data array contains (0.00;0.00;-798.71;-798.71;0.00;-798.71;-798.71;0.00;Yes;798.71;798.71;0.00) this information.

jzhuang94
 
Posts: 4
Joined: Tue Jan 28, 2020 4:01 pm

Fri Jan 31, 2020 3:04 am

Hello,

Thanks for your post.
Your issue is caused that you commented out "table.DataSource = datasource". I tested the following code and found that it could work well. If there is any other question, just feel free to contact us.
Code: Select all
PdfDocument doc = new PdfDocument();
PdfSection sec = doc.Sections.Add();
sec.PageSettings.Width = PdfPageSize.A4.Width;
PdfPageBase page = sec.Pages.Add();
String[] data= {
   "0.00;0.00;-798.71;-798.71;0.00;-798.71",
   "-798.71;0.00;Yes;798.71;798.71;0.00"
       };
String[][] datasource = new String[data.Length][];
for (int i = 0; i < data.Length; i++)
{
    datasource[i] = data[i].Split(';');
}
PdfTrueTypeFont font1 = new PdfTrueTypeFont(new Font("Arial", 16f, FontStyle.Bold));
PdfStringFormat format1 = new PdfStringFormat(PdfTextAlignment.Center);
float y = 10;
y = y + font1.MeasureString("Country List", format1).Height;
y = y + 5;
PdfTable table = new PdfTable();
table.Style.CellPadding = 2;
table.Style.HeaderStyle.StringFormat = new PdfStringFormat(PdfTextAlignment.Center);
table.Style.HeaderSource = PdfHeaderSource.Rows;
table.Style.HeaderRowCount = 1;
table.Style.ShowHeader = true;
table.Style.HeaderStyle.BackgroundBrush = PdfBrushes.CadetBlue;
table.DataSource = datasource;
foreach (PdfColumn col in table.Columns)
    col.StringFormat = new PdfStringFormat(PdfTextAlignment.Center, PdfVerticalAlignment.Middle);
table.Draw(page, new PointF(0f, y));
doc.SaveToFile("result.pdf");

Sincerely,
Lisa
E-iceblue support team
User avatar

Lisa.Li
 
Posts: 1261
Joined: Wed Apr 25, 2018 3:20 am

Fri Jan 31, 2020 4:10 pm

Thank you for replying Lisa,

I found out my problem is that my input string values are in different length, thats why it was breaking
Is there anyway that i can populate the table with different length of strings?
thanks

jzhuang94
 
Posts: 4
Joined: Tue Jan 28, 2020 4:01 pm

Mon Feb 03, 2020 5:44 am

Hello,

Thanks for your feedback.
Populating table needs the same length of string values. However, you can refer to the following updated sample code to add empty strings to get rid of the ArgumentException. If there is any other question, just feel free to write back.
Code: Select all
 PdfDocument doc = new PdfDocument();
 PdfSection sec = doc.Sections.Add();
 sec.PageSettings.Width = PdfPageSize.A4.Width;
 PdfPageBase page = sec.Pages.Add();
 String[] data = {
    "0.00;0.00;-798.71;0.001;-798.71",
    "-798.71;0.00;798.71;798.71;0.00;0.005"
        };
 String[][] datasource = new String[data.Length][];
 for (int i = 0; i < data.Length; i++)
 {
     datasource[i] = data[i].Split(';');
 }
 for (int j = 0; j < datasource.Length; j++)
 {
     if (j + 1 < datasource.Length)
     {
         if (datasource[j].Count() > datasource[j + 1].Count())
         {
             int m = (datasource[j].Count() - datasource[j + 1].Count());
             for (int add = 0; add < m; add++)
             {
                 List<string> strList = new List<string>(datasource[j + 1]);
                 strList.Add("");
                 datasource[j + 1] = strList.ToArray();
             }
         }
         if (datasource[j].Length < datasource[j + 1].Length)
         {
             int m = (datasource[j + 1].Length - datasource[j].Length);
             for (int add = 0; add < m; add++)
             {
                 List<string> strList = new List<string>(datasource[j]);
                 strList.Add("");
                 datasource[j] = strList.ToArray();
             }
         }
     }
 }
 PdfTrueTypeFont font1 = new PdfTrueTypeFont(new Font("Arial", 16f, FontStyle.Bold));
 PdfStringFormat format1 = new PdfStringFormat(PdfTextAlignment.Center);
 float y = 10;
 y = y + font1.MeasureString("Country List", format1).Height;
 y = y + 5;
 PdfTable table = new PdfTable();
 table.Style.CellPadding = 2;
 table.Style.HeaderStyle.StringFormat = new PdfStringFormat(PdfTextAlignment.Center);
 table.Style.HeaderSource = PdfHeaderSource.Rows;
 table.Style.HeaderRowCount = 1;
 table.Style.ShowHeader = true;
 table.Style.HeaderStyle.BackgroundBrush = PdfBrushes.CadetBlue;
 table.DataSource = datasource;
 foreach (PdfColumn col in table.Columns)
     col.StringFormat = new PdfStringFormat(PdfTextAlignment.Center, PdfVerticalAlignment.Middle);
 table.Draw(page, new PointF(0f, y));
 doc.SaveToFile("result.pdf");

Sincerely,
Lisa
E-iceblue support team
User avatar

Lisa.Li
 
Posts: 1261
Joined: Wed Apr 25, 2018 3:20 am

Return to Spire.PDF

cron