/*************************************************************************** Muhimbi PDF Converter Copyright 2010, Muhimbi Ltd - www.muhimbi.com - All rights reserved The following code is a simplification of the code that is normally executed by the Muhimbi PDF Converter Workflow Action. Error and permission checking as well as other minor features have been omitted for the sake of brevity and clarity. This code allows more control over the PDF Conversion process compared to what is possible with the Out-Of-The-Box action, for example: PDF Security Options, User PDF/A, Quality, Enable Macros and the ability to specify a password to open a source file. This code requires the Muhimbi's PDF Converter and Workflow Power Pack to be installed. For details about the Web Services interface see: http://www.muhimbi.com/blog/2009/12/converting-office-files-to-pdf-format.html ***************************************************************************/ using Muhimbi.SharePoint.DocumentConverter.PDF; using Muhimbi.SharePoint.DocumentConverter.PDF.WebServiceClient; using System.Collections; // ** Set all variables required for the conversion of the file. SPFile sourceFile = MyWorkflow.Item.File; // ** Specify an empty string or null to use the same directory as the source file. // ** Alternatively specify an absolute or relative (to the web) path, e.g. // ** subsite1/Shared Documents/PDF or /sites/HumanResources/subsite1/Shared Documents/PDF string destinationFolderName = MyWorkflow.Parameter1 as string; string destinationFileName = Utility.ExtractFileName(sourceFile.Name) + ".pdf"; string openPassword = ""; string ownerPassword = "A Nice And Strong Password!"; SecurityOptions securityOptions = SecurityOptions.DisablePrint | SecurityOptions.DisableContentCopy; bool copyMetadata = true; // ** Get a reference to the PDF Converter web service. DocumentConverterServiceClient client = WebServiceConverterHelper.OpenService(); //** Set the various open options OpenOptions openOptions = new OpenOptions(); openOptions.Password = ""; openOptions.OriginalFileName = sourceFile.Name; openOptions.FileExtension = Utility.ExtractFileExtension(sourceFile.Name); openOptions.AllowMacros = MacroSecurityOption.None; openOptions.RefreshContent = true; //** Specify the various security settings ConversionSettings conversionSettings = new ConversionSettings(); conversionSettings.Fidelity = ConversionFidelities.Full; conversionSettings.Format = OutputFormat.PDF; conversionSettings.Quality = ConversionQuality.OptimizeForPrint; conversionSettings.Range = ConversionRange.VisibleDocuments; conversionSettings.StartPage = 0; conversionSettings.EndPage = 0; conversionSettings.GenerateBookmarks = BookmarkGenerationOption.Automatic; conversionSettings.PDFProfile = PDFProfile.PDF_1_5; // ** Specify either an Open or Owner Password in order to activate the security Options conversionSettings.OpenPassword = openPassword; conversionSettings.OwnerPassword = ownerPassword; conversionSettings.SecurityOptions = securityOptions; // ** Read the content of the source file byte[] sourceFileArray = sourceFile.OpenBinary(); // ** Carry out the actual conversion to PDF byte[] convertedFile = client.Convert(sourceFileArray, openOptions, conversionSettings); // ** Construct the path and file to write the PDF file to. if (string.IsNullOrEmpty(destinationFolderName) == true) destinationFolderName = sourceFile.ParentFolder.Url; SPFolder destinationFolder = Utility.GetSPFolder(destinationFolderName, MyWorkflow.Web); string destinationFilePath = string.Format("{0}/{1}", destinationFolder.Url, destinationFileName); SPWeb destinationWeb = destinationFolder.ParentWeb; SPFile spDestinationFile = destinationWeb.GetFile(destinationFilePath); // ** If a document library requires manual checkout and the file is not checked out, then // ** check the file out before uploading. bool requiresCheckIn = false; if (spDestinationFile.Exists && spDestinationFile.Item.ParentList.ForceCheckout && spDestinationFile.CheckOutStatus == SPFile.SPCheckOutStatus.None) { spDestinationFile.CheckOut(); } // ** Copy metadata, if required by the user. if (copyMetadata == true && sourceFile.Name.EndsWith("x", StringComparison.InvariantCultureIgnoreCase) == false) { // ** Pre office 2007 formats (without the trailing x) are treated differently // ** from the office 2007 formats when it comes to copying meta data. Hashtable metadata = sourceFile.Item.Properties; // ** Add the file to the site including the meta data spDestinationFile = destinationWeb.Files.Add(destinationFilePath, convertedFile, metadata, true); } else if (copyMetadata == true) { // ** Add the file to the site. spDestinationFile = destinationWeb.Files.Add(destinationFilePath, convertedFile, null, true); // ** Copy the Actual meta data to the newly created file. foreach (SPField field in sourceFile.Item.Fields) { if (field.ReadOnlyField == false && spDestinationFile.Item.Fields.ContainsField(field.InternalName) == true) { spDestinationFile.Item[field.InternalName] = sourceFile.Item[field.InternalName]; } } // ** Update the meta data spDestinationFile.Item.Update(); } else { // ** Actively strip all meta data Hashtable metadata = new Hashtable(sourceFile.Item.Properties.Count); foreach (Object key in sourceFile.Item.Properties.Keys) { metadata.Add(key, string.Empty); } // ** Add the file to the site with all meta data stripped spDestinationFile = destinationWeb.Files.Add(destinationFilePath, convertedFile, metadata, true); } // ** Check the file back in if this script was responsible for checking it out. if (spDestinationFile.Item.ParentList.ForceCheckout == true) { spDestinationFile.CheckIn("Auto check-in after PDF Conversion"); }