前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >User Word Automation Services and Open XML SDK to generate word files in SharePoint2010

User Word Automation Services and Open XML SDK to generate word files in SharePoint2010

作者头像
深蓝studyzy
发布2022-06-16 15:17:57
2320
发布2022-06-16 15:17:57
举报
文章被收录于专栏:深蓝居

SharePoint 2010 has established a new service called “Word Automation Services” to operate word files. This service will be installed when install SharePoint 2010. It is useful for archive documents or convert word format in server. But we need initialize and configure this service on Central Administration or PowerShell.(How to set up Word Automation Services? see:http://msdn.microsoft.com/en-us/library/ee557330(v=office.14).aspx)

After initialized, SharePoint 2010 will setup a database named:”WordAutomationServices_XXXX”. We can find the service status in Central Administration->Application Management->Service Applications->Manage services on server:

image
image

Make sure the service is started.

Call Word Automation Services

Next, we can use C# code to call the Word Automation Services. For example, this is a word file in Shared Documents, we need to convert the word file to pdf format and saved in another document library (named Docs), we can write code like this:

代码语言:javascript
复制
string siteUrl = "http://localhost";
string wordAutomationServiceName = "Word Automation Services";
using (SPSite spSite = new SPSite(siteUrl))
{
    ConversionJob job = new ConversionJob(wordAutomationServiceName);
    job.UserToken = spSite.UserToken;
    job.Settings.UpdateFields = true;
    job.Settings.OutputFormat = SaveFormat.PDF;
    job.AddFile(siteUrl + "/Shared%20Documents/Contract%20Management.docx",
        siteUrl + "/Docs/Contract%20Management.pdf");
    job.Start();
}

Note: Word Automation Services use a timer to process our job, so even we finished our process in code, we still cannot find the pdf file in document library. After a few minutes, the timer will complate our job, refresh the document library page we can see the pdf file. If we want to make the timer work frequently, we can go to Central Administration->Monitoring->Timer Jobs->Review job definitions, click the “Word Automation Services Timer Job”, change the “Recurring Schedule” value such as 1 minute. If we want to run the job immediately, we can click the "Run Now” button.

image
image
User Open XML SDK to generate word file

Open XML is a common standard format for Office files since Office 2007. We can use Open XML SDK to develop the application that combine the user define template and data (from database, SharePoint list, interface or other user input).

Open XML SDK is not contained in Visual Studio by default, so we must download the SDK from Microsoft.(Download address:http://www.microsoft.com/en-us/download/details.aspx?id=5124) After install the SDK, we can reference the component: DocumentFormat.OpenXml and WindowsBase.

In the code, we can read the template from disk or SharePoint document library. The template contains some special word than we will replace them by user data.

For example, there is a template, we need to fill the vender name, amount and date, so we can define the template like this:

Purchasing Contract Vender:$Vender, Description:bala bala~~~ Total Amount: $Amount Signature Date:$Today

We save the template as a docx file in disk, and we can read the template by Open XML SDK and replace the words like this:

代码语言:javascript
复制
FileStream fs=new FileStream("Template.docx",FileMode.Open,FileAccess.Read);
byte[] byteArray = new byte[fs.Length];
fs.Read(byteArray, 0, (int)(fs.Length));


using (MemoryStream memStr = new MemoryStream())
{
    memStr.Write(byteArray, 0, byteArray.Length);
    using (WordprocessingDocument wordDoc = WordprocessingDocument.Open(memStr, true))
    {

        string docText = null;
        using (StreamReader sr = new StreamReader(wordDoc.MainDocumentPart.GetStream()))
        {
            docText = sr.ReadToEnd();
        }
        docText = docText.Replace("$Vender", "Microsoft");
        docText = docText.Replace("$Amount", "1234.56");
        docText = docText.Replace("$Today", DateTime.Today.ToShortDateString());

        using (StreamWriter sw = new StreamWriter(wordDoc.MainDocumentPart.GetStream(FileMode.Create)))
        {
            sw.Write(docText);
        }
}
Console.WriteLine("Saving");
//save new file from stream memStr...

Run this code, we can find the result document content like this:

Purchasing Contract Vender:Microsoft, Description:bala bala~~~ Total Amount: 1234.56 Signature Date:2013/9/27

Use both the Word Automation Services and Open XML SDK, we can generate the word document by template and user data, and convert the result as a pdf file and save to another document library.

This my example code:

代码语言:javascript
复制
string siteUrl = "http://localhost";
using (SPSite spSite = new SPSite(siteUrl))
{
    //Querying for Template.docx
    SPList list = spSite.RootWeb.GetList("http://localhost/Shared%20Documents");
    SPQuery query = new SPQuery();
    query.ViewFields = @"<FieldRef Name='FileLeafRef' />";
    query.Query =
        @"<Where>
<Eq>
<FieldRef Name='FileLeafRef' />
<Value Type='Text'>Template.docx</Value>
</Eq>
</Where>";
    SPListItemCollection collection = list.GetItems(query);
    if (collection.Count != 1)
    {
        Console.WriteLine("Test.docx not found");
        Environment.Exit(0);
    }
    Console.WriteLine("Opening");
    SPFile file = collection[0].File;
    byte[] byteArray = file.OpenBinary();

    using (MemoryStream memStr = new MemoryStream())
    {
        memStr.Write(byteArray, 0, byteArray.Length);
        using (WordprocessingDocument wordDoc = WordprocessingDocument.Open(memStr, true))
        {

            string docText = null;
            using (StreamReader sr = new StreamReader(wordDoc.MainDocumentPart.GetStream()))
            {
                docText = sr.ReadToEnd();
            }
            docText = docText.Replace("$Vender", "Microsoft");
            docText = docText.Replace("$Amount", "1234.56");
            docText = docText.Replace("$Today", DateTime.Today.ToShortDateString());

            using (StreamWriter sw = new StreamWriter(wordDoc.MainDocumentPart.GetStream(FileMode.Create)))
            {
                sw.Write(docText);
            }
        }
        Console.WriteLine("Saving");
        string newDocName = "MicrosoftContract.docx";
        file.ParentFolder.Files.Add(newDocName, memStr, true);
        Console.WriteLine("Starting conversion job");
        string wordAutomationServiceName = "Word Automation Services";
        ConversionJob job = new ConversionJob(wordAutomationServiceName);
        job.UserToken = spSite.UserToken;
        job.Settings.UpdateFields = true;
        job.Settings.OutputFormat = SaveFormat.PDF;
        job.AddFile(siteUrl + "/Shared%20Documents/"+newDocName,
            siteUrl + "/Docs/MicrosoftContract.pdf");
        job.Start();
    }
}
本文参与 腾讯云自媒体同步曝光计划,分享自作者个人站点/博客。
原始发表:2013-09-27,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • Call Word Automation Services
  • User Open XML SDK to generate word file
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档