Question

My task is to get page count of a pdf content. I made use of SSRS tools to generate pdf byte array. Once I get byte array I need to get the page count of this byte array generated. C# is the programming language used.

Currently I am creating a pdf file physically, using the generated pdf byte array. Later I am opening the pdf file in the memory and getting the page count. Finally I am deleting the file that has been created for temporary purpose.

Is there any way to get the page count of the generated pdf byte array without creating a physical file? Or can I create a pdf file object in the memory?

PDF creation needs extra security permissions and we don't use pdf file for any other purpose.

Was it helpful?

Solution

I fixed this problem using the below code.

Thanks for your help.

int pageCount;
MemoryStream stream = new MemoryStream(pdfContent);
using (var r = new StreamReader(stream))
{
    string pdfText = r.ReadToEnd();
    System.Text.RegularExpressions.Regex regx = new Regex(@"/Type\s*/Page[^s]");
    System.Text.RegularExpressions.MatchCollection matches = regx.Matches(pdfText);
    pageCount = matches.Count;
}

OTHER TIPS

Save the PDF stream. Use an iTextSharp reference to open the PDF on the server and count pages.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top