PoDoFo 1.2.0
Loading...
Searching...
No Matches
PdfDocument.h
1// SPDX-FileCopyrightText: 2006 Dominik Seichter <domseichter@web.de>
2// SPDX-FileCopyrightText: 2020 Francesco Pretto <ceztko@gmail.com>
3// SPDX-License-Identifier: LGPL-2.0-or-later OR MPL-2.0
4
5#ifndef PDF_DOCUMENT_H
6#define PDF_DOCUMENT_H
7
8#include "PdfTrailer.h"
9#include "PdfCatalog.h"
10#include "PdfIndirectObjectList.h"
11#include "PdfAcroForm.h"
12#include "PdfFontManager.h"
13#include "PdfMetadata.h"
14#include "PdfPageCollection.h"
15#include "PdfNameTrees.h"
16#include "PdfXObjectForm.h"
17#include "PdfImage.h"
18#include "PdfColorSpace.h"
19#include "PdfPattern.h"
20#include "PdfFunction.h"
21#include "PdfInfo.h"
22#include "PdfOutlines.h"
23#include "PdfExtension.h"
24
25namespace PoDoFo {
26
27class PdfAction;
28class PdfExtGState;
29class PdfEncrypt;
30class PdfDocument;
31
32template <typename TField>
33class PdfDocumentFieldIterableBase final
34{
35 friend class PdfDocument;
36
37public:
38 PdfDocumentFieldIterableBase()
39 : m_doc(nullptr) { }
40
41private:
42 PdfDocumentFieldIterableBase(PdfDocument& doc)
43 : m_doc(&doc) { }
44
45public:
46 class Iterator final
47 {
48 friend class PdfDocumentFieldIterableBase;
49 public:
50 using difference_type = void;
51 using value_type = TField*;
52 using pointer = void;
53 using reference = void;
54 using iterator_category = std::forward_iterator_tag;
55 public:
56 Iterator();
57 private:
58 Iterator(PdfDocument& doc);
59 public:
60 Iterator(const Iterator&) = default;
61 Iterator& operator=(const Iterator&) = default;
62 bool operator==(const Iterator& rhs) const;
63 bool operator!=(const Iterator& rhs) const;
64 Iterator& operator++();
65 Iterator operator++(int);
66 value_type operator*() { return m_Field; }
67 value_type operator->() { return m_Field; }
68 private:
69 void increment();
70 void stepIntoPageOrForm(PdfPageCollection& pages);
71 bool stepIntoPageAnnot(PdfAnnotationCollection& annots);
72 void stepIntoFormField(PdfAcroForm& form);
73 private:
74 PdfDocument* m_doc;
75 unsigned m_pageIndex;
76 PdfAnnotationCollection::iterator m_pageAnnotIterator;
77 PdfAcroForm::iterator m_acroFormIterator;
78 value_type m_Field;
79 std::unordered_set<PdfReference> m_visitedObjs;
80 };
81
82public:
83 Iterator begin() const;
84 Iterator end() const;
85
86private:
87 PdfDocument* m_doc;
88};
89
90using PdfDocumentFieldIterable = PdfDocumentFieldIterableBase<PdfField>;
91using PdfDocumentConstFieldIterable = PdfDocumentFieldIterableBase<const PdfField>;
92
107class PODOFO_API PdfDocument
108{
109 friend class PdfMetadata;
110 friend class PdfXObjectForm;
111 friend class PdfPageCollection;
112 friend class PdfMemDocument;
113 friend class PdfStreamedDocument;
114
115public:
117 virtual ~PdfDocument();
118
123 PdfOutlines& GetOrCreateOutlines();
124
131 PdfNameTrees& GetOrCreateNames();
132
140 PdfAcroForm& GetOrCreateAcroForm(PdfAcroFormDefaulAppearance eDefaultAppearance = PdfAcroFormDefaulAppearance::ArialBlack);
141
142 void CollectGarbage(PdfGarbageCollectionFlags flags = PdfGarbageCollectionFlags::None);
143
145 std::unique_ptr<PdfImage> CreateImage();
146
147 std::unique_ptr<PdfXObjectForm> CreateXObjectForm(const Rect& rect);
148
149 std::unique_ptr<PdfDestination> CreateDestination();
150
151 std::unique_ptr<PdfColorSpace> CreateColorSpace(PdfColorSpaceFilterPtr filter);
152
153 std::unique_ptr<PdfFunction> CreateFunction(PdfFunctionDefinitionPtr definition);
154
155 std::unique_ptr<PdfUncolouredTilingPattern> CreateTilingPattern(std::shared_ptr<PdfUncolouredTilingPatternDefinition> definition);
156
157 std::unique_ptr<PdfColouredTilingPattern> CreateTilingPattern(std::shared_ptr<PdfColouredTilingPatternDefinition> definition);
158
159 std::unique_ptr<PdfShadingPattern> CreateShadingPattern(PdfShadingPatternDefinitionPtr definition);
160
161 std::unique_ptr<PdfShadingDictionary> CreateShadingDictionary(PdfShadingDefinitionPtr definition);
162
163 std::unique_ptr<PdfExtGState> CreateExtGState(PdfExtGStateDefinitionPtr definition);
164
165 template <typename Taction>
166 std::unique_ptr<Taction> CreateAction();
167
168 std::unique_ptr<PdfAction> CreateAction(PdfActionType type);
169
170 std::unique_ptr<PdfFileSpec> CreateFileSpec();
171
178 bool IsPrintAllowed() const;
179
186 bool IsEditAllowed() const;
187
194 bool IsCopyAllowed() const;
195
202 bool IsEditNotesAllowed() const;
203
210 bool IsFillAndSignAllowed() const;
211
218 bool IsAccessibilityAllowed() const;
219
226 bool IsDocAssemblyAllowed() const;
227
234 bool IsHighPrintAllowed() const;
235
238 void PushPdfExtension(const PdfExtension& extension);
239
245 bool HasPdfExtension(const std::string_view& ns, int64_t level) const;
246
250 void RemovePdfExtension(const std::string_view& ns, int64_t level);
251
254 std::vector<PdfExtension> GetPdfExtensions() const;
255
256 PdfAcroForm& MustGetAcroForm();
257
258 const PdfAcroForm& MustGetAcroForm() const;
259
260 PdfNameTrees& MustGetNames();
261
262 const PdfNameTrees& MustGetNames() const;
263
264 PdfOutlines& MustGetOutlines();
265
266 const PdfOutlines& MustGetOutlines() const;
267
271 PdfDocumentFieldIterable GetFieldsIterator();
272 PdfDocumentConstFieldIterable GetFieldsIterator() const;
273
275 void Reset();
276
277public:
285 virtual bool HasOwnerPermissions() const = 0;
286
287 virtual const PdfEncrypt* GetEncrypt() const = 0;
288
290 bool IsEncrypted() const;
291
292public:
293 bool IsStrictParsing() const { return m_IsStrictParsing; }
294
299 PdfCatalog& GetCatalog() { return *m_Catalog; }
300
305 const PdfCatalog& GetCatalog() const { return *m_Catalog; }
306
309 PdfPageCollection& GetPages() { return *m_Pages; }
310
313 const PdfPageCollection& GetPages() const { return *m_Pages; }
314
319 PdfTrailer &GetTrailer() { return *m_Trailer; }
320
325 const PdfTrailer& GetTrailer() const { return *m_Trailer; }
326
332 const PdfInfo* GetInfo() const;
333
334 PdfMetadata& GetMetadata() { return m_Metadata; }
335
336 const PdfMetadata& GetMetadata() const { return m_Metadata; }
337
342 PdfIndirectObjectList& GetObjects() { return m_Objects; }
343
348 const PdfIndirectObjectList& GetObjects() const { return m_Objects; }
349
350 PdfAcroForm* GetAcroForm() { return m_AcroForm.get(); }
351
352 const PdfAcroForm* GetAcroForm() const { return m_AcroForm.get(); }
353
354 PdfNameTrees* GetNames() { return m_NameTrees.get(); }
355
356 const PdfNameTrees* GetNames() const { return m_NameTrees.get(); }
357
358 PdfOutlines* GetOutlines();
359
360 const PdfOutlines* GetOutlines() const;
361
362 PdfFontManager& GetFonts() { return m_FontManager; }
363
364protected:
366 void SetEntryPoints(std::unique_ptr<PdfObject>&& trailer, PdfObject& catalog);
367
368 void SetStrictParsing(bool value);
369
371 void Init();
372
373 virtual void reset();
374
376 void Clear();
377
378 virtual void clear();
379
382 virtual PdfVersion GetPdfVersion() const = 0;
383
387
388private:
391 PdfDocument(bool empty = false);
392
394
395 // Called by PdfPageCollection
396 void AppendDocumentPages(const PdfDocument& doc);
397 void InsertDocumentPageAt(unsigned atIndex, const PdfDocument& doc, unsigned pageIndex, std::unordered_map<PdfReference, PdfObject*>& map);
398 void AppendDocumentPages(const PdfDocument& doc, unsigned pageIndex, unsigned pageCount, std::unordered_map<PdfReference, PdfObject*>& map);
399
400 // Called by PdfXObjectForm
401 Rect FillXObjectFromPage(PdfXObjectForm& xobj, const PdfPage& page, PdfFillFormFlags flags, std::unordered_map<PdfReference, PdfObject*>* map);
402
403 PdfInfo& GetOrCreateInfo();
404
405 void createAction(PdfActionType type, std::unique_ptr<PdfAction>& action);
406
407private:
408 void deletePages(unsigned atIndex, unsigned pageCount);
409
410 void appendOutlineItems(PdfOutlineItem& destParent, const PdfOutlineItem* srcItem,
411 const PdfIndirectObjectList& srcObjects, std::unordered_map<PdfReference, PdfObject*>& mappedObjects);
412
413 void resetPrivate();
414
415 void lazyLoadOutlines();
416 void lazyLoadInfo();
417
418private:
419 PdfDocument& operator=(const PdfDocument&) = delete;
420
421private:
422 PdfIndirectObjectList m_Objects;
423 PdfMetadata m_Metadata;
424 PdfFontManager m_FontManager;
425 bool m_IsStrictParsing;
426 bool m_InfoLazyLoaded;
427 bool m_OutlinesLazyLoaded;
428 std::unique_ptr<PdfObject> m_TrailerObj;
429 std::unique_ptr<PdfTrailer> m_Trailer;
430 std::unique_ptr<PdfCatalog> m_Catalog;
431 std::unique_ptr<PdfInfo> m_Info;
432 std::unique_ptr<PdfPageCollection> m_Pages;
433 std::unique_ptr<PdfAcroForm> m_AcroForm;
434 std::unique_ptr<PdfOutlines> m_Outlines;
435 std::unique_ptr<PdfNameTrees> m_NameTrees;
436};
437
438template<typename TAction>
439std::unique_ptr<TAction> PdfDocument::CreateAction()
440{
441 std::unique_ptr<TAction> ret;
442 createAction(PdfAction::GetActionType<TAction>(), reinterpret_cast<std::unique_ptr<PdfAction>&>(ret));
443 return ret;
444}
445
446template<typename TField>
447typename PdfDocumentFieldIterableBase<TField>::Iterator PdfDocumentFieldIterableBase<TField>::begin() const
448{
449 if (m_doc == nullptr)
450 return Iterator();
451 else
452 return Iterator(*m_doc);
453}
454
455template<typename TField>
456typename PdfDocumentFieldIterableBase<TField>::Iterator PdfDocumentFieldIterableBase<TField>::end() const
457{
458 return Iterator();
459}
460
461template<typename TField>
462PdfDocumentFieldIterableBase<TField>::Iterator::Iterator()
463 : m_doc(nullptr), m_pageIndex(0), m_Field(nullptr)
464{
465}
466
467template<typename TField>
468PdfDocumentFieldIterableBase<TField>::Iterator::Iterator(PdfDocument& doc)
469 : m_doc(&doc), m_pageIndex(0), m_Field(nullptr)
470{
471 stepIntoPageOrForm(doc.GetPages());
472}
473
474template<typename TField>
475bool PdfDocumentFieldIterableBase<TField>::Iterator::operator==(const Iterator& rhs) const
476{
477 if (m_doc == nullptr && rhs.m_doc == nullptr)
478 return true;
479
480 return m_doc == rhs.m_doc && m_pageIndex == rhs.m_pageIndex && m_pageAnnotIterator == rhs.m_pageAnnotIterator && m_acroFormIterator == rhs.m_acroFormIterator;
481}
482
483template<typename TField>
484bool PdfDocumentFieldIterableBase<TField>::Iterator::operator!=(const Iterator& rhs) const
485{
486 if (m_doc == nullptr && rhs.m_doc == nullptr)
487 return false;
488
489 return m_doc != rhs.m_doc || m_pageIndex != rhs.m_pageIndex || m_pageAnnotIterator != rhs.m_pageAnnotIterator || m_acroFormIterator != rhs.m_acroFormIterator;
490}
491
492template<typename TField>
493typename PdfDocumentFieldIterableBase<TField>::Iterator& PdfDocumentFieldIterableBase<TField>::Iterator::operator++()
494{
495 increment();
496 return *this;
497}
498
499template<typename TField>
500typename PdfDocumentFieldIterableBase<TField>::Iterator PdfDocumentFieldIterableBase<TField>::Iterator::operator++(int)
501{
502 auto copy = *this;
503 increment();
504 return copy;
505}
506
507template<typename TField>
508void PdfDocumentFieldIterableBase<TField>::Iterator::increment()
509{
510 if (m_doc == nullptr)
511 return;
512
513 auto& pages = m_doc->GetPages();
514 if (m_pageIndex < pages.GetCount())
515 {
516 m_pageAnnotIterator++;
517 if (stepIntoPageAnnot(pages.GetPageAt(m_pageIndex).GetAnnotations()))
518 return;
519
520 m_pageIndex++;
521 stepIntoPageOrForm(pages);
522 }
523 else
524 {
525 m_acroFormIterator++;
526 stepIntoFormField(m_doc->MustGetAcroForm());
527 }
528}
529
530// Update the iterator for the current page index, or switch to form iteration
531template<typename TField>
532void PdfDocumentFieldIterableBase<TField>::Iterator::stepIntoPageOrForm(PdfPageCollection& pages)
533{
534 while (true)
535 {
536 if (m_pageIndex >= pages.GetCount())
537 break;
538
539 auto& annots = pages.GetPageAt(m_pageIndex).GetAnnotations();
540 m_pageAnnotIterator = annots.begin();
541 if (stepIntoPageAnnot(annots))
542 return;
543
544 m_pageIndex++;
545 }
546
547 auto form = m_doc->GetAcroForm();
548 if (form != nullptr)
549 {
550 m_acroFormIterator = form->begin();
551 stepIntoFormField(*form);
552 return;
553 }
554
555 // End of iteration
556 m_doc = nullptr;
557 m_Field = nullptr;
558 m_visitedObjs.clear();
559}
560
561// Verify the current page annotation iterator. It updates the current field
562// and returns true if a valid unvisited field is found, false otherwise
563template<typename TField>
564bool PdfDocumentFieldIterableBase<TField>::Iterator::stepIntoPageAnnot(PdfAnnotationCollection& annots)
565{
566 while (true)
567 {
568 if (m_pageAnnotIterator == annots.end())
569 break;
570
571 auto& annot = **m_pageAnnotIterator;
572 PdfField* field = nullptr;
573 if (annot.GetType() == PdfAnnotationType::Widget &&
574 (field = &static_cast<PdfAnnotationWidget&>(annot).GetField(),
575 m_visitedObjs.find(field->GetObject().GetIndirectReference()) == m_visitedObjs.end()))
576 {
577 m_Field = field;
578 m_visitedObjs.insert(field->GetObject().GetIndirectReference());
579 return true;
580 }
581
582 m_pageAnnotIterator++;
583 }
584
585 return false;
586}
587
588// Verify the current AcroForm field iterator. It updates the current field
589// if a valid unvisited leaf field is found, or it ends the iteration otherwise
590template<typename TField>
591void PdfDocumentFieldIterableBase<TField>::Iterator::stepIntoFormField(PdfAcroForm& form)
592{
593 while (true)
594 {
595 if (m_acroFormIterator == form.end())
596 break;
597
598 auto& field = **m_acroFormIterator;
599 if (field.GetChildren().GetCount() == 0
600 && m_visitedObjs.find(field.GetObject().GetIndirectReference()) == m_visitedObjs.end())
601 {
602 m_Field = &field;
603 m_visitedObjs.insert(field.GetObject().GetIndirectReference());
604 return;
605 }
606
607 m_acroFormIterator++;
608 }
609
610 // End of iteration
611 m_doc = nullptr;
612 m_Field = nullptr;
613 m_visitedObjs.clear();
614}
615
616};
617
618
619#endif // PDF_DOCUMENT_H
PdfDocument is the core interface for working with PDF documents.
Definition PdfDocument.h:108
PdfPageCollection & GetPages()
Get access to the page tree.
Definition PdfDocument.h:309
const PdfPageCollection & GetPages() const
Get access to the page tree.
Definition PdfDocument.h:313
virtual PdfVersion GetPdfVersion() const =0
Get the PDF version of the document.
const PdfTrailer & GetTrailer() const
Get access to the internal trailer dictionary or root object.
Definition PdfDocument.h:325
virtual bool HasOwnerPermissions() const =0
Checks if document has been opened with full owner privileges.
PdfTrailer & GetTrailer()
Get access to the internal trailer dictionary or root object.
Definition PdfDocument.h:319
virtual void SetPdfVersion(PdfVersion version)=0
Get the PDF version of the document.
PdfCatalog & GetCatalog()
Get access to the internal Catalog dictionary or root object.
Definition PdfDocument.h:299
PdfIndirectObjectList & GetObjects()
Get access to the internal vector of objects or root object.
Definition PdfDocument.h:342
const PdfIndirectObjectList & GetObjects() const
Get access to the internal vector of objects or root object.
Definition PdfDocument.h:348
const PdfCatalog & GetCatalog() const
Get access to the internal Catalog dictionary or root object.
Definition PdfDocument.h:305
A class that is used to encrypt a PDF file and set document permissions on the PDF file.
Definition PdfEncrypt.h:111
PdfExtension is a simple class that describes a vendor-specific extension to the official specificati...
Definition PdfExtension.h:15
This class assists PdfDocument with caching font information.
Definition PdfFontManager.h:50
A list of PdfObjects that constitutes the indirect object list of the document The PdfParser will rea...
Definition PdfIndirectObjectList.h:34
This class provides access to the documents info dictionary, which provides information about the PDF...
Definition PdfInfo.h:18
PdfMemDocument is the core class for reading and manipulating PDF files and writing them back to disk...
Definition PdfMemDocument.h:35
Interface to access names trees in the document.
Definition PdfNameTrees.h:18
A PDF outline item has a title and a destination.
Definition PdfOutlines.h:33
The main PDF outlines dictionary.
Definition PdfOutlines.h:175
Class for managing the tree of Pages in a PDF document Don't use this class directly.
Definition PdfPageCollection.h:37
PdfPage is one page in the pdf document.
Definition PdfPage.h:133
PdfStreamedDocument is the preferred class for creating new PDF documents.
Definition PdfStreamedDocument.h:47
An normalized rectangle defined by position (left-bottom) and size.
Definition Rect.h:17
Convenient type for char array storage and/or buffer with std::string compatibility.
Definition basetypes.h:30
All classes, functions, types and enums of PoDoFo are members of these namespace.
Definition basetypes.h:13
std::shared_ptr< const PdfColorSpaceFilter > PdfColorSpaceFilterPtr
Convenience alias for a constant PdfColorSpaceFilter shared ptr.
Definition PdfColorSpaceFilter.h:71
PdfGarbageCollectionFlags
Definition PdfIndirectObjectList.h:17
std::shared_ptr< const PdfShadingDefinition > PdfShadingDefinitionPtr
Convenience alias for a constant PdfShadingDefinition shared ptr.
Definition PdfPatternDefinition.h:180
PdfAcroFormDefaulAppearance
Definition PdfAcroForm.h:15
std::shared_ptr< const PdfShadingPatternDefinition > PdfShadingPatternDefinitionPtr
Convenience alias for a constant PdfShadingPatternDefinition shared ptr.
Definition PdfPatternDefinition.h:409
PdfFillFormFlags
Definition PdfXObjectForm.h:18
std::shared_ptr< const PdfExtGStateDefinition > PdfExtGStateDefinitionPtr
Convenience alias for a constant PdfExtGStateDefinition shared ptr.
Definition PdfExtGStateDefinition.h:30
PdfActionType
The type of the action.
Definition PdfAction.h:25
PdfVersion
Enum to identify different versions of the PDF file format.
Definition PdfDeclarations.h:61
std::shared_ptr< const PdfFunctionDefinition > PdfFunctionDefinitionPtr
Convenience alias for a constant PdfFunction shared ptr.
Definition PdfFunctionDefinition.h:54