PoDoFo 1.2.0
Loading...
Searching...
No Matches
PdfPage.h
1// SPDX-FileCopyrightText: 2005 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_PAGE_H
6#define PDF_PAGE_H
7
8#include "PdfDeclarations.h"
9
10#include <podofo/auxiliary/Rect.h>
11
12#include "PdfAnnotationCollection.h"
13#include "PdfCanvas.h"
14#include "PdfContents.h"
15#include "PdfField.h"
16#include "PdfResources.h"
17
18namespace PoDoFo {
19
20class PdfDocument;
21class InputStream;
22class PdfPage;
23
24struct PODOFO_API PdfTextEntry final
25{
26 std::string Text;
27 int Page = -1;
28 double X = -1;
29 double Y = -1;
30 double Length = -1;
31 nullable<Rect> BoundingBox;
32};
33
35struct PODOFO_API AbortCheckInfo final
36{
37 unsigned ReadCount = 0;
38};
39
40struct PODOFO_API PdfTextExtractParams final
41{
42 nullable<Rect> ClipRect;
43 PdfTextExtractFlags Flags = PdfTextExtractFlags::None;
44
46 std::function<bool(const AbortCheckInfo& info)> AbortCheck = nullptr;
47};
48
49template <typename TField>
50class PdfPageFieldIterableBase final
51{
52 friend class PdfPage;
53
54public:
55 PdfPageFieldIterableBase()
56 : m_page(nullptr) { }
57
58private:
59 PdfPageFieldIterableBase(PdfPage& page)
60 : m_page(&page) { }
61
62public:
63 class Iterator final
64 {
65 friend class PdfPageFieldIterableBase;
66 public:
67 using difference_type = void;
68 using value_type = TField*;
69 using pointer = void;
70 using reference = void;
71 using iterator_category = std::forward_iterator_tag;
72 public:
73 Iterator()
74 : m_Field(nullptr) { }
75 private:
76 void stepIntoPageAnnot();
77
78 Iterator(PdfAnnotationCollection::iterator begin,
79 PdfAnnotationCollection::iterator end)
80 : m_annotsIterator(std::move(begin)), m_annotsEnd(std::move(end)), m_Field(nullptr)
81 {
82 stepIntoPageAnnot();
83 }
84
85 public:
86 Iterator(const Iterator&) = default;
87 Iterator& operator=(const Iterator&) = default;
88 bool operator==(const Iterator& rhs) const
89 {
90 return m_annotsIterator == rhs.m_annotsIterator;
91 }
92 bool operator!=(const Iterator& rhs) const
93 {
94 return m_annotsIterator != rhs.m_annotsIterator;
95 }
96 Iterator& operator++()
97 {
98 m_annotsIterator++;
99 stepIntoPageAnnot();
100 return *this;
101 }
102 Iterator operator++(int)
103 {
104 auto copy = *this;
105 m_annotsIterator++;
106 stepIntoPageAnnot();
107 return copy;
108 }
109 value_type operator*() { return m_Field; }
110 value_type operator->() { return m_Field; }
111 private:
112 PdfAnnotationCollection::iterator m_annotsIterator;
113 PdfAnnotationCollection::iterator m_annotsEnd;
114 value_type m_Field;
115 std::unordered_set<PdfReference> m_visitedObjs;
116 };
117
118public:
119 Iterator begin() const;
120 Iterator end() const;
121
122private:
123 PdfPage* m_page;
124};
125
126using PdfPageFieldIterable = PdfPageFieldIterableBase<PdfField>;
127using PdfPageConstFieldIterable = PdfPageFieldIterableBase<const PdfField>;
128
132class PODOFO_API PdfPage final : public PdfDictionaryElement, public PdfCanvas
133{
134 PODOFO_PRIVATE_FRIEND(class PdfPageTest);
135 friend class PdfPageCollection;
136 friend class PdfDocument;
137
138private:
142 PdfPage(PdfDocument& parent, const Rect& size);
143
152 PdfPage(PdfObject& obj, std::vector<PdfObject*>&& parents);
153
154public:
158 void ExtractTextTo(std::vector<PdfTextEntry>& entries,
159 const PdfTextExtractParams& params) const;
160
165 void ExtractTextTo(std::vector<PdfTextEntry>& entries,
166 const std::string_view& pattern = { },
167 const PdfTextExtractParams& params = { }) const;
168
171 Rect GetRect() const { return m_Rect; }
172
175 void SetRect(const Rect& rect);
176
177 Corners GetRectRaw() const override;
178
179 void SetRectRaw(const Corners& rect);
180
181 bool TryGetRotationRadians(double& teta) const override;
182
185 double GetRotationRadians() const;
186
189 void SetMediaBox(const Rect& rect);
190
193 void SetCropBox(const Rect& rect);
194
197 void SetTrimBox(const Rect& rect);
198
201 void SetBleedBox(const Rect& rect);
202
205 void SetArtBox(const Rect& rect);
206
211 unsigned GetPageNumber() const;
212
219 static Rect CreateStandardPageSize(const PdfPageSize pageSize, bool landscape = false);
220
223 Rect GetMediaBox() const;
224 Corners GetMediaBoxRaw() const;
225
228 Rect GetCropBox() const;
229 Corners GetCropBoxRaw() const;
230
233 Rect GetTrimBox() const;
234 Corners GetTrimBoxRaw() const;
235
238 Rect GetBleedBox() const;
239 Corners GetBleedBoxRaw() const;
240
243 Rect GetArtBox() const;
244 Corners GetArtBoxRaw() const;
245
248 unsigned GetRotation() const { return m_Rotation; }
249
253 bool TryGetRotationRaw(double& rotation) const;
254
258 void SetRotation(int rotation);
259
262 bool MoveTo(unsigned index);
263
264 template <typename TField>
265 TField& CreateField(const std::string_view& name, const Rect& rect);
266
267 PdfField& CreateField(const std::string_view& name, PdfFieldType fieldType, const Rect& rect);
268
271 PdfPageFieldIterable GetFieldsIterator();
272 PdfPageConstFieldIterable GetFieldsIterator() const;
273
274public:
275 unsigned GetIndex() const { return m_Index; }
276 PdfContents& GetOrCreateContents();
277 inline const PdfContents* GetContents() const { return m_Contents.get(); }
278 inline PdfContents* GetContents() { return m_Contents.get(); }
279 const PdfContents& MustGetContents() const;
280 PdfContents& MustGetContents();
281 const PdfResources& GetResources() const;
282 PdfResources& GetResources();
283 inline PdfAnnotationCollection& GetAnnotations() { return m_Annotations; }
284 inline const PdfAnnotationCollection& GetAnnotations() const { return m_Annotations; }
285
286private:
287 // To be called by PdfPageCollection
288 void FlattenStructure();
289 void SetIndex(unsigned index) { m_Index = index; }
290
291 void CopyContentsTo(OutputStream& stream) const override;
292
293 PdfObjectStream& GetOrCreateContentsStream(PdfStreamAppendFlags flags) override;
294
295 PdfObjectStream& ResetContentsStream() override;
296
297 PdfResources& GetOrCreateResources() override;
298
299 PdfResources* getResources() override;
300
301 PdfObject* getContentsObject() override;
302
303 PdfDictionaryElement& getElement() override;
304
305 PdfObject* findInheritableAttribute(const std::string_view& name) const;
306
307 PdfObject* findInheritableAttribute(const std::string_view& name, bool& isShallow) const;
308
309 void ensureContentsCreated();
310
316 Rect getPageBox(const std::string_view& inBox, bool isInheritable) const;
317
323 Corners getPageBoxRaw(const std::string_view& inBox, bool isInheritable) const;
324
328 void setPageBox(const PdfName& inBox, const Rect& rect);
329
330 void adjustRectToCurrentRotation(Rect& rect) const;
331
332private:
333 // Remove some PdfCanvas methods to maintain the class API surface clean
334 PdfElement& GetElement() = delete;
335 const PdfElement& GetElement() const = delete;
336 PdfObject* GetContentsObject() = delete;
337 const PdfObject* GetContentsObject() const = delete;
338
339private:
340 unsigned m_Index;
341 unsigned m_Rotation;
342 Rect m_Rect;
343 std::vector<PdfObject*> m_parents;
344 std::unique_ptr<PdfContents> m_Contents;
345 std::unique_ptr<PdfResources> m_Resources;
346 PdfAnnotationCollection m_Annotations;
347};
348
349template<typename TField>
350TField& PdfPage::CreateField(const std::string_view& name, const Rect & rect)
351{
352 return static_cast<TField&>(CreateField(name, PdfField::GetFieldType<TField>(), rect));
353}
354
355template<typename TField>
356typename PdfPageFieldIterableBase<TField>::Iterator PdfPageFieldIterableBase<TField>::begin() const
357{
358 if (m_page == nullptr)
359 return Iterator();
360 else
361 return Iterator(m_page->GetAnnotations().begin(), m_page->GetAnnotations().end());
362}
363
364template<typename TField>
365typename PdfPageFieldIterableBase<TField>::Iterator PdfPageFieldIterableBase<TField>::end() const
366{
367 if (m_page == nullptr)
368 return Iterator();
369 else
370 return Iterator(m_page->GetAnnotations().end(), m_page->GetAnnotations().end());
371}
372
373template<typename TField>
374void PdfPageFieldIterableBase<TField>::Iterator::stepIntoPageAnnot()
375{
376 while (true)
377 {
378 if (m_annotsIterator == m_annotsEnd)
379 break;
380
381 auto& annot = **m_annotsIterator;
382 PdfField* field = nullptr;
383 if (annot.GetType() == PdfAnnotationType::Widget &&
384 (field = &static_cast<PdfAnnotationWidget&>(annot).GetField(),
385 m_visitedObjs.find(field->GetObject().GetIndirectReference()) == m_visitedObjs.end()))
386 {
387 m_Field = field;
388 m_visitedObjs.insert(field->GetObject().GetIndirectReference());
389 return;
390 }
391
392 m_annotsIterator++;
393 }
394
395 m_Field = nullptr;
396 m_visitedObjs.clear();
397}
398
399};
400
401#endif // PDF_PAGE_H
This file should be included as the FIRST file in every header of PoDoFo lib.
An unoriented rectangle defined by 2 points.
Definition Corners.h:16
An interface that provides the necessary features for a painter to draw onto a PdfObject.
Definition PdfCanvas.h:25
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
Rect GetRect() const
Get the rectangle of this page.
Definition PdfPage.h:171
void ExtractTextTo(std::vector< PdfTextEntry > &entries, const PdfTextExtractParams &params) const
Extract text from the page.
void ExtractTextTo(std::vector< PdfTextEntry > &entries, const std::string_view &pattern={ }, const PdfTextExtractParams &params={ }) const
Extract text from the page.
unsigned GetRotation() const
Get the normalized page rotation (0, 90, 180 or 270)
Definition PdfPage.h:248
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
PdfFieldType
The type of PDF field.
Definition PdfDeclarations.h:610
PdfTextExtractFlags
Definition PdfDeclarations.h:184
A structure with status progress attributes of certain operations.
Definition PdfPage.h:36