Free Support Forum - aspose.com

Crystal "Group" filed in word template

Hi,

We are converting crystal report in the word templates and using aspose to merge dynamic data in teh templates. We are running in some of the reports where it uses Vrystal feature called "group" field. These fields basically have Header, footer and body section. Then these Hedaer, footer and body section can refer to some dataset filed and then repeats itself as many times as the records in the resultset. In addition, teh hedaer and footer section can have conditions in it to display the value. So in the end you have repeating header,footer and body part for as many number of records you have in the resultset.

I am wondering which MS word feature I can use to mimic this behavior which also is nicely supported thru Aspose APIs so that I can get the resultset thry my code, call API and then produce these dynamic data in word template? I will really appreciate any insight into it.

Please let me know if I need to provide any example doc to demonstrate this.

Thx

Thanmal.

Hi

Thanks for your request. I think in your case, you can use MailMerge with regions to fill your document with data. Please see the following link to learn more:

http://www.aspose.com/documentation/.net-components/aspose.words-for-.net/mail-merge-with-regions-explained.html

Please let me know if such approach is acceptable for you.

Best regards,

Hi,

Thx for the prompt response as usual. I like what I see but the only question I have is, this looks like will create a tabular structure with only one header and repeating rows. I have a case where we want to repeat the header and footer for each row in the resultset. How will that work, can you clarify?

Thx

Thanmal.

Hi

Thanks for your inquiry. Could you please provide me your expected output document? I will investigate it on my side and provide you more information.

Best regards,

Hi,

Attaching the sample document. This doumnet has many special instruction paragraphs texts repeating with a header and footer (Both header and footer values are marked as "special instruction" but can be different values coming from the database in other scenarios, The body text is the paragraph text coming from the database). So all this is getting printed with the help of a group field which has a header and footer which repeat itself for the number of times as the records in the resultset.

Hopefully this clarifies what I am looking for. Please let me know if you have any additional questions. I will provide more examples tomorrow.

Thx

Thanmal.

Hi

Thanks for your request. You can easy create such document from scratch using Aspose.Words. For example you can try using DocumentBuilder:

http://www.aspose.com/documentation/.net-components/aspose.words-for-.net/using-documentbuilder-to-modify-a-document-easily.html

Or you can use predefined template and mail merge feature.

Best regards,

Hi Andrey,

We have been using the document builder to merge lot of different documents. We primarily use autotext fields in our word documents and than replace these values with the dynamic data passed by the application where we replace the autotext fields with the corresponding value.

However, In this partcular example we have paragraph texts stored in a DB table and the number of paragraph texts can vary for each order number. We need to repeat these paragraphs in the word document with a header like line (Not a word document page header) and footer like line for each row in the database table. The header and footer values can be conditional in nature where by we compare another DB value (say a flag in paragraph table to print 'Special exception' or 'General exception' etc.). We need to generalize our template so that various paragraphs coming from different tables can be generated on the different documents. I am looking for a way to define these in a word template and than use Aspose API to merge the data. To simplify this example lets say I will have a recordset or a datatable with multiple rows having 3 values from a DB table std_paragraph (ParaText, HeaderFlag, FooterFlag). I want to put a condition in the header line area saying that if HeaderFlag is 'X', I want to print 'General Exception' and if HeaderFlag is 'Y', I want to put 'Special Exception'. Also similar processing for Footer like line which will appear after each paragraph text. The paragraph text will appear in between these two lines.

Can you help us by defining which kind of mail merge filed or any other word template feature will help me creating this data on template so that I can than use Aspose APIs to merge the data and have the desired effect. May be I am missing something obvious here because I only have limited experience with Aspose features and APIs as well as the word template features.

I am going thru the links you have provided but in the meanwhile if you can provide some more guidance, I will truly appreciate it.

Thx

Thanmal.

Hi,

I just looked in teh mailmerge with regions link you sent earlier. I think I now understand what you meant by predefined template and mailmerge filed can achieve the example I was mentioning in my sample document. The only question I have in in the case of my example document is that how will I have autoincrementing numbers for the repeating rows. Like 1. 2. 3. or a. b. c. etc.

Thx

Thanmal.

Hello.

Thanks for your inquiry. In your case, the easiest way would probably be:

1. Create another merge field just where the number should be, for example MERGEFIELD MyNumber.
2. Create a field with the same name in your data source. If you mail merge from a DataTable, then you can just add a column with the appropriate name (MyNumber) to the table at any time.
3. Once your data source has a column, you can either run a loop through the rows and fill the numbers before calling mail merge or you can provide a MergeField event handler and return a number for each row from the event handler.

Another way, you could try to mark the paragraphs that require numbering as numbered list in MS Word, you can create custom numbering that can look like "Paragraph 1.." and so on. During mail merge, the numbered list will grow and the numbers will increment.

Please try using the following code:

// Create dummy datasource.

DataTable data = new DataTable("TestTable");

data.Columns.Add("Test1");

data.Columns.Add("Test2");

// Add few rows.

for (int i = 0; i < 9; i++)

data.Rows.Add(new object[] { "Product Name " + i.ToString(), "Product Price " + i.ToString() });

Document doc = new Document("C:\\Temp\\in.docx");

doc.MailMerge.ExecuteWithRegions(data);

doc.Save("C:\\Temp\\out.docx");

And please see the attached template and output produced using this code.

Best regards,

I will try this and get back to you. Thx a lot for the prompt response.

Hi,

I tried and evrything looks good. I have one followup question on this. When I use the word autosequnce numbers and if my paragraphtext has a new line character in it, it is putting the text after the new line on a new line with a new sequence number rather than putting the whole paragraph together with same sequence number. Is there a way to control this behavior?

Thx

Thanmal.

Hello

Thanks for your inquiry. You can easily check it. Please see the following code:

// Create dummy datasource.

DataTable data = new DataTable("TestTable");

data.Columns.Add("Test1");

data.Columns.Add("Test2");

// Add few rows.

for (int i = 0; i < 9; i++)

data.Rows.Add(new object[] { "First line" + ControlChar.LineBreak + "Second line " + i.ToString(), "Test" + i.ToString() });

Document doc = new Document("C:\\Temp\\in.docx");

doc.MailMerge.ExecuteWithRegions(data);

doc.Save("C:\\Temp\\out.docx");

Best regards,

Hi,

We are working with mailmergeregions as suggested by you. Need your help in solving a particular situation. We have many templates where we are using mailmergeregion concept in a word table. We are getting data from a paragraph table in our database which has paragraph text and lot of flag fields. We want to use same query in our backend to fetch all the data from the table and than use different "IF" conditions in our mailmergeregions on different word templates to decide which data to display on which template. The problem with this approach is that we are getting empty rows in our word table. I have attached the example demo template(TestTemplate1.docx) and the output word document (out_merged.doc) for you to have a look. As you can see in the template we are using if conditions insdie the mailmergeregion to decide whether to print the values or not. But this approach is getting us empty rows in word table.

I also tried to use the If condition outside the word table, and was able to print multiple word tables but because of surrounding IF blocks we still get empty lines(spaces) in resultant word document.

Will appreciate your help or suggestions in resolving this issue. The data I am using in this case are:

{ "1.","this is atest paragraph1", "N" };

{"2.", "This is a test paragraph2", "Y" };

{ "3.", "this is atest paragraph3", "N" };

{ "4.", "This is a test paragraph4", "Y" };

Where 1,2,34 is the SeqNum, second column value is paragraph text and third value is the flag value.

Thx

Thanmal.

Hi

Thanks for your request. You can use code like the following to remove empty rows from your document:

[Test]

public void Test001()

{

Document doc = new Document(@"Test001\out_Merged.doc");

RemoveEmptyRows(doc);

doc.Save(@"Test001\out.doc");

}

private static void RemoveEmptyRows(Document doc)

{

// Get all rows in the document.

Node[] rows = doc.GetChildNodes(NodeType.Row, true).ToArray();

foreach (Row row in rows)

{

// Remove row if it is empty.

if (string.IsNullOrEmpty(row.ToTxt().Trim()))

row.Remove();

}

}

Hope this helps.

Best regards,

Thx for your reply. This seems to be working.