Hi Atul,
Thanks for sharing the detail.
impexdocs:
Thanks for the Reply and the Code Snippet . We were able to get the First page Total using this .
Now we are working on subsequent page Totals.
You can use the same approach as shared in my previous post to get the total of each page.
impexdocs:
It will be good if we can gave a Call conference also where we can explain what we want .
We do not provide phone support for technical issues but you can contact us via forums and live chat.
impexdocs:
Note : This is possoble in Crustal Reports but we want to replace Crystal Reports with ASPOSE WORD…
Could you please share your expected output document generated by Crystal Reports here for our reference? We will then provide you more information about your query along with code.
impexdocs:
Also let us know how we can merge the Body Section and Footer so that there is no Gap in it.
Please note that Aspose.Words mimics the same behavior as MS Word does. You can create/modify Word documents using Aspose.Words APIs which you can create/modify using MS Words.
A MS Word document contains page header/footer, tables, paragraphs etc. In your scenario, you want followings:
- Total of table’s column for each page. For this case, please check code example shared in my previous post.
- For last page, increase the table’s height so that it will look like merged table with footer’s table
- Remove the distance between tables inside header, footer and body of page so that it looks like one table.
We have modified the template document for your kind reference. Please check the table’s first section headers and footers in attached input and output documents. Please check following code example. Hope this helps you.
Document doc = new Document(MyDir + "input.docx");
DocumentBuilder dcbuild = new DocumentBuilder(doc);
DataTable dtlblPermitNumber = new DataTable();
DataTable dtedn = new DataTable();
DataTable dtPaking = new DataTable();
DataSet dsLineDetail = new DataSet();
dtlblPermitNumber.TableName = "Product";
dtPaking.TableName = "Packing";
dtedn.Columns.Add("EXPORTER");
dtedn.Columns.Add("ShipmentID");
DataRow row;
DataRow row1;
DataRow drPaking;
row = dtedn.NewRow();
row["EXPORTER"] = "ABC PTY LTD\r\nUNIT 12 HILL AVE\r\nROSUSE NSW 2154\r\nAUSTRALIA";
row["ShipmentID"] = "1";
dtedn.Rows.Add(row);
dtlblPermitNumber.Columns.Add("ShipmentID");
dtlblPermitNumber.Columns.Add("PCODE");
dtlblPermitNumber.Columns.Add("DGOODS");
dtlblPermitNumber.Columns.Add("QTY");
dtlblPermitNumber.Columns.Add("UPRICE");
dtlblPermitNumber.Columns.Add("Total");
for (int j = 0; j < 48; j++)
{
row1 = dtlblPermitNumber.NewRow();
row1["ShipmentID"] = "1";
row1["PCODE"] = "PO000" + j;
row1["DGOODS"] = j + ": 100 CARTONS ASSORTED FOODS 100 CARTONS";
row1["QTY"] = j + "000";
row1["UPRICE"] = j + "00" + j;
row1["Total"] = "1500" + j;
dtlblPermitNumber.Rows.Add(row1);
}
dtPaking.Columns.Add("ShipmentID");
dtPaking.Columns.Add("Marks");
dtPaking.Columns.Add("NoOfPKGS");
dtPaking.Columns.Add("DesOfGoods");
dtPaking.Columns.Add("NetWT");
dtPaking.Columns.Add("GrossWT");
dtPaking.Columns.Add("Volume");
drPaking = dtPaking.NewRow();
drPaking["ShipmentID"] = "1";
drPaking["Marks"] = "AS ADDRESSED";
drPaking["NoOfPKGS"] = "100 CARTON";
drPaking["DesOfGoods"] = " 1 X 20' CONTAINER STC\r\n100 CARTONS\r\nASSORTED FOODS\r\n15000 KGS";
drPaking["NetWT"] = "22";
drPaking["GrossWT"] = "33";
drPaking["Volume"] = "44";
dtPaking.Rows.Add(drPaking);
dsLineDetail.Tables.Add(dtedn);
dsLineDetail.Tables.Add(dtPaking);
dsLineDetail.Tables.Add(dtlblPermitNumber);
if (dtlblPermitNumber.Rows.Count > 0)
{
dsLineDetail.Relations.Add(new DataRelation("LineItem", dtedn.Columns["ShipmentID"], dtlblPermitNumber.Columns["ShipmentID"], false));
}
if (dtPaking.Rows.Count > 0)
{
dsLineDetail.Relations.Add(new DataRelation("LineItem2", dtedn.Columns["ShipmentID"], dtPaking.Columns["ShipmentID"], false));
}
doc.MailMerge.CleanupOptions = MailMergeCleanupOptions.RemoveContainingFields;
doc.MailMerge.CleanupOptions |= MailMergeCleanupOptions.RemoveStaticFields;
doc.MailMerge.CleanupOptions |= MailMergeCleanupOptions.RemoveEmptyParagraphs;
doc.MailMerge.CleanupOptions |= MailMergeCleanupOptions.RemoveUnusedRegions;
doc.MailMerge.CleanupOptions |= MailMergeCleanupOptions.RemoveUnusedFields;
doc.MailMerge.Execute(dtedn);
// doc.MailMerge.ExecuteWithRegions(dtlblPermitNumber);
doc.MailMerge.ExecuteWithRegions(dsLineDetail);
// doc.InsertBreak(BreakType.PageBreak);
doc.MailMerge.DeleteFields();
// Add total
Table table = (Table)doc.FirstSection.Body.GetChild(NodeType.Table, 0, true);
LayoutCollector collector = new LayoutCollector(doc);
int page = 1;
double pageSum = 0.0;
foreach (Row r in table.Rows)
{
if (collector.GetStartPageIndex(r.LastCell) == page)
{
double sum = 0.0;
bool isNumeric = double.TryParse(r.LastCell.ToString(SaveFormat.Text).Trim(), out sum);
if (isNumeric)
pageSum = pageSum + sum;
}
else
{
Shape shape = new Shape(doc, ShapeType.TextBox);
shape.AppendChild(new Paragraph(doc));
shape.FirstParagraph.AppendChild(new Run(doc, pageSum.ToString()));
shape.Width = 50;
shape.Height = 20;
shape.WrapType = WrapType.None;
shape.RelativeHorizontalPosition = RelativeHorizontalPosition.Character;
shape.RelativeVerticalPosition = RelativeVerticalPosition.Paragraph;
shape.Top = 50;
shape.Left = -30;
LayoutEnumerator layoutEnumerator = new LayoutEnumerator(doc);
var renderObject = collector.GetEntity(r.LastCell.FirstParagraph);
layoutEnumerator.Current = renderObject;
RectangleF location = layoutEnumerator.Rectangle;
// Console.WriteLine(r.LastCell.FirstParagraph.GetText());
((Row)r.PreviousSibling).LastCell.FirstParagraph.AppendChild(shape);
pageSum = 0.0;
page++;
}
}
collector = new LayoutCollector(doc);
int pagenumber = collector.GetStartPageIndex(table.LastRow.FirstCell.LastParagraph);
int nextpagenumber = pagenumber + 1;
Row lastrow = (Row)table.LastRow.Clone(true);
foreach (Cell cell in lastrow.Cells)
{
cell.RemoveAllChildren();
cell.EnsureMinimum();
}
while (pagenumber != nextpagenumber)
{
table.Rows.Add(lastrow.Clone(true));
collector = new LayoutCollector(doc);
pagenumber = collector.GetStartPageIndex(table.LastRow.FirstCell.LastParagraph);
}
table.LastRow.Remove();
doc.Save(MyDir + "Out.docx");