TextBoxHistory.cs 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294
  1. using ComPDFKit.PDFAnnotation;
  2. using ComPDFKit.PDFAnnotation.Form;
  3. using ComPDFKit.PDFPage;
  4. using ComPDFKit.Tool.Help;
  5. using static ComPDFKit.PDFAnnotation.CTextAttribute.CFontNameHelper;
  6. namespace ComPDFKit.Tool.UndoManger
  7. {
  8. public class TextBoxHistory:AnnotHistory
  9. {
  10. public override int GetAnnotIndex()
  11. {
  12. if (CurrentParam != null)
  13. {
  14. return CurrentParam.AnnotIndex;
  15. }
  16. return base.GetAnnotIndex();
  17. }
  18. public override int GetPageIndex()
  19. {
  20. if (CurrentParam != null)
  21. {
  22. return CurrentParam.PageIndex;
  23. }
  24. return base.GetPageIndex();
  25. }
  26. public override void SetAnnotIndex(int newIndex)
  27. {
  28. if (CurrentParam != null)
  29. {
  30. CurrentParam.AnnotIndex = newIndex;
  31. }
  32. if (PreviousParam != null)
  33. {
  34. PreviousParam.AnnotIndex = newIndex;
  35. }
  36. }
  37. internal override bool Add()
  38. {
  39. TextBoxParam currentParam = CurrentParam as TextBoxParam;
  40. if (currentParam == null || PDFDoc == null || !PDFDoc.IsValid())
  41. {
  42. return false;
  43. }
  44. CPDFPage pdfPage = PDFDoc.PageAtIndex(currentParam.PageIndex);
  45. CPDFTextWidget textWidget = pdfPage.CreateWidget(C_WIDGET_TYPE.WIDGET_TEXTFIELD) as CPDFTextWidget;
  46. if(textWidget != null)
  47. {
  48. int annotIndex = pdfPage.GetAnnotCount() - 1;
  49. if (!string.IsNullOrEmpty(currentParam.FieldName))
  50. {
  51. textWidget.SetFieldName(currentParam.FieldName);
  52. }
  53. if (currentParam.HasLineColor)
  54. {
  55. if (currentParam.LineColor != null && currentParam.LineColor.Length == 3)
  56. {
  57. textWidget.SetWidgetBorderRGBColor(currentParam.LineColor);
  58. }
  59. }
  60. if(currentParam.HasBgColor)
  61. {
  62. if (currentParam.BgColor != null && currentParam.BgColor.Length == 3)
  63. {
  64. textWidget.SetWidgetBgRGBColor(currentParam.BgColor);
  65. }
  66. }
  67. if(!string.IsNullOrEmpty(currentParam.Text))
  68. {
  69. textWidget.SetText(currentParam.Text);
  70. }
  71. CTextAttribute textAttr = new CTextAttribute();
  72. byte[] fontColor = new byte[3];
  73. if(currentParam.FontColor != null && currentParam.FontColor.Length==3)
  74. {
  75. fontColor = currentParam.FontColor;
  76. }
  77. textAttr.FontColor = fontColor;
  78. textAttr.FontSize = (float)currentParam.FontSize;
  79. textAttr.FontName = ObtainFontName(
  80. GetFontType(currentParam.FontName),
  81. currentParam.IsBold,
  82. currentParam.IsItalic);
  83. textWidget.SetTextAttribute(textAttr);
  84. textWidget.SetJustification(currentParam.Alignment);
  85. textWidget.SetBorderWidth((float)currentParam.LineWidth);
  86. textWidget.SetWidgetBorderStyle(currentParam.BorderStyle);
  87. textWidget.SetMultiLine(currentParam.IsMultiLine);
  88. textWidget.SetRect(currentParam.ClientRect);
  89. textWidget.SetFlags(currentParam.Flags);
  90. textWidget.SetIsLocked(currentParam.Locked);
  91. textWidget.SetIsReadOnly(currentParam.IsReadOnly);
  92. textWidget.SetIsHidden(currentParam.IsHidden);
  93. textWidget.SetCreationDate(PDFHelp.GetCurrentPdfTime());
  94. textWidget.UpdateFormAp();
  95. textWidget.ReleaseAnnot();
  96. if (CurrentParam != null)
  97. {
  98. currentParam.AnnotIndex = annotIndex;
  99. }
  100. if (PreviousParam != null)
  101. {
  102. PreviousParam.AnnotIndex = annotIndex;
  103. }
  104. return true;
  105. }
  106. return false;
  107. }
  108. internal override bool Update(bool isUndo)
  109. {
  110. if (CurrentParam as TextBoxParam == null || PreviousParam as TextBoxParam == null)
  111. {
  112. return false;
  113. }
  114. if (MakeAnnotValid(CurrentParam))
  115. {
  116. CPDFTextWidget textWidget = Annot as CPDFTextWidget;
  117. if (textWidget == null || !textWidget.IsValid())
  118. {
  119. return false;
  120. }
  121. TextBoxParam updateParam = (isUndo ? PreviousParam : CurrentParam) as TextBoxParam;
  122. TextBoxParam checkParam = (isUndo ? CurrentParam : PreviousParam) as TextBoxParam;
  123. if(updateParam.FieldName!=checkParam.FieldName)
  124. {
  125. textWidget.SetFieldName(updateParam.FieldName);
  126. }
  127. if (!CheckArrayEqual(updateParam.LineColor, checkParam.LineColor))
  128. {
  129. if (updateParam.HasLineColor)
  130. {
  131. if (updateParam.LineColor != null && updateParam.LineColor.Length == 3)
  132. {
  133. textWidget.SetWidgetBorderRGBColor(updateParam.LineColor);
  134. }
  135. }
  136. else
  137. {
  138. textWidget.ClearWidgetBorderRGBColor();
  139. }
  140. }
  141. if (!CheckArrayEqual(updateParam.BgColor, checkParam.BgColor))
  142. {
  143. if (updateParam.HasBgColor)
  144. {
  145. if (updateParam.BgColor != null && updateParam.BgColor.Length == 3)
  146. {
  147. textWidget.SetWidgetBgRGBColor(updateParam.BgColor);
  148. }
  149. }
  150. else
  151. {
  152. textWidget.ClearWidgetBgRGBColor();
  153. }
  154. }
  155. if (updateParam.Text!=checkParam.Text)
  156. {
  157. textWidget.SetText(updateParam.Text);
  158. }
  159. if (updateParam.FontName != checkParam.FontName)
  160. {
  161. CTextAttribute textAttr = textWidget.GetTextAttribute();
  162. bool isBold = IsBold(textAttr.FontName);
  163. bool isItalic = IsItalic(textAttr.FontName);
  164. FontType fontType = GetFontType(updateParam.FontName);
  165. textAttr.FontName = ObtainFontName(fontType, isBold, isItalic);
  166. textWidget.SetTextAttribute(textAttr);
  167. }
  168. if (updateParam.FontSize != checkParam.FontSize)
  169. {
  170. CTextAttribute textAttr = textWidget.GetTextAttribute();
  171. textAttr.FontSize = (float)updateParam.FontSize;
  172. textWidget.SetTextAttribute(textAttr);
  173. }
  174. if (!CheckArrayEqual(updateParam.FontColor, checkParam.FontColor))
  175. {
  176. if(updateParam.FontColor != null && updateParam.FontColor.Length==3)
  177. {
  178. CTextAttribute textAttr = textWidget.GetTextAttribute();
  179. textAttr.FontColor = updateParam.FontColor;
  180. textWidget.SetTextAttribute(textAttr);
  181. }
  182. }
  183. if (updateParam.IsBold != checkParam.IsBold)
  184. {
  185. CTextAttribute textAttr = textWidget.GetTextAttribute();
  186. bool isItalic = IsItalic(textAttr.FontName);
  187. FontType fontType = GetFontType(textAttr.FontName);
  188. textAttr.FontName = ObtainFontName(fontType, updateParam.IsBold, isItalic);
  189. textWidget.SetTextAttribute(textAttr);
  190. }
  191. if (updateParam.IsItalic != checkParam.IsItalic)
  192. {
  193. CTextAttribute textAttr = textWidget.GetTextAttribute();
  194. bool isBold = IsBold(textAttr.FontName);
  195. FontType fontType = GetFontType(textAttr.FontName);
  196. textAttr.FontName = ObtainFontName(fontType, isBold, updateParam.IsItalic);
  197. textWidget.SetTextAttribute(textAttr);
  198. }
  199. if (updateParam.Alignment != checkParam.Alignment)
  200. {
  201. textWidget.SetJustification(updateParam.Alignment);
  202. }
  203. if(updateParam.LineWidth != checkParam.LineWidth)
  204. {
  205. textWidget.SetBorderWidth((float)updateParam.LineWidth);
  206. }
  207. if(updateParam.BorderStyle != checkParam.BorderStyle)
  208. {
  209. textWidget.SetWidgetBorderStyle(updateParam.BorderStyle);
  210. }
  211. if(updateParam.IsMultiLine!=checkParam.IsMultiLine)
  212. {
  213. textWidget.SetMultiLine(updateParam.IsMultiLine);
  214. }
  215. if (!updateParam.ClientRect.Equals(checkParam.ClientRect))
  216. {
  217. textWidget.SetRect(updateParam.ClientRect);
  218. }
  219. if (updateParam.Flags != checkParam.Flags)
  220. {
  221. textWidget.SetFlags(updateParam.Flags);
  222. }
  223. if (updateParam.Locked != checkParam.Locked)
  224. {
  225. textWidget.SetIsLocked(updateParam.Locked);
  226. }
  227. if (updateParam.IsReadOnly != checkParam.IsReadOnly)
  228. {
  229. textWidget.SetIsReadOnly(updateParam.IsReadOnly);
  230. }
  231. if (updateParam.IsHidden != checkParam.IsHidden)
  232. {
  233. textWidget.SetIsHidden(updateParam.IsHidden);
  234. }
  235. textWidget.SetModifyDate(PDFHelp.GetCurrentPdfTime());
  236. textWidget.UpdateFormAp();
  237. return true;
  238. }
  239. return false;
  240. }
  241. internal override bool Remove()
  242. {
  243. if (MakeAnnotValid(CurrentParam))
  244. {
  245. Annot.RemoveAnnot();
  246. return true;
  247. }
  248. return false;
  249. }
  250. }
  251. }