PoDoFo 1.1.0
Loading...
Searching...
No Matches
PdfDocument.h
1
7#ifndef PDF_DOCUMENT_H
8#define PDF_DOCUMENT_H
9
10#include "PdfTrailer.h"
11#include "PdfCatalog.h"
12#include "PdfIndirectObjectList.h"
13#include "PdfAcroForm.h"
14#include "PdfFontManager.h"
15#include "PdfMetadata.h"
16#include "PdfPageCollection.h"
17#include "PdfNameTrees.h"
18#include "PdfXObjectForm.h"
19#include "PdfImage.h"
20#include "PdfColorSpace.h"
21#include "PdfPattern.h"
22#include "PdfFunction.h"
23#include "PdfInfo.h"
24#include "PdfOutlines.h"
25#include "PdfExtension.h"
26
27namespace PoDoFo {
28
29class PdfAction;
30class PdfExtGState;
31class PdfEncrypt;
32class PdfDocument;
33
34template <typename TField>
35class PdfDocumentFieldIterableBase final
36{
37 friend class PdfDocument;
38
39public:
40 PdfDocumentFieldIterableBase()
41 : m_doc(nullptr) { }
42
43private:
44 PdfDocumentFieldIterableBase(PdfDocument& doc)
45 : m_doc(&doc) { }
46
47public:
48 class Iterator final
49 {
50 friend class PdfDocumentFieldIterableBase;
51 public:
52 using difference_type = void;
53 using value_type = TField*;
54 using pointer = void;
55 using reference = void;
56 using iterator_category = std::forward_iterator_tag;
57 public:
58 Iterator();
59 private:
60 Iterator(PdfDocument& doc);
61 public:
62 Iterator(const Iterator&) = default;
63 Iterator& operator=(const Iterator&) = default;
64 bool operator==(const Iterator& rhs) const;
65 bool operator!=(const Iterator& rhs) const;
66 Iterator& operator++();
67 Iterator operator++(int);
68 value_type operator*() { return m_Field; }
69 value_type operator->() { return m_Field; }
70 private:
71 void increment();
72 void stepIntoPageOrForm(PdfPageCollection& pages);
73 bool stepIntoPageAnnot(PdfAnnotationCollection& annots);
74 void stepIntoFormField(PdfAcroForm& form);
75 private:
76 PdfDocument* m_doc;
77 unsigned m_pageIndex;
78 PdfAnnotationCollection::iterator m_pageAnnotIterator;
79 PdfAcroForm::iterator m_acroFormIterator;
80 value_type m_Field;
81 std::unordered_set<PdfReference> m_visitedObjs;
82 };
83
84public:
85 Iterator begin() const;
86 Iterator end() const;
87
88private:
89 PdfDocument* m_doc;
90};
91
92using PdfDocumentFieldIterable = PdfDocumentFieldIterableBase<PdfField>;
93using PdfDocumentConstFieldIterable = PdfDocumentFieldIterableBase<const PdfField>;
94
110class PODOFO_API PdfDocument
111{
112 friend class PdfMetadata;
113 friend class PdfXObjectForm;
114 friend class PdfPageCollection;
115 friend class PdfMemDocument;
116 friend class PdfStreamedDocument;
117
118public:
121 virtual ~PdfDocument();
122
130 PdfOutlines& GetOrCreateOutlines();
131
139 PdfNameTrees& GetOrCreateNames();
140
149 PdfAcroForm& GetOrCreateAcroForm(PdfAcroFormDefaulAppearance eDefaultAppearance = PdfAcroFormDefaulAppearance::ArialBlack);
150
151 void CollectGarbage();
152
155 std::unique_ptr<PdfImage> CreateImage();
156
157 std::unique_ptr<PdfXObjectForm> CreateXObjectForm(const Rect& rect);
158
159 std::unique_ptr<PdfDestination> CreateDestination();
160
161 std::unique_ptr<PdfColorSpace> CreateColorSpace(PdfColorSpaceFilterPtr filter);
162
163 std::unique_ptr<PdfFunction> CreateFunction(PdfFunctionDefinitionPtr definition);
164
165 std::unique_ptr<PdfUncolouredTilingPattern> CreateTilingPattern(std::shared_ptr<PdfUncolouredTilingPatternDefinition> definition);
166
167 std::unique_ptr<PdfColouredTilingPattern> CreateTilingPattern(std::shared_ptr<PdfColouredTilingPatternDefinition> definition);
168
169 std::unique_ptr<PdfShadingPattern> CreateShadingPattern(PdfShadingPatternDefinitionPtr definition);
170
171 std::unique_ptr<PdfShadingDictionary> CreateShadingDictionary(PdfShadingDefinitionPtr definition);
172
173 std::unique_ptr<PdfExtGState> CreateExtGState(PdfExtGStateDefinitionPtr definition);
174
175 template <typename Taction>
176 std::unique_ptr<Taction> CreateAction();
177
178 std::unique_ptr<PdfAction> CreateAction(PdfActionType type);
179
180 std::unique_ptr<PdfFileSpec> CreateFileSpec();
181
189 bool IsPrintAllowed() const;
190
198 bool IsEditAllowed() const;
199
207 bool IsCopyAllowed() const;
208
216 bool IsEditNotesAllowed() const;
217
225 bool IsFillAndSignAllowed() const;
226
234 bool IsAccessibilityAllowed() const;
235
243 bool IsDocAssemblyAllowed() const;
244
252 bool IsHighPrintAllowed() const;
253
257 void PushPdfExtension(const PdfExtension& extension);
258
264 bool HasPdfExtension(const std::string_view& ns, int64_t level) const;
265
270 void RemovePdfExtension(const std::string_view& ns, int64_t level);
271
276 std::vector<PdfExtension> GetPdfExtensions() const;
277
278 PdfAcroForm& MustGetAcroForm();
279
280 const PdfAcroForm& MustGetAcroForm() const;
281
282 PdfNameTrees& MustGetNames();
283
284 const PdfNameTrees& MustGetNames() const;
285
286 PdfOutlines& MustGetOutlines();
287
288 const PdfOutlines& MustGetOutlines() const;
289
295 PdfDocumentFieldIterable GetFieldsIterator();
296 PdfDocumentConstFieldIterable GetFieldsIterator() const;
297
300 void Reset();
301
302public:
311 virtual bool HasOwnerPermissions() const = 0;
312
313 virtual const PdfEncrypt* GetEncrypt() const = 0;
314
318 bool IsEncrypted() const;
319
320public:
326 PdfCatalog& GetCatalog() { return *m_Catalog; }
327
333 const PdfCatalog& GetCatalog() const { return *m_Catalog; }
334
338 PdfPageCollection& GetPages() { return *m_Pages; }
339
343 const PdfPageCollection& GetPages() const { return *m_Pages; }
344
350 PdfTrailer &GetTrailer() { return *m_Trailer; }
351
357 const PdfTrailer& GetTrailer() const { return *m_Trailer; }
358
365 const PdfInfo* GetInfo() const { return m_Info.get(); }
366
367 PdfMetadata& GetMetadata() { return m_Metadata; }
368
369 const PdfMetadata& GetMetadata() const { return m_Metadata; }
370
376 PdfIndirectObjectList& GetObjects() { return m_Objects; }
377
383 const PdfIndirectObjectList& GetObjects() const { return m_Objects; }
384
385 PdfAcroForm* GetAcroForm() { return m_AcroForm.get(); }
386
387 const PdfAcroForm* GetAcroForm() const { return m_AcroForm.get(); }
388
389 PdfNameTrees* GetNames() { return m_NameTrees.get(); }
390
391 const PdfNameTrees* GetNames() const { return m_NameTrees.get(); }
392
393 PdfOutlines* GetOutlines();
394
395 const PdfOutlines* GetOutlines() const;
396
397 PdfFontManager& GetFonts() { return m_FontManager; }
398
399protected:
406 void SetTrailer(std::unique_ptr<PdfObject> obj);
407
410 void Init();
411
412 virtual void reset();
413
416 void Clear();
417
418 virtual void clear();
419
423 virtual PdfVersion GetPdfVersion() const = 0;
424
429
430private:
434 PdfDocument(bool empty = false);
435
437
438 // Called by PdfPageCollection
439 void AppendDocumentPages(const PdfDocument& doc);
440 void InsertDocumentPageAt(unsigned atIndex, const PdfDocument& doc, unsigned pageIndex);
441 void AppendDocumentPages(const PdfDocument& doc, unsigned pageIndex, unsigned pageCount);
442
443 // Called by PdfXObjectForm
444 Rect FillXObjectFromPage(PdfXObjectForm& xobj, const PdfPage& page, bool useTrimBox);
445
446 PdfInfo& GetOrCreateInfo();
447
448 void createAction(PdfActionType type, std::unique_ptr<PdfAction>& action);
449
450private:
451 void append(const PdfDocument& doc, bool appendAll);
459 void fixObjectReferences(PdfObject& obj, int difference);
460
461 void deletePages(unsigned atIndex, unsigned pageCount);
462
463 void resetPrivate();
464
465 void initOutlines();
466
467private:
468 PdfDocument& operator=(const PdfDocument&) = delete;
469
470private:
471 PdfIndirectObjectList m_Objects;
472 PdfMetadata m_Metadata;
473 PdfFontManager m_FontManager;
474 std::unique_ptr<PdfObject> m_TrailerObj;
475 std::unique_ptr<PdfTrailer> m_Trailer;
476 std::unique_ptr<PdfCatalog> m_Catalog;
477 std::unique_ptr<PdfInfo> m_Info;
478 std::unique_ptr<PdfPageCollection> m_Pages;
479 std::unique_ptr<PdfAcroForm> m_AcroForm;
481 std::unique_ptr<PdfNameTrees> m_NameTrees;
482};
483
484template<typename TAction>
485std::unique_ptr<TAction> PdfDocument::CreateAction()
486{
487 std::unique_ptr<TAction> ret;
488 createAction(PdfAction::GetActionType<TAction>(), reinterpret_cast<std::unique_ptr<PdfAction>&>(ret));
489 return ret;
490}
491
492template<typename TField>
493typename PdfDocumentFieldIterableBase<TField>::Iterator PdfDocumentFieldIterableBase<TField>::begin() const
494{
495 if (m_doc == nullptr)
496 return Iterator();
497 else
498 return Iterator(*m_doc);
499}
500
501template<typename TField>
502typename PdfDocumentFieldIterableBase<TField>::Iterator PdfDocumentFieldIterableBase<TField>::end() const
503{
504 return Iterator();
505}
506
507template<typename TField>
508PdfDocumentFieldIterableBase<TField>::Iterator::Iterator()
509 : m_doc(nullptr), m_pageIndex(0), m_Field(nullptr)
510{
511}
512
513template<typename TField>
514PdfDocumentFieldIterableBase<TField>::Iterator::Iterator(PdfDocument& doc)
515 : m_doc(&doc), m_pageIndex(0), m_Field(nullptr)
516{
517 stepIntoPageOrForm(doc.GetPages());
518}
519
520template<typename TField>
521bool PdfDocumentFieldIterableBase<TField>::Iterator::operator==(const Iterator& rhs) const
522{
523 if (m_doc == nullptr && rhs.m_doc == nullptr)
524 return true;
525
526 return m_doc == rhs.m_doc && m_pageIndex == rhs.m_pageIndex && m_pageAnnotIterator == rhs.m_pageAnnotIterator && m_acroFormIterator == rhs.m_acroFormIterator;
527}
528
529template<typename TField>
530bool PdfDocumentFieldIterableBase<TField>::Iterator::operator!=(const Iterator& rhs) const
531{
532 if (m_doc == nullptr && rhs.m_doc == nullptr)
533 return false;
534
535 return m_doc != rhs.m_doc || m_pageIndex != rhs.m_pageIndex || m_pageAnnotIterator != rhs.m_pageAnnotIterator || m_acroFormIterator != rhs.m_acroFormIterator;
536}
537
538template<typename TField>
539typename PdfDocumentFieldIterableBase<TField>::Iterator& PdfDocumentFieldIterableBase<TField>::Iterator::operator++()
540{
541 increment();
542 return *this;
543}
544
545template<typename TField>
546typename PdfDocumentFieldIterableBase<TField>::Iterator PdfDocumentFieldIterableBase<TField>::Iterator::operator++(int)
547{
548 auto copy = *this;
549 increment();
550 return copy;
551}
552
553template<typename TField>
554void PdfDocumentFieldIterableBase<TField>::Iterator::increment()
555{
556 if (m_doc == nullptr)
557 return;
558
559 auto& pages = m_doc->GetPages();
560 if (m_pageIndex < pages.GetCount())
561 {
562 m_pageAnnotIterator++;
563 if (stepIntoPageAnnot(pages.GetPageAt(m_pageIndex).GetAnnotations()))
564 return;
565
566 m_pageIndex++;
567 stepIntoPageOrForm(pages);
568 }
569 else
570 {
571 m_acroFormIterator++;
572 stepIntoFormField(m_doc->MustGetAcroForm());
573 }
574}
575
576// Update the iterator for the current page index, or swith to form iteration
577template<typename TField>
578void PdfDocumentFieldIterableBase<TField>::Iterator::stepIntoPageOrForm(PdfPageCollection& pages)
579{
580 while (true)
581 {
582 if (m_pageIndex >= pages.GetCount())
583 break;
584
585 auto& annots = pages.GetPageAt(m_pageIndex).GetAnnotations();
586 m_pageAnnotIterator = annots.begin();
587 if (stepIntoPageAnnot(annots))
588 return;
589
590 m_pageIndex++;
591 }
592
593 auto form = m_doc->GetAcroForm();
594 if (form != nullptr)
595 {
596 m_acroFormIterator = form->begin();
597 stepIntoFormField(*form);
598 return;
599 }
600
601 // End of iteration
602 m_doc = nullptr;
603 m_Field = nullptr;
604 m_visitedObjs.clear();
605}
606
607// Verify the current page annotation iterator. It updates the current field
608// and returns true if a valid unvisited field is found, false otherwise
609template<typename TField>
610bool PdfDocumentFieldIterableBase<TField>::Iterator::stepIntoPageAnnot(PdfAnnotationCollection& annots)
611{
612 while (true)
613 {
614 if (m_pageAnnotIterator == annots.end())
615 break;
616
617 auto& annot = **m_pageAnnotIterator;
618 PdfField* field = nullptr;
619 if (annot.GetType() == PdfAnnotationType::Widget &&
620 (field = &static_cast<PdfAnnotationWidget&>(annot).GetField(),
621 m_visitedObjs.find(field->GetObject().GetIndirectReference()) == m_visitedObjs.end()))
622 {
623 m_Field = field;
624 m_visitedObjs.insert(field->GetObject().GetIndirectReference());
625 return true;
626 }
627
628 m_pageAnnotIterator++;
629 }
630
631 return false;
632}
633
634// Verify the current AcroForm field iterator. It updates the current field
635// if a valid unvisited leaf field is found, or it ends the iteration otherwise
636template<typename TField>
637void PdfDocumentFieldIterableBase<TField>::Iterator::stepIntoFormField(PdfAcroForm& form)
638{
639 while (true)
640 {
641 if (m_acroFormIterator == form.end())
642 break;
643
644 auto& field = **m_acroFormIterator;
645 if (field.GetChildren().GetCount() == 0
646 && m_visitedObjs.find(field.GetObject().GetIndirectReference()) == m_visitedObjs.end())
647 {
648 m_Field = &field;
649 m_visitedObjs.insert(field.GetObject().GetIndirectReference());
650 return;
651 }
652
653 m_acroFormIterator++;
654 }
655
656 // End of iteration
657 m_doc = nullptr;
658 m_Field = nullptr;
659 m_visitedObjs.clear();
660}
661
662};
663
664
665#endif // PDF_DOCUMENT_H
PdfDocument is the core interface for working with PDF documents.
Definition PdfDocument.h:111
PdfPageCollection & GetPages()
Get access to the page tree.
Definition PdfDocument.h:338
const PdfInfo * GetInfo() const
Get access to the internal Info dictionary You can set the author, title etc.
Definition PdfDocument.h:365
const PdfPageCollection & GetPages() const
Get access to the page tree.
Definition PdfDocument.h:343
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:357
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:350
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:326
PdfIndirectObjectList & GetObjects()
Get access to the internal vector of objects or root object.
Definition PdfDocument.h:376
const PdfIndirectObjectList & GetObjects() const
Get access to the internal vector of objects or root object.
Definition PdfDocument.h:383
const PdfCatalog & GetCatalog() const
Get access to the internal Catalog dictionary or root object.
Definition PdfDocument.h:333
A class that is used to encrypt a PDF file and set document permissions on the PDF file.
Definition PdfEncrypt.h:118
PdfExtension is a simple class that describes a vendor-specific extension to the official specificati...
Definition PdfExtension.h:18
This class assists PdfDocument with caching font information.
Definition PdfFontManager.h:54
A list of PdfObjects that constitutes the indirect object list of the document The PdfParser will rea...
Definition PdfIndirectObjectList.h:31
This class provides access to the documents info dictionary, which provides information about the PDF...
Definition PdfInfo.h:21
PdfMemDocument is the core class for reading and manipulating PDF files and writing them back to disk...
Definition PdfMemDocument.h:38
Interface to access names trees in the document.
Definition PdfNameTrees.h:22
This class represents a PDF indirect Object in memory.
Definition PdfObject.h:35
The main PDF outlines dictionary.
Definition PdfOutlines.h:208
Class for managing the tree of Pages in a PDF document Don't use this class directly.
Definition PdfPageCollection.h:24
PdfPage is one page in the pdf document.
Definition PdfPage.h:137
PdfStreamedDocument is the preferred class for creating new PDF documents.
Definition PdfStreamedDocument.h:50
An normalized rectangle defined by position (left-bottom) and size.
Definition Rect.h:20
Convenient type for char array storage and/or buffer with std::string compatibility.
Definition basetypes.h:38
SPDX-FileCopyrightText: (C) 2022 Francesco Pretto ceztko@gmail.com SPDX-License-Identifier: LGPL-2....
Definition basetypes.h:16
std::shared_ptr< const PdfColorSpaceFilter > PdfColorSpaceFilterPtr
Convenience alias for a constant PdfColorSpaceFilter shared ptr.
Definition PdfColorSpaceFilter.h:85
std::shared_ptr< const PdfShadingDefinition > PdfShadingDefinitionPtr
Convenience alias for a constant PdfShadingDefinition shared ptr.
Definition PdfPatternDefinition.h:167
PdfAcroFormDefaulAppearance
Definition PdfAcroForm.h:17
std::shared_ptr< const PdfShadingPatternDefinition > PdfShadingPatternDefinitionPtr
Convenience alias for a constant PdfShadingPatternDefinition shared ptr.
Definition PdfPatternDefinition.h:414
std::shared_ptr< const PdfExtGStateDefinition > PdfExtGStateDefinitionPtr
Convenience alias for a constant PdfExtGStateDefinition shared ptr.
Definition PdfExtGStateDefinition.h:34
PdfActionType
The type of the action.
Definition PdfAction.h:28
PdfVersion
Enum to identify different versions of the PDF file format.
Definition PdfDeclarations.h:71
std::shared_ptr< const PdfFunctionDefinition > PdfFunctionDefinitionPtr
Convenience alias for a constant PdfFunction shared ptr.
Definition PdfFunctionDefinition.h:58