3

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?

1
  • Good question, if it is possible you will need an ifilter to read PDF files.
    – Russell
    Commented Jan 4, 2012 at 13:01

3 Answers 3

1

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

        }
0

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.

0

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.

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.