Debian Codegraph @

podofo/tools/podofopdfinfo/pdfinfo.cpp

raw
1 /**
2 * SPDX-FileCopyrightText: (C) 2005 Dominik Seichter <[email protected]>
3 * SPDX-License-Identifier: GPL-2.0-or-later
4 */
5
6 #include <podofo/private/PdfDeclarationsPrivate.h>
7 #include "pdfinfo.h"
8 #include <sstream>
9
10 using namespace std;
11 using namespace PoDoFo;
12
13 PdfInfoHelper::PdfInfoHelper(const string& filepath)
14 {
15 m_doc = new PdfMemDocument();
16 m_doc->Load(filepath);
17 }
18
19 PdfInfoHelper::~PdfInfoHelper()
20 {
21 if (m_doc)
22 {
23 delete m_doc;
24 m_doc = nullptr;
25 }
26 }
27
28 void PdfInfoHelper::OutputDocumentInfo(ostream& sOutStream)
29 {
30 sOutStream << "\tPDF Version: " << PoDoFo::GetPdfVersionName(m_doc->GetMetadata().GetPdfVersion()).GetString() << endl;
31 sOutStream << "\tPage Count: " << m_doc->GetPages().GetCount() << endl;
32 sOutStream << "\tPage Size: " << GuessFormat() << endl;
33 sOutStream << endl;
34 sOutStream << "\tTagged: " << (m_doc->GetCatalog().GetStructTreeRootObject() != nullptr ? "Yes" : "No") << endl;
35 sOutStream << "\tEncrypted: " << (m_doc->GetEncrypt() != nullptr ? "Yes" : "No") << endl;
36 sOutStream << "\tPrinting Allowed: " << (m_doc->IsPrintAllowed() ? "Yes" : "No") << endl;
37 sOutStream << "\tModification Allowed: " << (m_doc->IsEditAllowed() ? "Yes" : "No") << endl;
38 sOutStream << "\tCopy&Paste Allowed: " << (m_doc->IsCopyAllowed() ? "Yes" : "No") << endl;
39 sOutStream << "\tAdd/Modify Annotations Allowed: " << (m_doc->IsEditNotesAllowed() ? "Yes" : "No") << endl;
40 sOutStream << "\tFill&Sign Allowed: " << (m_doc->IsFillAndSignAllowed() ? "Yes" : "No") << endl;
41 sOutStream << "\tAccessibility Allowed: " << (m_doc->IsAccessibilityAllowed() ? "Yes" : "No") << endl;
42 sOutStream << "\tDocument Assembly Allowed: " << (m_doc->IsDocAssemblyAllowed() ? "Yes" : "No") << endl;
43 sOutStream << "\tHigh Quality Print Allowed: " << (m_doc->IsHighPrintAllowed() ? "Yes" : "No") << endl;
44 }
45
46 void PdfInfoHelper::OutputInfoDict(ostream& outStream)
47 {
48 if (m_doc->GetInfo() == nullptr)
49 {
50 outStream << "No info dictionary in this PDF file!" << endl;
51 }
52 else
53 {
54 auto author = m_doc->GetInfo()->GetAuthor();
55 if (author != nullptr)
56 outStream << "\tAuthor: " << author->GetString() << endl;
57
58 auto creator = m_doc->GetInfo()->GetCreator();
59 if (creator != nullptr)
60 outStream << "\tCreator: " << creator->GetString() << endl;
61
62 auto subject = m_doc->GetInfo()->GetSubject();
63 if (subject != nullptr)
64 outStream << "\tSubject: " << subject->GetString() << endl;
65
66 auto title = m_doc->GetInfo()->GetTitle();
67 if (title != nullptr)
68 outStream << "\tTitle: " << title->GetString() << endl;
69
70 auto keywords = m_doc->GetInfo()->GetKeywords();
71 if (keywords != nullptr)
72 outStream << "\tKeywords: " << keywords->GetString() << endl;
73
74 auto trapped = m_doc->GetInfo()->GetTrapped();
75 if (trapped != nullptr)
76 outStream << "\tTrapped: " << trapped->GetEscapedName() << endl;
77 }
78 }
79
80 void PdfInfoHelper::OutputPageInfo(ostream& outstream)
81 {
82 PdfArray arr;
83 string str;
84
85 unsigned annotCount;
86 unsigned pageCount = m_doc->GetPages().GetCount();
87 outstream << "Page Count: " << pageCount << endl;
88 for (unsigned pg = 0; pg < pageCount; pg++)
89 {
90 outstream << "Page " << pg << ":" << endl;
91
92 auto& curPage = m_doc->GetPages().GetPageAt(pg);
93 outstream << "->Internal Number:" << curPage.GetPageNumber() << endl;
94 outstream << "->Object Number:" << curPage.GetObject().GetIndirectReference().ObjectNumber()
95 << " " << curPage.GetObject().GetIndirectReference().GenerationNumber() << " R" << endl;
96
97 curPage.GetMediaBox().ToArray(arr);
98 arr.ToString(str);
99
100 annotCount = curPage.GetAnnotations().GetCount();
101 outstream << "\tMediaBox: " << str << endl;
102 outstream << "\tRotation: " << curPage.GetRotation() << endl;
103 outstream << "\t# of Annotations: " << annotCount << endl;
104
105 for (unsigned i = 0; i < annotCount; i++)
106 {
107 auto& curAnnot = curPage.GetAnnotations().GetAnnotAt(i);
108
109 curAnnot.GetRect().ToArray(arr);
110 arr.ToString(str);
111
112 outstream << endl;
113 outstream << "\tAnnotation " << i << endl;
114 outstream << "\t\tType: " << PoDoFo::ToString(curAnnot.GetType()) << endl;
115 auto contents = curAnnot.GetContents();
116 if (contents != nullptr)
117 outstream << "\t\tContents: " << contents->GetString() << endl;
118
119 auto title = curAnnot.GetTitle();
120 if (title != nullptr)
121 outstream << "\t\tTitle: " << title->GetString() << endl;
122
123 outstream << "\t\tFlags: " << (int)curAnnot.GetFlags() << endl;
124 outstream << "\t\tRect: " << str << endl;
125 if (curAnnot.GetType() == PdfAnnotationType::Link)
126 {
127 auto action = static_cast<PdfAnnotationLink&>(curAnnot).GetAction();
128 if (action != nullptr && action->GetType() == PdfActionType::URI)
129 {
130 auto uri = static_cast<PdfActionURI&>(*action).GetURI();
131 if (uri != nullptr)
132 outstream << "\t\tAction URI: " << uri->GetString() << endl;
133 }
134 }
135 }
136 }
137 }
138
139 void PdfInfoHelper::OutputOutlines(ostream& outstream, PdfOutlineItem* item, int level)
140 {
141 if (item == nullptr)
142 {
143 auto outlines = m_doc->GetOutlines();
144 if (outlines == nullptr || !outlines->First())
145 {
146 outstream << "\tNone Found" << endl;
147 return;
148 }
149 item = outlines->First();
150 }
151
152 for (int i = 0; i < level; i++)
153 outstream << "-";
154
155 outstream << ">" << item->GetTitle().GetString();
156 auto dest = item->GetDestination();
157 if (dest == nullptr)
158 {
159 // then it's one or more actions
160 outstream << "\tAction: " << "???";
161 }
162 else
163 {
164 // then it's a destination
165 auto page = dest->GetPage();
166 if (page == nullptr)
167 outstream << "\tDestination: Page #" << "???";
168 else
169 outstream << "\tDestination: Page #" << page->GetPageNumber();
170 }
171
172 outstream << endl;
173
174 if (item->First())
175 this->OutputOutlines(outstream, item->First(), level + 1);
176
177 if (item->Next())
178 this->OutputOutlines(outstream, item->Next(), level);
179 }
180
181 void PdfInfoHelper::OutputOneName(ostream& outStream, PdfNameTrees& names,
182 PdfKnownNameTree treeName, const string_view& title)
183 {
184 outStream << "\t" << title << endl;
185 PdfStringMap<PdfObject> dict;
186 names.ToDictionary(treeName, dict);
187
188 string str;
189 for (auto& pair : dict)
190 {
191 pair.second.ToString(str);
192 outStream << "\t-> " << pair.first.GetString() << "=" << str << endl;
193 }
194
195 outStream << endl;
196 }
197
198 void PdfInfoHelper::OutputNames(ostream& outStream)
199 {
200 auto nameTree = m_doc->GetNames();
201 if (nameTree == nullptr)
202 {
203 outStream << "\t\tNone Found" << endl;
204 }
205 else
206 {
207 OutputOneName(outStream, *nameTree, PdfKnownNameTree::Dests, "Destinations");
208 OutputOneName(outStream, *nameTree, PdfKnownNameTree::JavaScript, "JavaScripts");
209 OutputOneName(outStream, *nameTree, PdfKnownNameTree::EmbeddedFiles, "Embedded Files");
210 }
211 }
212
213 string PdfInfoHelper::GuessFormat()
214 {
215 using Format = pair<double, double>;
216
217 unsigned pageCount = m_doc->GetPages().GetCount();
218 map<Format, int> sizes;
219 map<Format, int>::iterator it;
220 Rect rect;
221 for (unsigned i = 0; i < pageCount; i++)
222 {
223 auto& currPage = m_doc->GetPages().GetPageAt(i);
224 rect = currPage.GetMediaBox();
225 Format s(rect.Width - rect.X, rect.Height - rect.Y);
226 it = sizes.find(s);
227 if (it == sizes.end())
228 sizes.insert(pair<Format, int>(s, 1));
229 else
230 it->second++;
231 }
232
233 Format format;
234 stringstream ss;
235 if (sizes.size() == 1)
236 {
237 format = sizes.begin()->first;
238 ss << format.first << " x " << format.second << " pts";
239 }
240 else
241 {
242 // We’re looking for the most represented format
243 int max = 0;
244 for (it = sizes.begin(); it != sizes.end(); ++it)
245 {
246 if (it->second > max)
247 {
248 max = it->second;
249 format = it->first;
250 }
251 }
252 ss << format.first << " x " << format.second << " pts " << string(sizes.size(), '*');
253 }
254
255 return ss.str();
256 }