Query Link: RTF Formfield/Mergefield manipulation query- Posting on Public forum as well
@vijeesh.kannoly Yes, you can implement the above requirements using Aspose.Words. Please see our documentation to learn how to use mail merge feature using Aspose.Words:
https://docs.aspose.com/words/net/types-of-mail-merge-operations/
Here you can learn how to work with forma fields:
https://docs.aspose.com/words/net/working-with-form-fields/
Great.
As a novice , have many queries as i am trying to dig deep into these features for my use case.
1)I do not see this below code line reading the FormFields(from existing RTF) into the string array.
string[] fieldNames = doc.MailMerge.GetFieldNames();
What would be the right code snippet to identify these fields to be applied with Mail Merge?
2)Can i use MailMerge with mustache syntax and FormField manipulation together on an existing RTF?
I ask this because as soon as i use below code with the intention of applying mustache syntax technique to merge/replace all new[] with another object[] it removes the Formfield manipulation(a simple field replacement with read in value) already done before.
doc.MailMerge.UseNonMergeFields = true;
doc.MailMerge.Execute(new[] { “someName” }, new object[] { “someOne” });
What am i doing wrong here?
@alexey.noskov Thanks for your quick responses. I will get a free license to test and prove the proposal, eventually we would move ahead to purchase license.
The above code is not supposed to read form fields. It returns merge field names in the document. If you need to work with form fields you should use Document.Range.FormFields property. Form fields and merge fields are different things.
Sure you can. But you should note that upon executing mail merge Aspose.Words the same as MS Word update all fields in the document. When fields are updated field values are recalculated and form fields values are reset to their original state. This is an expected behavior. You will observe the same if update fields in MS Word.
You can try to lock form fields to avoid this. Please see the following code:
Document doc = new Document("C:\\Temp\\in.docx");
// Lock form fields.
foreach (Field f in doc.Range.Fields)
{
if (f.Type == FieldType.FieldFormTextInput ||
f.Type == FieldType.FieldFormDropDown ||
f.Type == FieldType.FieldFormCheckBox)
f.IsLocked = true;
}
// Execute mail merge here
// .............
doc.Save("C:\\Temp\\out.docx");
Alternatively, you can simply execute mail merge before editing form fields in your document.
1)FormField vs Mailmerge-i got that. I tried [Document.Range.FormFields] already and that is working for my use case as expected. So would you be able to give me a syntactical representation of fields on a docx/RTF for MergeFields?
Anything like below would be identified as Merge fields or plus some tagstart and tagend?
Hello <<Name>>
2)FormField Lock- That’s something pretty useful!
One more interesting challenge to resolve-I have existing RTF template with FormFields and the formfield help text acting as code/logic based on the input values read in/send in while the document rendering:
Formfield help text 1:
<?if@inlines:srName!=''?><?srName?><?end if?>
Formfield help text 2:
<?for-each@inlines:DefBen?>
Formfield help text 3:
<?xdoxslt:set_variable($_XDOCTX, 'DdName', DdName)?>
Below image shows how its captured now(some example of those in Formfield.StatusText are provided above):-
image.png (33.4 KB)
Hope you understand what my scenario is, if not please shoot back. if you get it, Do you think this is something Aspose FormFields feature can programmatically(may be with some additional customized code) could handle?
Obviously some code snippets could help.
In a nutshell, i have some RTF templates with formfields and those formfield help text with simple conditional code/logic which would work based on input values(datasource could be xml) and this eventually render into the Final Letter/Document-DO YOU THINK THERE’s ANY OTHER BEST SUITABLE ASPOSE FEATURE/s FOR THIS USE CASE- something like a LINQ Reporting Engine or other??
thanks a bunch @alexey.noskov !!
@vijeesh.kannoly Merge field is a regular MS Word field with MERGEFIELD
field code:
test.zip (10.6 KB)
Alternatively you can use mustache syntax and define merge field like this {{test}}
.
Unfortunately, the requirement is not quite clear. If possible, could you please provide you input and expected output documents? This will help us to better understand the requirements.
I dont think i can share artifacts.
In a nutshell, i have some RTF templates with formfields and those formfield help text with simple conditional code/logic which would work based on input values(datasource could be xml) and this eventually render into the Final Letter/Document-DO YOU THINK THERE’s ANY OTHER BEST SUITABLE ASPOSE FEATURE/s FOR THIS USE CASE- something like a LINQ Reporting Engine or other??
Let me provide some snips:
- Input RTF template with form fields
formfield: DATE_MM/DD/YY, logic inside formfield help text: `<?format-date:date;'MM/DD/YYYY'?>
formfield: IF_nameTypesrNameEF, logic inside formfield help text: if@inlines:srName!=‘’?><?srName?><?end if?>`
- Input data XML:
Output Document(one of those name as per condition eventually appears on output document and its address):
The best i could do-hope you could help and guide me! @alexey.noskov
@vijeesh.kannoly Conditional blocks in the documents can be achieved in two ways:
-
Using standard MS Word
IF
fields conditions. IF fields are updated upon executing mail merge or when fields in the document are updated explicitly. In addition, you can configure mail merge to remove containing fields upon execution using MailMergeCleanupOptions.RemoveContainingFields. -
Using LINQ Reporting Syntax. This approach is more flexible than mail merge and IF fields conditions.
@alexey.noskov
I think you are referring towards IF field or conditional block which Aspose can handle while rendering the document programmatically-like Aspose provides features/interfaces for such conditional handing to stitch paragraph/data replacement and yes this is work well with a project rendering/creating the document
If Field condition work the way on RTF as well? The constraint would be to integrate the way it should work on with existing RTF template conditional blocks i depicted in previous messages- Do you think you could provide a code snippet working with RTF?
Yep, as i was researching further I noticed LINQ reporting also looked more suitable for the use case. Above artifacts or example did not help articulate the requirement. I was looking more in to this part of code under LINQ Reporting Engine - Accessing XML Data in C#|Aspose.Words for .NET segment to be customized for the requirement. what’s your thoughts?
The major challenge i am trying to resolve(or trying to figure out the best suitable feature available to handle) using Aspose is, the internal macro/conditional logic blocks in the document form field help text as i have shown above.
@vijeesh.kannoly The code is the same for RTF as for any other supported document formats.
Yes, RTF format supports fields. So this option will work with RFT too.
Unfortunately, I am not sure what conditions are used in your templates. Could you please provide a simple template example for our reference?
@alexey.noskov
Ok, i think i did above.
Please refer to below attached docx(remember originally its an RTF).
TEST.docx (31.9 KB)
So double-click on each FormField and go to ‘Add Help Text’, you would see a conditional logic which works as it recognizes the data/value coming in.
See the portions where you have more than one IF_nameType, all those has similar condition logic behind in Add Help Text and it works like many to one-only one will have data coming in and it just resolves or renders into that specified form field location on final document.
Refer also to Foreach_DBData one which conditionally in some cases fill in data to render a tabular structure into the final document.
At present, these RTF are rendered by Oracle BIP tool(low code or no code technique). As part of migration i need to know if there’s Aspose compatible feature for these behind the scene logic(conditional block) to work (low code - no code).
@vijeesh.kannoly Thank you for additional information. The conditions set in status text is kind of custom logic handled by your old third-party tool. I am afraid Aspose.Words does not have a built-in functionality to handle this conditional logic. So you should either modify your template and use standard MS Word IF
fields conditions and merge fields or LINQ Reporting syntax.
@alexey.noskov Yeah my trials were also showing same. Thanks for your confirmation.
a) But do you think this status field conditions are being fed with required data and programmatically achieve the resultant rendering(which i believe isn’t impossible).
b) As far as the MS Word IF Fields goes, will you be able to provide a reference function/code to replicate?
c) Possibly one with LINQ too.
d) What’s the functionality difference between MergeField and LINQ from Aspose perspective?
FYA-Oracle BI Publisher is the tool being utilized which helps below functionalities(for which i was looking replacement features in ASPOSE).
- You can add BI Publisher’s simplified syntax (e.g.,
<?if:condition?>
,<?end if?>
) to your RTF template to control data flow, conditional formatting, and other advanced features. This syntax is then translated into XSL-FO during the rendering process. - Form Field Method: Use Microsoft Word’s form field feature to store BI Publisher syntax in hidden fields, allowing for more complex templates and the use of XSL or XSL-FO code.
- Conditional Formatting: BI Publisher’s
<?if:condition?>
syntax allows you to conditionally display data, format elements, or apply
Most likely the tool uses content of status filed for the field value formatting and calculation. What is the internal implementation is not clear.
Sure, here is simple template with IF field condition and code:
Document doc = new Document(@"C:\Temp\in.rtf");
doc.MailMerge.CleanupOptions = MailMergeCleanupOptions.RemoveContainingFields;
doc.MailMerge.Execute(new string[] { "test" }, new string[] { "true" });
doc.Save(@"C:\Temp\out.rtf");
MailMerge.zip (18.9 KB)
Press Alt+F9
in MS Word to see field codes.
Here is the same for LINQ:
Document doc = new Document(@"C:\Temp\in.rtf");
ReportingEngine engine = new ReportingEngine();
engine.BuildReport(doc, "true", "test");
doc.Save(@"C:\Temp\out.rtf");
linq.zip (18.8 KB)
Mail merge uses standard MS Word merge fields, the same as MS Word mail merge functionality. LINQ Reporting Engine was developed to provide more flexibility and overcome mail merge report generation restrictions.
Thanks a lot @alexey.noskov
Exactly the internal handing by Oracle BIP is the caveat.
One constraint to keep that internal conditional logic is because that’s something been utilized by non technical business people(with little training) to change or edit with ease so that final product/letter outcome is the way they want it to be.
Would you mind providing an extra briefing on what Mail merge restrictions does LINQ reporting essentially overcome?
@vijeesh.kannoly Mail Merge compared to LINQ Reporting Engine have limited syntax. For example, LINQ Reporting Engine allows filling DML Charts with data. It supports Table-Column Data Bands ad a lot more features that are not available in standard mail merge. You can find full list of features in our documentation:
https://docs.aspose.com/words/net/template-syntax/