PushButtonHistory.cs 9.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327
  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 = ObtainFontName(
  84. GetFontType(currentParam.FontName),
  85. currentParam.IsBold,
  86. currentParam.IsItalic);
  87. pushbuttonWidget.SetTextAttribute(textAttr);
  88. switch (currentParam.Action)
  89. {
  90. case C_ACTION_TYPE.ACTION_TYPE_GOTO:
  91. {
  92. CPDFGoToAction gotoAction = new CPDFGoToAction();
  93. CPDFDestination destination = new CPDFDestination();
  94. destination.Position_X = currentParam.DestinationPosition.x;
  95. destination.Position_Y = currentParam.DestinationPosition.y;
  96. destination.PageIndex = currentParam.DestinationPageIndex;
  97. gotoAction.SetDestination(PDFDoc, destination);
  98. pushbuttonWidget.SetButtonAction(gotoAction);
  99. }
  100. break;
  101. case C_ACTION_TYPE.ACTION_TYPE_URI:
  102. {
  103. CPDFUriAction uriAction = new CPDFUriAction();
  104. uriAction.SetUri(currentParam.Uri);
  105. pushbuttonWidget.SetButtonAction(uriAction);
  106. }
  107. break;
  108. default:
  109. break;
  110. }
  111. pushbuttonWidget.SetRect(currentParam.ClientRect);
  112. pushbuttonWidget.SetFlags(currentParam.Flags);
  113. pushbuttonWidget.SetIsLocked(currentParam.Locked);
  114. pushbuttonWidget.SetIsReadOnly(currentParam.IsReadOnly);
  115. pushbuttonWidget.SetIsHidden(currentParam.IsHidden);
  116. pushbuttonWidget.SetCreationDate(PDFHelp.GetCurrentPdfTime());
  117. pushbuttonWidget.UpdateFormAp();
  118. pushbuttonWidget.ReleaseAnnot();
  119. if (CurrentParam != null)
  120. {
  121. currentParam.AnnotIndex = annotIndex;
  122. }
  123. if (PreviousParam != null)
  124. {
  125. PreviousParam.AnnotIndex = annotIndex;
  126. }
  127. return true;
  128. }
  129. return false;
  130. }
  131. internal override bool Update(bool isUndo)
  132. {
  133. if (CurrentParam as PushButtonParam == null || PreviousParam as PushButtonParam == null)
  134. {
  135. return false;
  136. }
  137. if (MakeAnnotValid(CurrentParam))
  138. {
  139. CPDFPushButtonWidget pushbutton = Annot as CPDFPushButtonWidget;
  140. if (pushbutton == null || !pushbutton.IsValid())
  141. {
  142. return false;
  143. }
  144. PushButtonParam updateParam = (isUndo ? PreviousParam : CurrentParam) as PushButtonParam;
  145. PushButtonParam checkParam = (isUndo ? CurrentParam : PreviousParam) as PushButtonParam;
  146. if (updateParam.FieldName != checkParam.FieldName)
  147. {
  148. pushbutton.SetFieldName(updateParam.FieldName);
  149. }
  150. if (!CheckArrayEqual(updateParam.LineColor, checkParam.LineColor))
  151. {
  152. if (updateParam.HasLineColor)
  153. {
  154. if (updateParam.LineColor != null && updateParam.LineColor.Length == 3)
  155. {
  156. pushbutton.SetWidgetBorderRGBColor(updateParam.LineColor);
  157. }
  158. }
  159. else
  160. {
  161. pushbutton.ClearWidgetBorderRGBColor();
  162. }
  163. }
  164. if (!CheckArrayEqual(updateParam.BgColor, checkParam.BgColor))
  165. {
  166. if (updateParam.HasBgColor)
  167. {
  168. if (updateParam.BgColor != null && updateParam.BgColor.Length == 3)
  169. {
  170. pushbutton.SetWidgetBgRGBColor(updateParam.BgColor);
  171. }
  172. }
  173. else
  174. {
  175. pushbutton.ClearWidgetBgRGBColor();
  176. }
  177. }
  178. if (updateParam.LineWidth != checkParam.LineWidth)
  179. {
  180. pushbutton.SetBorderWidth((float)updateParam.LineWidth);
  181. }
  182. if (updateParam.BorderStyle != checkParam.BorderStyle)
  183. {
  184. pushbutton.SetWidgetBorderStyle(updateParam.BorderStyle);
  185. }
  186. if (updateParam.FontName != checkParam.FontName)
  187. {
  188. CTextAttribute textAttr = pushbutton.GetTextAttribute();
  189. bool isBold = IsBold(textAttr.FontName);
  190. bool isItalic = IsItalic(textAttr.FontName);
  191. FontType fontType = GetFontType(updateParam.FontName);
  192. textAttr.FontName = ObtainFontName(fontType, isBold, isItalic);
  193. pushbutton.SetTextAttribute(textAttr);
  194. }
  195. if (updateParam.FontSize != checkParam.FontSize)
  196. {
  197. CTextAttribute textAttr = pushbutton.GetTextAttribute();
  198. textAttr.FontSize = (float)updateParam.FontSize;
  199. pushbutton.SetTextAttribute(textAttr);
  200. }
  201. if (!CheckArrayEqual(updateParam.FontColor, checkParam.FontColor))
  202. {
  203. if (updateParam.FontColor != null && updateParam.FontColor.Length == 3)
  204. {
  205. CTextAttribute textAttr = pushbutton.GetTextAttribute();
  206. textAttr.FontColor = updateParam.FontColor;
  207. pushbutton.SetTextAttribute(textAttr);
  208. }
  209. }
  210. if (updateParam.IsBold != checkParam.IsBold)
  211. {
  212. CTextAttribute textAttr = pushbutton.GetTextAttribute();
  213. bool isItalic = IsItalic(textAttr.FontName);
  214. FontType fontType = GetFontType(textAttr.FontName);
  215. textAttr.FontName = ObtainFontName(fontType, updateParam.IsBold, isItalic);
  216. pushbutton.SetTextAttribute(textAttr);
  217. }
  218. if (updateParam.IsItalic != checkParam.IsItalic)
  219. {
  220. CTextAttribute textAttr = pushbutton.GetTextAttribute();
  221. bool isBold = IsBold(textAttr.FontName);
  222. FontType fontType = GetFontType(textAttr.FontName);
  223. textAttr.FontName = ObtainFontName(fontType, isBold, updateParam.IsItalic);
  224. pushbutton.SetTextAttribute(textAttr);
  225. }
  226. switch (updateParam.Action)
  227. {
  228. case C_ACTION_TYPE.ACTION_TYPE_GOTO:
  229. {
  230. CPDFGoToAction gotoAction = new CPDFGoToAction();
  231. CPDFDestination destination = new CPDFDestination();
  232. destination.Position_X = updateParam.DestinationPosition.x;
  233. destination.Position_Y = updateParam.DestinationPosition.y;
  234. destination.PageIndex = updateParam.DestinationPageIndex;
  235. gotoAction.SetDestination(PDFDoc, destination);
  236. pushbutton.SetButtonAction(gotoAction);
  237. }
  238. break;
  239. case C_ACTION_TYPE.ACTION_TYPE_URI:
  240. {
  241. CPDFUriAction uriAction = new CPDFUriAction();
  242. uriAction.SetUri(updateParam.Uri);
  243. pushbutton.SetButtonAction(uriAction);
  244. }
  245. break;
  246. default:
  247. break;
  248. }
  249. if (!updateParam.ClientRect.Equals(checkParam.ClientRect))
  250. {
  251. pushbutton.SetRect(updateParam.ClientRect);
  252. }
  253. if (updateParam.Flags != checkParam.Flags)
  254. {
  255. pushbutton.SetFlags(updateParam.Flags);
  256. }
  257. if (updateParam.Locked != checkParam.Locked)
  258. {
  259. pushbutton.SetIsLocked(updateParam.Locked);
  260. }
  261. if (updateParam.IsReadOnly != checkParam.IsReadOnly)
  262. {
  263. pushbutton.SetIsReadOnly(updateParam.IsReadOnly);
  264. }
  265. if (updateParam.IsHidden != checkParam.IsHidden)
  266. {
  267. pushbutton.SetIsHidden(updateParam.IsHidden);
  268. }
  269. pushbutton.SetModifyDate(PDFHelp.GetCurrentPdfTime());
  270. pushbutton.UpdateFormAp();
  271. return true;
  272. }
  273. return false;
  274. }
  275. internal override bool Remove()
  276. {
  277. if (MakeAnnotValid(CurrentParam))
  278. {
  279. Annot.RemoveAnnot();
  280. return true;
  281. }
  282. return false;
  283. }
  284. }
  285. }