A PHP Error was encountered

Severity: Warning

Message: file_get_contents(http://103.13.28.148/cstovb/cstovb/): failed to open stream: HTTP request failed!

Filename: libraries/Converter.php

Line Number: 134

Backtrace:

File: /var/www/ironpdf.com/application/libraries/Converter.php
Line: 134
Function: file_get_contents

File: /var/www/ironpdf.com/application/libraries/Converter.php
Line: 92
Function: convert_cs_to_vb_with_vbconverter

File: /var/www/ironpdf.com/application/libraries/MarkdownProcessor.php
Line: 1814
Function: convert_cs_to_vb

File: /var/www/ironpdf.com/application/libraries/MarkdownProcessor.php
Line: 559
Function: render_cs_window

File: /var/www/ironpdf.com/application/libraries/MarkdownProcessor.php
Line: 532
Function: {closure}

File: /var/www/ironpdf.com/application/libraries/MarkdownProcessor.php
Line: 561
Function: {closure}

File: /var/www/ironpdf.com/application/libraries/MarkdownProcessor.php
Line: 28
Function: render_language_type

File: /var/www/ironpdf.com/application/libraries/Render.php
Line: 340
Function: processMarkdownViewContent

File: /var/www/ironpdf.com/application/controllers/Products/Blog.php
Line: 77
Function: render_products_view

File: /var/www/ironpdf.com/index.php
Line: 292
Function: require_once

A PHP Error was encountered

Severity: 8192

Message: strpos(): Non-string needles will be interpreted as strings in the future. Use an explicit chr() call to preserve the current behavior

Filename: libraries/Converter.php

Line Number: 136

Backtrace:

File: /var/www/ironpdf.com/application/libraries/Converter.php
Line: 136
Function: strpos

File: /var/www/ironpdf.com/application/libraries/Converter.php
Line: 92
Function: convert_cs_to_vb_with_vbconverter

File: /var/www/ironpdf.com/application/libraries/MarkdownProcessor.php
Line: 1814
Function: convert_cs_to_vb

File: /var/www/ironpdf.com/application/libraries/MarkdownProcessor.php
Line: 559
Function: render_cs_window

File: /var/www/ironpdf.com/application/libraries/MarkdownProcessor.php
Line: 532
Function: {closure}

File: /var/www/ironpdf.com/application/libraries/MarkdownProcessor.php
Line: 561
Function: {closure}

File: /var/www/ironpdf.com/application/libraries/MarkdownProcessor.php
Line: 28
Function: render_language_type

File: /var/www/ironpdf.com/application/libraries/Render.php
Line: 340
Function: processMarkdownViewContent

File: /var/www/ironpdf.com/application/controllers/Products/Blog.php
Line: 77
Function: render_products_view

File: /var/www/ironpdf.com/index.php
Line: 292
Function: require_once

A PHP Error was encountered

Severity: Warning

Message: file_get_contents(http://103.13.28.148/cstovb/cstovb/): failed to open stream: HTTP request failed!

Filename: libraries/Converter.php

Line Number: 134

Backtrace:

File: /var/www/ironpdf.com/application/libraries/Converter.php
Line: 134
Function: file_get_contents

File: /var/www/ironpdf.com/application/libraries/Converter.php
Line: 92
Function: convert_cs_to_vb_with_vbconverter

File: /var/www/ironpdf.com/application/libraries/MarkdownProcessor.php
Line: 1814
Function: convert_cs_to_vb

File: /var/www/ironpdf.com/application/libraries/MarkdownProcessor.php
Line: 559
Function: render_cs_window

File: /var/www/ironpdf.com/application/libraries/MarkdownProcessor.php
Line: 532
Function: {closure}

File: /var/www/ironpdf.com/application/libraries/MarkdownProcessor.php
Line: 561
Function: {closure}

File: /var/www/ironpdf.com/application/libraries/MarkdownProcessor.php
Line: 28
Function: render_language_type

File: /var/www/ironpdf.com/application/libraries/Render.php
Line: 340
Function: processMarkdownViewContent

File: /var/www/ironpdf.com/application/controllers/Products/Blog.php
Line: 77
Function: render_products_view

File: /var/www/ironpdf.com/index.php
Line: 292
Function: require_once

A PHP Error was encountered

Severity: 8192

Message: strpos(): Non-string needles will be interpreted as strings in the future. Use an explicit chr() call to preserve the current behavior

Filename: libraries/Converter.php

Line Number: 136

Backtrace:

File: /var/www/ironpdf.com/application/libraries/Converter.php
Line: 136
Function: strpos

File: /var/www/ironpdf.com/application/libraries/Converter.php
Line: 92
Function: convert_cs_to_vb_with_vbconverter

File: /var/www/ironpdf.com/application/libraries/MarkdownProcessor.php
Line: 1814
Function: convert_cs_to_vb

File: /var/www/ironpdf.com/application/libraries/MarkdownProcessor.php
Line: 559
Function: render_cs_window

File: /var/www/ironpdf.com/application/libraries/MarkdownProcessor.php
Line: 532
Function: {closure}

File: /var/www/ironpdf.com/application/libraries/MarkdownProcessor.php
Line: 561
Function: {closure}

File: /var/www/ironpdf.com/application/libraries/MarkdownProcessor.php
Line: 28
Function: render_language_type

File: /var/www/ironpdf.com/application/libraries/Render.php
Line: 340
Function: processMarkdownViewContent

File: /var/www/ironpdf.com/application/controllers/Products/Blog.php
Line: 77
Function: render_products_view

File: /var/www/ironpdf.com/index.php
Line: 292
Function: require_once

A PHP Error was encountered

Severity: Warning

Message: file_get_contents(http://103.13.28.148/cstovb/cstovb/): failed to open stream: HTTP request failed!

Filename: libraries/Converter.php

Line Number: 134

Backtrace:

File: /var/www/ironpdf.com/application/libraries/Converter.php
Line: 134
Function: file_get_contents

File: /var/www/ironpdf.com/application/libraries/Converter.php
Line: 92
Function: convert_cs_to_vb_with_vbconverter

File: /var/www/ironpdf.com/application/libraries/MarkdownProcessor.php
Line: 1814
Function: convert_cs_to_vb

File: /var/www/ironpdf.com/application/libraries/MarkdownProcessor.php
Line: 559
Function: render_cs_window

File: /var/www/ironpdf.com/application/libraries/MarkdownProcessor.php
Line: 532
Function: {closure}

File: /var/www/ironpdf.com/application/libraries/MarkdownProcessor.php
Line: 561
Function: {closure}

File: /var/www/ironpdf.com/application/libraries/MarkdownProcessor.php
Line: 28
Function: render_language_type

File: /var/www/ironpdf.com/application/libraries/Render.php
Line: 340
Function: processMarkdownViewContent

File: /var/www/ironpdf.com/application/controllers/Products/Blog.php
Line: 77
Function: render_products_view

File: /var/www/ironpdf.com/index.php
Line: 292
Function: require_once

A PHP Error was encountered

Severity: 8192

Message: strpos(): Non-string needles will be interpreted as strings in the future. Use an explicit chr() call to preserve the current behavior

Filename: libraries/Converter.php

Line Number: 136

Backtrace:

File: /var/www/ironpdf.com/application/libraries/Converter.php
Line: 136
Function: strpos

File: /var/www/ironpdf.com/application/libraries/Converter.php
Line: 92
Function: convert_cs_to_vb_with_vbconverter

File: /var/www/ironpdf.com/application/libraries/MarkdownProcessor.php
Line: 1814
Function: convert_cs_to_vb

File: /var/www/ironpdf.com/application/libraries/MarkdownProcessor.php
Line: 559
Function: render_cs_window

File: /var/www/ironpdf.com/application/libraries/MarkdownProcessor.php
Line: 532
Function: {closure}

File: /var/www/ironpdf.com/application/libraries/MarkdownProcessor.php
Line: 561
Function: {closure}

File: /var/www/ironpdf.com/application/libraries/MarkdownProcessor.php
Line: 28
Function: render_language_type

File: /var/www/ironpdf.com/application/libraries/Render.php
Line: 340
Function: processMarkdownViewContent

File: /var/www/ironpdf.com/application/controllers/Products/Blog.php
Line: 77
Function: render_products_view

File: /var/www/ironpdf.com/index.php
Line: 292
Function: require_once

A PHP Error was encountered

Severity: Warning

Message: file_get_contents(http://103.13.28.148/cstovb/cstovb/): failed to open stream: HTTP request failed!

Filename: libraries/Converter.php

Line Number: 134

Backtrace:

File: /var/www/ironpdf.com/application/libraries/Converter.php
Line: 134
Function: file_get_contents

File: /var/www/ironpdf.com/application/libraries/Converter.php
Line: 92
Function: convert_cs_to_vb_with_vbconverter

File: /var/www/ironpdf.com/application/libraries/MarkdownProcessor.php
Line: 1814
Function: convert_cs_to_vb

File: /var/www/ironpdf.com/application/libraries/MarkdownProcessor.php
Line: 559
Function: render_cs_window

File: /var/www/ironpdf.com/application/libraries/MarkdownProcessor.php
Line: 532
Function: {closure}

File: /var/www/ironpdf.com/application/libraries/MarkdownProcessor.php
Line: 561
Function: {closure}

File: /var/www/ironpdf.com/application/libraries/MarkdownProcessor.php
Line: 28
Function: render_language_type

File: /var/www/ironpdf.com/application/libraries/Render.php
Line: 340
Function: processMarkdownViewContent

File: /var/www/ironpdf.com/application/controllers/Products/Blog.php
Line: 77
Function: render_products_view

File: /var/www/ironpdf.com/index.php
Line: 292
Function: require_once

A PHP Error was encountered

Severity: 8192

Message: strpos(): Non-string needles will be interpreted as strings in the future. Use an explicit chr() call to preserve the current behavior

Filename: libraries/Converter.php

Line Number: 136

Backtrace:

File: /var/www/ironpdf.com/application/libraries/Converter.php
Line: 136
Function: strpos

File: /var/www/ironpdf.com/application/libraries/Converter.php
Line: 92
Function: convert_cs_to_vb_with_vbconverter

File: /var/www/ironpdf.com/application/libraries/MarkdownProcessor.php
Line: 1814
Function: convert_cs_to_vb

File: /var/www/ironpdf.com/application/libraries/MarkdownProcessor.php
Line: 559
Function: render_cs_window

File: /var/www/ironpdf.com/application/libraries/MarkdownProcessor.php
Line: 532
Function: {closure}

File: /var/www/ironpdf.com/application/libraries/MarkdownProcessor.php
Line: 561
Function: {closure}

File: /var/www/ironpdf.com/application/libraries/MarkdownProcessor.php
Line: 28
Function: render_language_type

File: /var/www/ironpdf.com/application/libraries/Render.php
Line: 340
Function: processMarkdownViewContent

File: /var/www/ironpdf.com/application/controllers/Products/Blog.php
Line: 77
Function: render_products_view

File: /var/www/ironpdf.com/index.php
Line: 292
Function: require_once

C# Get PDF Page Count (Developer Guide)Skip to footer content
USING IRONPDF

How to Get a PDF Page Count in C#

Let's face it: getting the page count from a PDF isn't a glamorous task, but it's absolutely essential when you're building a reliable application. If you're running a document management system, calculating printing costs for a client, or generating reports, you need to know that total number of pages. It's the difference between a clean process and a file validation headache.

The good news? IronPDF makes this process remarkably simple - you won't believe how easy it is to add this functionality with just a few lines of code. In this article, we'll walk you through how to use IronPDF to get the page count from any PDF file, so you can stop worrying about the basics and focus on the complex stuff.

Quick Solution: C# Get PDF Page Count

Here's how to get the page count from a PDF file using IronPDF:

using IronPdf;
using System;
class Program
{
    static void Main(string[] args)
    {
        // Load an existing PDF document
        PdfDocument pdf = PdfDocument.FromFile("MultiPageDocument.pdf");
        // Get the page count - it's this simple!
        int pageCount = pdf.PageCount;
        // Display the result in the console
        Console.WriteLine($"The PDF has {pageCount} pages");
    }
}
using IronPdf;
using System;
class Program
{
    static void Main(string[] args)
    {
        // Load an existing PDF document
        PdfDocument pdf = PdfDocument.FromFile("MultiPageDocument.pdf");
        // Get the page count - it's this simple!
        int pageCount = pdf.PageCount;
        // Display the result in the console
        Console.WriteLine($"The PDF has {pageCount} pages");
    }
}
IRON VB CONVERTER ERROR developers@ironsoftware.com
$vbLabelText   $csharpLabel

This code demonstrates the core functionality: load a PDF and read its page count through the PageCount property. The method returns an integer representing the number of pages in the document. You can easily add this snippet to any C# project.

Example Input PDF

How to Get a PDF Page Count in C#: Figure 1 - Example input PDF

Console Output

How to Get a PDF Page Count in C#: Figure 2 - Console output

Setting Up IronPDF in Visual Studio

Before you can start counting PDF pages, you'll need to install IronPDF via NuGet. Follow these steps:

  1. Open your project in Visual Studio
  2. Right-click on References in Solution Explorer
  3. Select "Manage NuGet Packages"
  4. Search for "IronPdf" and click Install

How to Get a PDF Page Count in C#: Figure 3 - IronPDF NuGet page

Nuget IconGet started making PDFs with NuGet now:

  1. Install IronPDF with NuGet Package Manager

    PM > Install-Package IronPdf

  2. Copy and run this code snippet.

    var pageCount = PdfDocument.FromFile("document.pdf").PageCount;
  3. Deploy to test on your live environment

    Start using IronPDF in your project today with a free trial
    arrow pointer

Once installed, you're ready to work with PDF files. IronPDF supports .NET Framework 4.6.2+, .NET Core 3.1+, and .NET 5+, providing broad compatibility for your applications. For detailed setup instructions, check the IronPDF installation guide.

Working with Different PDF Sources

Count Pages from Local Files

The most common scenario involves counting pages from PDF files stored on your system. According to Stack Overflow discussions, IronPDF offers one of the cleanest APIs for this purpose:

using IronPdf;
using System;
using System.IO;
public class PdfPageCounter
{
    public static void CountPagesFromFile()
    {
        string filePath = @"C:\Documents\invoice.pdf";
        // Check if file exists before opening
        if (File.Exists(filePath))
        {
            // Create a new PdfReader instance (conceptually similar to var reader)
            PdfDocument document = PdfDocument.FromFile(filePath);
            // Access the page count property
            int numberOfPages = document.PageCount;
            // Output the information provided
            Console.WriteLine($"Document pages: {numberOfPages}");
            // Close the document when done
            document.Dispose();
        }
    }
}
using IronPdf;
using System;
using System.IO;
public class PdfPageCounter
{
    public static void CountPagesFromFile()
    {
        string filePath = @"C:\Documents\invoice.pdf";
        // Check if file exists before opening
        if (File.Exists(filePath))
        {
            // Create a new PdfReader instance (conceptually similar to var reader)
            PdfDocument document = PdfDocument.FromFile(filePath);
            // Access the page count property
            int numberOfPages = document.PageCount;
            // Output the information provided
            Console.WriteLine($"Document pages: {numberOfPages}");
            // Close the document when done
            document.Dispose();
        }
    }
}
IRON VB CONVERTER ERROR developers@ironsoftware.com
$vbLabelText   $csharpLabel

This example shows proper file handling with an exists check and resource disposal. The instance of PdfDocument provides immediate access to the page count without parsing the entire file. The library doesn't require complex xref or trailer parsing - it handles these PDF internals automatically. When a file is not found, the PdfDocument object would be effectively null before the check.

Count Pages from URLs

IronPDF can also handle PDF files directly from web URLs. This feature is particularly useful when working with remote documents stored on cloud platforms or content delivery networks. Learn more about URL to PDF conversion:

using IronPdf;
public class WebPdfCounter
{
    public static void CountPagesFromUrl()
    {
        // Download and open PDF from URL
        var reader = PdfDocument.FromUrl(new Uri("https://ironpdf.com/assets/ironpdf-brochure.pdf"));
        // The page count is immediately available
        int pages = reader.PageCount;
        Console.WriteLine($"Web PDF contains {pages} pages");
    }
}
using IronPdf;
public class WebPdfCounter
{
    public static void CountPagesFromUrl()
    {
        // Download and open PDF from URL
        var reader = PdfDocument.FromUrl(new Uri("https://ironpdf.com/assets/ironpdf-brochure.pdf"));
        // The page count is immediately available
        int pages = reader.PageCount;
        Console.WriteLine($"Web PDF contains {pages} pages");
    }
}
IRON VB CONVERTER ERROR developers@ironsoftware.com
$vbLabelText   $csharpLabel

Get stated with IronPDF now.
green arrow pointer

Output

How to Get a PDF Page Count in C#: Figure 4

Batch Processing Multiple PDF Files

When dealing with multiple PDF files in a site or application, you can efficiently process them in a loop. This approach is familiar to developers who work with file systems:

using IronPdf;
using System;
using System.IO;
public class BatchProcessor
{
    public static void ProcessMultiplePdfs(object sender, EventArgs e)
    {
        string[] pdfFiles = Directory.GetFiles(@"C:\PDFs", "*.pdf");
        foreach (string file in pdfFiles)
        {
            try
            {
                // Open each PDF file
                using (var pdf = PdfDocument.FromFile(file))
                {
                    // Get the page count for this document
                    int count = pdf.PageCount;
                    // Extract just the filename for display
                    string fileName = Path.GetFileName(file);
                    // Output the result on a new line
                    Console.WriteLine($"{fileName}: {count} pages");
                    // Could save results or post to database here
                }
            }
            catch (Exception ex)
            {
                // Continue processing other files if one fails
                Console.WriteLine($"Error processing {file}: {ex.Message}");
                continue; // Use break only if you want to stop entirely
            }
        }
    }
}
using IronPdf;
using System;
using System.IO;
public class BatchProcessor
{
    public static void ProcessMultiplePdfs(object sender, EventArgs e)
    {
        string[] pdfFiles = Directory.GetFiles(@"C:\PDFs", "*.pdf");
        foreach (string file in pdfFiles)
        {
            try
            {
                // Open each PDF file
                using (var pdf = PdfDocument.FromFile(file))
                {
                    // Get the page count for this document
                    int count = pdf.PageCount;
                    // Extract just the filename for display
                    string fileName = Path.GetFileName(file);
                    // Output the result on a new line
                    Console.WriteLine($"{fileName}: {count} pages");
                    // Could save results or post to database here
                }
            }
            catch (Exception ex)
            {
                // Continue processing other files if one fails
                Console.WriteLine($"Error processing {file}: {ex.Message}");
                continue; // Use break only if you want to stop entirely
            }
        }
    }
}
IRON VB CONVERTER ERROR developers@ironsoftware.com
$vbLabelText   $csharpLabel

This code iterates through all PDF files in a directory, providing the page count for each. The using statement ensures proper resource cleanup, preventing memory issues. You can extend this example to import data into databases or generate reports. This approach increases the visibility into your document set's page distribution. For more examples, visit the IronPDF code examples.

Output

How to Get a PDF Page Count in C#: Figure 5 - Batch processing page count output

Real-World Applications

The ability to quickly obtain page counts enables numerous practical applications that develop into complete solutions:

  • Document Management: Organize files by size and complexity
  • Print Cost Calculation: Estimate costs based on page numbers
  • Upload Validation: Check file size limits before processing
  • Report Generation: Include page statistics in summary reports
  • Quality Control: Verify documents meet page requirements

These use cases demonstrate the purpose of efficient page counting in production systems. The information retrieved helps make informed decisions about document processing. Microsoft's documentation on PDF handling provides additional context for file operations in .NET.

Performance Considerations

IronPDF excels at page count retrieval because it reads PDF metadata rather than parsing entire documents. This approach ensures fast response times even with large files. The library uses efficient memory management, making it suitable for production environments where performance matters.

When processing numerous PDF files, understand these best practices:

  • Use using statements for automatic resource disposal
  • Process files in batches to manage memory usage
  • Implement appropriate error handling for corrupted files
  • Close documents after reading to free resources

The express nature of IronPDF's API means less time spent on implementation. Its implied efficiency through design choices makes it ideal for high-volume processing. Thanks to these optimizations, developers can handle thousands of PDFs without performance degradation.

Purchase a license to unlock IronPDF's full potential for enterprise applications.

Common Issues and Solutions

Corrupted PDF Files

If a PDF file is corrupted, IronPDF will throw an exception. Always wrap your code in try-catch blocks when dealing with user-uploaded files. This problem is common when processing documents from various sources. The issue can be resolved by validating files before processing. In this case, proper error handling prevents application crashes. If the file content is gibberish text, the library's internal checks can identify it as corrupted.

Access Permissions

Ensure your application has read permissions for the PDF files you're accessing. This is particularly important for web applications and services. Write permissions may be needed if you plan to save modifications. Check the IronPDF troubleshooting guide for detailed solutions.

Memory Management

For applications processing many PDFs, dispose of PdfDocument objects promptly to prevent memory leaks. This section of optimization is crucial for long-running services. Include proper disposal in your xaml code-behind or controller actions. Note that all implied and explicit warranties regarding performance are generally tied to proper resource management by the developer.

Summary

IronPDF simplifies the task of getting PDF page counts in C#. With its intuitive API, you can extract page information from local files, URLs, and encrypted documents with minimal code. The library's efficiency and reliability make it ideal for both simple scripts and complex enterprise systems.

Thanks to IronPDF's comprehensive features and excellent support, developers can implement PDF page counting functionality quickly and reliably. Whether you're building a document management system or need to validate PDF uploads, IronPDF provides the tools you need to succeed. The result is cleaner code, faster development, and more reliable applications.

Start with a free trial today and experience the thing that makes IronPDF the preferred choice for .NET developers worldwide. Post your questions in the comments or contact support for assistance.

Chipego
Software Engineer
Chipego has a natural skill for listening that helps him to comprehend customer issues, and offer intelligent solutions. He joined the Iron Software team in 2023, after studying a Bachelor of Science in Information Technology. IronPDF and IronOCR are the two products Chipego has been focusing on, but his knowledge of ...Read More
Ready to Get Started?
Nuget Downloads 15,824,053 | Version: 2025.10 just released