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.
Leave a Reply