SharePoint Stack Exchange is a question and answer site for SharePoint enthusiasts. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

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
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);
            StreamReader sr = new StreamReader(File.OpenRead(downloadPath));

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


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

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


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.