Create a document with two different sections from two different documents

I’m trying to create a single DOC/DOCX document with two sections from two different HTML input documents, one section for each input document. The reason for the different sections is that they must have different formatting (margins, line spacing, etc…).

I have come up with a solution that creates a new Document object for each of the input documents, adds formatting to them individually, and then appends the second to the first using the Document.AppendDocument method. The problem with this is that it requires me to write new formatting logic every time I want the formatting to be different (and so far that has not been straightforward). Ideally, what I would like to do is create a template for each type of output DOC/DOCX file that I would want to create, and then just load the two input documents into different sections within the template. I’m wondering what’s the best way to do this.

Thanks,
Matt

Hi Matt,

Thanks for your inquiry.

Sure, I think you can use code like below to achieve this. Please let us know if we can help with anything else.

Document template = new Document("Template.docx");
template.RemoveAllChildren();
Document doc1 = new Document("Doc1.html");
Document doc2 = new Document("Doc2.html");
template.AppendDocument(doc1, ImportFormatMode.UseDestinationStyles);
template.AppendDocument(do2, ImportFormatMode.UseDestinationStyles);

Thanks,

Does the method you outlined place each appended document into a different section? My template would have two sections, each with different margins, page-numbering in a footer, and line spacing. I want the content of the first input document to use the formatting of the first section in the template, and the content of the second input document to use the formatting of the second section in the template. It seems like the code you provided wouldn’t necessarily apply the formatting of the different sections to the content of the two different input documents as desired.

Please correct me if I am wrong.

Thanks for you timely response,
Matt

Hi Matt,

Thanks for this additional information.

You’re right, please try using the code below instead. This should generate the correct output. You can find the InsertDocument method here: https://docs.aspose.com/words/java/insert-and-append-documents/

Document template = new Document("Template.docx");
Document doc1 = new Document("Doc1.html");
Document doc2 = new Document("Doc2.html");
InsertDocument(template.FirstSection.Body.FirstParagraph, doc1);
InsertDocument(template.Sections[1].Body.FirstParagraph, doc2);

Thanks,

Unfortunately, this is still not quite what is desired, as the inserted paragraphs do not adopt the formatting of the paragraph that is already in the section. Also, the InsertDocument function as implemented appends nodes as opposed to replacing all the content in the section-- resulting in an empty paragraph at the beginning of each section.

I have created my own modified version of InsertDocument that uses the CompositeNode.InsertBefore method instead of InsertAfter, and then deletes the original first paragraph node (now the last) after all the imported nodes have been inserted. However, I still need these copied nodes to have the formatting of the original first paragraph. Is there a way to copy the formatting of that original first paragraph node into all the paragraphs that are being inserted?

Thanks,
matt

Hi Matt,

Thanks for your inquiry.

Could you please attach your input documents and expected output here for testing? There is no native way to copy formatting from one paragraph to another but we will be able to find a way which can achieve what you are looking for.

Thanks,