PushButtonHistory.cs 8.9 KB

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