PoDoFo  1.0.0-dev
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 
14 namespace PoDoFo {
15 
16 class PdfPage;
17 class PdfXObjectForm;
18 class PdfAnnotationText;
19 class PdfAnnotationLink;
20 class PdfAnnotationFreeText;
21 class PdfAnnotationLine;
22 class PdfAnnotationSquare;
23 class PdfAnnotationCircle;
24 class PdfAnnotationPolygon;
25 class PdfAnnotationPolyLine;
26 class PdfAnnotationHighlight;
27 class PdfAnnotationUnderline;
28 class PdfAnnotationSquiggly;
29 class PdfAnnotationStrikeOut;
30 class PdfAnnotationStamp;
31 class PdfAnnotationCaret;
32 class PdfAnnotationInk;
33 class PdfAnnotationPopup;
34 class PdfAnnotationFileAttachment;
35 class PdfAnnotationSound;
36 class PdfAnnotationMovie;
37 class PdfAnnotationWidget;
38 class PdfAnnotationScreen;
39 class PdfAnnotationPrinterMark;
40 class PdfAnnotationTrapNet;
41 class PdfAnnotationWatermark;
42 class PdfAnnotationModel3D;
43 class PdfAnnotationRichMedia;
44 class PdfAnnotationWebMedia;
45 class PdfAnnotationRedact;
46 class PdfAnnotationProjection;
47 
50 struct PODOFO_API PdfAppearanceStream final
51 {
52  const PdfObject* Object = nullptr;
54  PdfName State;
55 };
56 
62 class 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 
95 private:
96  PdfAnnotation(PdfPage& page, PdfAnnotationType annotType, const Rect& rect);
97  PdfAnnotation(PdfObject& obj, PdfAnnotationType annotType);
98 
99 public:
100  static bool TryCreateFromObject(PdfObject& obj, std::unique_ptr<PdfAnnotation>& xobj);
101 
102  static bool TryCreateFromObject(const PdfObject& obj, std::unique_ptr<const PdfAnnotation>& xobj);
103 
104  template <typename TAnnotation>
105  static bool TryCreateFromObject(PdfObject& obj, std::unique_ptr<TAnnotation>& xobj);
106 
107  template <typename TAnnotation>
108  static bool TryCreateFromObject(const PdfObject& obj, std::unique_ptr<const TAnnotation>& xobj);
109 
110 public:
117  void SetAppearanceStream(const PdfXObjectForm& xobj, PdfAppearanceType appearance = PdfAppearanceType::Normal, const PdfName& state = "");
118 
125  void SetAppearanceStreamRaw(const PdfXObjectForm& xobj, PdfAppearanceType appearance = PdfAppearanceType::Normal, const PdfName& state = "");
126 
129  void GetAppearanceStreams(std::vector<PdfAppearanceStream>& states) const;
130 
131  void ClearAppearances();
132 
136  PdfObject* GetAppearanceDictionaryObject();
137  const PdfObject* GetAppearanceDictionaryObject() const;
138 
144  PdfObject* GetAppearanceStream(PdfAppearanceType appearance = PdfAppearanceType::Normal, const std::string_view& state = { });
145  const PdfObject* GetAppearanceStream(PdfAppearanceType appearance = PdfAppearanceType::Normal, const std::string_view& state = { }) const;
146 
150  Rect GetRect() const;
151  Rect GetRectRaw() const;
152 
156  void SetRect(const Rect& rect);
157  void SetRectRaw(const Rect& rect);
158 
162  void SetFlags(PdfAnnotationFlags flags);
163 
170  PdfAnnotationFlags GetFlags() const;
171 
177  void SetBorderStyle(double hCorner, double vCorner, double width);
178 
185  void SetBorderStyle(double hCorner, double vCorner, double width, const PdfArray& strokeStyle);
186 
192  void SetTitle(nullable<const PdfString&> title);
193 
200  nullable<const PdfString&> GetTitle() const;
201 
208  void SetContents(nullable<const PdfString&> contents);
209 
216  nullable<const PdfString&> GetContents() const;
217 
223  PdfColor GetColor() const;
224 
228  void SetColor(nullable<const PdfColor&> color);
229 
230 public:
234  inline PdfAnnotationType GetType() const { return m_AnnotationType; }
235 
240  inline PdfPage* GetPage() { return m_Page; }
241  inline const PdfPage* GetPage() const { return m_Page; }
242  PdfPage& MustGetPage();
243  const PdfPage& MustGetPage() const;
244 
245 private:
246  template <typename TAnnot>
247  static constexpr PdfAnnotationType GetAnnotationType();
248 
249  static std::unique_ptr<PdfAnnotation> Create(PdfPage& page, PdfAnnotationType annotType, const Rect& rect);
250 
251  void SetPage(PdfPage& page) { m_Page = &page; }
252 
253 private:
254  static bool tryCreateFromObject(const PdfObject& obj, PdfAnnotationType targetType, PdfAnnotation*& xobj);
255  static PdfAnnotationType getAnnotationType(const PdfObject& obj);
256  PdfObject* getAppearanceStream(PdfAppearanceType appearance, const std::string_view& state) const;
257  PdfDictionary* getAppearanceDictionary() const;
258 
259 private:
260  PdfAnnotationType m_AnnotationType;
261  PdfPage* m_Page;
262 };
263 
264 template<typename TAnnotation>
265 bool PdfAnnotation::TryCreateFromObject(PdfObject& obj, std::unique_ptr<TAnnotation>& xobj)
266 {
267  PdfAnnotation* xobj_;
268  if (!tryCreateFromObject(obj, GetAnnotationType<TAnnotation>(), xobj_))
269  return false;
270 
271  xobj.reset((TAnnotation*)xobj_);
272  return true;
273 }
274 
275 template<typename TAnnotation>
276 bool PdfAnnotation::TryCreateFromObject(const PdfObject& obj, std::unique_ptr<const TAnnotation>& xobj)
277 {
278  PdfAnnotation* xobj_;
279  if (!tryCreateFromObject(obj, GetAnnotationType<TAnnotation>(), xobj_))
280  return false;
281 
282  xobj.reset((const TAnnotation*)xobj_);
283  return true;
284 }
285 
286 template<typename TAnnot>
287 constexpr PdfAnnotationType PdfAnnotation::GetAnnotationType()
288 {
289  if (std::is_same_v<TAnnot, PdfAnnotationText>)
290  return PdfAnnotationType::Text;
291  else if (std::is_same_v<TAnnot, PdfAnnotationLink>)
292  return PdfAnnotationType::Link;
293  else if (std::is_same_v<TAnnot, PdfAnnotationFreeText>)
294  return PdfAnnotationType::FreeText;
295  else if (std::is_same_v<TAnnot, PdfAnnotationLine>)
296  return PdfAnnotationType::Line;
297  else if (std::is_same_v<TAnnot, PdfAnnotationSquare>)
298  return PdfAnnotationType::Square;
299  else if (std::is_same_v<TAnnot, PdfAnnotationCircle>)
300  return PdfAnnotationType::Circle;
301  else if (std::is_same_v<TAnnot, PdfAnnotationPolygon>)
302  return PdfAnnotationType::Polygon;
303  else if (std::is_same_v<TAnnot, PdfAnnotationPolyLine>)
304  return PdfAnnotationType::PolyLine;
305  else if (std::is_same_v<TAnnot, PdfAnnotationHighlight>)
306  return PdfAnnotationType::Highlight;
307  else if (std::is_same_v<TAnnot, PdfAnnotationUnderline>)
308  return PdfAnnotationType::Underline;
309  else if (std::is_same_v<TAnnot, PdfAnnotationSquiggly>)
310  return PdfAnnotationType::Squiggly;
311  else if (std::is_same_v<TAnnot, PdfAnnotationStrikeOut>)
312  return PdfAnnotationType::StrikeOut;
313  else if (std::is_same_v<TAnnot, PdfAnnotationStamp>)
314  return PdfAnnotationType::Stamp;
315  else if (std::is_same_v<TAnnot, PdfAnnotationCaret>)
316  return PdfAnnotationType::Caret;
317  else if (std::is_same_v<TAnnot, PdfAnnotationInk>)
318  return PdfAnnotationType::Ink;
319  else if (std::is_same_v<TAnnot, PdfAnnotationPopup>)
320  return PdfAnnotationType::Popup;
321  else if (std::is_same_v<TAnnot, PdfAnnotationFileAttachment>)
322  return PdfAnnotationType::FileAttachement;
323  else if (std::is_same_v<TAnnot, PdfAnnotationSound>)
324  return PdfAnnotationType::Sound;
325  else if (std::is_same_v<TAnnot, PdfAnnotationMovie>)
326  return PdfAnnotationType::Movie;
327  else if (std::is_same_v<TAnnot, PdfAnnotationWidget>)
328  return PdfAnnotationType::Widget;
329  else if (std::is_same_v<TAnnot, PdfAnnotationScreen>)
330  return PdfAnnotationType::Screen;
331  else if (std::is_same_v<TAnnot, PdfAnnotationPrinterMark>)
332  return PdfAnnotationType::PrinterMark;
333  else if (std::is_same_v<TAnnot, PdfAnnotationTrapNet>)
334  return PdfAnnotationType::TrapNet;
335  else if (std::is_same_v<TAnnot, PdfAnnotationWatermark>)
336  return PdfAnnotationType::Watermark;
337  else if (std::is_same_v<TAnnot, PdfAnnotationModel3D>)
338  return PdfAnnotationType::Model3D;
339  else if (std::is_same_v<TAnnot, PdfAnnotationRichMedia>)
340  return PdfAnnotationType::RichMedia;
341  else if (std::is_same_v<TAnnot, PdfAnnotationWebMedia>)
342  return PdfAnnotationType::WebMedia;
343  else if (std::is_same_v<TAnnot, PdfAnnotationRedact>)
344  return PdfAnnotationType::Redact;
345  else if (std::is_same_v<TAnnot, PdfAnnotationProjection>)
346  return PdfAnnotationType::Projection;
347  else
348  return PdfAnnotationType::Unknown;
349 }
350 
351 };
352 
353 #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:234
PdfPage * GetPage()
Get the page of this PdfField.
Definition: PdfAnnotation.h:240
PdfObject * GetAppearanceStream(PdfAppearanceType appearance=PdfAppearanceType::Normal, const std::string_view &state={ })
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:127
A rectangle defined by position and size.
Definition: Rect.h:20
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:559
PdfAppearanceType
Type of the annotation appearance.
Definition: PdfDeclarations.h:653
@ Normal
Normal appearance.
PdfAnnotationFlags
Flags that control the appearance of a PdfAnnotation.
Definition: PdfDeclarations.h:597
Templatized object type getter helper.
Definition: PdfObject.h:607
A qualified appearance stream, with type and state name.
Definition: PdfAnnotation.h:51