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");
}
}
);