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
55{
56 None = 0,
57 Raw = 1 << 0,
58 SkipSelectedState = 1 << 1,
59 InplaceRotation = 1 << 2,
61};
62
67class PODOFO_API PdfAnnotation : public PdfDictionaryElement
68{
69 friend class PdfAnnotationCollection;
70 friend class PdfAnnotationTextMarkupBase;
71 friend class PdfAnnotationPopup;
72 friend class PdfAnnotationText;
73 friend class PdfAnnotationCaret;
74 friend class PdfAnnotationFileAttachment;
75 friend class PdfAnnotationFreeText;
76 friend class PdfAnnotationHighlight;
77 friend class PdfAnnotationInk;
78 friend class PdfAnnotationLine;
79 friend class PdfAnnotationModel3D;
80 friend class PdfAnnotationMovie;
81 friend class PdfAnnotationPolygon;
82 friend class PdfAnnotationPolyLine;
83 friend class PdfAnnotationPrinterMark;
84 friend class PdfAnnotationRichMedia;
85 friend class PdfAnnotationScreen;
86 friend class PdfAnnotationSquiggly;
87 friend class PdfAnnotationStrikeOut;
88 friend class PdfAnnotationSound;
89 friend class PdfAnnotationSquare;
90 friend class PdfAnnotationCircle;
91 friend class PdfAnnotationStamp;
92 friend class PdfAnnotationTrapNet;
93 friend class PdfAnnotationUnderline;
94 friend class PdfAnnotationWatermark;
95 friend class PdfAnnotationWebMedia;
96 friend class PdfAnnotationRedact;
97 friend class PdfAnnotationProjection;
98 friend class PdfAnnotationActionBase;
99 friend class PdfToggleButton;
100
101private:
104 PdfAnnotation(const PdfAnnotation&) = delete;
105
106public:
107 static bool TryCreateFromObject(PdfObject& obj, std::unique_ptr<PdfAnnotation>& xobj);
108
109 static bool TryCreateFromObject(const PdfObject& obj, std::unique_ptr<const PdfAnnotation>& xobj);
110
111 template <typename TAnnotation>
112 static bool TryCreateFromObject(PdfObject& obj, std::unique_ptr<TAnnotation>& xobj);
113
114 template <typename TAnnotation>
115 static bool TryCreateFromObject(const PdfObject& obj, std::unique_ptr<const TAnnotation>& xobj);
116
117public:
124 void SetAppearanceStream(const PdfXObject& xobj, PdfSetAppearanceFlags flags = PdfSetAppearanceFlags::None,
125 PdfAppearanceType appearance = PdfAppearanceType::Normal, const PdfName& state = { });
126 void SetAppearanceStream(PdfXObject& xobj, PdfSetAppearanceFlags flags,
127 PdfAppearanceType appearance = PdfAppearanceType::Normal, const PdfName& state = { });
128 void SetAppearanceStream(const PdfXObject& xobj, PdfAppearanceType appearance, const PdfName& state = { });
129
136 [[deprecated("Use the SetAppearanceStream methods with flags instead")]]
137 void SetAppearanceStream(const PdfXObject& xobj, PdfAppearanceType appearance,
138 const PdfName& state, bool skipSelectedState);
139
146 [[deprecated("Use the SetAppearanceStream methods with flags instead")]]
147 void SetAppearanceStreamRaw(const PdfXObject& xobj, PdfAppearanceType appearance = PdfAppearanceType::Normal,
148 const PdfName& state = { }, bool skipSelectedState = false);
149
152 void GetAppearanceStreams(std::vector<PdfAppearanceStream>& states) const;
153
154 void ClearAppearances();
155
157 PdfObject* GetAppearanceDictionaryObject();
158 const PdfObject* GetAppearanceDictionaryObject() const;
159
163 PdfObject* GetAppearanceStream(PdfAppearanceType appearance = PdfAppearanceType::Normal, const std::string_view& state = { });
164 const PdfObject* GetAppearanceStream(PdfAppearanceType appearance = PdfAppearanceType::Normal, const std::string_view& state = { }) const;
165
168 Rect GetRect() const;
169
172 void SetRect(const Rect& rect);
173
174 Corners GetRectRaw() const;
175
176 void SetRectRaw(const Corners& rect);
177
180 void SetFlags(PdfAnnotationFlags flags);
181
187 PdfAnnotationFlags GetFlags() const;
188
193 void SetBorderStyle(double hCorner, double vCorner, double width);
194
200 void SetBorderStyle(double hCorner, double vCorner, double width, const PdfArray& strokeStyle);
201
206 void SetTitle(nullable<const PdfString&> title);
207
213 nullable<const PdfString&> GetTitle() const;
214
220 void SetContents(nullable<const PdfString&> contents);
221
227 nullable<const PdfString&> GetContents() const;
228
234 PdfColor GetColor() const;
235
239 void SetColor(nullable<const PdfColor&> color);
240
241public:
244 inline PdfAnnotationType GetType() const { return m_AnnotationType; }
245
249 inline PdfPage* GetPage() { return m_Page; }
250 inline const PdfPage* GetPage() const { return m_Page; }
251 PdfPage& MustGetPage();
252 const PdfPage& MustGetPage() const;
253
254private:
255 template <typename TAnnot>
256 static constexpr PdfAnnotationType GetAnnotationType();
257
258 static std::unique_ptr<PdfAnnotation> Create(PdfPage& page, PdfAnnotationType annotType, const Rect& rect);
259
260 void SetPage(PdfPage& page) { m_Page = &page; }
261
262private:
263 void setAppearanceStream(PdfXObject& xobj, PdfSetAppearanceFlags flags, PdfAppearanceType appearance, const PdfName& state);
264 static bool tryCreateFromObject(const PdfObject& obj, PdfAnnotationType targetType, PdfAnnotation*& xobj);
265 static PdfAnnotationType getAnnotationType(const PdfObject& obj);
266 PdfObject* getAppearanceStream(PdfAppearanceType appearance, const std::string_view& state) const;
267 PdfDictionary* getAppearanceDictionary() const;
268
269private:
270 PdfAnnotationType m_AnnotationType;
271 PdfPage* m_Page;
272};
273
274template<typename TAnnotation>
275bool PdfAnnotation::TryCreateFromObject(PdfObject& obj, std::unique_ptr<TAnnotation>& xobj)
276{
277 PdfAnnotation* xobj_;
278 if (!tryCreateFromObject(obj, GetAnnotationType<TAnnotation>(), xobj_))
279 return false;
280
281 // NOTE: A class GetAnnotationType() maps to Unknown accepts any annotation, so the class created is checked
282 std::unique_ptr<PdfAnnotation> created(xobj_);
283 if (dynamic_cast<TAnnotation*>(created.get()) == nullptr)
284 return false;
285
286 xobj.reset(static_cast<TAnnotation*>(created.release()));
287 return true;
288}
289
290template<typename TAnnotation>
291bool PdfAnnotation::TryCreateFromObject(const PdfObject& obj, std::unique_ptr<const TAnnotation>& xobj)
292{
293 PdfAnnotation* xobj_;
294 if (!tryCreateFromObject(obj, GetAnnotationType<TAnnotation>(), xobj_))
295 return false;
296
297 std::unique_ptr<const PdfAnnotation> created(xobj_);
298 if (dynamic_cast<const TAnnotation*>(created.get()) == nullptr)
299 return false;
300
301 xobj.reset(static_cast<const TAnnotation*>(created.release()));
302 return true;
303}
304
305template<typename TAnnot>
306constexpr PdfAnnotationType PdfAnnotation::GetAnnotationType()
307{
308 if (std::is_same_v<TAnnot, PdfAnnotationText>)
309 return PdfAnnotationType::Text;
310 else if (std::is_same_v<TAnnot, PdfAnnotationLink>)
311 return PdfAnnotationType::Link;
312 else if (std::is_same_v<TAnnot, PdfAnnotationFreeText>)
313 return PdfAnnotationType::FreeText;
314 else if (std::is_same_v<TAnnot, PdfAnnotationLine>)
315 return PdfAnnotationType::Line;
316 else if (std::is_same_v<TAnnot, PdfAnnotationSquare>)
317 return PdfAnnotationType::Square;
318 else if (std::is_same_v<TAnnot, PdfAnnotationCircle>)
319 return PdfAnnotationType::Circle;
320 else if (std::is_same_v<TAnnot, PdfAnnotationPolygon>)
321 return PdfAnnotationType::Polygon;
322 else if (std::is_same_v<TAnnot, PdfAnnotationPolyLine>)
323 return PdfAnnotationType::PolyLine;
324 else if (std::is_same_v<TAnnot, PdfAnnotationHighlight>)
325 return PdfAnnotationType::Highlight;
326 else if (std::is_same_v<TAnnot, PdfAnnotationUnderline>)
327 return PdfAnnotationType::Underline;
328 else if (std::is_same_v<TAnnot, PdfAnnotationSquiggly>)
329 return PdfAnnotationType::Squiggly;
330 else if (std::is_same_v<TAnnot, PdfAnnotationStrikeOut>)
331 return PdfAnnotationType::StrikeOut;
332 else if (std::is_same_v<TAnnot, PdfAnnotationStamp>)
333 return PdfAnnotationType::Stamp;
334 else if (std::is_same_v<TAnnot, PdfAnnotationCaret>)
335 return PdfAnnotationType::Caret;
336 else if (std::is_same_v<TAnnot, PdfAnnotationInk>)
337 return PdfAnnotationType::Ink;
338 else if (std::is_same_v<TAnnot, PdfAnnotationPopup>)
339 return PdfAnnotationType::Popup;
340 else if (std::is_same_v<TAnnot, PdfAnnotationFileAttachment>)
341 return PdfAnnotationType::FileAttachement;
342 else if (std::is_same_v<TAnnot, PdfAnnotationSound>)
343 return PdfAnnotationType::Sound;
344 else if (std::is_same_v<TAnnot, PdfAnnotationMovie>)
345 return PdfAnnotationType::Movie;
346 else if (std::is_same_v<TAnnot, PdfAnnotationWidget>)
347 return PdfAnnotationType::Widget;
348 else if (std::is_same_v<TAnnot, PdfAnnotationScreen>)
349 return PdfAnnotationType::Screen;
350 else if (std::is_same_v<TAnnot, PdfAnnotationPrinterMark>)
351 return PdfAnnotationType::PrinterMark;
352 else if (std::is_same_v<TAnnot, PdfAnnotationTrapNet>)
353 return PdfAnnotationType::TrapNet;
354 else if (std::is_same_v<TAnnot, PdfAnnotationWatermark>)
355 return PdfAnnotationType::Watermark;
356 else if (std::is_same_v<TAnnot, PdfAnnotationModel3D>)
357 return PdfAnnotationType::Model3D;
358 else if (std::is_same_v<TAnnot, PdfAnnotationRichMedia>)
359 return PdfAnnotationType::RichMedia;
360 else if (std::is_same_v<TAnnot, PdfAnnotationWebMedia>)
361 return PdfAnnotationType::WebMedia;
362 else if (std::is_same_v<TAnnot, PdfAnnotationRedact>)
363 return PdfAnnotationType::Redact;
364 else if (std::is_same_v<TAnnot, PdfAnnotationProjection>)
365 return PdfAnnotationType::Projection;
366 else
367 return PdfAnnotationType::Unknown;
368}
369
370};
371
372ENABLE_BITMASK_OPERATORS(PoDoFo::PdfSetAppearanceFlags);
373
374#endif // PDF_ANNOTATION_H
An annotation to a PdfPage To create an annotation use PdfPage::CreateAnnotation.
Definition PdfAnnotation.h:68
PdfAnnotationType GetType() const
Get the type of this annotation.
Definition PdfAnnotation.h:244
PdfObject * GetAppearanceStream(PdfAppearanceType appearance=PdfAppearanceType::Normal, const std::string_view &state={ })
PdfPage * GetPage()
Get the page of this PdfField.
Definition PdfAnnotation.h:249
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:566
PdfSetAppearanceFlags
Definition PdfAnnotation.h:55
@ Raw
Insert the appearance stream without handling page rotations.
@ SkipSelectedState
Skip setting the selected state.
@ InplaceRotation
Try skip inserting a XObject trampoline for the appearance stream in case of page rotations.
@ None
Do not add a default appearance.
PdfAppearanceType
Type of the annotation appearance.
Definition PdfDeclarations.h:655
A qualified appearance stream, with type and state name.
Definition PdfAnnotation.h:48