PoDoFo 1.0.0-dev
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(const PdfColorSpaceFilterPtr& filter);
162
163 std::unique_ptr<PdfFunction> CreateFunction(const PdfFunctionDefinitionPtr& definition);
164
165 std::unique_ptr<PdfUncolouredTilingPattern> CreateTilingPattern(const std::shared_ptr<PdfUncolouredTilingPatternDefinition> &definition);
166
167 std::unique_ptr<PdfColouredTilingPattern> CreateTilingPattern(const std::shared_ptr<PdfColouredTilingPatternDefinition>& definition);
168
169 std::unique_ptr<PdfShadingPattern> CreateShadingPattern(const PdfShadingPatternDefinitionPtr& definition);
170
171 std::unique_ptr<PdfShadingDictionary> CreateShadingDictionary(const PdfShadingDefinitionPtr& definition);
172
173 std::unique_ptr<PdfExtGState> CreateExtGState(const 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:
303 virtual const PdfEncrypt* GetEncrypt() const = 0;
304
308 bool IsEncrypted() const;
309
310public:
316 PdfCatalog& GetCatalog() { return *m_Catalog; }
317
323 const PdfCatalog& GetCatalog() const { return *m_Catalog; }
324
328 PdfPageCollection& GetPages() { return *m_Pages; }
329
333 const PdfPageCollection& GetPages() const { return *m_Pages; }
334
340 PdfTrailer &GetTrailer() { return *m_Trailer; }
341
347 const PdfTrailer& GetTrailer() const { return *m_Trailer; }
348
355 const PdfInfo* GetInfo() const { return m_Info.get(); }
356
357 PdfMetadata& GetMetadata() { return m_Metadata; }
358
359 const PdfMetadata& GetMetadata() const { return m_Metadata; }
360
366 PdfIndirectObjectList& GetObjects() { return m_Objects; }
367
373 const PdfIndirectObjectList& GetObjects() const { return m_Objects; }
374
375 PdfAcroForm* GetAcroForm() { return m_AcroForm.get(); }
376
377 const PdfAcroForm* GetAcroForm() const { return m_AcroForm.get(); }
378
379 PdfNameTrees* GetNames() { return m_NameTrees.get(); }
380
381 const PdfNameTrees* GetNames() const { return m_NameTrees.get(); }
382
383 PdfOutlines* GetOutlines();
384
385 const PdfOutlines* GetOutlines() const;
386
387 PdfFontManager& GetFonts() { return m_FontManager; }
388
389protected:
396 void SetTrailer(std::unique_ptr<PdfObject> obj);
397
400 void Init();
401
402 virtual void reset();
403
406 void Clear();
407
408 virtual void clear();
409
413 virtual PdfVersion GetPdfVersion() const = 0;
414
419
420private:
424 PdfDocument(bool empty = false);
425
427
428 // Called by PdfPageCollection
429 void AppendDocumentPages(const PdfDocument& doc);
430 void InsertDocumentPageAt(unsigned atIndex, const PdfDocument& doc, unsigned pageIndex);
431 void AppendDocumentPages(const PdfDocument& doc, unsigned pageIndex, unsigned pageCount);
432
433 // Called by PdfXObjectForm
434 Rect FillXObjectFromPage(PdfXObjectForm& xobj, const PdfPage& page, bool useTrimBox);
435
436 PdfInfo& GetOrCreateInfo();
437
438 void createAction(PdfActionType type, std::unique_ptr<PdfAction>& action);
439
440private:
441 void append(const PdfDocument& doc, bool appendAll);
449 void fixObjectReferences(PdfObject& obj, int difference);
450
451 void deletePages(unsigned atIndex, unsigned pageCount);
452
453 void resetPrivate();
454
455 void initOutlines();
456
457private:
458 PdfDocument& operator=(const PdfDocument&) = delete;
459
460private:
461 PdfIndirectObjectList m_Objects;
462 PdfMetadata m_Metadata;
463 PdfFontManager m_FontManager;
464 std::unique_ptr<PdfObject> m_TrailerObj;
465 std::unique_ptr<PdfTrailer> m_Trailer;
466 std::unique_ptr<PdfCatalog> m_Catalog;
467 std::unique_ptr<PdfInfo> m_Info;
468 std::unique_ptr<PdfPageCollection> m_Pages;
469 std::unique_ptr<PdfAcroForm> m_AcroForm;
471 std::unique_ptr<PdfNameTrees> m_NameTrees;
472};
473
474template<typename TAction>
475std::unique_ptr<TAction> PdfDocument::CreateAction()
476{
477 std::unique_ptr<TAction> ret;
478 createAction(PdfAction::GetActionType<TAction>(), reinterpret_cast<std::unique_ptr<PdfAction>&>(ret));
479 return ret;
480}
481
482template<typename TField>
483typename PdfDocumentFieldIterableBase<TField>::Iterator PdfDocumentFieldIterableBase<TField>::begin() const
484{
485 if (m_doc == nullptr)
486 return Iterator();
487 else
488 return Iterator(*m_doc);
489}
490
491template<typename TField>
492typename PdfDocumentFieldIterableBase<TField>::Iterator PdfDocumentFieldIterableBase<TField>::end() const
493{
494 return Iterator();
495}
496
497template<typename TField>
498PdfDocumentFieldIterableBase<TField>::Iterator::Iterator()
499 : m_doc(nullptr), m_pageIndex(0), m_Field(nullptr)
500{
501}
502
503template<typename TField>
504PdfDocumentFieldIterableBase<TField>::Iterator::Iterator(PdfDocument& doc)
505 : m_doc(&doc), m_pageIndex(0), m_Field(nullptr)
506{
507 stepIntoPageOrForm(doc.GetPages());
508}
509
510template<typename TField>
511bool PdfDocumentFieldIterableBase<TField>::Iterator::operator==(const Iterator& rhs) const
512{
513 if (m_doc == nullptr && rhs.m_doc == nullptr)
514 return true;
515
516 return m_doc == rhs.m_doc && m_pageIndex == rhs.m_pageIndex && m_pageAnnotIterator == rhs.m_pageAnnotIterator && m_acroFormIterator == rhs.m_acroFormIterator;
517}
518
519template<typename TField>
520bool PdfDocumentFieldIterableBase<TField>::Iterator::operator!=(const Iterator& rhs) const
521{
522 if (m_doc == nullptr && rhs.m_doc == nullptr)
523 return false;
524
525 return m_doc != rhs.m_doc || m_pageIndex != rhs.m_pageIndex || m_pageAnnotIterator != rhs.m_pageAnnotIterator || m_acroFormIterator != rhs.m_acroFormIterator;
526}
527
528template<typename TField>
529typename PdfDocumentFieldIterableBase<TField>::Iterator& PdfDocumentFieldIterableBase<TField>::Iterator::operator++()
530{
531 increment();
532 return *this;
533}
534
535template<typename TField>
536typename PdfDocumentFieldIterableBase<TField>::Iterator PdfDocumentFieldIterableBase<TField>::Iterator::operator++(int)
537{
538 auto copy = *this;
539 increment();
540 return copy;
541}
542
543template<typename TField>
544void PdfDocumentFieldIterableBase<TField>::Iterator::increment()
545{
546 if (m_doc == nullptr)
547 return;
548
549 auto& pages = m_doc->GetPages();
550 if (m_pageIndex < pages.GetCount())
551 {
552 m_pageAnnotIterator++;
553 if (stepIntoPageAnnot(pages.GetPageAt(m_pageIndex).GetAnnotations()))
554 return;
555
556 m_pageIndex++;
557 stepIntoPageOrForm(pages);
558 }
559 else
560 {
561 m_acroFormIterator++;
562 stepIntoFormField(m_doc->MustGetAcroForm());
563 }
564}
565
566// Update the iterator for the current page index, or swith to form iteration
567template<typename TField>
568void PdfDocumentFieldIterableBase<TField>::Iterator::stepIntoPageOrForm(PdfPageCollection& pages)
569{
570 while (true)
571 {
572 if (m_pageIndex >= pages.GetCount())
573 break;
574
575 auto& annots = pages.GetPageAt(m_pageIndex).GetAnnotations();
576 m_pageAnnotIterator = annots.begin();
577 if (stepIntoPageAnnot(annots))
578 return;
579
580 m_pageIndex++;
581 }
582
583 auto form = m_doc->GetAcroForm();
584 if (form != nullptr)
585 {
586 m_acroFormIterator = form->begin();
587 stepIntoFormField(*form);
588 return;
589 }
590
591 // End of iteration
592 m_doc = nullptr;
593 m_Field = nullptr;
594 m_visitedObjs.clear();
595}
596
597// Verify the current page annotation iterator. It updates the current field
598// and returns true if a valid unvisited field is found, false otherwise
599template<typename TField>
600bool PdfDocumentFieldIterableBase<TField>::Iterator::stepIntoPageAnnot(PdfAnnotationCollection& annots)
601{
602 while (true)
603 {
604 if (m_pageAnnotIterator == annots.end())
605 break;
606
607 auto& annot = **m_pageAnnotIterator;
608 PdfField* field = nullptr;
609 if (annot.GetType() == PdfAnnotationType::Widget &&
610 (field = &static_cast<PdfAnnotationWidget&>(annot).GetField(),
611 m_visitedObjs.find(field->GetObject().GetIndirectReference()) == m_visitedObjs.end()))
612 {
613 m_Field = field;
614 m_visitedObjs.insert(field->GetObject().GetIndirectReference());
615 return true;
616 }
617
618 m_pageAnnotIterator++;
619 }
620
621 return false;
622}
623
624// Verify the current AcroForm field iterator. It updates the current field
625// if a valid unvisited leaf field is found, or it ends the iteration otherwise
626template<typename TField>
627void PdfDocumentFieldIterableBase<TField>::Iterator::stepIntoFormField(PdfAcroForm& form)
628{
629 while (true)
630 {
631 if (m_acroFormIterator == form.end())
632 break;
633
634 auto& field = **m_acroFormIterator;
635 if (field.GetChildren().GetCount() == 0
636 && m_visitedObjs.find(field.GetObject().GetIndirectReference()) == m_visitedObjs.end())
637 {
638 m_Field = &field;
639 m_visitedObjs.insert(field.GetObject().GetIndirectReference());
640 return;
641 }
642
643 m_acroFormIterator++;
644 }
645
646 // End of iteration
647 m_doc = nullptr;
648 m_Field = nullptr;
649 m_visitedObjs.clear();
650}
651
652};
653
654
655#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:328
const PdfInfo * GetInfo() const
Get access to the internal Info dictionary You can set the author, title etc.
Definition PdfDocument.h:355
const PdfPageCollection & GetPages() const
Get access to the page tree.
Definition PdfDocument.h:333
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:347
PdfTrailer & GetTrailer()
Get access to the internal trailer dictionary or root object.
Definition PdfDocument.h:340
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:316
PdfIndirectObjectList & GetObjects()
Get access to the internal vector of objects or root object.
Definition PdfDocument.h:366
const PdfIndirectObjectList & GetObjects() const
Get access to the internal vector of objects or root object.
Definition PdfDocument.h:373
const PdfCatalog & GetCatalog() const
Get access to the internal Catalog dictionary or root object.
Definition PdfDocument.h:323
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:30
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:129
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