QuestPDF icon indicating copy to clipboard operation
QuestPDF copied to clipboard

Get Page Count Without Writing to Text

Open robertmiles3 opened this issue 2 years ago • 1 comments

Is your feature request related to a problem? Please describe. I am developing PDF documents that will go to a printer and need the ability to know if the generated PDF is a single page or a multi-page document so that I can file them appropriately. There currently seems to be no way to simply get the total page count without writing it to a text element using something like column.Item().Text(content => content.TotalPages());. Even then I can't access the count that it writes to text in order to grab it for other purposes.

Describe the solution you'd like I would like the ability to get the total page count of a generated document without writing it on the document itself. A property accessible like IDocument.TotalPages would be perfect...

var document = Document.Create(container =>
{
    // foo
});
Console.WriteLine($"Page Count = {document.TotalPages}");

Describe alternatives you've considered The only alternative I have right now is to use a tool like pdfinfo outside of the dotnet application in order to determine how many pages the document has. It goes without saying that this isn't ideal. 😉

robertmiles3 avatar Oct 16 '22 09:10 robertmiles3

By looking at some of the code and realizing you can write a text element to null, I did just figure out a workaround albeit hacky by adding the following that writes to a new property...

column.Item().Text(text =>
{
    text.TotalPages().Format(x =>
    {
        TotalPages = Math.Max(x.Value, TotalPages);
        return null;
    });
});

Is this trustworthy enough to use until this feature possible gets added?

robertmiles3 avatar Oct 16 '22 10:10 robertmiles3