Convert Content via SharePoint – Word Automation Services

I already made a post about the word automation services in SharePoint 2010. I then used Powershell to convert a word document to a pdf, which both were stored in a document library in SharePoint.

That was typically for 2010:

  • Solutions have to wait for a timer job to complete the converting process
  • Files can only be in SharePoint
  • No UI
  • No message if it’s done

The reason to create a new post is, that SharePoint 2013 offers a lot more things now which make the word automation services powerful and interesting in every direction. It now can offer

  • Conversion process is synchronized, immediate response is possible
  • Convert one file at a time per request
  • Setting options in Central Administration for simoultaneous requests
  • Notify / Updating files in SharePoint items if conversion is ready
  • Support of streams
    • Convert streams as inputs and outputs for file operations
    • Storing streams in the application server and worker manager not in the database

Let’s see how we can use it. I created a small console project to check the functionality. I found some cool functions which I used from this post and expand it to my needs. A second post was about using it as web service which might be really cool if you need a converter service for other applications or applications within sharepoint.

After we prepared the console project, we need to add some references

  • Microsoft.Office.Word.Server
  • Microsoft.SharePoint

Let’s add the code, therefore i am using methods, which i can call with parameters. In the main method i send a filestream to the method and get a new filestream back.

[sourcecode language=”csharp”]

public static byte[] ConvertToPDF(Stream docByte)
{
using (Stream read = docByte)
{
using (MemoryStream write = new MemoryStream())
{
string wordAutomationServiceName = “Word Automation Services”;
SyncConverter sc = new SyncConverter(wordAutomationServiceName);
SPSite spSite = SPContext.Current.Site;

sc.UserToken = spSite.UserToken;
sc.Settings.UpdateFields = true;
sc.Settings.OutputFormat = SaveFormat.PDF;

ConversionItemInfo info = sc.Convert(read, write);
if (info.Succeeded)
{
return write.ToArray();
}
}
}
return null;
}

[/sourcecode]

Ok, that’s the basis. This maybe the starting point for a great season for pdf converting of SharePoint and non-SharePoint things 🙂

Hope you like it like i do. One of my next posts will target this as a basic for trying to convert a SharePoint listform to pdf. Would like to see if it’s working and maybe we can provide a template for the output, like for a invoice.

The article or information provided here represents completely my own personal view & thought. It is recommended to test the content or scripts of the site in the lab, before making use in the production environment & use it completely at your own risk. The articles, scripts, suggestions or tricks published on the site are provided AS-IS with no warranties or guarantees and confers no rights.

About Karsten Schneider 312 Articles
Consultant for Microsoft 365 Applications with a strong focus in Teams, SharePoint Online, OneDrive for Business as well as PowerPlatform with PowerApps, Flow and PowerBI. I provide Workshops for Governance & Security in Office 365 and Development of Solutions in the area of Collaboration and Teamwork based on Microsoft 365 and Azure Cloud Solutions. In his free time he tries to collect tipps and worthy experience in this blog.

Be the first to comment

Leave a Reply

Your email address will not be published.


*