PoDoFo 1.2.0
Loading...
Searching...
No Matches
PdfAnnotation.h
1// SPDX-FileCopyrightText: 2006 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_ANNOTATION_H
6#define PDF_ANNOTATION_H
7
8#include "PdfElement.h"
9#include <podofo/auxiliary/Rect.h>
10#include "PdfColor.h"
11
12namespace PoDoFo {
13
14class PdfPage;
15class PdfXObject;
16class PdfAnnotationText;
17class PdfAnnotationLink;
18class PdfAnnotationFreeText;
19class PdfAnnotationLine;
20class PdfAnnotationSquare;
21class PdfAnnotationCircle;
22class PdfAnnotationPolygon;
23class PdfAnnotationPolyLine;
24class PdfAnnotationHighlight;
25class PdfAnnotationUnderline;
26class PdfAnnotationSquiggly;
27class PdfAnnotationStrikeOut;
28class PdfAnnotationStamp;
29class PdfAnnotationCaret;
30class PdfAnnotationInk;
31class PdfAnnotationPopup;
32class PdfAnnotationFileAttachment;
33class PdfAnnotationSound;
34class PdfAnnotationMovie;
35class PdfAnnotationWidget;
36class PdfAnnotationScreen;
37class PdfAnnotationPrinterMark;
38class PdfAnnotationTrapNet;
39class PdfAnnotationWatermark;
40class PdfAnnotationModel3D;
41class PdfAnnotationRichMedia;
42class PdfAnnotationWebMedia;
43class PdfAnnotationRedact;
44class PdfAnnotationProjection;
45
47struct PODOFO_API PdfAppearanceStream final
48{
49 const PdfObject* Object = nullptr;
50 PdfAppearanceType Type = PdfAppearanceType::Normal;
51 PdfName State;
52};
53
58class PODOFO_API PdfAnnotation : public PdfDictionaryElement
59{
60 friend class PdfAnnotationCollection;
61 friend class PdfAnnotationTextMarkupBase;
62 friend class PdfAnnotationPopup;
63 friend class PdfAnnotationText;
64 friend class PdfAnnotationCaret;
65 friend class PdfAnnotationFileAttachment;
66 friend class PdfAnnotationFreeText;
67 friend class PdfAnnotationHighlight;
68 friend class PdfAnnotationInk;
69 friend class PdfAnnotationLine;
70 friend class PdfAnnotationModel3D;
71 friend class PdfAnnotationMovie;
72 friend class PdfAnnotationPolygon;
73 friend class PdfAnnotationPolyLine;
74 friend class PdfAnnotationPrinterMark;
75 friend class PdfAnnotationRichMedia;
76 friend class PdfAnnotationScreen;
77 friend class PdfAnnotationSquiggly;
78 friend class PdfAnnotationStrikeOut;
79 friend class PdfAnnotationSound;
80 friend class PdfAnnotationSquare;
81 friend class PdfAnnotationCircle;
82 friend class PdfAnnotationStamp;
83 friend class PdfAnnotationTrapNet;
84 friend class PdfAnnotationUnderline;
85 friend class PdfAnnotationWatermark;
86 friend class PdfAnnotationWebMedia;
87 friend class PdfAnnotationRedact;
88 friend class PdfAnnotationProjection;
89 friend class PdfAnnotationActionBase;
90 friend class PdfToggleButton;
91
92private:
95 PdfAnnotation(const PdfAnnotation&) = delete;
96
97public:
98 static bool TryCreateFromObject(PdfObject& obj, std::unique_ptr<PdfAnnotation>& xobj);
99
100 static bool TryCreateFromObject(const PdfObject& obj, std::unique_ptr<const PdfAnnotation>& xobj);
101
102 template <typename TAnnotation>
103 static bool TryCreateFromObject(PdfObject& obj, std::unique_ptr<TAnnotation>& xobj);
104
105 template <typename TAnnotation>
106 static bool TryCreateFromObject(const PdfObject& obj, std::unique_ptr<const TAnnotation>& xobj);
107
108public:
115 void SetAppearanceStream(const PdfXObject& xobj, PdfAppearanceType appearance = PdfAppearanceType::Normal,
116 const PdfName& state = { }, bool skipSelectedState = false);
117
124 void SetAppearanceStreamRaw(const PdfXObject& xobj, PdfAppearanceType appearance = PdfAppearanceType::Normal,
125 const PdfName& state = { }, bool skipSelectedState = false);
126
129 void GetAppearanceStreams(std::vector<PdfAppearanceStream>& states) const;
130
131 void ClearAppearances();
132
134 PdfObject* GetAppearanceDictionaryObject();
135 const PdfObject* GetAppearanceDictionaryObject() const;
136
140 PdfObject* GetAppearanceStream(PdfAppearanceType appearance = PdfAppearanceType::Normal, const std::string_view& state = { });
141 const PdfObject* GetAppearanceStream(PdfAppearanceType appearance = PdfAppearanceType::Normal, const std::string_view& state = { }) const;
142
145 Rect GetRect() const;
146
149 void SetRect(const Rect& rect);
150
151 Corners GetRectRaw() const;
152
153 void SetRectRaw(const Corners& rect);
154
157 void SetFlags(PdfAnnotationFlags flags);
158
164 PdfAnnotationFlags GetFlags() const;
165
170 void SetBorderStyle(double hCorner, double vCorner, double width);
171
177 void SetBorderStyle(double hCorner, double vCorner, double width, const PdfArray& strokeStyle);
178
183 void SetTitle(nullable<const PdfString&> title);
184
190 nullable<const PdfString&> GetTitle() const;
191
197 void SetContents(nullable<const PdfString&> contents);
198
204 nullable<const PdfString&> GetContents() const;
205
211 PdfColor GetColor() const;
212
216 void SetColor(nullable<const PdfColor&> color);
217
218public:
221 inline PdfAnnotationType GetType() const { return m_AnnotationType; }
222
226 inline PdfPage* GetPage() { return m_Page; }
227 inline const PdfPage* GetPage() const { return m_Page; }
228 PdfPage& MustGetPage();
229 const PdfPage& MustGetPage() const;
230
231private:
232 template <typename TAnnot>
233 static constexpr PdfAnnotationType GetAnnotationType();
234
235 static std::unique_ptr<PdfAnnotation> Create(PdfPage& page, PdfAnnotationType annotType, const Rect& rect);
236
237 void SetPage(PdfPage& page) { m_Page = &page; }
238
239 void PushAppearanceStream(const PdfXObject& xobj, PdfAppearanceType appearance, const PdfName& state, bool raw);
240
241private:
242 static bool tryCreateFromObject(const PdfObject& obj, PdfAnnotationType targetType, PdfAnnotation*& xobj);
243 static PdfAnnotationType getAnnotationType(const PdfObject& obj);
244 PdfObject* getAppearanceStream(PdfAppearanceType appearance, const std::string_view& state) const;
245 PdfDictionary* getAppearanceDictionary() const;
246
247private:
248 PdfAnnotationType m_AnnotationType;
249 PdfPage* m_Page;
250};
251
252template<typename TAnnotation>
253bool PdfAnnotation::TryCreateFromObject(PdfObject& obj, std::unique_ptr<TAnnotation>& xobj)
254{
255 PdfAnnotation* xobj_;
256 if (!tryCreateFromObject(obj, GetAnnotationType<TAnnotation>(), xobj_))
257 return false;
258
259 xobj.reset((TAnnotation*)xobj_);
260 return true;
261}
262
263template<typename TAnnotation>
264bool PdfAnnotation::TryCreateFromObject(const PdfObject& obj, std::unique_ptr<const TAnnotation>& xobj)
265{
266 PdfAnnotation* xobj_;
267 if (!tryCreateFromObject(obj, GetAnnotationType<TAnnotation>(), xobj_))
268 return false;
269
270 xobj.reset((const TAnnotation*)xobj_);
271 return true;
272}
273
274template<typename TAnnot>
275constexpr PdfAnnotationType PdfAnnotation::GetAnnotationType()
276{
277 if (std::is_same_v<TAnnot, PdfAnnotationText>)
278 return PdfAnnotationType::Text;
279 else if (std::is_same_v<TAnnot, PdfAnnotationLink>)
280 return PdfAnnotationType::Link;
281 else if (std::is_same_v<TAnnot, PdfAnnotationFreeText>)
282 return PdfAnnotationType::FreeText;
283 else if (std::is_same_v<TAnnot, PdfAnnotationLine>)
284 return PdfAnnotationType::Line;
285 else if (std::is_same_v<TAnnot, PdfAnnotationSquare>)
286 return PdfAnnotationType::Square;
287 else if (std::is_same_v<TAnnot, PdfAnnotationCircle>)
288 return PdfAnnotationType::Circle;
289 else if (std::is_same_v<TAnnot, PdfAnnotationPolygon>)
290 return PdfAnnotationType::Polygon;
291 else if (std::is_same_v<TAnnot, PdfAnnotationPolyLine>)
292 return PdfAnnotationType::PolyLine;
293 else if (std::is_same_v<TAnnot, PdfAnnotationHighlight>)
294 return PdfAnnotationType::Highlight;
295 else if (std::is_same_v<TAnnot, PdfAnnotationUnderline>)
296 return PdfAnnotationType::Underline;
297 else if (std::is_same_v<TAnnot, PdfAnnotationSquiggly>)
298 return PdfAnnotationType::Squiggly;
299 else if (std::is_same_v<TAnnot, PdfAnnotationStrikeOut>)
300 return PdfAnnotationType::StrikeOut;
301 else if (std::is_same_v<TAnnot, PdfAnnotationStamp>)
302 return PdfAnnotationType::Stamp;
303 else if (std::is_same_v<TAnnot, PdfAnnotationCaret>)
304 return PdfAnnotationType::Caret;
305 else if (std::is_same_v<TAnnot, PdfAnnotationInk>)
306 return PdfAnnotationType::Ink;
307 else if (std::is_same_v<TAnnot, PdfAnnotationPopup>)
308 return PdfAnnotationType::Popup;
309 else if (std::is_same_v<TAnnot, PdfAnnotationFileAttachment>)
310 return PdfAnnotationType::FileAttachement;
311 else if (std::is_same_v<TAnnot, PdfAnnotationSound>)
312 return PdfAnnotationType::Sound;
313 else if (std::is_same_v<TAnnot, PdfAnnotationMovie>)
314 return PdfAnnotationType::Movie;
315 else if (std::is_same_v<TAnnot, PdfAnnotationWidget>)
316 return PdfAnnotationType::Widget;
317 else if (std::is_same_v<TAnnot, PdfAnnotationScreen>)
318 return PdfAnnotationType::Screen;
319 else if (std::is_same_v<TAnnot, PdfAnnotationPrinterMark>)
320 return PdfAnnotationType::PrinterMark;
321 else if (std::is_same_v<TAnnot, PdfAnnotationTrapNet>)
322 return PdfAnnotationType::TrapNet;
323 else if (std::is_same_v<TAnnot, PdfAnnotationWatermark>)
324 return PdfAnnotationType::Watermark;
325 else if (std::is_same_v<TAnnot, PdfAnnotationModel3D>)
326 return PdfAnnotationType::Model3D;
327 else if (std::is_same_v<TAnnot, PdfAnnotationRichMedia>)
328 return PdfAnnotationType::RichMedia;
329 else if (std::is_same_v<TAnnot, PdfAnnotationWebMedia>)
330 return PdfAnnotationType::WebMedia;
331 else if (std::is_same_v<TAnnot, PdfAnnotationRedact>)
332 return PdfAnnotationType::Redact;
333 else if (std::is_same_v<TAnnot, PdfAnnotationProjection>)
334 return PdfAnnotationType::Projection;
335 else
336 return PdfAnnotationType::Unknown;
337}
338
339};
340
341#endif // PDF_ANNOTATION_H
An annotation to a PdfPage To create an annotation use PdfPage::CreateAnnotation.
Definition PdfAnnotation.h:59
PdfAnnotationType GetType() const
Get the type of this annotation.
Definition PdfAnnotation.h:221
PdfObject * GetAppearanceStream(PdfAppearanceType appearance=PdfAppearanceType::Normal, const std::string_view &state={ })
PdfPage * GetPage()
Get the page of this PdfField.
Definition PdfAnnotation.h:226
This class represents a PdfName.
Definition PdfName.h:21
This class represents a PDF indirect Object in memory.
Definition PdfObject.h:31
PdfPage is one page in the pdf document.
Definition PdfPage.h:133
A XObject is a content stream with several drawing commands and data which can be used throughout a P...
Definition PdfXObject.h:28
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
PdfAnnotationType
The type of the annotation.
Definition PdfDeclarations.h:557
PdfAppearanceType
Type of the annotation appearance.
Definition PdfDeclarations.h:646
A qualified appearance stream, with type and state name.
Definition PdfAnnotation.h:48