1

In SharePoint 2013 i was convert the .doc and .docx document to PDF.

when we are converting to PDF in document library we are storing some field values like ContractID, DocumentType.

converting .docx document to PDF that time ContractID, DocumentType is stored in Document Library.

But when converting .doc document to PDF list time ContractID, DocumentType is not stored in Document Library.

Here is my code :

Document.cs

                        SPQuery oQry = new SPQuery();
                        oQry.Query = "<Where><And><Eq><FieldRef Name='ContractID' /><Value Type='Lookup'>" + ContractID + "</Value></Eq><Eq><FieldRef Name='ID' /><Value Type='Counter'>" + DocID + "</Value></Eq></And></Where>";
                        oQry.ViewAttributes = "Scope='RecursiveAll'";
                        SPListItemCollection ocol = oDocLib.GetItems(oQry);
                        //SPListItem oTempItem = olist.GetItemById(ContractID);
                        SPListItem oTempItem = ocol[0];
                        string url = string.Empty;
                        string FileUrl = oTempItem.Url;
                        int fileExtPos = FileUrl.LastIndexOf(".");
                        if (fileExtPos >= 0)
                            url = FileUrl.Substring(0, fileExtPos);
                        SPFile oFile = oTempItem.File;
                       oFile.Web.AllowUnsafeUpdates = true;
                        // oFile.MoveTo(oFile.ParentFolder + "/" + docName+".pdf", true);
                        // oTempItem.File.MoveTo(url + ".pdf", true); 
                        //still this step in oFile ContractID is there.(oFile.MoveTo(url + ".pdf", //true);)
                        //in this step while converting pdf ContractID become null.

                        oFile.MoveTo(url + ".pdf", true);
                        oFile.SaveBinary(data1);
                        oFile.Update();



                    }
                }
            });
        }
        catch (Exception ex)
        {
            ex.ToString();
        }
    }

why that contractID become null please someone tell me. Thanks in Advance.

1

As far as i know, document properties promotion and demotion does not happen for Office 2003 documents. So the properties are not demoted for the doc file and hence the list columns loose the values after converting document to pdf. For detailed explanation, please refer to this article.

As an alternative/workaround, you can store the required metadata to be updated in some variable(s) and after the document is converted successfully to pdf, you can fetch the corresponding list item using the new document URL and update the required metadata explicitly. This would be needed only for Office 2003 documents.

Code to update the list item associated with the document

//Store the contract id in temporary variable
var contractId = oTempItem["contractID"]
string fileUrl = url + ".pdf";
//Move the file
oFile.MoveTo(fileUrl, true);
oFile.SaveBinary(data1);
oFile.Update();
// Get the list item.
SPListItem fileItem = web.GetListItem(fileUrl);
//Update required fields
fileItem["contractID"] = contractId;
fileItem.Update();
  • i try to fetch the list item with new document url but there also list item are null. – Elina Apr 6 '16 at 7:39
  • Can you please share the code snippet? – Vineet Desai Apr 6 '16 at 8:07
  • @kavitha I have updated a sample snippet in the comments above. – Vineet Desai Apr 6 '16 at 10:12
  • Hello Kavitha, did you try the code snippet mentioned in the comments? If yes, what is the error/exception you are getting? – Vineet Desai Apr 6 '16 at 10:27
  • yes vineet like that only i debug and tried ,but when the document convert to pdf in Documentlist ContractID field is empty .so here while executing null exeption came. SPListItem item = oweb.GetListItem(url); item["ContractID"] =ContractID; contractID is null. – Elina Apr 6 '16 at 10:32
-1

you can download WrodToPDF dll and include the same in event receiver in itemadded event, check extension of added file, if it resembles with doc or docx just convert it and then add using the dll,

http://www.c-sharpcorner.com/UploadFile/698727/convert-word-file-to-pdf-using-C-Sharp/

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.