We're sorry Aspose doesn't work properply without JavaScript enabled.

Free Support Forum - aspose.com

Re-order/move the contents within the worddoc

Hi,

I would like to know if we can re-order/move the contents within the word doc. re-ording like moving the paragraphs from one page to another page in a document.

suppose I have a word doc which has two pages, each page has a paragraph and i want to move the paragraph in page 2 to page 1.

Please let me know if there is way to achieve this requirement using Aspose words.

Regards,

Dharmender

Hi Dharmender,


Thanks for your inquiry. Yes, this is possible. Please attach your input Word document and expected document here for our reference. You can create your expected Word document using Microsoft Word. We will then provide you code to achieve the same using Aspose.Words.

Best regards,

Hi,

I have attached the input and expected output document for your reference.

The input document has two pages, on page I have section with some paragraphs and image and on page 2 I have a different section with some paragraph. Now I want to move the sections on page 2 to page 1, but before section 1 on page 1.

please let me know if we can achieve this through aspose words.

Hi Dharmender,

Thanks for your inquiry. In this case, I think you can implement the following work flow to achieve this:

  • Extract content of a particular page to a new temporary document in memory
  • Remove the content of that page from original document
  • Get a reference to node in main document where you want to place the extracted content
  • Insert document by using the code from this article

I have attached a few classes (pagesplitter.zip) here for your reference, please try executing the following code:

Document doc = new Document("sections.docx");

LayoutCollector layoutCollector = new LayoutCollector(doc);
doc.updatePageLayout();

DocumentPageSplitter splitter = new DocumentPageSplitter(layoutCollector);

Document pageDoc = splitter.getDocumentOfPage(2);

for (Node node : (Iterable<Node>) doc.getChildNodes(NodeType.ANY, true)) {
    if (layoutCollector.getStartPageIndex(node) == 2) {
        node.remove();
    }
}

insertDocument(doc.getFirstSection().getBody().getFirstParagraph(), pageDoc);

doc.save("out.docx");

I hope, this helps.

Best regards,

Hi,

Thanx for your sample code.

I have a document which has three sections in it, I am trying to reverse all these sections however while doing this all the sections breaks are getting lost. Below is the code that i wrote for reversing all the section and also attached the sample document.

Document srcDoc = new Document(location+srcPath);
System.out.println("no of sections "+srcDoc.getSections().getCount());

for (int i = srcDoc.getSections().getCount() - 2; i >= 0; i–){
// Copy the content of the current section to the beginning of the last section.
srcDoc.getLastSection().appendContent(srcDoc.getSections().get(i));
// Remove the copied section.
srcDoc.getSections().get(i).remove();
srcDoc.getLastSection().getPageSetup().setSectionStart(SectionStart.CONTINUOUS);
}
srcDoc.save(location + “sections_output.docx”);

Please help me if I went wrong anywhere.

And also I would like to know, if is there any possibility of re-order the sub-sections in a document.?

Hi Dharmender,


Thanks for your inquiry. You can use the following simple code to reverse Sections in a Document:
Document doc = new Document(getMyDir() + “sections.docx”);
Document reversedDoc = (Document)doc.deepClone(true);

reversedDoc.removeAllChildren();

for (int i = doc.getSections().getCount() - 1; i >= 0; i--)
{
reversedDoc.appendChild(reversedDoc.importNode(doc.getSections().get(i), true));
}

reversedDoc.save(getMyDir() + “out.docx”);

I hope, this helps.

Best regards,

Hi,

I also posted a query, if there is any chance of dividing a section into multiple sections using Aspose words api. like i have section (Section I) with three paragraphs, now i want each paragraph in this section as a seperate sub-section referring to the parent section (Section I)

is it possible?

If so please let me know the class using for the same.

I have attached the document FYR

Regards,
Dharmender

Hi Dharmender,


Thanks for your inquiry. Yes, this is possible using Aspose.Words. You just need to move the cursor to a location where you want to create a new Section and insert a section break using DocumentBuilder as follows:
Document doc = new Document(getMyDir() + “srcDocument.docx”);
DocumentBuilder builder = new DocumentBuilder(doc);

builder.moveToSection(2);
builder.moveToParagraph(2, 0);

builder.insertBreak(BreakType.SECTION_BREAK_CONTINUOUS);

doc.save(getMyDir() + “out.docx”);

I hope, this helps.

Best regards,

Hi,

I tried the above, however i am getting the below error when I testing with sample document. The sample document (PFA) contains 1 section with 3 paragraphs in it. I am inserting a section at the end of each paragraph. But when I am doing this I am getting below error.

I am using a for loop through which I am getting the paragraphs dynamically and inserting the section at the end of each paragraph.

for (int i = 1; i < paraCnt; i++) {
    System.out.println("currently working for paragraph " + i);
    builder.moveToParagraph(i, -1);
    //builder.insertBreak](https://builder.insertbreak/)(BreakType.PARAGRAPH_BREAK);
    builder.insertBreak(BreakType.SECTION_BREAK_CONTINUOUS);
}

java.lang.IllegalArgumentException: Parameter name: paraIdx
at com.aspose.words.DocumentBuilder.zzZ(Unknown Source)
at com.aspose.words.DocumentBuilder.moveToParagraph(Unknown Source)
at mergeworddocuments.java.MergeWordDocuments.splitSection(MergeWordDocuments.java:133)
at mergeworddocuments.java.MergeWordDocuments.main(MergeWordDocuments.java:42).

Please let me know if I am going wrong any where?

Hi Dharmender,


Thanks for sharing the detail. We are working over your query and will update you asap.

Hi Dharmender,


Thanks for your inquiry. Please use the following code to insert section after every paragraph:
Document doc = new Document(getMyDir() + “srcDocument.docx”);
DocumentBuilder builder = new DocumentBuilder(doc);

Node[] paras = doc.getChildNodes(NodeType.PARAGRAPH, true).toArray();
for (int i = 0; i < paras.length - 1; i++)
{
Paragraph para = (Paragraph)paras[i];
builder.moveTo(para);
builder.insertBreak(BreakType.SECTION_BREAK_CONTINUOUS);
}

doc.save(getMyDir() + “out.docx”);

I hope, this helps.

Best regards,