Creating document which uses a .dot template file?

Hello,

This is a quick question. How do I create a new document based on an existing template? I cant seem to find this info on the wiki, also if my new document is made up of other documents using the importnode does this cause the new document to lose its formatting from the template it should be attached too?

Thanks!

The procedure of creation of document based on existing template is pretty much the same as opening the existing document and saving it under different name. For example,

Document doc = new Document(Application.StartupPath + @"\template.dot");

doc.Save(Application.StartupPath + @"\template.doc");

Concerning your second question - you can actually choose whether you want to keep the existing formatting of the appended sections or use the destination document styles.

That can be done by using ImportFormatMode parameter of the Document.ImportNode method.

I have tried the method you describe but rather then basing the new
document on the selected template and then Appending the other
documents to it.



I am getting a “copy” of the template and then, after the template pages I am getting the appended documents.



If that makes sense.





In other words the appended docs are appearing after the pages of the
template document that the new document should be based on.



Appended documents should appear from page 1 and onwards but are appearing at page 3 and onwards.

That is an expected behaviour. You can see the same thing when you append documents to template in MS Word. If you want your template to be just a source of styles and don’t want the content that is already in it then you need to remove the very first section of the resulting document by using Document.Sections[0].Remove.

I used this,



Document audDoc = new Document(myDocs + “\” + “Auditdocs.dot”);

audDoc.Document.Sections[0].Remove();



But now no documents that are appended appear?? I take it this is
because all sections are removed and these sections are then appeneded
to the remove section?



I decided to try what the Wiki suggests



audDoc.Document.Sections.Clear;



Although this is slightly better I still get some pages prior to the appended information.

Can you send me the template file you are using?

This is one of two templates that are used. Each template creates a seperate document. The problem occurs with both templates.

You should at first append your documents and then remove the first section by

doc.Sections[0].Remove();

Send me the code you are using if you still have trouble with this.

I can’t use this code because although it removes the pages it also
removes the headers and footers (as you are removing sections[0])



I do not wish to display the code here publically but perhaps I could
send you two dummy documents and you could write a simple demo for me
to work from?



I have attached two source documents. I would like to get 1.doc and
2.doc on the same page, with a header and footer using the template I
have provided earlier.



I would also like to see a demo where 1.doc and 2.doc are not on the
same page but on the same document, with header and footer using the
template.



If you could provide me with these two demos, I am sure I could apply the same methods to my actual code.




And 2.doc

Try this code. It worked fine on your sample documents.

private void buttonAppendDocs_Click(object sender, System.EventArgs e) {

string filename = Application.StartupPath + @"\Auditdocs.dot";

Document doc = new Document(filename);

AppendDocument(doc, new Document(Application.StartupPath + @"\1.doc"));

AppendDocument(doc, new Document(Application.StartupPath + @"\2.doc"));

doc.Save(System.IO.Path.GetFileNameWithoutExtension(filename) + "_modified.doc");

}

private void AppendDocument(Document dst, Document src) {

foreach(Section section in src.Sections) {

foreach(Node node in section.Body.ChildNodes) {

Node dstNode = dst.ImportNode(node, true);

((Section)dst.Sections[0]).Body.ChildNodes.Add(dstNode);

}

}

}