cpdf_watermark.dart 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213
  1. /*
  2. * Copyright © 2014-2025 PDF Technologies, Inc. All Rights Reserved.
  3. *
  4. * THIS SOURCE CODE AND ANY ACCOMPANYING DOCUMENTATION ARE PROTECTED BY INTERNATIONAL COPYRIGHT LAW
  5. * AND MAY NOT BE RESOLD OR REDISTRIBUTED. USAGE IS BOUND TO THE ComPDFKit LICENSE AGREEMENT.
  6. * UNAUTHORIZED REPRODUCTION OR DISTRIBUTION IS SUBJECT TO CIVIL AND CRIMINAL PENALTIES.
  7. * This notice may not be removed from this file.
  8. *
  9. */
  10. import 'package:compdfkit_flutter/configuration/cpdf_options.dart';
  11. import 'package:compdfkit_flutter/util/extension/cpdf_color_extension.dart';
  12. import 'package:flutter/material.dart';
  13. class CPDFWatermark {
  14. /// Watermark type, can be either [CPDFWatermarkType.text] or [CPDFWatermarkType.image]
  15. final CPDFWatermarkType type;
  16. /// Text content for text-type watermark
  17. final String textContent;
  18. /// Image path for image-type watermark
  19. final String imagePath;
  20. final Color textColor;
  21. final int fontSize;
  22. /// Scaling factor, default is 1.0
  23. final double scale;
  24. /// Watermark rotation angle, default is 45°
  25. final double rotation;
  26. /// Watermark opacity, default is 1.0, range is 0.0 to 1.0
  27. final double opacity;
  28. /// Vertical alignment of the watermark, default is vertical center alignment
  29. final CPDFWatermarkVerticalAlignment verticalAlignment;
  30. /// Horizontal alignment of the watermark, default is center alignment
  31. final CPDFWatermarkHorizontalAlignment horizontalAlignment;
  32. /// Vertical offset for watermark position
  33. final double verticalOffset;
  34. /// Horizontal offset for watermark position
  35. final double horizontalOffset;
  36. /// Pages to add the watermark to, e.g., "1,2,3,4,5"
  37. final List<int> pages;
  38. /// Position the watermark in front of the content
  39. final bool isFront;
  40. /// Enable watermark tiling
  41. final bool isTilePage;
  42. /// Set the horizontal spacing for tiled watermarks
  43. final double horizontalSpacing;
  44. /// Set the vertical spacing for tiled watermarks
  45. final double verticalSpacing;
  46. CPDFWatermark(
  47. {required this.type,
  48. required this.pages,
  49. this.textContent = "",
  50. this.imagePath = "",
  51. this.textColor = Colors.black,
  52. this.fontSize = 24,
  53. this.scale = 1.0,
  54. this.rotation = 45,
  55. this.opacity = 1,
  56. this.verticalAlignment = CPDFWatermarkVerticalAlignment.center,
  57. this.horizontalAlignment = CPDFWatermarkHorizontalAlignment.center,
  58. this.verticalOffset = 0,
  59. this.horizontalOffset = 0,
  60. this.isFront = true,
  61. this.isTilePage = false,
  62. this.horizontalSpacing = 0,
  63. this.verticalSpacing = 0});
  64. /// Text watermark constructor
  65. ///
  66. /// This constructor creates a text watermark with customizable properties.
  67. ///
  68. /// - [textContent]: The text content of the watermark. (Required)
  69. /// - [pages]: A list of page indices where the watermark should be applied, e.g., [0, 1, 2, 3] represents pages 1 through 4. (Required)
  70. /// - [textColor]: The color of the watermark text. Default is `Colors.black`.
  71. /// - [fontSize]: The font size of the watermark text. Default is `24`.
  72. /// - [scale]: The scaling factor for the text. Default is `1.0`.
  73. /// - [rotation]: The rotation angle of the watermark in degrees. Default is `45.0`.
  74. /// - [opacity]: The transparency of the watermark, where `1.0` is fully opaque and `0.0` is fully transparent. Default is `1.0`.
  75. /// - [verticalAlignment]: The vertical alignment of the watermark on the page. Default is `CPDFWatermarkVerticalAlignment.center`.
  76. /// - [horizontalAlignment]: The horizontal alignment of the watermark on the page. Default is `CPDFWatermarkHorizontalAlignment.center`.
  77. /// - [verticalOffset]: The vertical offset of the watermark relative to the alignment position. Default is `0.0`.
  78. /// - [horizontalOffset]: The horizontal offset of the watermark relative to the alignment position. Default is `0.0`.
  79. /// - [isFront]: Whether the watermark should appear in front of the page content. Default is `true`.
  80. /// - [isTilePage]: Whether the watermark should be tiled across the page. Default is `false`.
  81. /// - [horizontalSpacing]: The horizontal spacing between tiled watermarks. Default is `0.0`.
  82. /// - [verticalSpacing]: The vertical spacing between tiled watermarks. Default is `0.0`.
  83. CPDFWatermark.text(
  84. {required String textContent,
  85. required List<int> pages,
  86. Color textColor = Colors.black,
  87. int fontSize = 24,
  88. double scale = 1.0,
  89. double rotation = 45.0,
  90. double opacity = 1.0,
  91. CPDFWatermarkVerticalAlignment verticalAlignment =
  92. CPDFWatermarkVerticalAlignment.center,
  93. CPDFWatermarkHorizontalAlignment horizontalAlignment =
  94. CPDFWatermarkHorizontalAlignment.center,
  95. double verticalOffset = 0.0,
  96. double horizontalOffset = 0.0,
  97. bool isFront = true,
  98. bool isTilePage = false,
  99. double horizontalSpacing = 0.0,
  100. double verticalSpacing = 0.0})
  101. : this(
  102. type: CPDFWatermarkType.text,
  103. textContent: textContent,
  104. textColor: textColor,
  105. fontSize: fontSize,
  106. pages: pages,
  107. scale: scale,
  108. rotation: rotation,
  109. opacity: opacity,
  110. verticalAlignment: verticalAlignment,
  111. horizontalAlignment: horizontalAlignment,
  112. verticalOffset: verticalOffset,
  113. horizontalOffset: horizontalOffset,
  114. isFront: isFront,
  115. isTilePage: isTilePage,
  116. horizontalSpacing: horizontalSpacing,
  117. verticalSpacing: verticalSpacing);
  118. /// Image watermark constructor
  119. ///
  120. /// This constructor creates an image watermark with customizable properties.
  121. ///
  122. /// - [imagePath]: The file path of the image to be used as the watermark. (Required)
  123. /// - [pages]: A list of page indices where the watermark should be applied, e.g., [0, 1, 2, 3] represents pages 1 through 4. (Required)
  124. /// - [scale]: The scaling factor for the image. Default is 1.0.
  125. /// - [rotation]: The rotation angle of the watermark in degrees. Default is 45.0.
  126. /// - [opacity]: The transparency of the watermark, where 1.0 is fully opaque and 0.0 is fully transparent. Default is 1.0.
  127. /// - [verticalAlignment]: The vertical alignment of the watermark on the page. Default is `CPDFWatermarkVerticalAlignment.center`.
  128. /// - [horizontalAlignment]: The horizontal alignment of the watermark on the page. Default is `CPDFWatermarkHorizontalAlignment.center`.
  129. /// - [verticalOffset]: The vertical offset of the watermark relative to the alignment position. Default is 0.0.
  130. /// - [horizontalOffset]: The horizontal offset of the watermark relative to the alignment position. Default is 0.0.
  131. /// - [isFront]: Whether the watermark should appear in front of the page content. Default is `true`.
  132. /// - [isTilePage]: Whether the watermark should be tiled across the page. Default is `false`.
  133. /// - [horizontalSpacing]: The horizontal spacing between tiled watermarks. Default is 0.0.
  134. /// - [verticalSpacing]: The vertical spacing between tiled watermarks. Default is 0.0.
  135. CPDFWatermark.image({
  136. required String imagePath,
  137. required List<int> pages,
  138. double scale = 1.0,
  139. double rotation = 45.0,
  140. double opacity = 1.0,
  141. CPDFWatermarkVerticalAlignment verticalAlignment =
  142. CPDFWatermarkVerticalAlignment.center,
  143. CPDFWatermarkHorizontalAlignment horizontalAlignment =
  144. CPDFWatermarkHorizontalAlignment.center,
  145. double verticalOffset = 0.0,
  146. double horizontalOffset = 0.0,
  147. bool isFront = true,
  148. bool isTilePage = false,
  149. double horizontalSpacing = 0.0,
  150. double verticalSpacing = 0.0,
  151. }) : this(
  152. type: CPDFWatermarkType.image,
  153. imagePath: imagePath,
  154. pages: pages,
  155. scale: scale,
  156. rotation: rotation,
  157. opacity: opacity,
  158. verticalAlignment: verticalAlignment,
  159. horizontalAlignment: horizontalAlignment,
  160. verticalOffset: verticalOffset,
  161. horizontalOffset: horizontalOffset,
  162. isFront: isFront,
  163. isTilePage: isTilePage,
  164. horizontalSpacing: horizontalSpacing,
  165. verticalSpacing: verticalSpacing,
  166. );
  167. Map<String, dynamic> toJson() => {
  168. 'type': type.name,
  169. 'text_content': textContent,
  170. 'image_path' : imagePath,
  171. 'text_color' : textColor.toHex(),
  172. 'font_size' : fontSize,
  173. 'scale' : scale,
  174. 'rotation' : rotation,
  175. 'opacity' : opacity,
  176. 'vertical_alignment' : verticalAlignment.name,
  177. 'horizontal_alignment' : horizontalAlignment.name,
  178. 'vertical_offset' : verticalOffset,
  179. 'horizontal_offset' : horizontalOffset,
  180. 'pages' : pages.join(','),
  181. 'is_front' : isFront,
  182. 'is_tile_page' : isTilePage,
  183. 'horizontal_spacing' : horizontalSpacing,
  184. 'vertical_spacing' : verticalSpacing
  185. };
  186. }
  187. enum CPDFWatermarkVerticalAlignment { top, center, bottom }
  188. enum CPDFWatermarkHorizontalAlignment { left, center, right }