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
36struct PODOFO_API AbortCheckInfo final
37{
38 unsigned ReadCount = 0;
39};
40
41struct PODOFO_API PdfTextExtractParams final
42{
43 nullable<Rect> ClipRect;
44 PdfTextExtractFlags Flags = PdfTextExtractFlags::None;
45
47 std::function<bool(const AbortCheckInfo& info)> AbortCheck = nullptr;
48};
49
50template <typename TField>
51class PdfPageFieldIterableBase final
52{
53 friend class PdfPage;
54
55public:
56 PdfPageFieldIterableBase()
57 : m_page(nullptr) { }
58
59private:
60 PdfPageFieldIterableBase(PdfPage& page)
61 : m_page(&page) { }
62
63public:
64 class Iterator final
65 {
66 friend class PdfPageFieldIterableBase;
67 public:
68 using difference_type = void;
69 using value_type = TField*;
70 using pointer = void;
71 using reference = void;
72 using iterator_category = std::forward_iterator_tag;
73 public:
74 Iterator()
75 : m_Field(nullptr) { }
76 private:
77 void stepIntoPageAnnot();
78
79 Iterator(PdfAnnotationCollection::iterator begin,
80 PdfAnnotationCollection::iterator end)
81 : m_annotsIterator(std::move(begin)), m_annotsEnd(std::move(end)), m_Field(nullptr)
82 {
83 stepIntoPageAnnot();
84 }
85
86 public:
87 Iterator(const Iterator&) = default;
88 Iterator& operator=(const Iterator&) = default;
89 bool operator==(const Iterator& rhs) const
90 {
91 return m_annotsIterator == rhs.m_annotsIterator;
92 }
93 bool operator!=(const Iterator& rhs) const
94 {
95 return m_annotsIterator != rhs.m_annotsIterator;
96 }
97 Iterator& operator++()
98 {
99 m_annotsIterator++;
100 stepIntoPageAnnot();
101 return *this;
102 }
103 Iterator operator++(int)
104 {
105 auto copy = *this;
106 m_annotsIterator++;
107 stepIntoPageAnnot();
108 return copy;
109 }
110 value_type operator*() { return m_Field; }
111 value_type operator->() { return m_Field; }
112 private:
113 PdfAnnotationCollection::iterator m_annotsIterator;
114 PdfAnnotationCollection::iterator m_annotsEnd;
115 value_type m_Field;
116 std::unordered_set<PdfReference> m_visitedObjs;
117 };
118
119public:
120 Iterator begin() const;
121 Iterator end() const;
122
123private:
124 PdfPage* m_page;
125};
126
127using PdfPageFieldIterable = PdfPageFieldIterableBase<PdfField>;
128using PdfPageConstFieldIterable = PdfPageFieldIterableBase<const PdfField>;
129
134class PODOFO_API PdfPage final : public PdfDictionaryElement, public PdfCanvas
135{
136 PODOFO_PRIVATE_FRIEND(class PdfPageTest);
137 friend class PdfPageCollection;
138 friend class PdfDocument;
139
140private:
145 PdfPage(PdfDocument& parent, const Rect& size);
146
156 PdfPage(PdfObject& obj, std::vector<PdfObject*>&& parents);
157
158public:
159 void ExtractTextTo(std::vector<PdfTextEntry>& entries,
160 const PdfTextExtractParams& params) const;
161
162 void ExtractTextTo(std::vector<PdfTextEntry>& entries,
163 const std::string_view& pattern = { },
164 const PdfTextExtractParams& params = { }) const;
165
169 Rect GetRect() const { return m_Rect; }
170
174 void SetRect(const Rect& rect);
175
176 Corners GetRectRaw() const override;
177
178 void SetRectRaw(const Corners& rect);
179
180 bool TryGetRotationRadians(double& teta) const override;
181
185 double GetRotationRadians() const;
186
190 void SetMediaBox(const Rect& rect);
191
195 void SetCropBox(const Rect& rect);
196
200 void SetTrimBox(const Rect& rect);
201
205 void SetBleedBox(const Rect& rect);
206
210 void SetArtBox(const Rect& rect);
211
217 unsigned GetPageNumber() const;
218
226 static Rect CreateStandardPageSize(const PdfPageSize pageSize, bool landscape = false);
227
231 Rect GetMediaBox() const;
232 Corners GetMediaBoxRaw() const;
233
237 Rect GetCropBox() const;
238 Corners GetCropBoxRaw() const;
239
243 Rect GetTrimBox() const;
244 Corners GetTrimBoxRaw() const;
245
249 Rect GetBleedBox() const;
250 Corners GetBleedBoxRaw() const;
251
255 Rect GetArtBox() const;
256 Corners GetArtBoxRaw() const;
257
261 unsigned GetRotation() const { return m_Rotation; }
262
267 bool TryGetRotationRaw(double& rotation) const;
268
273 void SetRotation(int rotation);
274
277 bool MoveTo(unsigned index);
278
279 template <typename TField>
280 TField& CreateField(const std::string_view& name, const Rect& rect);
281
282 PdfField& CreateField(const std::string_view& name, PdfFieldType fieldType, const Rect& rect);
283
288 PdfPageFieldIterable GetFieldsIterator();
289 PdfPageConstFieldIterable GetFieldsIterator() const;
290
291public:
292 unsigned GetIndex() const { return m_Index; }
293 PdfContents& GetOrCreateContents();
294 inline const PdfContents* GetContents() const { return m_Contents.get(); }
295 inline PdfContents* GetContents() { return m_Contents.get(); }
296 const PdfContents& MustGetContents() const;
297 PdfContents& MustGetContents();
298 const PdfResources& GetResources() const;
299 PdfResources& GetResources();
300 inline PdfAnnotationCollection& GetAnnotations() { return m_Annotations; }
301 inline const PdfAnnotationCollection& GetAnnotations() const { return m_Annotations; }
302
303private:
304 // To be called by PdfPageCollection
305 void FlattenStructure();
306 void SetIndex(unsigned index) { m_Index = index; }
307
308 void CopyContentsTo(OutputStream& stream) const override;
309
310 PdfObjectStream& GetOrCreateContentsStream(PdfStreamAppendFlags flags) override;
311
312 PdfObjectStream& ResetContentsStream() override;
313
314 PdfResources& GetOrCreateResources() override;
315
316 PdfResources* getResources() override;
317
318 PdfObject* getContentsObject() override;
319
320 PdfDictionaryElement& getElement() override;
321
322 PdfObject* findInheritableAttribute(const std::string_view& name) const;
323
324 PdfObject* findInheritableAttribute(const std::string_view& name, bool& isShallow) const;
325
326 void ensureContentsCreated();
327
332 Rect getPageBox(const std::string_view& inBox, bool isInheritable) const;
333
334 Corners getPageBoxRaw(const std::string_view& inBox, bool isInheritable) const;
335
336 void setPageBox(const PdfName& inBox, const Rect& rect);
337
338 void adjustRectToCurrentRotation(Rect& rect) const;
339
340private:
341 // Remove some PdfCanvas methods to maintain the class API surface clean
342 PdfElement& GetElement() = delete;
343 const PdfElement& GetElement() const = delete;
344 PdfObject* GetContentsObject() = delete;
345 const PdfObject* GetContentsObject() const = delete;
346
347private:
348 unsigned m_Index;
349 unsigned m_Rotation;
350 Rect m_Rect;
351 std::vector<PdfObject*> m_parents;
352 std::unique_ptr<PdfContents> m_Contents;
353 std::unique_ptr<PdfResources> m_Resources;
354 PdfAnnotationCollection m_Annotations;
355};
356
357template<typename TField>
358TField& PdfPage::CreateField(const std::string_view& name, const Rect & rect)
359{
360 return static_cast<TField&>(CreateField(name, PdfField::GetFieldType<TField>(), rect));
361}
362
363template<typename TField>
364typename PdfPageFieldIterableBase<TField>::Iterator PdfPageFieldIterableBase<TField>::begin() const
365{
366 if (m_page == nullptr)
367 return Iterator();
368 else
369 return Iterator(m_page->GetAnnotations().begin(), m_page->GetAnnotations().end());
370}
371
372template<typename TField>
373typename PdfPageFieldIterableBase<TField>::Iterator PdfPageFieldIterableBase<TField>::end() const
374{
375 if (m_page == nullptr)
376 return Iterator();
377 else
378 return Iterator(m_page->GetAnnotations().end(), m_page->GetAnnotations().end());
379}
380
381template<typename TField>
382void PdfPageFieldIterableBase<TField>::Iterator::stepIntoPageAnnot()
383{
384 while (true)
385 {
386 if (m_annotsIterator == m_annotsEnd)
387 break;
388
389 auto& annot = **m_annotsIterator;
390 PdfField* field = nullptr;
391 if (annot.GetType() == PdfAnnotationType::Widget &&
392 (field = &static_cast<PdfAnnotationWidget&>(annot).GetField(),
393 m_visitedObjs.find(field->GetObject().GetIndirectReference()) == m_visitedObjs.end()))
394 {
395 m_Field = field;
396 m_visitedObjs.insert(field->GetObject().GetIndirectReference());
397 return;
398 }
399
400 m_annotsIterator++;
401 }
402
403 m_Field = nullptr;
404 m_visitedObjs.clear();
405}
406
407};
408
409#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:17
An interface that provides the necessary features for a painter to draw onto a PdfObject.
Definition PdfCanvas.h:26
PdfDocument is the core interface for working with PDF documents.
Definition PdfDocument.h:109
This class represents a PDF indirect Object in memory.
Definition PdfObject.h:33
Class for managing the tree of Pages in a PDF document Don't use this class directly.
Definition PdfPageCollection.h:22
PdfPage is one page in the pdf document.
Definition PdfPage.h:135
Rect GetRect() const
Get the rectangle of this page.
Definition PdfPage.h:169
unsigned GetRotation() const
Get the normalized page rotation (0, 90, 180 or 270)
Definition PdfPage.h:261
An normalized rectangle defined by position (left-bottom) and size.
Definition Rect.h:18
Convenient type for char array storage and/or buffer with std::string compatibility.
Definition basetypes.h:35
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:533
PdfFieldType
The type of PDF field.
Definition PdfDeclarations.h:665
PdfTextExtractFlags
Definition PdfDeclarations.h:197
A structure with status progress attributes of certain operations.
Definition PdfPage.h:37