Free Support Forum - aspose.com

Multiple Tables from one datatable

I am new to Aspose.pdf and apologize if this is covered somewhere else, but I can't seem to find anything like what I am trying to accomplish.

I have a C# project where I am getting results from a SQL stored procedure back as a datatable and I would like to use portions of this data in different tables inside my PDF. The data contains (among other things) Item numbers, invoice numbers and exception codes. What I need to do, is add a new table for each invoice number that comes in.

The number of tables will be different each time, so I am not sure how to programaticallly generate the new pdf tables. My loop generates a datatable each time it goes through, but using ImportDataTable imports it to the same pdf table each time through, so my items just get appended to the end of the existing table. I need a way to create the new table each time, but without knowing how many tables I will end up with.

This seems like it should be a very easy task, but for some reason I can't seem to wrap my mind around it.

Any tips would be appreciated.

Thanks.

Hi,

Can you please provide us with your existing code and point out the problems in it. This way we can better understand your requirements and be in a better position to offer a solution.

Thanks.

I believe I grabbed enough code to demonstrate where the problem is. I create a workTable each time through and then try to import that into the PDF table. I don't know how to create the arbitrary number of tables, so they all end up in the same table table1. Because I am only importing it in one place, I am getting all the results as I write the PDF at the end.

Additionally, I am getting my headers created each time a new invoice number comes through, but they all end up in section1 since I don't know how to create an arbitrary number of sections on the fly.

Thanks.

static void ProcessEachSubscription(Layer.Subscription subscription)

{

StringBuilder sbTextMessage = new StringBuilder();

// Access information about the subscription's subscriber

sbTextMessage.AppendFormat(

"Hello {0} {1} ({2}), attached is your order information...\r\n\r\n",

subscription.FirstName,

subscription.LastName,

subscription.Email

);

// Track headers

bool isFirstTime = true;

Aspose.Pdf.License license = new Aspose.Pdf.License();

license.SetLicense("Aspose.Pdf.lic");

Pdf pdf1 = new Pdf();

pdf1.DestinationType = DestinationType.FitWidth;

MarginInfo marginInfo = new MarginInfo();

TextInfo tinfo = new TextInfo();

marginInfo.Top = 50;

marginInfo.Bottom = 50;

marginInfo.Left = 30;

marginInfo.Right = 30;

Section sec1 = pdf1.Sections.Add();

sec1.IsLandscape = true;

sec1.PageInfo.Margin = marginInfo;

Table tab1 = new Table();

tab1.Margin.Left = 1;

tab1.DefaultCellTextInfo.FontName = "Arial";

tab1.DefaultCellTextInfo.FontSize = 8;

tab1.DefaultCellTextInfo.IsTrueTypeFontBold = false;

tab1.IsFirstRowRepeated = true;

tab1.IsRowBroken = false;

sec1.Paragraphs.Add(tab1);

tab1.ColumnWidths = "1.5cm 6cm 1.5cm 1.5cm 2cm 1cm 2cm 2cm 2cm 2cm 2.5cm";

tab1.DefaultCellBorder = new BorderInfo((int)BorderSide.None);

// Process each subscription customer

subscription.SubscriptionCustomerList.ForEach(

delegate(Layer.SubscriptionCustomer currentCustomer)

{

// Query for the order audit results for this customer (for today's ship date)

DataTable auditResults = Layer.SubscriptionCustomer.GetOrderAuditData(

currentCustomer.Company,

currentCustomer.Division,

currentCustomer.Location,

currentCustomer.Customer,

//DateTime.Today.AddDays(-1)

DateTime.Parse("1/9/2008 12:00:00 AM")

);

//Build workTable to hold data from audit results

DataTable workTable = new DataTable("Order");

DataColumn workCol = workTable.Columns.Add("Item", typeof(String));

workTable.Columns.Add("Item Description", typeof(String));

workTable.Columns.Add("Pack", typeof(String));

workTable.Columns.Add("Size", typeof(String));

workTable.Columns.Add("Weight", typeof(String));

workTable.Columns.Add("UM", typeof(String));

workTable.Columns.Add("Ordered", typeof(String));

workTable.Columns.Add("Shipped", typeof(String));

workTable.Columns.Add("Received", typeof(String));

workTable.Columns.Add("Difference", typeof(String));

workTable.Columns.Add("Reason", typeof(String));

string invoice = "new";

foreach (DataRow dr in auditResults.Rows)

{

if (invoice != dr["Invoice"].ToString()) //Check for invoice number change and re-use isFirstTime code

{

isFirstTime = true;

}

if (isFirstTime) //Create the header table

{

Table tab_cust = new Table();

sec1.Paragraphs.Add(tab_cust);

tab_cust.ColumnWidths = "2.5cm 2.5cm 2.5cm 5cm 2.5cm 2.5cm";

Row row1 = tab_cust.Rows.Add();

Cell cell1Row1 = row1.Cells.Add("Customer");

Cell cell2Row1 = row1.Cells.Add("Invoice");

Cell cell3Row1 = row1.Cells.Add("Memo");

Cell cell4Row1 = row1.Cells.Add("Shipdate");

Cell cell5Row1 = row1.Cells.Add("PO");

//Cell cell6Row1 = row1.Cells.Add("Status");

Cell cell7Row1 = row1.Cells.Add("Report Date");

Row row2 = tab_cust.Rows.Add();

Cell cell1Row2 = row2.Cells.Add(dr["Customer"].ToString().Trim());

Cell cell2Row2 = row2.Cells.Add(dr["Invoice"].ToString());

Cell cell3Row2 = row2.Cells.Add(dr["Memo"].ToString());

Cell cell4Row2 = row2.Cells.Add(DateTime.ParseExact(dr["ShipDate"].ToString(), "yyyyMMdd", System.Globalization.CultureInfo.CurrentCulture).ToString("M/d/yyyy"));

Cell cell5Row2 = row2.Cells.Add(dr["PO"].ToString().Trim());

//Cell cell6Row2 = row2.Cells.Add("Printed");

Cell cell7Row2 = row2.Cells.Add(DateTime.Today.ToString("M/d/yyyy"));

invoice = dr["Invoice"].ToString();

isFirstTime = false;

}

string display = dr["ReasonCode"].ToString();

switch (display) //used to choose what is put in the table

{

case "A":

if (subscription.IsIncludeAdds == true)

{

DataRow workRow = workTable.NewRow();

workRow["Item"] = dr["Item"].ToString().Trim();

workRow["Item Description"] = dr["ItemDescription"].ToString();

workRow["Pack"] = dr["ItemPack"].ToString();

workRow["Size"] = dr["ItemSize"].ToString();

if (((decimal)dr["Weight"]).ToString("F2") == "0.00")

{

workRow["Weight"] = "";

}

else

{

workRow["Weight"] = ((decimal)dr["Weight"]).ToString("F2");

}

workRow["UM"] = dr["UOM"].ToString();

workRow["Ordered"] = dr["OrderQuantity"].ToString();

workRow["Shipped"] = dr["ShipQuantity"].ToString();

workRow["Received"] = dr["ReceivedQuantity"].ToString();

workRow["Difference"] = dr["Difference"].ToString();

workRow["Reason"] = dr["Reason"].ToString();

//workRow["Invoice"] = dr["Invoice"].ToString();

//workRow["ReasonCode"] = dr["ReasonCode"].ToString();

workTable.Rows.Add(workRow);

}

break;

case "S":

if (subscription.IsIncludeShorts == true)

{

DataRow workRow = workTable.NewRow();

workRow["Item"] = dr["Item"].ToString().Trim();

workRow["Item Description"] = dr["ItemDescription"].ToString();

workRow["Pack"] = dr["ItemPack"].ToString();

workRow["Size"] = dr["ItemSize"].ToString();

if (((decimal)dr["Weight"]).ToString("F2") == "0.00")

{

workRow["Weight"] = "";

}

else

{

workRow["Weight"] = ((decimal)dr["Weight"]).ToString("F2");

}

workRow["UM"] = dr["UOM"].ToString();

workRow["Ordered"] = dr["OrderQuantity"].ToString();

workRow["Shipped"] = dr["ShipQuantity"].ToString();

workRow["Received"] = dr["ReceivedQuantity"].ToString();

workRow["Difference"] = dr["Difference"].ToString();

workRow["Reason"] = dr["Reason"].ToString();

workTable.Rows.Add(workRow);

}

break;

case "X":

if (subscription.IsIncludeSubs == true)

{

DataRow workRow = workTable.NewRow();

workRow["Item"] = dr["Item"].ToString().Trim();

workRow["Item Description"] = dr["ItemDescription"].ToString();

workRow["Pack"] = dr["ItemPack"].ToString();

workRow["Size"] = dr["ItemSize"].ToString();

if (((decimal)dr["Weight"]).ToString("F2") == "0.00")

{

workRow["Weight"] = "";

}

else

{

workRow["Weight"] = ((decimal)dr["Weight"]).ToString("F2");

}

workRow["UM"] = dr["UOM"].ToString();

workRow["Ordered"] = dr["OrderQuantity"].ToString();

workRow["Shipped"] = dr["ShipQuantity"].ToString();

workRow["Received"] = dr["ReceivedQuantity"].ToString();

workRow["Difference"] = dr["Difference"].ToString();

workRow["Reason"] = dr["Reason"].ToString();

workTable.Rows.Add(workRow);

}

break;

case "N":

if (subscription.IsIncludeNonExceptions == true)

{

DataRow workRow = workTable.NewRow();

workRow["Item"] = dr["Item"].ToString().Trim();

workRow["Item Description"] = dr["ItemDescription"].ToString();

workRow["Pack"] = dr["ItemPack"].ToString();

workRow["Size"] = dr["ItemSize"].ToString();

if (((decimal)dr["Weight"]).ToString("F2") == "0.00")

{

workRow["Weight"] = "";

}

else

{

workRow["Weight"] = ((decimal)dr["Weight"]).ToString("F2");

}

workRow["UM"] = dr["UOM"].ToString();

workRow["Ordered"] = dr["OrderQuantity"].ToString();

workRow["Shipped"] = dr["ShipQuantity"].ToString();

workRow["Received"] = dr["ReceivedQuantity"].ToString();

workRow["Difference"] = dr["Difference"].ToString();

workRow["Reason"] = dr["Reason"].ToString();

workTable.Rows.Add(workRow);

}

break;

default:

break;

}

}

Table tab1 = new Table(); // table to import workTable into

tab1.Margin.Left = 1;

tab1.DefaultCellTextInfo.FontName = "Arial";

tab1.DefaultCellTextInfo.FontSize = 8;

tab1.DefaultCellTextInfo.IsTrueTypeFontBold = false;

tab1.IsFirstRowRepeated = true;

tab1.IsRowBroken = false;

sec1.Paragraphs.Add(tab1);

tab1.ColumnWidths = "1.5cm 6cm 1.5cm 1.5cm 2cm 1cm 2cm 2cm 2cm 2cm 2.5cm";

tab1.DefaultCellBorder = new BorderInfo((int)BorderSide.None);

tab1.ImportDataTable(workTable, true, 0, 0);

bool altColor = false;

foreach (Row curRow in tab1.Rows)

{

altColor = !altColor;

foreach (Cell curCell in curRow.Cells)

{

if (altColor)

{

curCell.BackgroundColor = new Color((string)"#BFFFFF");

}

else

{

curCell.BackgroundColor = new Color("White");

}

}

}

pdf1.Save(".\\PDF\\" + subscription.LastName + currentCustomer.Customer + ".pdf");

}

}

);

Hi,

According to my understanding, what I understand is that you need to render Pdf as per some field, that datatable should be imported to Pdf table dynamically and splits whenever there is some change in that field. Is this correct? I think what you are doing is correct as a workaround and it's not supported in Aspose.Pdf.

I am not sure if I understand your question in full. If it's still some confusion, please feel free to ask.

Thanks.