| 1 | /** |
| 2 | * SPDX-FileCopyrightText: (C) 2009 Dominik Seichter <[email protected]> |
| 3 | * SPDX-License-Identifier: GPL-2.0-or-later |
| 4 | */ |
| 5 | |
| 6 | #include <podofo/podofo.h> |
| 7 | #include <cstdlib> |
| 8 | #include <cstdio> |
| 9 | |
| 10 | #include <iostream> |
| 11 | #include <sstream> |
| 12 | #include <string> |
| 13 | #include <stdexcept> |
| 14 | #include <vector> |
| 15 | |
| 16 | #include "DeleteOperation.h" |
| 17 | #include "MoveOperation.h" |
| 18 | |
| 19 | using namespace std; |
| 20 | using namespace PoDoFo; |
| 21 | |
| 22 | class BadConversion : public runtime_error { |
| 23 | public: |
| 24 | BadConversion(const string& s) |
| 25 | : runtime_error(s) |
| 26 | { } |
| 27 | }; |
| 28 | |
| 29 | void print_help() |
| 30 | { |
| 31 | printf("Usage: podofopages [inputfile] [outputfile]\n"); |
| 32 | printf("Options:\n"); |
| 33 | printf("\t--delete NUMBER\n"); |
| 34 | printf("\tDeletes the page NUMBER (number is 0-based)\n"); |
| 35 | printf("\tThe page will not really be deleted from the PDF.\n"); |
| 36 | printf("\tIt is only removed from the so called pagestree and\n"); |
| 37 | printf("\ttherefore invisible. The content of the page can still\n"); |
| 38 | printf("\tbe retrieved from the document though.\n\n"); |
| 39 | printf("\t--move FROM TO\n"); |
| 40 | printf("\tMoves a page FROM TO in the document (FROM and TO are 0-based)\n\n"); |
| 41 | printf("\nPoDoFo Version: %s\n\n", PODOFO_VERSION_STRING); |
| 42 | } |
| 43 | |
| 44 | void work(const string_view& inputPath, const string_view& outputPath, const vector<Operation*>& operations) |
| 45 | { |
| 46 | cout << "Input file: " << inputPath << endl; |
| 47 | cout << "Output file: " << outputPath << endl; |
| 48 | |
| 49 | PdfMemDocument doc; |
| 50 | doc.Load(inputPath); |
| 51 | |
| 52 | unsigned total = (unsigned)operations.size(); |
| 53 | unsigned i = 1; |
| 54 | for (auto operation : operations) |
| 55 | { |
| 56 | string msg = operation->ToString(); |
| 57 | cout << "Operation " << i << " of " << total << ": " << msg; |
| 58 | |
| 59 | operation->Perform(doc); |
| 60 | |
| 61 | i++; |
| 62 | } |
| 63 | |
| 64 | cout << "Operations done. Writing PDF to disk." << endl; |
| 65 | |
| 66 | doc.Save(outputPath); |
| 67 | |
| 68 | cout << "Done." << endl; |
| 69 | } |
| 70 | |
| 71 | double convertToInt(const string_view& s) |
| 72 | { |
| 73 | istringstream i((string)s); |
| 74 | int x; |
| 75 | if (!(i >> x)) |
| 76 | throw BadConversion("convertToInt()"); |
| 77 | return x; |
| 78 | } |
| 79 | |
| 80 | void Main(const cspan<string_view>& args) |
| 81 | { |
| 82 | string_view inputPath; |
| 83 | string_view outputPath; |
| 84 | |
| 85 | if (args.size() < 3) |
| 86 | { |
| 87 | print_help(); |
| 88 | exit(-1); |
| 89 | } |
| 90 | |
| 91 | // Fill operations vector |
| 92 | vector<Operation*> operations; |
| 93 | for (unsigned i = 1; i < args.size(); i++) |
| 94 | { |
| 95 | string_view argument = args[i]; |
| 96 | if (argument == "--delete" || argument == "-delete") |
| 97 | { |
| 98 | int page = static_cast<int>(convertToInt(args[i + 1])); |
| 99 | operations.push_back(new DeleteOperation(page)); |
| 100 | i++; |
| 101 | } |
| 102 | else if (argument == "--move" || argument == "-move") |
| 103 | { |
| 104 | int from = static_cast<int>(convertToInt(args[i + 1])); |
| 105 | int to = static_cast<int>(convertToInt(args[i + 2])); |
| 106 | operations.push_back(new MoveOperation(from, to)); |
| 107 | i++; |
| 108 | i++; |
| 109 | } |
| 110 | else |
| 111 | { |
| 112 | if (inputPath == NULL) |
| 113 | { |
| 114 | inputPath = args[i]; |
| 115 | } |
| 116 | else if (outputPath == NULL) |
| 117 | { |
| 118 | outputPath = args[i]; |
| 119 | } |
| 120 | else |
| 121 | { |
| 122 | cerr << "Ignoring unknown argument: " << argument << endl; |
| 123 | } |
| 124 | } |
| 125 | } |
| 126 | |
| 127 | if (args.empty()) |
| 128 | { |
| 129 | cerr << "Please specify an input file." << endl; |
| 130 | exit(-2); |
| 131 | } |
| 132 | |
| 133 | if (outputPath.empty()) |
| 134 | { |
| 135 | cerr << "Please specify an output file." << endl; |
| 136 | exit(-3); |
| 137 | } |
| 138 | |
| 139 | if (string(inputPath) == string(outputPath)) |
| 140 | { |
| 141 | cerr << "Input and outpuf file must point to different files." << endl; |
| 142 | exit(-4); |
| 143 | } |
| 144 | |
| 145 | work(inputPath, outputPath, operations); |
| 146 | |
| 147 | // Delete operations vectore |
| 148 | vector<Operation*>::iterator it = operations.begin(); |
| 149 | while (it != operations.end()) |
| 150 | { |
| 151 | delete (*it); |
| 152 | it++; |
| 153 | } |
| 154 | } |
| 155 |