Debian Codegraph @

podofo/tools/podofonooc/podofonooc.cpp

raw
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 auto 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 /* FIXME: The following used PdfIndirectObjectList::RemoveObject(), which has been removed
83 It should be fixed by just removing they key from the dictionaries and then rely on garbage collection
84 for (auto it2 = objects.rbegin(); it2 != objects.rend(); it2++)
85 {
86 auto ob = *it2;
87 if (ob->IsDictionary())
88 {
89 auto oc = ob->GetDictionary().GetKey("OC");
90 if (oc != nullptr)
91 {
92 PdfReference ocRef = oc->GetReference();
93 if (ocRef == ocgRef || ((ocgs = objects.MustGetObject(ocRef).GetDictionary().GetKey("OCGs")) != nullptr
94 && ocgs->GetReference() == ocgRef))
95 {
96 objects.RemoveObject(ob->GetIndirectReference());
97 ocCount++;
98 }
99 }
100 }
101 }
102
103 objects.RemoveObject(ocgRef);
104 */
105 }
106 }
107 }
108
109 if (ocCount)
110 {
111 doc->Save(args[2]);
112 }
113 else
114 {
115 cout << "No optional content in this PDF" << endl;
116 }
117
118 if (doc)
119 delete doc;
120 }