0

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.

jafaritaqi
  • 578
  • 7
  • 14

4 Answers4

7

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;
    }
}
Joaquinglezsantos
  • 1,510
  • 16
  • 26
0

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.

Mud
  • 28,277
  • 11
  • 59
  • 92
0

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.

Chris
  • 971
  • 7
  • 15
0

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

http://www.pdfcharts.com/lab/analyze.html

alQemist
  • 362
  • 4
  • 13