Page breaks inbetween merged documents - how to remove or disable page breaking?

For some reason, after I append/merge documents, there is a page break inserted inbetween each merged item. Is it possible to merge them without having page breaks inserted?

ArrayList documents = new ArrayList();
documents.add(new Document(_javaFileDocx1));
documents.add(new Document(_javaFileDocx3));
documents.add(new Document(_javaFileDocx3));
try
{
    Document blank = new Document();
    for (Document document: documents)
    {
        blank.appendDocument(document, ImportFormatMode.USE_DESTINATION_STYLES);
    }
    blank.save(dest, SaveFormat.DOCX);
}
catch (Exception e)
{
    System.out.println(e);
}

@emailtest99999999

Thank you for your inquiry. Yes you can remove the Page & Section Breaks. Please have a look at this sample code for continues document merge Merged_Docs.zip (48.7 KB).

String dataDir = Utils.getDataDir(MergeDocuments.class);

ArrayList<Document> lstdocuments = new ArrayList<Document>();
lstdocuments.add(new Document(dataDir + "JavaDoc1.docx"));
lstdocuments.add(new Document(dataDir + "JavaDoc2.docx"));
lstdocuments.add(new Document(dataDir + "JavaDoc3.docx"));

Document blank = new Document();

for (Document document:lstdocuments)
{
    blank.appendDocument(document, ImportFormatMode.USE_DESTINATION_STYLES);
}

for (int i = blank.getSections().getCount() - 2; i >= 0; i--)
{
    // Copy the content of the current section to the beginning of the last section.
    blank.getLastSection().prependContent(blank.getSections().get(i));
    // Remove the copied section.
    blank.getSections().get(i).remove();
}

// OR remove Page Break in documents

NodeCollection paragraphs = blank.getChildNodes(NodeType.PARAGRAPH, true);

// Iterate through all paragraphs
for (Paragraph para : (Iterable<Paragraph>)paragraphs)
{
    // If the paragraph has a page break before set then clear it.
    if (para.getParagraphFormat().getPageBreakBefore())
        para.getParagraphFormat().setPageBreakBefore(false);

    // Check all runs in the paragraph for page breaks and remove them.
    for (Run run : (Iterable<Run>)para.getRuns())
    {
        if (run.getText().contains(ControlChar.PAGE_BREAK))
            run.setText(run.getText().replace(ControlChar.PAGE_BREAK, ""));
    }
}

blank.save(dataDir + "Merged.docx", SaveFormat.DOCX);

hi,
thanks for the response…

I tried both methods, but the page breaks are still there…

@emailtest99999999,

Thanks for your inquiry. Please use the following code example to get the desired output.

If you still face problem, please ZIP and attach your input Word documents here for testing. We will investigate the issue and provide you more information about your query.

ArrayList<Document> lstdocuments = new ArrayList<Document>();
lstdocuments.add(new Document(MyDir + "JavaDoc1.docx"));
lstdocuments.add(new Document(MyDir + "JavaDoc2.docx"));
lstdocuments.add(new Document(MyDir + "JavaDoc3.docx"));

Document blank = new Document();
blank.removeAllChildren();
for (Document document : lstdocuments)
{
    document.getFirstSection().getPageSetup().setSectionStart(SectionStart.CONTINUOUS);
    blank.appendDocument(document, ImportFormatMode.USE_DESTINATION_STYLES);
}

blank.save(MyDir + "output.docx");
import java.io.File;
import java.util.ArrayList;
import java.util.List;

import com.aspose.words.Body;
import com.aspose.words.ControlChar;
import com.aspose.words.Document;
import com.aspose.words.DocumentBuilder;
import com.aspose.words.HtmlSaveOptions;
import com.aspose.words.ImportFormatMode;
import com.aspose.words.License;
import com.aspose.words.LoadOptions;
import com.aspose.words.MailMerge;
import com.aspose.words.NodeCollection;
import com.aspose.words.NodeType;
import com.aspose.words.Paragraph;
import com.aspose.words.Run;
import com.aspose.words.SaveFormat;
import com.aspose.words.SectionStart;
import com.aspose.words.net.System.Data.DataSet;

public class AsposeForum
{
    public static void main(String[] z)
    {
        final String S = File.separator;
        final String FOLDER = PropertiesUtil.javaroot() + S + "hello";
        new File(FOLDER).mkdir();
        String path1 = FOLDER + S + "1.docx";
        System.out.println(path1 + " exists?:" + new File(path1).exists());
        String path2 = FOLDER + S + "2.docx";
        System.out.println(path2 + " exists?:" + new File(path2).exists());
        String pathSoln1 = FOLDER + S + "result1.docx";
        String pathSoln2 = FOLDER + S + "result2.docx";
        try
        {
            new License().setLicense("Aspose.Words.lic");
            List <Document> documents = new ArrayList <Document> ();
            documents.add(new Document(path1));
            documents.add(new Document(path2));
            new AsposeForum().forumSolution1(documents, pathSoln1);
            new AsposeForum().forumSolution2(documents, pathSoln2);

        }
        catch (Exception e)
        {
            for (StackTraceElement se: e.getStackTrace())
            {
                System.out.println(se);
            }
        }
    }
    public void forumSolution1(java.util.List <Document> documents, String savePath)
    {
        try
        {
            Document blank = new Document();
            blank.removeAllChildren();
            for (Document document: documents)
            {
                document.getFirstSection().getPageSetup().setSectionStart(SectionStart.CONTINUOUS);
                blank.appendDocument(document, ImportFormatMode.USE_DESTINATION_STYLES);
            }
            blank.save(savePath);
            System.out.println("appended document:" + savePath);
        }
        catch (Exception e)
        {
            for (StackTraceElement se: e.getStackTrace())
            {
                System.out.println(se);
            }
        }
    }
    public void forumSolution2(java.util.List <Document> documents, String savePath)
    {
        try
        {
            Document blank = new Document();
            blank.removeAllChildren();
            for (Document document: documents)
            {
                document.getFirstSection().getPageSetup().setSectionStart(SectionStart.CONTINUOUS);
                blank.appendDocument(document, ImportFormatMode.USE_DESTINATION_STYLES);
            }

            NodeCollection paragraphs = blank.getChildNodes(NodeType.PARAGRAPH, true);

            // Iterate through all paragraphs
            for (Paragraph para: (Iterable <Paragraph> ) paragraphs)
            {
                // If the paragraph has a page break before set then clear it.
                if (para.getParagraphFormat().getPageBreakBefore())
                    para.getParagraphFormat().setPageBreakBefore(false);

                // Check all runs in the paragraph for page breaks and remove them.
                for (Run run: (Iterable <Run> ) para.getRuns())
                {
                    if (run.getText().contains(ControlChar.PAGE_BREAK))
                        run.setText(run.getText().replace(ControlChar.PAGE_BREAK, ""));
                }
            }

            blank.save(savePath);
            System.out.println("appended document:" + savePath);
        }
        catch (Exception e)
        {
            for (StackTraceElement se: e.getStackTrace())
            {
                System.out.println(se);
            }
        }
    }
}

the code is still not working.

I cut and pasted the sandboxed test code above. There are two files, 1.docx, 2.docx which both contain the text “hello world”.

https://docs.aspose.com/words/java/working-with-sections/
This link above has the code that you recommended.It looks the same as the code you recommended to me. after finding this, I tried this method too, but didn’t work either.

@emailtest99999999,

Thanks for your inquiry. In case, you are using older version of Aspose.Words, we suggest you please upgrade to the latest version of Aspose.Words for Java 18.1. If you still face problem, please attach the following resources here for testing:

  • Your input Word documents.
  • Please attach the output Word file that shows the undesired behavior.

As soon as you get these pieces of information ready, we’ll start investigation into your issue and provide you more information. Thanks for your cooperation.

PS: To attach these resources, please zip and upload them.

i had been using 17… i downloaded and added to build path the 18.1 version you gave link to… the issue with page breaks inserted between appended documents is still present…
here is java test + upload folder with the documents appended and result
asposetest.zip (29.8 KB)

@emailtest99999999,

Thanks for your inquiry. The result.docx contains the section break continuous. There is no page break in the document. Please check the attached image for detail. section break.png (15.2 KB)

If you want to remove the section breaks from the document, please use following method. Hope this helps you.

private static void RemoveSectionBreaks2(Document doc) throws  Exception
{
    // Loop through all sections starting from the section that precedes the last one
    // and moving to the first section.
    for (int i = doc.getSections().getCount() - 2; i >= 0; i--)
    {
        // Copy the content of the current section to the beginning of the last section.
        doc.getLastSection().prependContent(doc.getSections().get(i));
        // Remove the copied section.
        doc.getSections().get(i).remove();
    }
}

good, thank you for the clarification on that. I think you posted that code before- maybe I made mistake with something, but in any case now it’s working fine with no page/section breaks.

@emailtest99999999,

Thanks for your feedback. It is nice to hear from you that your problem has been solved. Please feel free to ask if you have any question about Aspose.Words, we will be happy to help you.