Determine the number of pages in a PDF file

I need to determine the number of pages in a specified PDF file using C # code (.NET 2.0). The PDF file will be read from the file system, not from the URL. Does anyone have any guidance on how this can be done? Note. Adobe Acrobat Reader is installed on the PC where this check will be performed.

+42
c # pdf
Nov 26 '08 at 10:50
source share
9 answers

You will need a PDF API for C #. iTextSharp is one of the possible APIs, although they may exist better.

ITextSharp Example

You must install iTextSharp.dll as a reference. Download iTextsharp from SourceForge.net. This is a complete working program using a console application.

using System; using System.Collections.Generic; using System.Linq; using System.Text; using iTextSharp.text.pdf; using iTextSharp.text.xml; namespace GetPages_PDF { class Program { static void Main(string[] args) { // Right side of equation is location of YOUR pdf file string ppath = "C:\\aworking\\Hawkins.pdf"; PdfReader pdfReader = new PdfReader(ppath); int numberOfPages = pdfReader.NumberOfPages; Console.WriteLine(numberOfPages); Console.ReadLine(); } } } 
+55
Nov 26 '08 at 11:07
source share

This should do the trick:

 public int getNumberOfPdfPages(string fileName) { using (StreamReader sr = new StreamReader(File.OpenRead(fileName))) { Regex regex = new Regex(@"/Type\s*/Page[^s]"); MatchCollection matches = regex.Matches(sr.ReadToEnd()); return matches.Count; } } 

From the answer is Rachel and this one .

+29
Nov 17 '09 at 19:57
source share

found a way http://www.dotnetspider.com/resources/21866-Count-pages-PDF-file.aspx it does not require the purchase of a PDF library

+6
Feb 27 '09 at 16:24
source share

I used pdflib for this.

  p = new pdflib(); /* Open the input PDF */ indoc = p.open_pdi_document("myTestFile.pdf", ""); pageCount = (int) p.pcos_get_number(indoc, "length:pages"); 
+4
Nov 26 '08 at 12:11
source share

Docotic.Pdf library can be used to complete the task.

Here is a sample code:

 PdfDocument document = new PdfDocument(); document.Open("file.pdf"); int pageCount = document.PageCount; 

The library will analyze as little as possible so that performance is in order.

Disclaimer: I am working on Bit Miracle.

+2
May 25 '11 at 15:45
source share

One line:

 int pdfPageCount = System.IO.File.ReadAllText("example.pdf").Split(new string[] { "/Type /Page" }, StringSplitOptions.None).Count()-2; 

Recommended: ITEXTSHARP

+2
Feb 24 '16 at 7:56
source share

Pdfsharp

it should be better =)

0
Nov 26 '08 at 11:09
source share

I have good success using CeTe Dynamic PDF products. They are not free, but well-documented. They did this job for me.

http://www.dynamicpdf.com/

0
Jan 09 '09 at 14:29
source share

I used the above code which solves the problem with regex and it works, but it is rather slow. It reads the entire file to determine the number of pages.

I used it in a web application, and the pages sometimes displayed 20 or 30 PDF files at a time, in which case the page load time was from a few seconds to almost a minute due to the page counting method.

I don’t know if third-party libraries will be much better, I hope they are, and I have successfully used pdflib in other scripts.

0
Feb 02 '10 at 19:07
source share



All Articles