| 1 | /** |
| 2 | * SPDX-FileCopyrightText: (C) 2020 Ivan Romanov <[email protected]> |
| 3 | * SPDX-License-Identifier: GPL-2.0-or-later |
| 4 | */ |
| 5 | |
| 6 | #include <iostream> |
| 7 | #include <iterator> |
| 8 | #include <string> |
| 9 | #include <cstdlib> |
| 10 | #include <cstdio> |
| 11 | #include <algorithm> |
| 12 | |
| 13 | #ifdef _MSC_VER |
| 14 | #include <io.h> |
| 15 | #include <fcntl.h> |
| 16 | #endif |
| 17 | |
| 18 | #include <podofo/podofo.h> |
| 19 | |
| 20 | using namespace std; |
| 21 | using namespace PoDoFo; |
| 22 | |
| 23 | void Main(const cspan<string_view>& args) |
| 24 | { |
| 25 | using namespace PoDoFo; |
| 26 | PdfMemDocument* doc = nullptr; |
| 27 | |
| 28 | PdfCommon::SetMaxLoggingSeverity(PdfLogSeverity::None); |
| 29 | if (args.size() < 3) |
| 30 | { |
| 31 | cout << "Usage" << endl; |
| 32 | cout << " " << args[0] << " <in.pdf> <out.pdf> [OC_name]..." << endl; |
| 33 | exit(-1); |
| 34 | } |
| 35 | |
| 36 | if (args[1] == "-") |
| 37 | { |
| 38 | cin >> std::noskipws; |
| 39 | #ifdef _MSC_VER |
| 40 | _setmode(_fileno(stdin), _O_BINARY); // @TODO: MSVC specific binary setmode -- not sure if other platforms need it |
| 41 | cin.sync_with_stdio(); |
| 42 | #endif |
| 43 | istream_iterator<char> it(std::cin); |
| 44 | istream_iterator<char> end; |
| 45 | string buffer(it, end); |
| 46 | doc = new PdfMemDocument(); |
| 47 | doc->LoadFromBuffer(buffer); |
| 48 | } |
| 49 | else |
| 50 | { |
| 51 | doc = new PdfMemDocument(); |
| 52 | doc->Load(args[1]); |
| 53 | } |
| 54 | |
| 55 | vector<string> ocToRemove; |
| 56 | for (unsigned i = 3; i < args.size(); i++) |
| 57 | { |
| 58 | ocToRemove.push_back(string(args[i])); |
| 59 | } |
| 60 | |
| 61 | int ocCount = 0; |
| 62 | PdfObject* ocProperties = doc->GetTrailer().GetDictionary().MustFindKey("Root").GetDictionary().FindKey("OCProperties"); |
| 63 | |
| 64 | if (ocProperties) |
| 65 | { |
| 66 | auto ocgs = ocProperties->GetDictionary().FindKey("OCGs"); |
| 67 | if (ocgs) |
| 68 | { |
| 69 | auto& objects = doc->GetObjects(); |
| 70 | PdfArray ocgsArr = ocgs->GetArray(); |
| 71 | for (PdfArray::iterator it1 = ocgsArr.begin(); it1 != ocgsArr.end(); it1++) |
| 72 | { |
| 73 | PdfReference ocgRef = (*it1).GetReference(); |
| 74 | if (!objects.GetObject(ocgRef)) |
| 75 | continue; |
| 76 | |
| 77 | const string& ocgName = objects.MustGetObject(ocgRef).GetDictionary().MustFindKey("Name").GetString().GetString(); |
| 78 | |
| 79 | if (!ocToRemove.empty() && find(ocToRemove.begin(), ocToRemove.end(), ocgName) == ocToRemove.end()) |
| 80 | continue; |
| 81 | |
| 82 | for (auto it2 = objects.rbegin(); it2 != objects.rend(); it2++) |
| 83 | { |
| 84 | auto ob = *it2; |
| 85 | if (ob->IsDictionary()) |
| 86 | { |
| 87 | auto oc = ob->GetDictionary().GetKey("OC"); |
| 88 | if (oc != nullptr) |
| 89 | { |
| 90 | PdfReference ocRef = oc->GetReference(); |
| 91 | if (ocRef == ocgRef || ((ocgs = objects.MustGetObject(ocRef).GetDictionary().GetKey("OCGs")) != nullptr |
| 92 | && ocgs->GetReference() == ocgRef)) |
| 93 | { |
| 94 | objects.RemoveObject(ob->GetIndirectReference()); |
| 95 | ocCount++; |
| 96 | } |
| 97 | } |
| 98 | } |
| 99 | } |
| 100 | |
| 101 | objects.RemoveObject(ocgRef); |
| 102 | } |
| 103 | } |
| 104 | } |
| 105 | |
| 106 | if (ocCount) |
| 107 | { |
| 108 | doc->Save(args[2]); |
| 109 | } |
| 110 | else |
| 111 | { |
| 112 | cout << "No optional content in this PDF" << endl; |
| 113 | } |
| 114 | |
| 115 | if (doc) |
| 116 | delete doc; |
| 117 | } |