Instantly Convert file into PDF using word automation service in sharepoint 2013
private void SaveAsPDF(SPSite siteAsAdmin, SPWeb web, SPFile DocFile)
{
try
{
SPFolder destFolder =
web.Lists.TryGetList(PdfDocLibName).RootFolder;
SPFile destFile = null;
destFile =
destFolder.Files.Add(Path.GetFileNameWithoutExtension(DocFile.Url) + ".pdf",
ConvertWordToPDF(siteAsAdmin, web, DocFile), false);
destFile.Item.Update();
if (destFile.CheckOutType != SPFile.SPCheckOutType.None)
{
destFile.CheckIn("Checked in programatically. Document generation
completed.");
}
}
catch (Exception ex)
{
ULSLogService.DisplayError(this.GetType().Name + "." + MethodBase.GetCurrentMethod().Name, ex);
}
}
private byte[] ConvertWordToPDF(SPSite siteAsAdmin, SPWeb web, SPFile DocFile)
{
byte[] result = null;
try
{
using (Stream read = DocFile.OpenBinaryStream())
{
using (MemoryStream write =
new MemoryStream())
{
// Initialise Word Automation Service
SyncConverter sc = new SyncConverter(wordAutomationServiceName);
sc.UserToken =
siteAsAdmin.SystemAccount.UserToken;
sc.Settings.UpdateFields = true;
sc.Settings.OutputFormat
= SaveFormat.PDF;
// Convert to PDF
ConversionItemInfo info =
sc.Convert(read, write);
if (info.Succeeded)
result =
write.ToArray();
}
}
}
catch (Exception ex)
{
ULSLogService.DisplayError(this.GetType().Name + "." + MethodBase.GetCurrentMethod().Name, ex);
}
return result;
}
No comments:
Post a Comment