| 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()) << 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.GetRotationRaw() << 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::AnnotationTypeToName(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& link = static_cast<PdfAnnotationLink&>(curAnnot); |
| 128 | if (link.GetAction() != nullptr && link.GetAction()->HasURI()) |
| 129 | outstream << "\t\tAction URI: " << link.GetAction()->GetURI().GetString() << endl; |
| 130 | } |
| 131 | } |
| 132 | } |
| 133 | } |
| 134 | |
| 135 | void PdfInfoHelper::OutputOutlines(ostream& outstream, PdfOutlineItem* item, int level) |
| 136 | { |
| 137 | if (item == nullptr) |
| 138 | { |
| 139 | auto outlines = m_doc->GetOutlines(); |
| 140 | if (outlines == nullptr || !outlines->First()) |
| 141 | { |
| 142 | outstream << "\tNone Found" << endl; |
| 143 | return; |
| 144 | } |
| 145 | item = outlines->First(); |
| 146 | } |
| 147 | |
| 148 | for (int i = 0; i < level; i++) |
| 149 | outstream << "-"; |
| 150 | |
| 151 | outstream << ">" << item->GetTitle().GetString(); |
| 152 | auto dest = item->GetDestination(); |
| 153 | if (dest == nullptr) |
| 154 | { |
| 155 | // then it's one or more actions |
| 156 | outstream << "\tAction: " << "???"; |
| 157 | } |
| 158 | else |
| 159 | { |
| 160 | // then it's a destination |
| 161 | auto page = dest->GetPage(); |
| 162 | if (page == nullptr) |
| 163 | outstream << "\tDestination: Page #" << "???"; |
| 164 | else |
| 165 | outstream << "\tDestination: Page #" << page->GetPageNumber(); |
| 166 | } |
| 167 | |
| 168 | outstream << endl; |
| 169 | |
| 170 | if (item->First()) |
| 171 | this->OutputOutlines(outstream, item->First(), level + 1); |
| 172 | |
| 173 | if (item->Next()) |
| 174 | this->OutputOutlines(outstream, item->Next(), level); |
| 175 | } |
| 176 | |
| 177 | void PdfInfoHelper::OutputOneName(ostream& outStream, PdfNameTree& nameTree, |
| 178 | const string_view& title, const string_view& key) |
| 179 | { |
| 180 | outStream << "\t" << title << endl; |
| 181 | PdfDictionary dict; |
| 182 | nameTree.ToDictionary(key, dict); |
| 183 | |
| 184 | string str; |
| 185 | for (auto& pair : dict) |
| 186 | { |
| 187 | pair.second.ToString(str); |
| 188 | outStream << "\t-> " << pair.first.GetString() << "=" << str << endl; |
| 189 | } |
| 190 | |
| 191 | outStream << endl; |
| 192 | } |
| 193 | |
| 194 | void PdfInfoHelper::OutputNames(ostream& outStream) |
| 195 | { |
| 196 | auto nameTree = m_doc->GetNames(); |
| 197 | if (nameTree == nullptr) |
| 198 | { |
| 199 | outStream << "\t\tNone Found" << endl; |
| 200 | } |
| 201 | else |
| 202 | { |
| 203 | OutputOneName(outStream, *nameTree, "Destinations", "Dests"); |
| 204 | OutputOneName(outStream, *nameTree, "JavaScripts", "JavaScript"); |
| 205 | OutputOneName(outStream, *nameTree, "Embedded Files", "EmbeddedFiles"); |
| 206 | } |
| 207 | } |
| 208 | |
| 209 | string PdfInfoHelper::GuessFormat() |
| 210 | { |
| 211 | using Format = pair<double, double>; |
| 212 | |
| 213 | unsigned pageCount = m_doc->GetPages().GetCount(); |
| 214 | map<Format, int> sizes; |
| 215 | map<Format, int>::iterator it; |
| 216 | Rect rect; |
| 217 | for (unsigned i = 0; i < pageCount; i++) |
| 218 | { |
| 219 | auto& currPage = m_doc->GetPages().GetPageAt(i); |
| 220 | rect = currPage.GetMediaBox(); |
| 221 | Format s(rect.Width - rect.X, rect.Height - rect.Y); |
| 222 | it = sizes.find(s); |
| 223 | if (it == sizes.end()) |
| 224 | sizes.insert(pair<Format, int>(s, 1)); |
| 225 | else |
| 226 | it->second++; |
| 227 | } |
| 228 | |
| 229 | Format format; |
| 230 | stringstream ss; |
| 231 | if (sizes.size() == 1) |
| 232 | { |
| 233 | format = sizes.begin()->first; |
| 234 | ss << format.first << " x " << format.second << " pts"; |
| 235 | } |
| 236 | else |
| 237 | { |
| 238 | // We’re looking for the most represented format |
| 239 | int max = 0; |
| 240 | for (it = sizes.begin(); it != sizes.end(); ++it) |
| 241 | { |
| 242 | if (it->second > max) |
| 243 | { |
| 244 | max = it->second; |
| 245 | format = it->first; |
| 246 | } |
| 247 | } |
| 248 | ss << format.first << " x " << format.second << " pts " << string(sizes.size(), '*'); |
| 249 | } |
| 250 | |
| 251 | return ss.str(); |
| 252 | } |