Take the 2-minute tour ×
SharePoint Stack Exchange is a question and answer site for SharePoint enthusiasts. It's 100% free, no registration required.

In my SharePoint Server 2010, I have a document library which contails word and pdf files.

For each file in the library, I want to display the number of pages in the file as a column.

I tried but did not find a solution.

Can any one please tell me if there any way out?

share|improve this question
    
Good question, if it is possible you will need an ifilter to read PDF files. –  Russell Jan 4 '12 at 13:01

3 Answers 3

up vote 1 down vote accepted

I did this using regular expression Here is my code :

downloadPath = HostingEnvironment.ApplicationPhysicalPath + properties.ListItem.File.Name;
               siteURL = properties.WebUrl;
               listTitle = properties.ListTitle;
               id = properties.ListItemId;
            WebClient webClient = new WebClient();
            webClient.DownloadFileCompleted += new System.ComponentModel.AsyncCompletedEventHandler(Completed);
            //webClient.Credentials = System.Net.CredentialCache.DefaultCredentials;
            System.Net.NetworkCredential credentials = new System.Net.NetworkCredential("abc", "abc","abc");
            webClient.Credentials = credentials;
            webClient.DownloadFileAsync(new Uri(fileUrl), HostingEnvironment.ApplicationPhysicalPath + properties.ListItem.File.Name);

private void Completed(object sender, System.ComponentModel.AsyncCompletedEventArgs e)
        {
            int pages = 0;
            FileStream fs = new FileStream(downloadPath, FileMode.Open, FileAccess.Read);
            fs.Dispose();
            fs.Close();
            StreamReader sr = new StreamReader(File.OpenRead(downloadPath));

            Regex regex = new Regex(@"/Type\s*/Page[^s]");
            MatchCollection matches = regex.Matches(sr.ReadToEnd());
            pages = matches.Count;
            sr.Close();
            sr.Dispose();
            FileStream fs1 = new FileStream(downloadPath, FileMode.Open, FileAccess.Read);
            fs1.Dispose();
            fs1.Close();

            File.Delete(downloadPath);

            SPSite oSite = new SPSite(siteURL);
            SPWeb oWeb = oSite.OpenWeb();
            SPList oList = oWeb.Lists[listTitle];
            SPListItem oListItem;//= oList.Items[];
            oListItem = oList.GetItemById(id);
            oListItem.File.CheckOut();
            oListItem["Number of pages"] =Convert.ToInt32(pages);
            oListItem.Update();
            oListItem.File.CheckIn("file name has been changed");
            oWeb.Dispose();

        }
share|improve this answer

You'll need a 3rd party solution to open the files and process them. You can attach this to an event receiver or workflow. Check out http://www.aspose.com/categories/.net-components/aspose.total-for-.net/default.aspx for a start.

share|improve this answer

If they are Word 2007 or better (docx) files you can use an event receiver in combination with the OpenXML SDK to get the number of pages. Not sure how you could do the same with PDF files.

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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