Creating a new Microsoft Word document from a template using OpenXml
来源:互联网 发布:域名dns服务器查询 编辑:程序博客网 时间:2024/06/16 03:38
http://blogs.msdn.com/b/mcsuksoldev/archive/2010/04/09/creating-a-new-microsoft-word-document-from-a-template-using-openxml.aspx
When working with Microsoft Word automation there is often the requirement to create documents from define templates. With the addition of the OpenXml specification it is now possible to do this safely within server side code.
In a recent project I came across this requirement where a new Word document needed to be created based upon a template, with the addition of adding a custom XML part into the document. The custom XML part was used to perform bindings to a content controls within the document. Using OpenXml this became an easy task, as I will show.
The main processing I performed to achieve the Word Document creation was opening the required template document as a stream, and then creating a new document, as a MemoryStream, based on this based on this template:
using (MemoryStream documentStream = new MemoryStream((int)this.templateStream.Length)){ templateStream.Position = 0L; byte[] buffer = new byte[2048]; int length = buffer.Length; int size; while ((size = templateStream.Read(buffer, 0, length)) != 0) { documentStream.Write(buffer, 0, size); } documentStream.Position = 0L; // Modify the document to ensure it is correctly marked as a Document and not Template using (WordprocessingDocument document = WordprocessingDocument.Open(documentStream, true)) { document.ChangeDocumentType(DocumentFormat.OpenXml.WordprocessingDocumentType.Document); } // Add the XML into the document and save to the correct location. ProcessDocumentXml(inputDocument, documentStream); File.WriteAllBytes(documentPath, documentStream.ToArray());}
The ChangeDocumentType operation is critical to this process. This operation, provided by the OpenXml SDK, ensures the document is no longer marked as a Template but rather as a Document.
In this example I save stream as a file to a defined document path. One could just as easily write the response to say a web response stream.
To define Word documents from templates this is all the code one needs to write. However as you can see there is an additional step defined in this process called ProcessDocumentXml. It is this operation that performs the addition requirement to insert a custom Xml document into the Word Document.
private static void ProcessDocumentXml(XDocument inputDocument, Stream documentStream){ // Open the document in the stream and replace the custom XML part using (Package packageFile = Package.Open(documentStream, FileMode.Open, FileAccess.ReadWrite)) { PackagePart packagePart = null; // Find part containing the correct namespace foreach (PackagePart part in packageFile.GetParts()) { if (part.ContentType.Equals("application/xml", StringComparison.OrdinalIgnoreCase)) { using (XmlReader reader = XmlReader.Create(part.GetStream())) { if (reader != null) { reader.MoveToContent(); if (reader.NamespaceURI == "MyCustomXmlNamespace") { packagePart = part; break; } } } } } if (packagePart != null) { // Delete the existing XML part Uri uriData = packagePart.Uri; if (packageFile.PartExists(uriData)) { packageFile.DeletePart(uriData); } // Load the custom XML data PackagePart pkgprtData = packageFile.CreatePart(uriData, CdsaHelper.DataContentType); using (XmlWriter writer = XmlWriter.Create(pkgprtData.GetStream())) { inputDocument.Save(writer); } } }}
To replace a custom XML part within a Word Document one merely has to locate the necessary document part, usually by document namespace, and then perform the necessary Delete and Create operations; effectively an Update.
Hopefully this demonstrates the ease with which one can create Word Document from templates, using server side code. The ability to also insert custom Xml parts into the document provides he additional ability to customise the content of a document, once again within server side code.
Written by Carl Nolan
- Creating a new Microsoft Word document from a template using OpenXml
- Creating a XML Document from scratch without using a file in C#
- Creating a multileader in AutoCAD using a jig from .NET
- Creating a New Project
- Creating a new application
- XenServer 6.5实战系列之九:Creating a VM Template from a VM Snapshot
- Modifying a JSON object by creating a New Field using existing Elements
- Creating a New Window Group
- CREATING A NEW CONTENT PROVIDER
- Creating a new Storm project
- js:Creating a new object
- Creating a document centric iPhone/iPad application with own file format using ZipArchive
- XenServer 6.5实战系列之八:Creating a VM Template from an Existing VM
- Stack Using a C++ Template:
- Creating a Shape from a Stroked Shape
- Creating a PHP Extension for Windows using Microsoft Visual C++ 2008
- 1.1. Creating a New Project from Maven Archetype 从Maven Archetype创建一个新项目
- Part 2: Creating a Template – A Technical Walkthrough
- ubuntu 配置mcrypt
- java类中各属性成员执行顺序
- Mac下安装tornado
- 硬盘故障误丢文件怎么恢复
- 按性别对国家人口进行横向罗列
- Creating a new Microsoft Word document from a template using OpenXml
- 转载-RMAN下CROSSCHECK命令详解
- 为SimpleCursorAdapter增加CheckBox支持
- 好博客摘录
- mysql binlog日志自动清理及手动删除
- android BLE
- static关键字
- WinCE的C#程序读写字符类型的注册表,使用Win32库的方法。直接复制代码使用
- Android如何将程序打成jar包