I am using Aspose.Words to dynamically build a document then I am converting it to PDF to render to our application. That is working fine. What I need to do now is dynamically hide or show some content in the resulting PDF. Is there a way I can do this?
Hello
Thanks for your inquiry. Yes of course it is possible. If you need to conditionally show/hide part of content in your document, then you should consider using IF fields. For instance, please see the attached template. If you press Alt+F9 you will see field codes. If condition is true you will see the first text if false the second. Here is a simple code I used for testing:
Document doc = new Document("in.doc");
doc.MailMerge.Execute(new string[]
{
"test"
}, new object[]
{
"true"
});
doc.Save("out.doc");
Hope this approach could help you.
In additional, you can do not use mergefields in IF fields at all. I put a mergefield just to demonstrate how IF fields work.
Please follow the link to learn more about IF fields:
https://support.microsoft.com/en-us/office/field-codes-if-field-9f79e82f-e53b-4ff5-9d2c-ae3b22b7eb5e
Best regards,
Does it work after the document is saved as PDF? Can you send me a link to where the IF fields are explained in your documentation? I am having trouble finding it.
Hello
Thanks for your request. It is MS Word feature.
https://support.microsoft.com/en-us/office/field-codes-if-field-9f79e82f-e53b-4ff5-9d2c-ae3b22b7eb5e
And yes after updating IF field you can convert your document to PDF.
Best regards,
Thanks for your quick response. My real question is can I have the dynamic content in the resulting pdf after conversion. I see that I can have it in Word.
Hello
Thanks for your request. Aspose.Words does not work with PDF documents; it works only with MS Word documents. So please ask your question regarding PDF in the Aspose.PDF.Kit forum:
https://forum.aspose.com/
Best regards,
AndreyN:
In additional, you can do not use mergefields in IF fields at all. I put a mergefield just to demonstrate how IF fields work.
Could you clarify what you mean by “you can do not use mergefields in IF fields at all.”
Can merge fields be used in IF fields or not?
Hi Charles,
Thanks for your inquiry. I would like to clarify here i.e. you can nest MergeFields inside IF fields for sure. I have attached a test document here for your reference. Please try running the following code snippet and observe the out.docx document:
Document doc = new Document(@"C:\temp\If-Merge-Fields.docx");
doc.MailMerge.Execute(new string[]
{
"testField"
},
new object[]
{
"a",
});
doc.UpdateFields();
doc.Save(@"c:\temp\out.docx");
Please let me know if I can be of any further assistance.
Best Regards,