Continuing from my previous article to convert word document to PDF, I would like to show you on how to utilize Power Point Automation Service to convert Power Point document into PDF document.
The requirement is Power Point Automation Service needs to be configured and started. I will not go through with you how to setup Power Point Automation Service, you could refer it here.
Below is the function to convert Power Point to PDF document, it will return file stream.
private SPFileStream ConvertPowerPointToPDF(SPFile file) { string sFileName = file.Name.Trim(); string sFileNameExtension = sFileName.Substring(sFileName.LastIndexOf('.') + 1); SPFileStream result = null; try { // Convert the file to a stream and create an SPFileStream object for the conversion output. Stream fStream = file.OpenBinaryStream(); SPFileStream stream = new SPFileStream(file.Web, 0x1000); // Create the presentation conversion request. PdfRequest request = new PdfRequest(fStream, sFileNameExtension, stream); // Send the request synchronously, passing in a ‘null’ value for the callback parameter and capturing the response in the result object. IAsyncResult aSyncResult = request.BeginConvert(SPServiceContext.GetContext(file.Web.Site), null, null); // Use the EndConvert method to get the result. request.EndConvert(aSyncResult); if (stream != null) result = stream; } catch (Exception ex) { // Do your error management here. } return result; }
The ItemUpdated event, it will look like below. The PDF document will be added into “Documents Public” library with all custom column / metadata updated as well.
public override void ItemUpdated(SPItemEventProperties properties) { base.ItemUpdated(properties); try { SPWeb web = properties.Web; SPFolder sourceFolder = properties.List.RootFolder; SPListItem sourceItem = properties.ListItem; SPFile sourceFile = sourceItem.File; string sourceFileName = sourceFile.Name.Trim(); SPFolder destFolder = web.Lists["Documents Public"].RootFolder; SPFile destFile = null; string destFileName = sourceFileName.Substring(0, sourceFileName.LastIndexOf('.')) + ".pdf"; bool isOverwrite = true; bool isDestApprovalRequired = false; // If File Approved if (sourceItem.ModerationInformation.Status == SPModerationStatusType.Approved) { // Convert the office file if (sourceFileName.EndsWith(".docx") || sourceFileName.EndsWith(".doc")) { // Check out existing PDF file if exists CheckOutFile(web, String.Format("{0}/{1}/{2}", web.Url, destFolder.Url, destFileName)); destFile = destFolder.Files.Add(destFileName, ConvertWordToPDF(sourceFile, properties.Site.UserToken), isOverwrite ); } else if (sourceFileName.EndsWith(".pptx") || sourceFileName.EndsWith(".ppt")) { // Check out existing PDF file if exists CheckOutFile(web, String.Format("{0}/{1}/{2}", web.Url, destFolder.Url, destFileName)); destFile = destFolder.Files.Add(destFileName, ConvertPowerPointToPDF(sourceFile), isOverwrite); } if (destFile != null) { // Update PDF File metadata foreach (SPField eachPdfField in destFile.Item.Fields) { if (eachPdfField.FromBaseType == false && !eachPdfField.ReadOnlyField) { destFile.Item[eachPdfField.Id] = sourceItem[eachPdfField.Id]; } } // Apply Update to PDF File destFile.Item.Update(); destFolder.Update(); // Check In PDF File if (destFile != null && destFile.CheckOutType != SPFile.SPCheckOutType.None) destFile.CheckIn(""); // Approve if(isDestApprovalRequired) destFile.Approve(""); destFolder.Update(); } } } catch (Exception ex) { // Do your error management here. } } private void CheckOutFile(SPWeb web, string sFileUrl) { try { SPFile currFile = web.GetFile(sFileUrl); if (currFile != null && currFile.CheckOutType == SPFile.SPCheckOutType.None) { currFile.CheckOut(); } } catch (Exception ex) { // Do your error management here. } }
Tagged: Approve, CheckIn, CheckOut, Document Library, event receiver, ItemUpdated, ModerationInformation, PDF, Power Point, SharePoint 2013, SPModerationStatusType
Hy , I have installed Power point automation services on my VM and started all related services, but when i try to make ppt convertion, i had an exception: The specified document cannot be converted, i tried on 3 VM and the same error occurs..
please any help !
Try to create and convert new simple power point document and see whether you face the same error again. Otherwise, you need to look on SharePoint log to get more obvious error. Cheers …
I tried it before, nothing in the LOGS, i think it’s a biug with SharePoint 2013, I tried on 3 VM and always had the same error.
Other than installing the latest Windows Update, I updated my farm with following KB:
– KB 2567680
– KB 2554876
– KB 2708075
– KB 2472264
You could find all KB links from my other post:
these hotfixes do not apply to Windows server 2012, PPT automation services, Translation services and Word services do not work in SP2013, it’s crazy !!
Hi I have installed Power point automation services on my farm. I can see services running from central administration. Below is my code:
SPFolder myDocs = myWeb.Folders[mySiteURL + “/myPresentationLib”];
SPFile myFile = myDocs.Files[mySiteURL + “/myPresentationLib/ComapnyBalPresent.pptx”];
// Create a stream object for the file
Stream myFileStream = myFile.OpenBinaryStream();
SPFileStream myStream = new SPFileStream(myWeb, 0x1000);
// Rrequest conversion to .pdf format.
// PdfRequest myRequest = new PdfRequest(myFileStream, “.pptx”, myStream);
PictureRequest req = new PictureRequest(myFileStream, “.pptx”, PictureFormat.Jpg, myStream);
// Rrequest is sent synchronously, when
// ‘null’ value is used for the callback parameter.
// Response is in the result object.
IAsyncResult result = req.BeginConvert(SPServiceContext.GetContext(mySite), null, null);
// Use the EndConvert method to get the result.
req.EndConvert(result);
// Add the converted file to the document library.
SPFile myNewPdfFile = myDocs.Files.Add(“ComapnyBalPresent.Jpg”, myStream, true);
But i am getting “Could not connect to the conversion service.” at ” req.EndConvert(result);” line. Can you please help me.
Also, I am having one more question Do we require Office App server to be installed to run PowerPoint Automation Service.
I would suggest you could start with creating simple power point file and convert it, who knows there might be problem with your power point file itself.
Second thing you could try is to convert to other format like (pdf or png).
you could find powerpoint automation service here:
http://msdn.microsoft.com/en-us/library/office/fp179894(v=office.15).aspx
Other than that, I could not help you further cause I can’t duplicate the issue in my environment.
I saw you’ve asked question in other forum, hopefully someone could help you out with the issue.
Cheers,
Tjen
Thanks Tjen for prompt reply. I have already referred same http://msdn.microsoft.com/en-us/library/office/fp179894(v=office.15) same link you have suggested. I tried to convert the format which used in the link. Unfortunate, result is same.
Do we require Office App server to be installed to run PowerPoint Automation Service.
Nope, I got it working without Office Web Apps installed and configured.
Thansk Tjen I just want to confirm my query. Is there any special access / permission needs to be given for the service?
I just assigned Local Farm as Full Control to my Power Point Automation service. The other thing, obviously the user need to have create / update permission to destination library.
Cheers,
Tjen
Thanks Tjen, getting one more error “AsyncResult operation exceeded timeout of 00:05:00”.