How to join two docs and save in one pdf?

I’m trying to join two docs and save in one pdf. I’m having problem with SectionStart.CONTINUOUS, I think. When the second doc has more than one page the SectionStart.CONTINUOUS doesn’t work. My code is:

BufferedInputStream in = new BufferedInputStream(new FileInputStream(new File("files/source.doc")));

Document doc = new Document(in);
doc.getRange().replace("_$#GANGUE#$_", "RATS", false, false);
doc.getFirstSection().getPageSetup().setSectionStart(SectionStart.CONTINUOUS);
Document result = new Document("files/result.doc");
for (Paragraph para : (Iterable)result.getChildNodes(NodeType.PARAGRAPH, true))
{
    para.getParagraphFormat().setKeepWithNext(true);
}

doc.appendDocument(result, ImportFormatMode.KEEP_SOURCE_FORMATTING);
doc.save("files/result.pdf");

Hi there,

Thanks for your inquiry. It would be great if you please share following detail for investigation purposes.

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

As soon as you get these pieces of information to
us we’ll start our investigation into your issue.

These are all the files. Thxs for you help. The result.doc is just a sample because the user can upload any doc.

Hi there,

Thanks for sharing the detail. Please set the SectionStart property as continuous of first section of result.doc as shown in following code snippet to get the required output.

Please let us know if you have any more queries.

BufferedInputStream in = new BufferedInputStream(new FileInputStream(new File("files/source.doc")));
Document doc = new Document(in);
doc.getRange().replace("_$#GANGUE#$_", "RATS", false, false);
Document result = new Document("files/result.doc");
result.getFirstSection().getPageSetup().setSectionStart(SectionStart.CONTINUOUS);
for (Paragraph para : (Iterable)result.getChildNodes(NodeType.PARAGRAPH, true))
{
    para.getParagraphFormat().setKeepWithNext(true);
}
doc.appendDocument(result, ImportFormatMode.KEEP_SOURCE_FORMATTING);
doc.save("files/result.pdf");
BufferedInputStream in = new BufferedInputStream(new FileInputStream(new File("files/source.doc")));

Document doc = new Document(in);
doc.getRange().replace("_$#GANGUE#$_", "RATS", false, false);
doc.getFirstSection().getPageSetup().setSectionStart(SectionStart.CONTINUOUS);
Document result = new Document("files/result2.doc");
result.getFirstSection().getPageSetup().setSectionStart(SectionStart.CONTINUOUS);
doc.appendDocument(result, ImportFormatMode.KEEP_SOURCE_FORMATTING);
doc.save("files/result.pdf");

Your code snippet is ok for the result.doc. So, I created a second file, result2.doc to test.With the result2.doc it doesn’t work. What I’m doing wrong?

Hi there,

Thanks for your inquiry. Please note that Aspose.Words mimics the same behavior as MS Word does. If you insert section break of type continues at the end of source.doc using MS Word and copy the contents of result2.doc after section break, you will get the same output.

In your case, I suggest you please use the following code example to achieve your
requirements. Please check the detail of insertDocument from here:
https://docs.aspose.com/words/java/insert-and-append-documents/

Document doc = new Document(MyDir + "source.doc");
doc.getRange().replace("_$#GANGUE#$_", "RATS", false, false);
Document result = new Document(MyDir + "result2.doc");
insertDocument(doc.getLastSection().getBody().getLastParagraph(), result);
doc.save(MyDir + "Out.pdf");