Duplicating Paragraphs

Hello,

I have a list of objects consistent of different attributes - such as number, name, description. I want the user to be able to define their own template in word, which we will be using to write our report.
For example:

{{Template_Begin}}
{{Name}} - {{Number}}
{{Description}}
{{Template_End}}

I want to keep the formatting from the user (Cursive, Bold, Font, etc.) and replace that section with a list of my objects with the same formating as the template.
I tried some approaches, but most of them have the problem, that it might be unnecessary complex or simply don’t work.
Currently the adding of new nodes (Paragraphs) to the NodeCollection is not yet supported. Is there an easy way to solve my problem?

Best Regards

Hi Devin,

Thanks for your inquiry. We will appreciate it if you please share following resources here. It will help us to understand your requirements and address it accordingly.

  • Your sample Word template document.
  • Your expected output document, you may create it using MS Word.
  • Your working existing code(without compilation errors)

Best Regards,

Hello,

Because I am not yet allowed to upload files, I will post an example of the document:
Template Document:

Hello World

{{#PINS}}
Pin {{NUMBER}} - {{NAME}}
{{DESCRIPTION}}

{{/PINS}}

Ending Text!

Target Document:

Hello World

Pin 1 - Mockname1
MockDescription1

Pin 2 - Mockname2
MockDescription2

Pin 3 - Mockname3
MockDescription3

Ending Text!

Current code:

    // Open the document.
    Document doc = new Document("TestDoc.docx");
    List<Paragraph> paraList = new ArrayList<>();
    // Get all paragraphs
    NodeCollection paragraphs = doc.getChildNodes(NodeType.PARAGRAPH, true);

    // Wether its template text or not
    boolean placeholder = false;
    // Go through all paragraphs and get template text
    for(int paraId = 0; paraId < paragraphs.getCount(); paraId++){
        Paragraph para = (Paragraph) paragraphs.get(paraId);
        if(para.getText().contains("{{#PINS}}")){
            placeholder = true;
        } else if (para.getText().contains("{{/PINS}}")) {
            placeholder = false;
        } else if (placeholder) {
            paraList.add(para);
        }
    }

    // Todo Duplicate Paragraphs and replace placeholder text with object attributes

    // Inserting paragraphs into the document
    for(Paragraph para : paraList){
        // Todo move to the right spot to insert
        // paragraphs.add(para); // Is not supported
    }
    doc.save("Finished.docx");

Best Regards,

Hi Devin,

Thanks for your inquiry. Please note adding Paragraph node to NodeCollection is supported in Aspose.Words. It seems your requirement is more related to Mail Merge. Please check following documentation links for more details. Hopefully it will help you to accomplish the task.

However, if there is some difference in your requirements and my understanding then please share some more details. Furthermore, our web team has resolved the file attachment issue. Now, you can attach your documents to post.

Best Regards,