PoDoFo 1.0.0-dev
Loading...
Searching...
No Matches
PdfAnnotation.h
1
7#ifndef PDF_ANNOTATION_H
8#define PDF_ANNOTATION_H
9
10#include "PdfElement.h"
11#include <podofo/auxiliary/Rect.h>
12#include "PdfColor.h"
13
14namespace PoDoFo {
15
16class PdfPage;
17class PdfXObject;
18class PdfAnnotationText;
19class PdfAnnotationLink;
20class PdfAnnotationFreeText;
21class PdfAnnotationLine;
22class PdfAnnotationSquare;
23class PdfAnnotationCircle;
24class PdfAnnotationPolygon;
25class PdfAnnotationPolyLine;
26class PdfAnnotationHighlight;
27class PdfAnnotationUnderline;
28class PdfAnnotationSquiggly;
29class PdfAnnotationStrikeOut;
30class PdfAnnotationStamp;
31class PdfAnnotationCaret;
32class PdfAnnotationInk;
33class PdfAnnotationPopup;
34class PdfAnnotationFileAttachment;
35class PdfAnnotationSound;
36class PdfAnnotationMovie;
37class PdfAnnotationWidget;
38class PdfAnnotationScreen;
39class PdfAnnotationPrinterMark;
40class PdfAnnotationTrapNet;
41class PdfAnnotationWatermark;
42class PdfAnnotationModel3D;
43class PdfAnnotationRichMedia;
44class PdfAnnotationWebMedia;
45class PdfAnnotationRedact;
46class PdfAnnotationProjection;
47
50struct PODOFO_API PdfAppearanceStream final
51{
52 const PdfObject* Object = nullptr;
53 PdfAppearanceType Type = PdfAppearanceType::Normal;
54 PdfName State;
55};
56
62class PODOFO_API PdfAnnotation : public PdfDictionaryElement
63{
64 friend class PdfAnnotationCollection;
65 friend class PdfAnnotationTextMarkupBase;
66 friend class PdfAnnotationPopup;
67 friend class PdfAnnotationText;
68 friend class PdfAnnotationCaret;
69 friend class PdfAnnotationFileAttachment;
70 friend class PdfAnnotationFreeText;
71 friend class PdfAnnotationHighlight;
72 friend class PdfAnnotationInk;
73 friend class PdfAnnotationLine;
74 friend class PdfAnnotationModel3D;
75 friend class PdfAnnotationMovie;
76 friend class PdfAnnotationPolygon;
77 friend class PdfAnnotationPolyLine;
78 friend class PdfAnnotationPrinterMark;
79 friend class PdfAnnotationRichMedia;
80 friend class PdfAnnotationScreen;
81 friend class PdfAnnotationSquiggly;
82 friend class PdfAnnotationStrikeOut;
83 friend class PdfAnnotationSound;
84 friend class PdfAnnotationSquare;
85 friend class PdfAnnotationCircle;
86 friend class PdfAnnotationStamp;
87 friend class PdfAnnotationTrapNet;
88 friend class PdfAnnotationUnderline;
89 friend class PdfAnnotationWatermark;
90 friend class PdfAnnotationWebMedia;
91 friend class PdfAnnotationRedact;
92 friend class PdfAnnotationProjection;
93 friend class PdfAnnotationActionBase;
94 friend class PdfToggleButton;
95
96private:
99 PdfAnnotation(const PdfAnnotation&) = delete;
100
101public:
102 static bool TryCreateFromObject(PdfObject& obj, std::unique_ptr<PdfAnnotation>& xobj);
103
104 static bool TryCreateFromObject(const PdfObject& obj, std::unique_ptr<const PdfAnnotation>& xobj);
105
106 template <typename TAnnotation>
107 static bool TryCreateFromObject(PdfObject& obj, std::unique_ptr<TAnnotation>& xobj);
108
109 template <typename TAnnotation>
110 static bool TryCreateFromObject(const PdfObject& obj, std::unique_ptr<const TAnnotation>& xobj);
111
112public:
120 void SetAppearanceStream(const PdfXObject& xobj, PdfAppearanceType appearance = PdfAppearanceType::Normal,
121 const PdfName& state = { }, bool skipSelectedState = false);
122
130 void SetAppearanceStreamRaw(const PdfXObject& xobj, PdfAppearanceType appearance = PdfAppearanceType::Normal,
131 const PdfName& state = { }, bool skipSelectedState = false);
132
135 void GetAppearanceStreams(std::vector<PdfAppearanceStream>& states) const;
136
137 void ClearAppearances();
138
142 PdfObject* GetAppearanceDictionaryObject();
143 const PdfObject* GetAppearanceDictionaryObject() const;
144
150 PdfObject* GetAppearanceStream(PdfAppearanceType appearance = PdfAppearanceType::Normal, const std::string_view& state = { });
151 const PdfObject* GetAppearanceStream(PdfAppearanceType appearance = PdfAppearanceType::Normal, const std::string_view& state = { }) const;
152
156 Rect GetRect() const;
157
161 void SetRect(const Rect& rect);
162
163 Corners GetRectRaw() const;
164
165 void SetRectRaw(const Corners& rect);
166
170 void SetFlags(PdfAnnotationFlags flags);
171
178 PdfAnnotationFlags GetFlags() const;
179
185 void SetBorderStyle(double hCorner, double vCorner, double width);
186
193 void SetBorderStyle(double hCorner, double vCorner, double width, const PdfArray& strokeStyle);
194
200 void SetTitle(nullable<const PdfString&> title);
201
208 nullable<const PdfString&> GetTitle() const;
209
216 void SetContents(nullable<const PdfString&> contents);
217
224 nullable<const PdfString&> GetContents() const;
225
231 PdfColor GetColor() const;
232
236 void SetColor(nullable<const PdfColor&> color);
237
238public:
242 inline PdfAnnotationType GetType() const { return m_AnnotationType; }
243
248 inline PdfPage* GetPage() { return m_Page; }
249 inline const PdfPage* GetPage() const { return m_Page; }
250 PdfPage& MustGetPage();
251 const PdfPage& MustGetPage() const;
252
253private:
254 template <typename TAnnot>
255 static constexpr PdfAnnotationType GetAnnotationType();
256
257 static std::unique_ptr<PdfAnnotation> Create(PdfPage& page, PdfAnnotationType annotType, const Rect& rect);
258
259 void SetPage(PdfPage& page) { m_Page = &page; }
260
261 void PushAppearanceStream(const PdfXObject& xobj, PdfAppearanceType appearance, const PdfName& state, bool raw);
262
263private:
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
363#endif // PDF_ANNOTATION_H
An annotation to a PdfPage To create an annotation use PdfPage::CreateAnnotation.
Definition PdfAnnotation.h:63
PdfAnnotationType GetType() const
Get the type of this annotation.
Definition PdfAnnotation.h:242
PdfObject * GetAppearanceStream(PdfAppearanceType appearance=PdfAppearanceType::Normal, const std::string_view &state={ })
PdfPage * GetPage()
Get the page of this PdfField.
Definition PdfAnnotation.h:248
This class represents a PdfName.
Definition PdfName.h:24
This class represents a PDF indirect Object in memory.
Definition PdfObject.h:35
PdfPage is one page in the pdf document.
Definition PdfPage.h:129
A XObject is a content stream with several drawing commands and data which can be used throughout a P...
Definition PdfXObject.h:31
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
PdfAnnotationType
The type of the annotation.
Definition PdfDeclarations.h:595
PdfAppearanceType
Type of the annotation appearance.
Definition PdfDeclarations.h:689
Templatized object type getter helper.
Definition PdfObject.h:609
A qualified appearance stream, with type and state name.
Definition PdfAnnotation.h:51