Would it be possible to obtain the total number of pages of PDF files in C#?

Asked 2 years ago, Updated 2 years ago, 120 views

I've done a lot of research, but I haven't found any Adobe Reader (Acrobat Reader) type library, and I've checked AcroPDFLib.AcroPDF, but I haven't found any methods or properties.

I came up with the idea that repeating the gotoNextPage() method until an exception occurs may not result in a page count, but it's not beautiful.I didn't try it, but I think it's slow in the first place (^_^;

).

For the time being, if you add up the total number of pages in multiple PDFs and the number of pages is greater than the user's set value, you can warn them. For example, if it is set to 10 pages, you don't need to check the number of pages.

Do we have no choice but to give up? Please let me know if you know.

"In addition to the following information, we also looked for items found by searching for ""C#PDF number of pages""."

AcroPDFLib.AcroPDF is the screen control. As for PDFs, it doesn't have to be a screen control, except for thumbnail information (which I'm trying to get in a different way) as long as I get the total number of pages.It's better not to use screen control.

xxxPDF pdf = new xxxPDF();
pdf.LoadFromStream(stream);
int pageNum=pdf.GetPageCount();
size.width=pdf.GetPDFInfo().PaperSize.width;
size.height=pdf.GetPDFInfo().PaperSize.height;

I would appreciate it if you could get PDF information like this^^

c# pdf

2022-09-30 17:33

2 Answers

Similar question seems to exist in the home SO.

Accoding to the API Reference there's a function called GetNumPages:

GetNumPages(); Returns The number of pages in a file, or -1 if the number of
pages cannot be determined.


2022-09-30 17:33

All operations on PDFs are supported using Spire.PDF for .NET, a component for C#.Download the free version to here.Once you have added to the reference, use the code below to find out how many pages are in the PDF:

using Spire.Pdf;
using System;

namespace GetNumberOfPages
{
    class program
    {
        static void Main (string[]args)
        {
            // Create a PDFDocument instance and load the file
            PdfDocument document = new PdfDocument();
            stringFileName="sample.pdf";
            document.LoadFromFile(FileName);

            // Get Page Count
            int PageNumber = document.Pages.Count;
            Console.WriteLine("Page count: {0}", PageNumber);

            // Delete one page of the document
            document.Pages.RemoveAt(1);

            // Get the numbers on the page
            PageNumber = document.Pages.Count;
            Console.WriteLine("Second page count: {0}", PageNumber);
            Console.ReadLine();

            // Close Documentation
            document.Close();
        }
    }
}


2022-09-30 17:33

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.