- Setting up Word Automation Services for SharePoint 2013
- Setting up PowerPoint Automation Services for SharePoint 2013
- Converting a Library’s Word Documents to PDF
- Converting a Library’s PowerPoint documents to PDF
- Using ITextSharp to Merge PDF’s into one
- Using ITextSharp to create basic PDF page from item property
In my last post, I showed you how to convert a Word document to PDF. In this post I’m going to walk through the code that you can use to convert your PowerPoint document to PDF.
Following on from my last Farm Solution, I have extended the solution by adding another button that when clicked, it looks up the document library and grabs all the PowerPoint documents. Looping through each PowerPoint document it finds, it converts them to PDF and then saves them back to the same library. As before, there is no current way of performing this in Office 365.
The Shared Document library for my site, I have added two PowerPoint documents to my library.
Test 3 is a simple PowerPoint slide that has some text.
And the second PowerPoint document is a “Create an Office Mix” Template created in PowerPoint. As with my word demo, this is just so you can see pictures and layouts converted correctly.
Below is the code and comments explaining what is happening at different stages of the code.
//Will require this using statement using Microsoft.Office.Server.PowerPoint.Conversion; protected void btnPowerPointConverttoPDF_Click(object sender, EventArgs e) { //Get current site context var site = SPContext.Current.Site; var list = web.Lists.TryGetList(libraryName.Text) if (list != null) { PowerPointDocsToConvertToPdf(list); } } private void PowerPointDocsToConvertToPdf(SPList library) { //Perform a SPQuery that returns only PowerPoint Documents. SPQuery query = new SPQuery(); query.Folder = library.RootFolder; //Include all subfolders so include Recursive Scope. query.ViewXml = @"<View Scope='Recursive'> <Query> <Where> <Or> <Contains> <FieldRef Name='File_x0020_Type'/> <Value Type='Text'>ppt</Value> </Contains> <Contains> <FieldRef Name='File_x0020_Type'/> <Value Type='Text'>pptx</Value> </Contains> </Or> </Where> </Query> </View>"; //Get Documents SPListItemCollection listItems = library.GetItems(query); //Check that there are any documents to convert. if (listItems.Count > 0) { foreach (SPListItem li in listItems) { try { //Perform the conversion in memory first using (MemoryStream destinationStream = new MemoryStream()) { //Create the conversion request, configure settings. Microsoft.Office.Server.PowerPoint.Conversion.FixedFormatSettings settings = new Microsoft.Office.Server.PowerPoint.Conversion.FixedFormatSettings(); settings.BitmapUnembeddableFonts = true; settings.FrameSlides = true; settings.IncludeDocumentProperties = true; settings.IncludeDocumentStructureTags = true; settings.IncludeHiddenSlides = true; settings.OptimizeForMinimumSize = true; settings.UsePdfA = true; settings.UseVerticalOrder = true; //Perform conversion, opening stream, passing in settings and destination stream. PdfRequest request = new PdfRequest(li.File.OpenBinaryStream(), ".pptx", settings, destinationStream); //Send the request synchronusly, passing in null value for the callback parameter and caputring the response in result object. IAsyncResult result = request.BeginConvert(SPServiceContext.GetContext(library.ParentWeb.Site), null, null); //Use the EndConvertMethod; request.EndConvert(result); //Add the converted file to the document library var fileName = Path.GetFileNameWithoutExtension(li.File.Name) + ".pdf"; SPFile newFile = library.RootFolder.Files.Add(fileName, destinationStream, true); } } catch(Exception ex) { throw new Exception("Error processing PowerPoint to PDF " + ex.message); } } } }
After typing in the Document Library name and clicking the button, the PowerPoint documents have been converted to PDF.
Link to Sample code : OneDrive