Advance WORD docx Manipulation

Hi All,

I am looking for a plug-in or third party api which can allow me to manipulate word docx template from outsystems.

Example of word DOCX template

Section 1
Sub Section 1.1
Sub Section 1.2
Sub Section 1.3
Sub Section 1.4

Depending on conditions/rules,

  1. entire section (i.e. Section 1 - including sub section 1.1 to 1.4) in docx file can be removed

  2. some of the Sub sections (e.g. Sub Section 1.2, Sub Section 1.4, etc.) can be removed

  3. specific words in individual sub sections can be removed /replaced

This can be achieved in a legacy low code system (OOTB apparently) which we are considering moving out.

Are there such features in Apose word api/plug-in so that i can leverage from outsystems?

Thank you for your guidance.

Cheers!

@KeithOon You can use Aspose.Words for document editing. Please see our documentation to learn more:
https://docs.aspose.com/words/net/aspose-words-document-object-model/

You can also use find/replace functionality:
https://docs.aspose.com/words/net/find-and-replace/

Please feel free to ask in case of any difficulties in implementing your requirements we will be glad to assist you.

thanks. need some further guidance. Does Aspose include features to delete?

Below example to depict how the existing solution is deleting

{{#Condition1=True}}
5. 
   5.1 ...
     5.1.1. ...
    {{#Condition2=True}}
     5.1.2 ... 
     5.1.3 ... 
     {{/}}
     5.1.4 ...
{{/}}

Note:

  1. If condition1 is not True, entire section 5 will be removed
  2. if condition1 is True and condition2 is also True, then 5.1.1 to 5.1.4 will be displayed
  3. if condition1 is True and condition2 is NOT True, then 5.1.2 and 5.1.3 will be removed; and because its removing 5.1.2 and 5.1.3, as a standard word behaviour, the original 5.1.4 will be reflected as 5.1.2 ensuring that the numbering is still correct.

This is quite an interesting way of doing, as it ensures that rules can be easily set, and the docx template is something which a normal end user can managed (with some basic level of training). Wonder can aspose achieve something like this? Or how will Aspose achieve this requirement need?

As we are currently moving out of the legacy solution, i am looking for a third party plug-in which can achieve the same. hope to get some good news from you.

cheers!

@KeithOon We have LINQ Reporting Engine, that provides similar functionality. In your case you can use <<if>> conditions, which will be evaluated upon building a report:
https://docs.aspose.com/words/net/using-conditional-blocks/

The syntax is quite simple and intuitive especially if the uses is a little familiar with programming.

Hi Alexey,

Looking at the reference link, couple of questions to clarify as I am trying to maniupulate a docx template,

  1. the earlier link you provided on “find and replace” is under the section “Programming with documents” which i presume can be used to manipulate docx template. please confirm

  2. the second link provided “using conditional blocks” is under “LINQ Reporting Engine”. Can this conditional block be also used on a docx template? or its only used for reporting purposes?

if there is any sample codes or links to how others used this conditional blocks in documents, appreciate if you can share them over. cheers!!

thank you for your clarification.

@KeithOon Aspose.Words supports a wide range of document formats as an input:
https://docs.aspose.com/words/net/supported-document-formats/

You can use both find/replace and LINQ Reporting Engine with any of them. After loading the document there is no difference what was the input, the document is already loaded into the Aspose.Words DOM and you can manipulate it as it is required.

Here is simple code that demonstrates how to use conditional blocks:

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

ReportingEngine engine = new ReportingEngine();
engine.Options = ReportBuildOptions.RemoveEmptyParagraphs;
// A simple variable is used as data source, but you can use JSON, XML or complex objects as datasource
engine.BuildReport(doc, true, "test");

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

in.docx (12.8 KB)
out.docx (10.0 KB)

Thank you once again Alexey.

  1. Is there any specific documentation which can guide me on how to get started with Aspose? (i.e. how to consume the API?). I believe there is a trial version of 30 days or so.

  2. If eventually i want to use aspose, can this be installed on a server of my choice? due to sensitivity of my project.

thank you.

@KeithOon

  1. Sure, you can learn how to get started with Aspose.Words in our documentation:
    https://docs.aspose.com/words/net/installation/
    Aspose.Words is a class library and you can install it from NuGet and use as any other .NET class library. In evaluation mode Aspose.Words has only two limitations: it limits the maximum size of the processed document to several hundred of paragraphs and injects an evaluation watermark into the document. If you would like to test Aspose.Words without evaluation version limitations, you can request a free 30-days temporary license . Please see our documentation to learn more about licensing:
    https://docs.aspose.com/words/net/licensing/

  2. Sure, as it was mentioned above, Aspose.Words is a stand alone .NET library, so you can use it on the server of your choice as any other .NET library.

Hi Alexey,

By installing the " Aspose.Words for .NET", does it include the “LINQ Reporting Engine” capability? or i need to install it separately? if yes, kindly guide how to get it installed?

thank you.

@KeithOon Yes, LINQ Reporting Engine is a part of Aspose.Words library, you don’t need to install any additional libraries.

hi,

Referring to the below which Alexey shared earlier, if i am passing over as binary (i.e. byte[]),

  1. How can i leverage on below to receive a byte array, remove the required text (as per earlier sample) and pass it back as a byte array?

  2. is there a function that i can convert a byte array to “Aspose.Word.Document” format which is required for engine.BuildReport below?

screenshot.png (42.0 KB)

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

ReportingEngine engine = new ReportingEngine();
engine.Options = ReportBuildOptions.RemoveEmptyParagraphs;
// A simple variable is used as data source, but you can use JSON, XML or complex objects as datasource
engine.BuildReport(doc, true, "test");

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

@KeithOon You can use stream to read bytes and save it to bytes:

Document doc;
using (MemoryStream inputStream = new MemoryStream(bytes))
    doc = new Document(inputStream);

using (MemoryStream outputStream = new MemoryStream())
{
    doc.Save(outputStream, SaveFormat.Docx);
    var outputBytes = outputStream.ToArray();
}

thanks Vyacheslav and Alexey, you both have being very helpful. Slowly but surely, i will get there.

understand for buildreport, i can pass in a json, is there any examples on this? or where can i find these examples on the aspose documentation myself? currently, struggling abit on this as i am trying to pass in multiple conditions, so that i can build many if else and nested if else if possible in docx template.

// A simple variable is used as data source, but you can use JSON, XML or complex objects as datasource
engine.BuildReport(doc, true, "test");
1 Like

@KeithOon You can check our GitHub examples with ReportingEngine. For example part with JSON is here.

hi,

Using below, i was able to achieve what i need of conditionally removing paragraphs or sections. Is it possible to achieve the same without having to conditionally declare a class structure in C#? will be good as if we are able to do so. In event if the template changes. the C# code is dynamic enough to handle it. I was trying to look at json as a source to see any examples of it but was unsuccessful. Can you provide some guidance?

using System;
using System.Collections; // Add this using directive for IEnumerator
using Aspose.Words;
using Aspose.Words.Reporting;

namespace ReportGeneratorApp
{
    public class ConditionalData : IEnumerable
    {
        public string TestA { get; set; }
        public string TestB { get; set; }

        // Implement IEnumerable explicitly
        public IEnumerator GetEnumerator()
        {
            yield return this;
        }
    }

    class Program
    {
        static void Main(string[] args)
        {
            // Load the document
            Document doc = new Document(@"C:\input.docx");

            // Create an instance of your data source
            var conditionalData = new ConditionalData
            {
               TestA = "CorrectApproach",
                TestB = "Yes"
            };

            // Initialize the reporting engine
            ReportingEngine engine = new ReportingEngine();
            engine.Options = ReportBuildOptions.RemoveEmptyParagraphs;

            // Build the report using the document and conditional data
            engine.BuildReport(doc, conditionalData, "");

            // Save the output document with a specific name
            doc.Save(@"C:\output.docx");

            Console.WriteLine("Report generation completed.");
        }
    }
}

@KeithOon Have you checked the json sources example provided in my previous post?

@KeithOon

Accessing JSON Data might also be helpful.

thanks @vyacheslav.deryushev and @ivan.lyagin . What Ivan suggested works for me. Just got to do more testing

// Initialize the JsonDataSource with the JSON string
JsonDataSource jsonDataSource = new JsonDataSource(new MemoryStream(System.Text.Encoding.UTF8.GetBytes(ssJsonString)));

// Build the report using the document and JSON data
engine.BuildReport(docx, jsonDataSource, "jsonData");

Hi Guys,

its me a again. now i have another need from business.

is there a plugin that can be installed in OFFICE WORD from Aspose?

check out this youtube on this platform “Neota Logic” which was previously used by business. (3:14min onwards for demo)
AL TV Product Walk Through - Neota Logic - Doc Automation (youtube.com)
With a plug-in installed in WORD, anyone is able to create “variables” or “conditions” in DOCX template via GUI and then subsequently import the word template in the platform
The experience liked by business is that this significantly reduce mistake due to typos, etc.

Does Aspose has any extended feature to install plugin in WORD?

If no, is there any possibility of achieving this kind of capability using the various methods/functions available in Aspose?

cheers!

@KeithOon Generally, Aspose.Words is a .NET library and you can use it in your add-in project just like any other .NET library. Please see Microsoft documentation to learn how to create add-in projects:
https://learn.microsoft.com/en-us/office/dev/add-ins/develop/develop-add-ins-visual-studio

Also, we have a task WORDSNET-25915 to investigate possibility of using Aspose.Words for creating Word add-in using Office JavaScript (Office.js). We will keep you updated and let you know once analysis is done.

1 Like