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"
20 #include "PdfColorSpace.h"
22 #include "PdfOutlines.h"
31 template <
typename TField>
32 class PdfDocumentFieldIterableBase final
34 friend class PdfDocument;
37 PdfDocumentFieldIterableBase()
41 PdfDocumentFieldIterableBase(PdfDocument& doc)
47 friend class PdfDocumentFieldIterableBase;
49 using difference_type = void;
50 using value_type = TField*;
52 using reference = void;
53 using iterator_category = std::forward_iterator_tag;
57 Iterator(PdfDocument& doc);
59 Iterator(
const Iterator&) =
default;
60 Iterator& operator=(
const Iterator&) =
default;
61 bool operator==(
const Iterator& rhs)
const;
62 bool operator!=(
const Iterator& rhs)
const;
63 Iterator& operator++();
64 Iterator operator++(
int);
65 value_type operator*() {
return m_Field; }
66 value_type operator->() {
return m_Field; }
69 void stepIntoPageOrForm(PdfPageCollection& pages);
70 bool stepIntoPageAnnot(PdfAnnotationCollection& annots);
71 void stepIntoFormField(PdfAcroForm& form);
75 PdfAnnotationCollection::iterator m_pageAnnotIterator;
76 PdfAcroForm::iterator m_acroFormIterator;
78 std::unordered_set<PdfReference> m_visitedObjs;
82 Iterator begin()
const;
89 using PdfDocumentFieldIterable = PdfDocumentFieldIterableBase<PdfField>;
90 using PdfDocumentConstFieldIterable = PdfDocumentFieldIterableBase<const PdfField>;
109 friend class PdfMetadata;
110 friend class PdfXObjectForm;
146 void CollectGarbage();
150 std::unique_ptr<PdfImage> CreateImage();
152 std::unique_ptr<PdfXObjectForm> CreateXObjectForm(
const Rect& rect);
154 std::unique_ptr<PdfDestination> CreateDestination();
158 std::unique_ptr<PdfExtGState> CreateExtGState();
160 template <
typename Taction>
161 std::unique_ptr<Taction> CreateAction();
165 std::unique_ptr<PdfFileSpec> CreateFileSpec();
174 bool IsPrintAllowed()
const;
183 bool IsEditAllowed()
const;
192 bool IsCopyAllowed()
const;
201 bool IsEditNotesAllowed()
const;
210 bool IsFillAndSignAllowed()
const;
219 bool IsAccessibilityAllowed()
const;
228 bool IsDocAssemblyAllowed()
const;
237 bool IsHighPrintAllowed()
const;
239 PdfAcroForm& MustGetAcroForm();
241 const PdfAcroForm& MustGetAcroForm()
const;
256 PdfDocumentFieldIterable GetFieldsIterator();
257 PdfDocumentConstFieldIterable GetFieldsIterator()
const;
264 virtual const PdfEncrypt* GetEncrypt()
const = 0;
269 bool IsEncrypted()
const;
318 PdfMetadata& GetMetadata() {
return m_Metadata; }
320 const PdfMetadata& GetMetadata()
const {
return m_Metadata; }
336 PdfAcroForm* GetAcroForm() {
return m_AcroForm.get(); }
338 const PdfAcroForm* GetAcroForm()
const {
return m_AcroForm.get(); }
340 PdfNameTrees* GetNames() {
return m_NameTrees.get(); }
342 const PdfNameTrees* GetNames()
const {
return m_NameTrees.get(); }
344 PdfOutlines* GetOutlines();
346 const PdfOutlines* GetOutlines()
const;
348 PdfFontManager& GetFonts() {
return m_FontManager; }
354 PdfDocument(
bool empty =
false);
356 PdfDocument(
const PdfDocument& doc);
364 void SetTrailer(std::unique_ptr<PdfObject> obj);
370 virtual void reset();
376 virtual void clear();
391 void InsertDocumentPageAt(
unsigned atIndex,
const PdfDocument& doc,
unsigned pageIndex);
392 void AppendDocumentPages(
const PdfDocument& doc,
unsigned pageIndex,
unsigned pageCount);
395 Rect FillXObjectFromPage(PdfXObjectForm& xobj,
const PdfPage& page,
bool useTrimBox);
399 void createAction(
PdfActionType type, std::unique_ptr<PdfAction>& action);
402 void append(
const PdfDocument& doc,
bool appendAll);
410 void fixObjectReferences(
PdfObject& obj,
int difference);
412 void deletePages(
unsigned atIndex,
unsigned pageCount);
423 PdfMetadata m_Metadata;
425 std::unique_ptr<PdfObject> m_TrailerObj;
426 std::unique_ptr<PdfTrailer> m_Trailer;
427 std::unique_ptr<PdfCatalog> m_Catalog;
428 std::unique_ptr<PdfInfo> m_Info;
429 std::unique_ptr<PdfPageCollection> m_Pages;
430 std::unique_ptr<PdfAcroForm> m_AcroForm;
432 std::unique_ptr<PdfNameTrees> m_NameTrees;
435 template<
typename TAction>
436 std::unique_ptr<TAction> PdfDocument::CreateAction()
438 std::unique_ptr<TAction> ret;
439 createAction(PdfAction::GetActionType<TAction>(),
reinterpret_cast<std::unique_ptr<PdfAction>&
>(ret));
443 template<
typename TField>
444 typename PdfDocumentFieldIterableBase<TField>::Iterator PdfDocumentFieldIterableBase<TField>::begin()
const
446 if (m_doc ==
nullptr)
449 return Iterator(*m_doc);
452 template<
typename TField>
453 typename PdfDocumentFieldIterableBase<TField>::Iterator PdfDocumentFieldIterableBase<TField>::end()
const
458 template<
typename TField>
459 PdfDocumentFieldIterableBase<TField>::Iterator::Iterator()
460 : m_doc(nullptr), m_pageIndex(0), m_Field(nullptr)
464 template<
typename TField>
465 PdfDocumentFieldIterableBase<TField>::Iterator::Iterator(PdfDocument& doc)
466 : m_doc(&doc), m_pageIndex(0), m_Field(nullptr)
468 stepIntoPageOrForm(doc.GetPages());
471 template<
typename TField>
472 bool PdfDocumentFieldIterableBase<TField>::Iterator::operator==(
const Iterator& rhs)
const
474 if (m_doc ==
nullptr && rhs.m_doc ==
nullptr)
477 return m_doc == rhs.m_doc && m_pageIndex == rhs.m_pageIndex && m_pageAnnotIterator == rhs.m_pageAnnotIterator && m_acroFormIterator == rhs.m_acroFormIterator;
480 template<
typename TField>
481 bool PdfDocumentFieldIterableBase<TField>::Iterator::operator!=(
const Iterator& rhs)
const
483 if (m_doc ==
nullptr && rhs.m_doc ==
nullptr)
486 return m_doc != rhs.m_doc || m_pageIndex != rhs.m_pageIndex || m_pageAnnotIterator != rhs.m_pageAnnotIterator || m_acroFormIterator != rhs.m_acroFormIterator;
489 template<
typename TField>
490 typename PdfDocumentFieldIterableBase<TField>::Iterator& PdfDocumentFieldIterableBase<TField>::Iterator::operator++()
496 template<
typename TField>
497 typename PdfDocumentFieldIterableBase<TField>::Iterator PdfDocumentFieldIterableBase<TField>::Iterator::operator++(
int)
504 template<
typename TField>
505 void PdfDocumentFieldIterableBase<TField>::Iterator::increment()
507 if (m_doc ==
nullptr)
510 auto& pages = m_doc->GetPages();
511 if (m_pageIndex < pages.GetCount())
513 m_pageAnnotIterator++;
514 if (stepIntoPageAnnot(pages.GetPageAt(m_pageIndex).GetAnnotations()))
518 stepIntoPageOrForm(pages);
522 m_acroFormIterator++;
523 stepIntoFormField(m_doc->MustGetAcroForm());
528 template<
typename TField>
529 void PdfDocumentFieldIterableBase<TField>::Iterator::stepIntoPageOrForm(PdfPageCollection& pages)
533 if (m_pageIndex >= pages.GetCount())
536 auto& annots = pages.GetPageAt(m_pageIndex).GetAnnotations();
537 m_pageAnnotIterator = annots.begin();
538 if (stepIntoPageAnnot(annots))
544 auto form = m_doc->GetAcroForm();
547 m_acroFormIterator = form->begin();
548 stepIntoFormField(*form);
555 m_visitedObjs.clear();
560 template<
typename TField>
561 bool PdfDocumentFieldIterableBase<TField>::Iterator::stepIntoPageAnnot(PdfAnnotationCollection& annots)
565 if (m_pageAnnotIterator == annots.end())
568 auto& annot = **m_pageAnnotIterator;
569 PdfField* field =
nullptr;
570 if (annot.GetType() == PdfAnnotationType::Widget &&
571 (field = &
static_cast<PdfAnnotationWidget&
>(annot).GetField(),
572 m_visitedObjs.find(field->GetObject().GetIndirectReference()) == m_visitedObjs.end()))
575 m_visitedObjs.insert(field->GetObject().GetIndirectReference());
579 m_pageAnnotIterator++;
587 template<
typename TField>
588 void PdfDocumentFieldIterableBase<TField>::Iterator::stepIntoFormField(PdfAcroForm& form)
592 if (m_acroFormIterator == form.end())
595 auto& field = **m_acroFormIterator;
596 if (field.GetChildren().GetCount() == 0
597 && m_visitedObjs.find(field.GetObject().GetIndirectReference()) == m_visitedObjs.end())
600 m_visitedObjs.insert(field.GetObject().GetIndirectReference());
604 m_acroFormIterator++;
610 m_visitedObjs.clear();
PdfDocument is the core interface for working with PDF documents.
Definition: PdfDocument.h:108
const PdfTrailer & GetTrailer() const
Get access to the internal trailer dictionary or root object.
Definition: PdfDocument.h:308
const PdfCatalog & GetCatalog() const
Get access to the internal Catalog dictionary or root object.
Definition: PdfDocument.h:284
const PdfPageCollection & GetPages() const
Get access to the page tree.
Definition: PdfDocument.h:294
virtual PdfVersion GetPdfVersion() const =0
Get the PDF version of the document.
const PdfInfo * GetInfo() const
Get access to the internal Info dictionary You can set the author, title etc.
Definition: PdfDocument.h:316
PdfCatalog & GetCatalog()
Get access to the internal Catalog dictionary or root object.
Definition: PdfDocument.h:277
virtual void SetPdfVersion(PdfVersion version)=0
Get the PDF version of the document.
PdfIndirectObjectList & GetObjects()
Get access to the internal vector of objects or root object.
Definition: PdfDocument.h:327
PdfPageCollection & GetPages()
Get access to the page tree.
Definition: PdfDocument.h:289
PdfTrailer & GetTrailer()
Get access to the internal trailer dictionary or root object.
Definition: PdfDocument.h:301
const PdfIndirectObjectList & GetObjects() const
Get access to the internal vector of objects or root object.
Definition: PdfDocument.h:334
A class that is used to encrypt a PDF file and set document permissions on the PDF file.
Definition: PdfEncrypt.h:122
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
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:206
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:127
A rectangle defined by position and size.
Definition: Rect.h:20
Alternative to std::optional that supports reference (but not pointer) types.
Definition: nullable.h:29
SPDX-FileCopyrightText: (C) 2022 Francesco Pretto ceztko@gmail.com SPDX-License-Identifier: LGPL-2....
Definition: basetypes.h:16
PdfActionType
The type of the action.
Definition: PdfAction.h:28
std::shared_ptr< const PdfColorSpaceFilter > PdfColorSpaceFilterPtr
Convenience alias for a constant PdfColorSpaceFilter shared ptr.
Definition: PdfColorSpaceFilter.h:77
PdfAcroFormDefaulAppearance
Definition: PdfAcroForm.h:17
@ ArialBlack
Add a default appearance with Arial embedded and black text if no other DA key is present.
PdfVersion
Enum to identify different versions of the PDF file format.
Definition: PdfDeclarations.h:71