| 1 | /** |
| 2 | * SPDX-FileCopyrightText: (C) 2010 Dominik Seichter <[email protected]> |
| 3 | * SPDX-License-Identifier: GPL-2.0-or-later |
| 4 | */ |
| 5 | |
| 6 | #include <podofo/podofo.h> |
| 7 | |
| 8 | #include <cstdlib> |
| 9 | #include <cstdio> |
| 10 | |
| 11 | using namespace std; |
| 12 | using namespace PoDoFo; |
| 13 | |
| 14 | void print_help() |
| 15 | { |
| 16 | printf("Usage: podofocountpages [-s] [-t] file1.pdf ... \n\n"); |
| 17 | printf(" This tool counts the pages in a PDF file.\n"); |
| 18 | printf(" -s will enable the short format, which ommites\n"); |
| 19 | printf(" printing of the filename in the output.\n"); |
| 20 | printf(" -t print the total sum of all pages.\n"); |
| 21 | printf("\nPoDoFo Version: %s\n\n", PODOFO_VERSION_STRING); |
| 22 | } |
| 23 | |
| 24 | int count_pages(const string_view filename, const bool& shortFormat) |
| 25 | { |
| 26 | PdfMemDocument document; |
| 27 | document.Load(filename); |
| 28 | unsigned nPages = document.GetPages().GetCount(); |
| 29 | |
| 30 | if (shortFormat) |
| 31 | printf("%i\n", nPages); |
| 32 | else |
| 33 | printf("%s:\t%u\n", filename.data(), nPages); |
| 34 | |
| 35 | return nPages; |
| 36 | } |
| 37 | |
| 38 | void Main(const cspan<string_view>& args) |
| 39 | { |
| 40 | PdfCommon::SetMaxLoggingSeverity(PdfLogSeverity::None); |
| 41 | |
| 42 | if (args.size() <= 1) |
| 43 | { |
| 44 | print_help(); |
| 45 | exit(-1); |
| 46 | } |
| 47 | |
| 48 | bool total = false; |
| 49 | bool shortFormat = false; |
| 50 | int sum = 0; |
| 51 | |
| 52 | for (unsigned i = 1; i < args.size(); i++) |
| 53 | { |
| 54 | auto arg = args[i]; |
| 55 | |
| 56 | if (arg == "-s") |
| 57 | { |
| 58 | shortFormat = true; |
| 59 | } |
| 60 | else if (arg == "-t") |
| 61 | { |
| 62 | total = true; |
| 63 | } |
| 64 | else |
| 65 | { |
| 66 | sum += count_pages(arg, shortFormat); |
| 67 | } |
| 68 | } |
| 69 | |
| 70 | if (total) |
| 71 | { |
| 72 | printf("Total:\t%i\n", sum); |
| 73 | } |
| 74 | } |