PoDoFo 1.2.0
Loading...
Searching...
No Matches
PdfPageCollection.h
1// SPDX-FileCopyrightText: 2006 Dominik Seichter <domseichter@web.de>
2// SPDX-FileCopyrightText: 2021 Francesco Pretto <ceztko@gmail.com>
3// SPDX-License-Identifier: LGPL-2.0-or-later OR MPL-2.0
4
5#ifndef PDF_PAGES_COLLECTION_H
6#define PDF_PAGES_COLLECTION_H
7
8#include "PdfDeclarations.h"
9
10#include "PdfElement.h"
11#include "PdfArray.h"
12#include "PdfPage.h"
13
14namespace PoDoFo {
15
16 class PODOFO_API PdfObjectRelocationMap final
17 {
18 friend class PdfXObjectForm;
19 friend class PdfPageCollection;
20 public:
21 PdfObjectRelocationMap();
22
23 void Clear();
24 private:
25 PdfObjectRelocationMap(const PdfObjectRelocationMap&) = delete;
26 PdfObjectRelocationMap& operator=(const PdfObjectRelocationMap& rhs) = delete;
27
28 private:
29 std::unordered_map<PdfReference, PdfObject*> Map;
30 };
31
36 class PODOFO_API PdfPageCollection final : public PdfDictionaryElement
37 {
38 friend class PdfDocument;
39 friend class PdfPage;
40
41 public:
44
48
50 virtual ~PdfPageCollection();
51
54 unsigned GetCount() const;
55
62 PdfPage& GetPageAt(unsigned index);
63 const PdfPage& GetPageAt(unsigned index) const;
64
71 PdfPage& GetPage(const PdfReference& ref);
72 const PdfPage& GetPage(const PdfReference& ref) const;
73 bool TryGetPage(const PdfReference& ref, PdfPage*& page);
74 bool TryGetPage(const PdfReference& ref, const PdfPage*& page) const;
75
83 PdfPage& CreatePage(const nullable<Rect>& size = nullptr);
84 PdfPage& CreatePage(PdfPageSize pageSize);
85
93 PdfPage& CreatePageAt(unsigned atIndex, const nullable<Rect>& size = nullptr);
94 PdfPage& CreatePageAt(unsigned atIndex, PdfPageSize pageSize);
95
102 void CreatePagesAt(unsigned atIndex, unsigned count, const nullable<Rect>& size = nullptr);
103 void CreatePagesAt(unsigned atIndex, unsigned count, PdfPageSize pageSize);
104
107 void AppendDocumentPages(const PdfDocument& doc);
108
114 void AppendDocumentPages(const PdfDocument& doc, unsigned pageIndex, unsigned pageCount, PdfObjectRelocationMap* map = nullptr);
115
121 void InsertDocumentPageAt(unsigned atIndex, const PdfDocument& doc, unsigned pageIndex, PdfObjectRelocationMap* map = nullptr);
122
132 void RemovePageAt(unsigned atIndex);
133
138 void FlattenStructure();
139
140 public:
141 template <typename TObject, typename TListIterator>
142 class Iterator final
143 {
144 friend class PdfPageCollection;
145 public:
146 using difference_type = void;
147 using value_type = TObject*;
148 using pointer = void;
149 using reference = void;
150 using iterator_category = std::forward_iterator_tag;
151 public:
152 Iterator() {}
153 private:
154 Iterator(const TListIterator& iterator) : m_iterator(iterator) {}
155 public:
156 Iterator(const Iterator&) = default;
157 Iterator& operator=(const Iterator&) = default;
158 bool operator==(const Iterator& rhs) const
159 {
160 return m_iterator == rhs.m_iterator;
161 }
162 bool operator!=(const Iterator& rhs) const
163 {
164 return m_iterator != rhs.m_iterator;
165 }
166 Iterator& operator++()
167 {
168 m_iterator++;
169 return *this;
170 }
171 Iterator operator++(int)
172 {
173 auto copy = *this;
174 m_iterator++;
175 return copy;
176 }
177 value_type operator*()
178 {
179 return *m_iterator;
180 }
181 value_type operator->()
182 {
183 return *m_iterator;
184 }
185 private:
186 TListIterator m_iterator;
187 };
188
189 using PageList = std::vector<PdfPage*>;
190
193
194 public:
195 iterator begin();
196 iterator end();
197 const_iterator begin() const;
198 const_iterator end() const;
199
200 private:
203 void InsertPageAt(unsigned atIndex, std::unique_ptr<PdfPage> page);
204
207 void InsertPagesAt(unsigned atIndex, mspan<std::unique_ptr<PdfPage>> pages);
208
209 bool TryMovePageTo(unsigned atIndex, unsigned toIndex);
210
211 private:
212 void insertPageAt(unsigned atIndex, std::unique_ptr<PdfPage> page);
213 void insertPagesAt(unsigned atIndex, mspan<std::unique_ptr<PdfPage>> pages);
214 Rect getActualRect(const nullable<Rect>& size);
215
216 PdfPage* getPage(const PdfReference& ref) const;
217
218 void initPages();
219
220 unsigned traversePageTreeNode(PdfObject& obj, unsigned count,
221 std::vector<PdfObject*>& parents, std::unordered_set<PdfObject*>& visitedNodes);
222
224 PdfPageCollection& operator=(PdfPageCollection&) = delete;
225
226 private:
227 bool m_initialized;
228 PageList m_Pages;
229 PdfArray* m_kidsArray;
230 };
231
232};
233
234#endif // PDF_PAGES_COLLECTION_H
This file should be included as the FIRST file in every header of PoDoFo lib.
This class represents a PdfArray Use it for all arrays that are written to a PDF file.
Definition PdfArray.h:76
PdfDocument is the core interface for working with PDF documents.
Definition PdfDocument.h:108
This class represents a PDF indirect Object in memory.
Definition PdfObject.h:31
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
A reference is a pointer to a object in the PDF file of the form "4 0 R", where 4 is the object numbe...
Definition PdfReference.h:20
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
PdfPageSize
Enum holding the supported page sizes by PoDoFo.
Definition PdfDeclarations.h:485
tcb::span< T, Extent > mspan
Mutable span.
Definition span.h:22