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.

Wed Nov 09, 2022 8:24 am

I keep getting this error. here is the code I have




PdfTable table = new PdfTable();

//Set font for header and the rest cells
table.Style.DefaultStyle.Font = new PdfTrueTypeFont(new Font("Arial", 9f, FontStyle.Regular), true);
table.Style.HeaderStyle.Font = new PdfTrueTypeFont(new Font("Arial", 9f, FontStyle.Bold), true);
table.Style.ShowHeader = true;

//Styling and alignment of table data
table.Style.HeaderStyle.BackgroundBrush = PdfBrushes.Black;
table.Style.HeaderStyle.TextBrush = PdfBrushes.White;
table.Style.HeaderStyle.StringFormat = new PdfStringFormat(PdfTextAlignment.Center, PdfVerticalAlignment.Middle);

//Crate a DataTable
DataTable dataTable = new DataTable();
dataTable.Columns.Add("Bud. Per.");
dataTable.Columns.Add("Funded Prog");
dataTable.Columns.Add("Funded Prog. Desc.");
dataTable.Columns.Add("ASAP Recipient ID");
dataTable.Columns.Add("ASAP Account ID");
dataTable.Columns.Add("Amount");

foreach(var lineItem in receivableLineItems)
{
lineItem.BUDGET_PD = lineItem.BUDGET_PD != null ? lineItem.BUDGET_PD : "";
lineItem.MEASURE = lineItem.MEASURE != null ? lineItem.MEASURE : "";
lineItem.MEASURE_DESC = lineItem.MEASURE_DESC != null ? lineItem.MEASURE_DESC : "";
lineItem.ASAP_RECIP_ID= lineItem.ASAP_RECIP_ID != null ? lineItem.ASAP_RECIP_ID : "";
lineItem.ASAP_ACC_ID = lineItem.ASAP_ACC_ID != null ? lineItem.ASAP_ACC_ID : "";
lineItem.AMOUNT = lineItem.AMOUNT != null ? lineItem.AMOUNT : 0;
string[] rowData = new string[]
{
lineItem.BUDGET_PD,
lineItem.MEASURE,
lineItem.MEASURE_DESC,
lineItem.ASAP_RECIP_ID,
lineItem.ASAP_ACC_ID,
lineItem.AMOUNT.ToString()
};
dataTable.Rows.Add(rowData);
}


//table.Columns[0].Width = 12;
//table.Columns[1].Width = 12;
//table.Columns[5].Width = 12;

//Set text alignment in other cells
for (int i = 0; i < table.Columns.Count; i++)
{
table.Columns[i].StringFormat = new PdfStringFormat(PdfTextAlignment.Center, PdfVerticalAlignment.Middle);
}

table.Draw(page, new PointF(0, 380)); -------------------------------------------------------------------------------------------I get error on this line

suryaks2002
 
Posts: 17
Joined: Wed Nov 09, 2022 7:57 am

Wed Nov 09, 2022 9:50 am

Hello,

Thanks for your inquiry.
For your issue, you need to specify the data source of the table before formatting the column, for example, you need to add this code: table.DataSource = dataTable;
I put the complete code below for your reference.
If you have any issue, just feel free to contact us.

Code: Select all
//Crate a DataTable
            DataTable dataTable = new DataTable();
            dataTable.Columns.Add("Bud. Per.");
            dataTable.Columns.Add("Funded Prog");
            dataTable.Columns.Add("Funded Prog. Desc.");
            dataTable.Columns.Add("ASAP Recipient ID");
            dataTable.Columns.Add("ASAP Account ID");
            dataTable.Columns.Add("Amount");

            foreach (var lineItem in receivableLineItems)
            {
                lineItem.BUDGET_PD = lineItem.BUDGET_PD != null ? lineItem.BUDGET_PD : "";
                lineItem.MEASURE = lineItem.MEASURE != null ? lineItem.MEASURE : "";
                lineItem.MEASURE_DESC = lineItem.MEASURE_DESC != null ? lineItem.MEASURE_DESC : "";
                lineItem.ASAP_RECIP_ID = lineItem.ASAP_RECIP_ID != null ? lineItem.ASAP_RECIP_ID : "";
                lineItem.ASAP_ACC_ID = lineItem.ASAP_ACC_ID != null ? lineItem.ASAP_ACC_ID : "";
                lineItem.AMOUNT = lineItem.AMOUNT != null ? lineItem.AMOUNT : 0;
                string[] rowData = new string[]
                {
                     lineItem.BUDGET_PD,
                     lineItem.MEASURE,
                     lineItem.MEASURE_DESC,
                     lineItem.ASAP_RECIP_ID,
                     lineItem.ASAP_ACC_ID,
                     lineItem.AMOUNT.ToString()
                };
                dataTable.Rows.Add(rowData);
            }

            table.DataSource = dataTable;
            //Set text alignment in other cells
            for (int i = 0; i < table.Columns.Count; i++)
            {
                table.Columns[i].StringFormat = new PdfStringFormat(PdfTextAlignment.Center, PdfVerticalAlignment.Middle);
            }

            table.Draw(page, new PointF(0, 380));


Sincerely
Abel
E-iceblue support team
User avatar

Abel.He
 
Posts: 1010
Joined: Tue Mar 08, 2022 2:02 am

Wed Nov 09, 2022 10:24 am

Thanks much for the prompt response. :)

suryaks2002
 
Posts: 17
Joined: Wed Nov 09, 2022 7:57 am

Thu Nov 10, 2022 1:38 am

Hello,

You're welcome. If you have any issue, just feel free to contact us.

Sincerely
Abel
E-iceblue support team
User avatar

Abel.He
 
Posts: 1010
Joined: Tue Mar 08, 2022 2:02 am

Return to Spire.PDF

cron