I want to know if there is any way by which I can make a copy of the file which is being added to a document library using c# event receiver and further convert it to pdf in SharePoint 2010 ?

i.e If a file A is being uploaded to Document Library Test then using Item Added event receiver I want to create a copy of the File A say File B and convert it into B.pdf programmatically.

Is it possible ?


Note : I know how to convert to pdf but dont know how can I make a copy of the document?

1 Answer 1


Use the SPFile.CopyTo method.

SPFile.CopyTo Reference


SPSite oSiteCollection = SPContext.Current.Site;
using(SPWeb oWebsite = oSite.AllWebs["Site_Name/Subsite_Name"])
    SPFileCollection collFiles = oWebsite.GetFolder("Shared 

    for (int intIndex=0; intIndex<collFiles.Count; intIndex++)
        collFiles[intIndex].CopyTo("Destination_DocLib/" + 
            collFiles[intIndex].Name, true);

You could also use the SPFileCollection.Add method.

SPFileCollection.Add Reference


using (SPSite oSiteCollection = new SPSite("http://localhost"))
    using(SPWeb oWebsite = oSiteCollection.AllWebs["MyWebSite"])
        SPWebCollection collWebsites = oWebsite.Webs;
        SPFile oFile = oWebsite.GetFile("MyDocLib/MyFile");
        foreach (SPWeb oWebsiteNext in collWebsites)
            SPFolder oFolder = oWebsiteNext.GetFolder("Shared Documents");
            SPFileCollection collFiles = oFolder.Files;

            string strDestUrl = oFolder.Url + "/" + oFile.Name;
            byte[] binFile = oFile.OpenBinary();
            collFiles.Add(strDestUrl, binFile);


Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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