TextBoxHistory.cs 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270
  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 = currentParam.FontName;
  80. textWidget.SetTextAttribute(textAttr);
  81. textWidget.SetJustification(currentParam.Alignment);
  82. textWidget.SetBorderWidth((float)currentParam.LineWidth);
  83. textWidget.SetWidgetBorderStyle(currentParam.BorderStyle);
  84. textWidget.SetMultiLine(currentParam.IsMultiLine);
  85. textWidget.SetRect(currentParam.ClientRect);
  86. textWidget.SetFlags(currentParam.Flags);
  87. textWidget.SetIsLocked(currentParam.Locked);
  88. textWidget.SetIsReadOnly(currentParam.IsReadOnly);
  89. textWidget.SetIsHidden(currentParam.IsHidden);
  90. textWidget.SetCreationDate(PDFHelp.GetCurrentPdfTime());
  91. textWidget.UpdateFormAp();
  92. textWidget.ReleaseAnnot();
  93. if (CurrentParam != null)
  94. {
  95. currentParam.AnnotIndex = annotIndex;
  96. }
  97. if (PreviousParam != null)
  98. {
  99. PreviousParam.AnnotIndex = annotIndex;
  100. }
  101. return true;
  102. }
  103. return false;
  104. }
  105. internal override bool Update(bool isUndo)
  106. {
  107. if (CurrentParam as TextBoxParam == null || PreviousParam as TextBoxParam == null)
  108. {
  109. return false;
  110. }
  111. if (MakeAnnotValid(CurrentParam))
  112. {
  113. CPDFTextWidget textWidget = Annot as CPDFTextWidget;
  114. if (textWidget == null || !textWidget.IsValid())
  115. {
  116. return false;
  117. }
  118. TextBoxParam updateParam = (isUndo ? PreviousParam : CurrentParam) as TextBoxParam;
  119. TextBoxParam checkParam = (isUndo ? CurrentParam : PreviousParam) as TextBoxParam;
  120. if(updateParam.FieldName!=checkParam.FieldName)
  121. {
  122. textWidget.SetFieldName(updateParam.FieldName);
  123. }
  124. if (!CheckArrayEqual(updateParam.LineColor, checkParam.LineColor))
  125. {
  126. if (updateParam.HasLineColor)
  127. {
  128. if (updateParam.LineColor != null && updateParam.LineColor.Length == 3)
  129. {
  130. textWidget.SetWidgetBorderRGBColor(updateParam.LineColor);
  131. }
  132. }
  133. else
  134. {
  135. textWidget.ClearWidgetBorderRGBColor();
  136. }
  137. }
  138. if (!CheckArrayEqual(updateParam.BgColor, checkParam.BgColor))
  139. {
  140. if (updateParam.HasBgColor)
  141. {
  142. if (updateParam.BgColor != null && updateParam.BgColor.Length == 3)
  143. {
  144. textWidget.SetWidgetBgRGBColor(updateParam.BgColor);
  145. }
  146. }
  147. else
  148. {
  149. textWidget.ClearWidgetBgRGBColor();
  150. }
  151. }
  152. if (updateParam.Text!=checkParam.Text)
  153. {
  154. textWidget.SetText(updateParam.Text);
  155. }
  156. if (updateParam.FontName != checkParam.FontName)
  157. {
  158. CTextAttribute textAttr = textWidget.GetTextAttribute();
  159. textAttr.FontName = updateParam.FontName;
  160. textWidget.SetTextAttribute(textAttr);
  161. }
  162. if (updateParam.FontSize != checkParam.FontSize)
  163. {
  164. CTextAttribute textAttr = textWidget.GetTextAttribute();
  165. textAttr.FontSize = (float)updateParam.FontSize;
  166. textWidget.SetTextAttribute(textAttr);
  167. }
  168. if (!CheckArrayEqual(updateParam.FontColor, checkParam.FontColor))
  169. {
  170. if(updateParam.FontColor != null && updateParam.FontColor.Length==3)
  171. {
  172. CTextAttribute textAttr = textWidget.GetTextAttribute();
  173. textAttr.FontColor = updateParam.FontColor;
  174. textWidget.SetTextAttribute(textAttr);
  175. }
  176. }
  177. if (updateParam.Alignment != checkParam.Alignment)
  178. {
  179. textWidget.SetJustification(updateParam.Alignment);
  180. }
  181. if(updateParam.LineWidth != checkParam.LineWidth)
  182. {
  183. textWidget.SetBorderWidth((float)updateParam.LineWidth);
  184. }
  185. if(updateParam.BorderStyle != checkParam.BorderStyle)
  186. {
  187. textWidget.SetWidgetBorderStyle(updateParam.BorderStyle);
  188. }
  189. if(updateParam.IsMultiLine!=checkParam.IsMultiLine)
  190. {
  191. textWidget.SetMultiLine(updateParam.IsMultiLine);
  192. }
  193. if (!updateParam.ClientRect.Equals(checkParam.ClientRect))
  194. {
  195. textWidget.SetRect(updateParam.ClientRect);
  196. }
  197. if (updateParam.Flags != checkParam.Flags)
  198. {
  199. textWidget.SetFlags(updateParam.Flags);
  200. }
  201. if (updateParam.Locked != checkParam.Locked)
  202. {
  203. textWidget.SetIsLocked(updateParam.Locked);
  204. }
  205. if (updateParam.IsReadOnly != checkParam.IsReadOnly)
  206. {
  207. textWidget.SetIsReadOnly(updateParam.IsReadOnly);
  208. }
  209. if (updateParam.IsHidden != checkParam.IsHidden)
  210. {
  211. textWidget.SetIsHidden(updateParam.IsHidden);
  212. }
  213. textWidget.SetModifyDate(PDFHelp.GetCurrentPdfTime());
  214. textWidget.UpdateFormAp();
  215. return true;
  216. }
  217. return false;
  218. }
  219. internal override bool Remove()
  220. {
  221. if (MakeAnnotValid(CurrentParam))
  222. {
  223. Annot.RemoveAnnot();
  224. return true;
  225. }
  226. return false;
  227. }
  228. }
  229. }