I am converting word files to PDF using word automation service using event receiver ItemCheckedIn
event. I have selected major-minor version settings in Versioning Settings. in The code works fine except it overwrite current file and does not change the version. below is my code sample:
public override void ItemCheckedIn(SPItemEventProperties properties) {
//////collFiles = m1list.RootFolder.Files;
////SPFile sourceFile = collFiles[Convert.ToString(items[0][SPBuiltInFieldId.EncodedAbsUrl])];
SPListItem curfileitem = properties.ListItem;
SPFile sourceFile = properties.ListItem.File;
SPList m1list = properties.List;
//SPFile sourceFile = curfileitem.File;
if (sourceFile == null) {
return;
}
SPSite sitename = properties.Site;
SPWeb webname = properties.Web;
SPSite sitename2 = webname.Site;
SPFolder destFolder = m1list.RootFolder;
SPFile destFile = null;
string sourceFileName = sourceFile.Name.Trim();
string filepath = sourceFile.Url;
string destFileName = sourceFileName.Substring(0, sourceFileName.LastIndexOf('.')) + ".pdf";
//// destFile = destFolder.Files.Add(destFileName, this.ConvertWordToPDF(sourceFile, SPContext.Current.Site.UserToken), true);
destFile = m1list.RootFolder.Files.Add(destFileName, ConvertWordToPDF(sourceFile, sitename.UserToken, sitename2), true);
if (destFile != null) {
SPListItem sourceitem = sourceFile.Item;
if (destFile.CheckOutType != SPFile.SPCheckOutType.None) {
destFile.CheckIn(string.Empty);
}
}
}
private byte[] ConvertWordToPDF(SPFile spfile, SPUserToken usrToken, SPSite sitefortoken) {
byte[] result = spfile.OpenBinary();
try {
using(Stream read = spfile.OpenBinaryStream()) {
using(MemoryStream write = new MemoryStream()) {
if (Path.GetExtension(spfile.Title) == ".docx" || Path.GetExtension(spfile.Title) == ".doc" || Path.GetExtension(spfile.Name) == ".docx" || Path.GetExtension(spfile.Name) == ".doc") {
WordServiceApplicationProxy proxy = (WordServiceApplicationProxy)
SPServiceContext.GetContext(sitefortoken)
.GetDefaultProxy(typeof(WordServiceApplicationProxy));
//SyncConverter sc = new SyncConverter((WordServiceApplicationProxy)proxy);
SyncConverter sc = new SyncConverter(proxy);
//SyncConverter sc = new SyncConverter(proxy);
sc.UserToken = usrToken;
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) {
throw ex;
}
return result;
}
Thanks in advance.