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

Free Support Forum - aspose.com

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.

<span style=“font-size:
10.0pt;font-family:“Courier New”;color:#2B91AF;mso-no-proof:yes”>Document<span style=“font-size:10.0pt;font-family:“Courier New”;mso-no-proof:yes”> template =
new Document(“Template.docx”);<o:p></o:p>

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: http://www.aspose.com/documentation/.net-components/aspose.words-for-.net/howto-insert-a-document-into-another-document.html

<span lang=“NL” style=“font-size:10.0pt;font-family:“Courier New”;color:#2B91AF;mso-ansi-language:
NL;mso-no-proof:yes”>Document<span lang=“NL” style=“font-size:10.0pt;
font-family:“Courier New”;mso-ansi-language:NL;mso-no-proof:yes”> template = new Document(“Template.docx”);<o:p></o:p>

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,