Multiple Files with Nested Table

Hi. I am making a program that needs to create multiple doc files, the template contains 2 pages, first page is covering letter and second page has a table.

The first page requires to have 2 inches margin top.

The number of files to be generated is based on the number of rows in the first table of the dataset to be merged to the document.

dsMerge = new DataSet()
Table 1: BankMain - first table
Table 2 BankDetails

How can I produce multiple files?

I have this code

private void CreateWordDocument()
{
    DataTable dtDetails = new DataTable();
    foreach (GridViewRow gvRow in gvBankExport.Rows)
    {
        DataTable dt = new DataTable();
        dt = dtBankMainCreate();
        Label lblBankCode = (Label)gvRow.FindControl("lblBankCode");
        Label lblBankTotal = (Label)gvRow.FindControl("lblBankTotal");
        TextBox txtBankName = (TextBox)gvRow.FindControl("txtBankName");
        TextBox txtAddress = (TextBox)gvRow.FindControl("txtAddress");
        TextBox txtAcctNo = (TextBox)gvRow.FindControl("txtAcctNo");
        TextBox txtCurrencyCode = (TextBox)gvRow.FindControl("txtCurrencyCode");
        TextBox txtOfficerTitle = (TextBox)gvRow.FindControl("txtOfficerTitle");
        TextBox txtFirstName = (TextBox)gvRow.FindControl("txtFirstName");
        TextBox txtLastName = (TextBox)gvRow.FindControl("txtLastName");
        TextBox txtPosition = (TextBox)gvRow.FindControl("txtPosition");
        string signatory1 = drpSignatory1.SelectedItem.Text.Split('|')[0].ToString();
        string signatory1Pos = drpSignatory1.SelectedItem.Text.Split('|')[1].ToString();
        string signatory2 = drpSignatory2.SelectedItem.Text.Split('|')[0].ToString();
        string signatory2Pos = drpSignatory2.SelectedItem.Text.Split('|')[1].ToString();
        string bankcode = lblBankCode.Text;
        string bankname = txtBankName.Text;
        string address = txtAddress.Text;
        string acctno = txtAcctNo.Text;
        string curr = txtCurrencyCode.Text;
        string offtitle = txtOfficerTitle.Text;
        string lastname = txtLastName.Text;
        string firstname = txtFirstName.Text;
        string offposition = txtPosition.Text;
        string banktotal = lblBankTotal.Text;
        string a = Convert.ToDecimal(Math.Truncate(Convert.ToDouble(banktotal))).ToString();
        string decimalPart = (Convert.ToDecimal(banktotal) - Math.Floor(Convert.ToDecimal(banktotal))).ToString().Replace("0.", "");
        string numberWordValue = NumberToWords(Convert.ToInt32(a)).Replace("-", " ");
        DataRow dRow = dt.NewRow();
        dRow["BankInstructionDate"] = DateTime.Now.ToString("MMMM dd, yyyy");
        dRow["BANKFULLNAME"] = bankname;
        dRow["BANKFULLADDRESS"] = address;
        dRow["BankFullContactPerson"] = firstname + " " + lastname;
        dRow["BankFullContactPerPosition"] = offposition;
        dRow["BankOfficerTitle"] = offtitle;
        dRow["BankLastNameContactPerson"] = lastname;
        dRow["BANKFULLACCOUNTNO"] = acctno;
        dRow["TOTALAMOUNTINWORDS"] = numberWordValue;
        dRow["NUMBERCENTS"] = decimalPart;
        dRow["CURRENCYCODE"] = curr;
        dRow["TOTALAMOUNTINDIGITS"] = banktotal;
        dRow["Signatory1"] = signatory1;
        dRow["Sign1Position"] = signatory1Pos;
        dRow["Signatory2"] = signatory2;
        dRow["Sign3Positionsss"] = signatory2Pos;
        dRow["BankCode"] = bankcode;
        dRow["PayrollPeriod"] = drpPayrollPeriod.SelectedItem.Text.ToString();
        dt.Rows.Add(dRow);
        dtDetails = dtBankDetails(bankcode);
        dt.TableName = "BankMain";
        dtDetails.TableName = "BankDetails";
        DataSet ds = new DataSet();
        ds.Tables.Add(dt);
        ds.Tables.Add(dtDetails);
        ds.Relations.Add(new DataRelation("BankRel", dt.Columns["BankCode"], dtDetails.Columns["BankCode"]));
        DocMailMerge(ds, bankcode);
    }
}
protected void DocMailMerge(DataSet dsMerge, string bcode)
{
    string sourceFile = Server.MapPath("~/Word Files/BankInstruction_CoveringLetter.docx");
    Document docBank = new Document(sourceFile);
    Document docClose = (Document)docBank.Clone(true);
    docBank.MailMerge.TrimWhitespaces = false;
    docBank.MailMerge.ExecuteWithRegions(dsMerge);
    docBank.Save(Response, "BankInstruction_" + bcode.Trim() + ".docx", ContentDisposition.Inline, null);
}

Hi Bob,

Thanks for your inquiry. Please use PageSetup.TopMargin property to get/set the distance (in points) between the top edge of the page and the top boundary of the body text.

bjay.tiamsic26:
The number of files to be generated is based on the number of rows in the first table of the dataset to be merged to the document.

In this case, we suggest you please iterate through all DataRows of table and use MailMerge.Execute method (DataRow) to perform mail merge from a DataRow into the document. Hope this helps you.

If you still face problem, please share following detail for our reference. We will then provide you more information about your query.

  • Your input Word document
  • Please attach the output Word files that shows the desired behavior.

Hello. I used the Header to adjust the margin, because I need to set 2 inches margin top on the 1st page/covering letter.
I used Response.End() and I get Duplicate Header error. So I just tried creating one file for the the datarow. Before every region is the covering letter.

I attached the sample output here. After the first region, the next covering letter follows and it is just under the end of the table instead of a new page and having 2 inches margin top again.

What can you suggest please? I really need this

Hi Bob,

Thanks for your inquiry. To ensure a timely and accurate response, please attach the following resources here for testing:

  • Your input Word document
  • Please attach the output Word file that shows the desired behavior.
  • Please create a standalone console application (source code without compilation errors) that helps us reproduce your problem on our end and attach it here for testing.

As soon as you get these pieces of information ready, we’ll start investigation into your issue and provide you more information. Thanks for your cooperation.

PS: To attach these resources, please zip them and Click ‘Reply’ button that will bring you to the ‘reply page’ and there at the bottom you can include any attachments with that post by clicking the ‘Add/Update’ button.

Hi. I attached here the program I made so you can evaluate. It is in zip filed. Also the Output and Input and the Actual Output of the program.

And I included in the Desired Output the grand totals which I could not make.

Thanks

Hi Bob,

Thanks for sharing the detail. Please turn on the ‘page break before’ property of first paragraph of the template document to get the required output. See the attached image for detail. We have attached the modified document with this post for your kind reference. Hope this helps you.

Thank you for the idea. But it did not work, I guess because my document produces “FOR EVALUATION ONLY…” on the first line? I’m just using your free source for testing.

Hi Bob,

Thanks for your inquiry. Please note that in evaluation mode there are some limitations applied. E.g. Aspose.Words injects an evaluation watermark at the top of the document. The document’s content are truncated after a certain number of paragraphs during import or export.

Please request for temporary license from here:
Get temporary license

Please read about applying license from here:
Applying a License

After applying the license, you will not get the shared issue. You may also workaround this issue by inserting page break at the end of template document. In this case, after performing the mail merge you need to remove last empty paragraphs from the document by using following code snippet.

// get last section
Section lastSect = docBank.LastSection;
// Remove empty paragraphs from the end of the document
while (string.IsNullOrEmpty(lastSect.Body.LastParagraph.ToString(SaveFormat.Text).Trim()))
{
    lastSect.Body.LastParagraph.Remove();
}