1

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 ?

How?

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

0

Use the SPFile.CopyTo method.

SPFile.CopyTo Reference

Example:

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

    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

Example:

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);

            oWebsiteNext.Dispose();
        }
    }
}

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.