PoDoFo 1.0.0-dev
Loading...
Searching...
No Matches
PdfPage.h
1
7#ifndef PDF_PAGE_H
8#define PDF_PAGE_H
9
10#include "PdfDeclarations.h"
11
12#include <podofo/auxiliary/Rect.h>
13
14#include "PdfAnnotationCollection.h"
15#include "PdfCanvas.h"
16#include "PdfContents.h"
17#include "PdfField.h"
18#include "PdfResources.h"
19
20namespace PoDoFo {
21
22class PdfDocument;
23class InputStream;
24class PdfPage;
25
26struct PODOFO_API PdfTextEntry final
27{
28 std::string Text;
29 int Page = -1;
30 double X = -1;
31 double Y = -1;
32 double Length = -1;
33 nullable<Rect> BoundingBox;
34};
35
38struct PODOFO_API AbortCheckInfo final
39{
40 unsigned ReadCount = 0;
41};
42
43struct PODOFO_API PdfTextExtractParams final
44{
45 nullable<Rect> ClipRect;
46 PdfTextExtractFlags Flags = PdfTextExtractFlags::None;
47
49 std::function<bool(const AbortCheckInfo& info)> AbortCheck = nullptr;
50};
51
52template <typename TField>
53class PdfPageFieldIterableBase final
54{
55 friend class PdfPage;
56
57public:
58 PdfPageFieldIterableBase()
59 : m_page(nullptr) { }
60
61private:
62 PdfPageFieldIterableBase(PdfPage& page)
63 : m_page(&page) { }
64
65public:
66 class Iterator final
67 {
68 friend class PdfPageFieldIterableBase;
69 public:
70 using difference_type = void;
71 using value_type = TField*;
72 using pointer = void;
73 using reference = void;
74 using iterator_category = std::forward_iterator_tag;
75 public:
76 Iterator()
77 : m_Field(nullptr) { }
78 private:
79 void stepIntoPageAnnot();
80
81 Iterator(PdfAnnotationCollection::iterator begin,
82 PdfAnnotationCollection::iterator end)
83 : m_annotsIterator(std::move(begin)), m_annotsEnd(std::move(end)), m_Field(nullptr)
84 {
85 stepIntoPageAnnot();
86 }
87
88 public:
89 Iterator(const Iterator&) = default;
90 Iterator& operator=(const Iterator&) = default;
91 bool operator==(const Iterator& rhs) const
92 {
93 return m_annotsIterator == rhs.m_annotsIterator;
94 }
95 bool operator!=(const Iterator& rhs) const
96 {
97 return m_annotsIterator != rhs.m_annotsIterator;
98 }
99 Iterator& operator++()
100 {
101 m_annotsIterator++;
102 stepIntoPageAnnot();
103 return *this;
104 }
105 Iterator operator++(int)
106 {
107 auto copy = *this;
108 m_annotsIterator++;
109 stepIntoPageAnnot();
110 return copy;
111 }
112 value_type operator*() { return m_Field; }
113 value_type operator->() { return m_Field; }
114 private:
115 PdfAnnotationCollection::iterator m_annotsIterator;
116 PdfAnnotationCollection::iterator m_annotsEnd;
117 value_type m_Field;
118 std::unordered_set<PdfReference> m_visitedObjs;
119 };
120
121public:
122 Iterator begin() const;
123 Iterator end() const;
124
125private:
126 PdfPage* m_page;
127};
128
129using PdfPageFieldIterable = PdfPageFieldIterableBase<PdfField>;
130using PdfPageConstFieldIterable = PdfPageFieldIterableBase<const PdfField>;
131
136class PODOFO_API PdfPage final : public PdfDictionaryElement, public PdfCanvas
137{
138 PODOFO_PRIVATE_FRIEND(class PdfPageTest);
139 friend class PdfPageCollection;
140 friend class PdfDocument;
141
142private:
147 PdfPage(PdfDocument& parent, const Rect& size);
148
158 PdfPage(PdfObject& obj, std::vector<PdfObject*>&& parents);
159
160public:
161 void ExtractTextTo(std::vector<PdfTextEntry>& entries,
162 const PdfTextExtractParams& params) const;
163
164 void ExtractTextTo(std::vector<PdfTextEntry>& entries,
165 const std::string_view& pattern = { },
166 const PdfTextExtractParams& params = { }) const;
167
171 Rect GetRect() const { return m_Rect; }
172
176 void SetRect(const Rect& rect);
177
178 Corners GetRectRaw() const override;
179
180 void SetRectRaw(const Corners& rect);
181
182 bool TryGetRotationRadians(double& teta) const override;
183
187 double GetRotationRadians() const;
188
192 void SetMediaBox(const Rect& rect);
193
197 void SetCropBox(const Rect& rect);
198
202 void SetTrimBox(const Rect& rect);
203
207 void SetBleedBox(const Rect& rect);
208
212 void SetArtBox(const Rect& rect);
213
219 unsigned GetPageNumber() const;
220
228 static Rect CreateStandardPageSize(const PdfPageSize pageSize, bool landscape = false);
229
233 Rect GetMediaBox() const;
234 Corners GetMediaBoxRaw() const;
235
239 Rect GetCropBox() const;
240 Corners GetCropBoxRaw() const;
241
245 Rect GetTrimBox() const;
246 Corners GetTrimBoxRaw() const;
247
251 Rect GetBleedBox() const;
252 Corners GetBleedBoxRaw() const;
253
257 Rect GetArtBox() const;
258 Corners GetArtBoxRaw() const;
259
263 unsigned GetRotation() const { return m_Rotation; }
264
269 bool TryGetRotationRaw(double& rotation) const;
270
275 void SetRotation(int rotation);
276
279 bool MoveTo(unsigned index);
280
281 template <typename TField>
282 TField& CreateField(const std::string_view& name, const Rect& rect);
283
284 PdfField& CreateField(const std::string_view& name, PdfFieldType fieldType, const Rect& rect);
285
290 PdfPageFieldIterable GetFieldsIterator();
291 PdfPageConstFieldIterable GetFieldsIterator() const;
292
293public:
294 unsigned GetIndex() const { return m_Index; }
295 PdfContents& GetOrCreateContents();
296 inline const PdfContents* GetContents() const { return m_Contents.get(); }
297 inline PdfContents* GetContents() { return m_Contents.get(); }
298 const PdfContents& MustGetContents() const;
299 PdfContents& MustGetContents();
300 const PdfResources& GetResources() const;
301 PdfResources& GetResources();
302 inline PdfAnnotationCollection& GetAnnotations() { return m_Annotations; }
303 inline const PdfAnnotationCollection& GetAnnotations() const { return m_Annotations; }
304
305private:
306 // To be called by PdfPageCollection
307 void FlattenStructure();
308 void SetIndex(unsigned index) { m_Index = index; }
309
310 void CopyContentsTo(OutputStream& stream) const override;
311
312 PdfObjectStream& GetOrCreateContentsStream(PdfStreamAppendFlags flags) override;
313
314 PdfObjectStream& ResetContentsStream() override;
315
316 PdfResources& GetOrCreateResources() override;
317
318 PdfResources* getResources() override;
319
320 PdfObject* getContentsObject() override;
321
322 PdfDictionaryElement& getElement() override;
323
324 PdfObject* findInheritableAttribute(const std::string_view& name) const;
325
326 PdfObject* findInheritableAttribute(const std::string_view& name, bool& isShallow) const;
327
328 void ensureContentsCreated();
329
334 Rect getPageBox(const std::string_view& inBox, bool isInheritable) const;
335
336 Corners getPageBoxRaw(const std::string_view& inBox, bool isInheritable) const;
337
338 void setPageBox(const PdfName& inBox, const Rect& rect);
339
340 void adjustRectToCurrentRotation(Rect& rect) const;
341
342private:
343 // Remove some PdfCanvas methods to maintain the class API surface clean
344 PdfElement& GetElement() = delete;
345 const PdfElement& GetElement() const = delete;
346 PdfObject* GetContentsObject() = delete;
347 const PdfObject* GetContentsObject() const = delete;
348
349private:
350 unsigned m_Index;
351 unsigned m_Rotation;
352 Rect m_Rect;
353 std::vector<PdfObject*> m_parents;
354 std::unique_ptr<PdfContents> m_Contents;
355 std::unique_ptr<PdfResources> m_Resources;
356 PdfAnnotationCollection m_Annotations;
357};
358
359template<typename TField>
360TField& PdfPage::CreateField(const std::string_view& name, const Rect & rect)
361{
362 return static_cast<TField&>(CreateField(name, PdfField::GetFieldType<TField>(), rect));
363}
364
365template<typename TField>
366typename PdfPageFieldIterableBase<TField>::Iterator PdfPageFieldIterableBase<TField>::begin() const
367{
368 if (m_page == nullptr)
369 return Iterator();
370 else
371 return Iterator(m_page->GetAnnotations().begin(), m_page->GetAnnotations().end());
372}
373
374template<typename TField>
375typename PdfPageFieldIterableBase<TField>::Iterator PdfPageFieldIterableBase<TField>::end() const
376{
377 if (m_page == nullptr)
378 return Iterator();
379 else
380 return Iterator(m_page->GetAnnotations().end(), m_page->GetAnnotations().end());
381}
382
383template<typename TField>
384void PdfPageFieldIterableBase<TField>::Iterator::stepIntoPageAnnot()
385{
386 while (true)
387 {
388 if (m_annotsIterator == m_annotsEnd)
389 break;
390
391 auto& annot = **m_annotsIterator;
392 PdfField* field = nullptr;
393 if (annot.GetType() == PdfAnnotationType::Widget &&
394 (field = &static_cast<PdfAnnotationWidget&>(annot).GetField(),
395 m_visitedObjs.find(field->GetObject().GetIndirectReference()) == m_visitedObjs.end()))
396 {
397 m_Field = field;
398 m_visitedObjs.insert(field->GetObject().GetIndirectReference());
399 return;
400 }
401
402 m_annotsIterator++;
403 }
404
405 m_Field = nullptr;
406 m_visitedObjs.clear();
407}
408
409};
410
411#endif // PDF_PAGE_H
SPDX-FileCopyrightText: (C) 2005 Dominik Seichter domseichter@web.de SPDX-FileCopyrightText: (C) 2020...
An unoriented rectangle defined by 2 points.
Definition Corners.h:20
A interface that provides the necessary features for a painter to draw onto a PdfObject.
Definition PdfCanvas.h:28
PdfDocument is the core interface for working with PDF documents.
Definition PdfDocument.h:111
This class represents a PDF indirect Object in memory.
Definition PdfObject.h:35
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:137
Rect GetRect() const
Get the rectangle of this page.
Definition PdfPage.h:171
unsigned GetRotation() const
Get the normalized page rotation (0, 90, 180 or 270)
Definition PdfPage.h:263
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
PdfPageSize
Enum holding the supported page sizes by PoDoFo.
Definition PdfDeclarations.h:518
PdfFieldType
The type of PDF field.
Definition PdfDeclarations.h:650
PdfTextExtractFlags
Definition PdfDeclarations.h:199
A structure with status progress attributes of certain operations.
Definition PdfPage.h:39