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 xobj.reset((TAnnotation*)xobj_);
282 return true;
283}
284
285template<typename TAnnotation>
286bool PdfAnnotation::TryCreateFromObject(const PdfObject& obj, std::unique_ptr<const TAnnotation>& xobj)
287{
288 PdfAnnotation* xobj_;
289 if (!tryCreateFromObject(obj, GetAnnotationType<TAnnotation>(), xobj_))
290 return false;
291
292 xobj.reset((const TAnnotation*)xobj_);
293 return true;
294}
295
296template<typename TAnnot>
297constexpr PdfAnnotationType PdfAnnotation::GetAnnotationType()
298{
299 if (std::is_same_v<TAnnot, PdfAnnotationText>)
300 return PdfAnnotationType::Text;
301 else if (std::is_same_v<TAnnot, PdfAnnotationLink>)
302 return PdfAnnotationType::Link;
303 else if (std::is_same_v<TAnnot, PdfAnnotationFreeText>)
304 return PdfAnnotationType::FreeText;
305 else if (std::is_same_v<TAnnot, PdfAnnotationLine>)
306 return PdfAnnotationType::Line;
307 else if (std::is_same_v<TAnnot, PdfAnnotationSquare>)
308 return PdfAnnotationType::Square;
309 else if (std::is_same_v<TAnnot, PdfAnnotationCircle>)
310 return PdfAnnotationType::Circle;
311 else if (std::is_same_v<TAnnot, PdfAnnotationPolygon>)
312 return PdfAnnotationType::Polygon;
313 else if (std::is_same_v<TAnnot, PdfAnnotationPolyLine>)
314 return PdfAnnotationType::PolyLine;
315 else if (std::is_same_v<TAnnot, PdfAnnotationHighlight>)
316 return PdfAnnotationType::Highlight;
317 else if (std::is_same_v<TAnnot, PdfAnnotationUnderline>)
318 return PdfAnnotationType::Underline;
319 else if (std::is_same_v<TAnnot, PdfAnnotationSquiggly>)
320 return PdfAnnotationType::Squiggly;
321 else if (std::is_same_v<TAnnot, PdfAnnotationStrikeOut>)
322 return PdfAnnotationType::StrikeOut;
323 else if (std::is_same_v<TAnnot, PdfAnnotationStamp>)
324 return PdfAnnotationType::Stamp;
325 else if (std::is_same_v<TAnnot, PdfAnnotationCaret>)
326 return PdfAnnotationType::Caret;
327 else if (std::is_same_v<TAnnot, PdfAnnotationInk>)
328 return PdfAnnotationType::Ink;
329 else if (std::is_same_v<TAnnot, PdfAnnotationPopup>)
330 return PdfAnnotationType::Popup;
331 else if (std::is_same_v<TAnnot, PdfAnnotationFileAttachment>)
332 return PdfAnnotationType::FileAttachement;
333 else if (std::is_same_v<TAnnot, PdfAnnotationSound>)
334 return PdfAnnotationType::Sound;
335 else if (std::is_same_v<TAnnot, PdfAnnotationMovie>)
336 return PdfAnnotationType::Movie;
337 else if (std::is_same_v<TAnnot, PdfAnnotationWidget>)
338 return PdfAnnotationType::Widget;
339 else if (std::is_same_v<TAnnot, PdfAnnotationScreen>)
340 return PdfAnnotationType::Screen;
341 else if (std::is_same_v<TAnnot, PdfAnnotationPrinterMark>)
342 return PdfAnnotationType::PrinterMark;
343 else if (std::is_same_v<TAnnot, PdfAnnotationTrapNet>)
344 return PdfAnnotationType::TrapNet;
345 else if (std::is_same_v<TAnnot, PdfAnnotationWatermark>)
346 return PdfAnnotationType::Watermark;
347 else if (std::is_same_v<TAnnot, PdfAnnotationModel3D>)
348 return PdfAnnotationType::Model3D;
349 else if (std::is_same_v<TAnnot, PdfAnnotationRichMedia>)
350 return PdfAnnotationType::RichMedia;
351 else if (std::is_same_v<TAnnot, PdfAnnotationWebMedia>)
352 return PdfAnnotationType::WebMedia;
353 else if (std::is_same_v<TAnnot, PdfAnnotationRedact>)
354 return PdfAnnotationType::Redact;
355 else if (std::is_same_v<TAnnot, PdfAnnotationProjection>)
356 return PdfAnnotationType::Projection;
357 else
358 return PdfAnnotationType::Unknown;
359}
360
361};
362
363ENABLE_BITMASK_OPERATORS(PoDoFo::PdfSetAppearanceFlags);
364
365#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:557
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:646
A qualified appearance stream, with type and state name.
Definition PdfAnnotation.h:48