Code to Copy or Deep Clone Last or Any Section using Aspose.Words for Java | Clone Lists | Update List Labels

Hi,
Is there is a way to deepclone the document section not the last section. For example. I want to clone the section 1.2.1 for each list value using java code.How to get the section index for this to iterate. Please advise its urgent.

  1. section1
    1.1 section2
    1.2 section3
    1.2.1 want this section to deepclone for the list of data.
    1.3 section4
    1.3.1 .deepclone this section is easy as we can take the last section count and deepclone

@Bips

Thanks for your inquiry. We will appreciate it if you please confirm that you are interested in Aspose.Words for Java(on-premise API) or Aspose.Words Cloud SDK for Java(Rest API). As currently deepclone method is supported in Aspose.Words for Java(on-premise API). It will help us to answer your query exactly.

@Ahmad,
Thanks for the reply. Is for Aspose on-primise API. for example

int c3 = 1;
while (c3 < total1)
{
    int lastSectionIdx = doc.getSections().getCount() - 1;
    System.out.println("lastSectionIdx" + lastSectionIdx);
    com.aspose.words.Section newSection = doc.getSections().get(lastSectionIdx).deepClone();
    doc.getSections().add(newSection);
    c3++;
}

the above code will get always the last section of the list (1.3 +1.3.1 section) and deepclone. but I want it should deep clone list (1.2+ 1.2.1) section and (1.2.1) section as per requirement. But it always take last section and deep clone which is not my requirement.
Hope this gives a better idea. Plz let me know if you need more details to give solution.

Thanks!

@Ahmed Thanks for moving it to correct forum. Any solution?

@Bips,

Please ZIP and attach the following resources here for testing:

  • Your simplified input Word document
  • Your expected DOCX file showing the desired output. You can create this document manually by using MS Word. Please also list the complete steps that you performed in MS Word to create the expected document on your end.
  • A screenshot of the area that you want to deep clone.

As soon as you get these pieces of information ready, we will start investigation into your particular scenario/issue and provide you code to achieve the same by using Aspose.Words for Java.

Hi,
steps:
created a word document template as below, so that first section is present then trying to deepclone this section(1.2.1) for a list of items to repeat under 1.2 section. same applied to 1.3 as well.

  1. section1
    1.1 section2
    1.2 section3
    1.2.1 want this section to deepclone for the list of data.
    1.3 section4
    1.3.1 .deepclone this section is easy as we can take the last section count and deepclone

1.2.1 section should be deepcloned . so how to get this section and repeat it with a list. Please let me know if you need more info
Simple question is: how to clone a document template section in a position which is not at end of the template.

Thanks!

@Bips,

Each section in Word document is represented by a Section object that can be obtained from the Document.Sections collection by the index. The following Java code deep clones a Section and inserts it right after the original Section:

Document doc = new Document("C:\\temp\\input.docx");

Section section = doc.getSections().get(0); // use any index
Section cloneOfSection = (Section)section.deepClone(true);

doc.insertAfter(cloneOfSection, section);

doc.save("C:\\temp\\awjava-20.11.docx");

You can also find Section of a particular list item and clone it by using the following code:

Document doc = new Document("C:\\temp\\list.docx");
doc.updateListLabels();

Paragraph targetPara = null;
for (Paragraph para : (Iterable<Paragraph>)doc.getChildNodes(NodeType.PARAGRAPH, true))
{
    if (para.isListItem() & para.getListLabel().getLabelString().equals("1.2.1"))
    {
        targetPara = para;
    }
}

if (targetPara != null)
{
    Section section = (Section)targetPara.getAncestor(NodeType.SECTION);
    Section cloneOfSection = (Section)section.deepClone(true);
    // Lets insert after the original section
    doc.insertAfter(cloneOfSection, section);
}

doc.save("C:\\temp\\awjava-20.11.docx");

If this is not what your requirement is, then please provide the resources which were requested in my previous post. We will then investigate the scenario/issue on our end and provide you code to achieve the same output by using Aspose.Words.

Hi,
Thanks for the info but the 1.2.1 is a dynamic section so we cant hardcode the value coz this value is for bookmark. we need some code which can dynamically identify this section.

Section section = doc.getSections().get(0); // use any index - for this finding the index dynamically is the requirement we cant hardcode 0. requirement is how to find this index for a particulate dynamic section
Will wait for your reply.

Also I tried your code which is cloning the 1.3 + 1.3.1 1 time not 1.2.1.
Please advice. Also is there a way to attach file not getting option

@Bips,

You will find an Upload button in the reply window that you can use to attach requested files. If your ZIP file has bigger size than the attachment limit in this forum then you may upload the ZIP file to Dropbox or any other file hosting service and share the download link here for testing.

https://drive.google.com/file/d/1eyjesr0ql07cupxxc1iune9xnchmsvlg/view?usp=sharing
please find the link let me know if you can able to open the files

@Bips,

Yes, I can access the link but it is only a Java code. But, we need sample documents as well (i.e. source & expected files). After that we will be able to provide code to achieve the same output by using Aspose.Words.

Hi Hafeez,
Thanks for the reply. I am able to achieve it with a different way of coding. If I need further help I will msg back.

Regards,
Bips

@Bips,

It is great that you were able to find what you were looking for. Please let us know any time you may have any further queries in future.