Hi from Afghanistan
I am developing a desktop application that sends and recieve fax using a soap message and I need to know the number of pages of the pdf file the user wants to fax.

- 578
- 7
- 14
4 Answers
There are multiples externals libraries to know the number pages of document PDF, when the document was uploaded to server:
ITEXTSHARP
http://sourceforge.net/projects/itextsharp/
private static int getNumberOfPdfPages(string pathDocument)
{
return new iTextSharp.text.pdf.PdfReader(pathDocument).NumberOfPages;
}
PDFLIB
http://www.pdflib.com/ - (Method tested in version 6.0.4.0)
private static int getNumberOfPdfPages(string pathDocument)
{
int doc = 0;
int numPages = 0;
PDFlib_dotnet.PDFlib oPDF = new PDFlib_dotnet.PDFlib();
doc = oPDF.open_pdi(pathDocument, "", 0); //open document
if (doc != -1) //if not problem open document
{
numPages = (int)oPDF.get_pdi_value("/Root/Pages/Count", doc, -1, 0);
oPDF.close_pdi(doc);//close document
}
return numPages;
}
PDFSHARP
private static int getNumberOfPdfPages(string pathDocument)
{
return PdfSharp.Pdf.IO.PdfReader.Open(pathDocument, PdfSharp.Pdf.IO.PdfDocumentOpenMode.InformationOnly).PageCount;
}
STREAMREADER
Not need external library
public static int getNumberOfPdfPages(string pathDocument)
{
using (StreamReader sr = new StreamReader(File.OpenRead(pathDocument)))
{
return new Regex(@"/Type\s*/Page[^s]").Matches(sr.ReadToEnd()).Count;
}
}

- 1,510
- 16
- 26
You'll need a PDF library to parse the PDF and determine the number of pages it contains. There are several free ones for .NET.

- 28,277
- 11
- 59
- 92
You may be able to find the /Count
value in the file and just parse that out without using an external library. It'll look like /Count 5
if the PDF has 5 pages.
I don't know how robust that solution is, but it's definitely worth trying before adding/learning another library.

- 971
- 7
- 15
I used PDFjs to evaluate PDF before uploading on the client side. Works great for getting page count, page size....though the payload for loading the PDFjs is a big heavy

- 362
- 4
- 13