RadioButtonHistory.cs 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242
  1. using ComPDFKit.PDFAnnotation;
  2. using ComPDFKit.PDFAnnotation.Form;
  3. using ComPDFKit.PDFPage;
  4. using ComPDFKit.Tool.Help;
  5. namespace ComPDFKit.Tool.UndoManger
  6. {
  7. public class RadioButtonHistory:AnnotHistory
  8. {
  9. public override int GetAnnotIndex()
  10. {
  11. if (CurrentParam != null)
  12. {
  13. return CurrentParam.AnnotIndex;
  14. }
  15. return base.GetAnnotIndex();
  16. }
  17. public override int GetPageIndex()
  18. {
  19. if (CurrentParam != null)
  20. {
  21. return CurrentParam.PageIndex;
  22. }
  23. return base.GetPageIndex();
  24. }
  25. public override void SetAnnotIndex(int newIndex)
  26. {
  27. if (CurrentParam != null)
  28. {
  29. CurrentParam.AnnotIndex = newIndex;
  30. }
  31. if (PreviousParam != null)
  32. {
  33. PreviousParam.AnnotIndex = newIndex;
  34. }
  35. }
  36. internal override bool Add()
  37. {
  38. RadioButtonParam currentParam = CurrentParam as RadioButtonParam;
  39. if (currentParam == null || PDFDoc == null || !PDFDoc.IsValid())
  40. {
  41. return false;
  42. }
  43. CPDFPage pdfPage = PDFDoc.PageAtIndex(currentParam.PageIndex);
  44. CPDFRadioButtonWidget radioWidget=pdfPage.CreateWidget(C_WIDGET_TYPE.WIDGET_RADIOBUTTON) as CPDFRadioButtonWidget;
  45. if (radioWidget != null)
  46. {
  47. int annotIndex = pdfPage.GetAnnotCount() - 1;
  48. if(!string.IsNullOrEmpty(currentParam.FieldName))
  49. {
  50. radioWidget.SetFieldName(currentParam.FieldName);
  51. }
  52. radioWidget.SetWidgetCheckStyle(currentParam.CheckStyle);
  53. radioWidget.SetChecked(currentParam.IsChecked);
  54. if (currentParam.HasLineColor)
  55. {
  56. if (currentParam.LineColor != null && currentParam.LineColor.Length == 3)
  57. {
  58. radioWidget.SetWidgetBorderRGBColor(currentParam.LineColor);
  59. }
  60. }
  61. if (currentParam.HasBgColor)
  62. {
  63. if (currentParam.BgColor != null && currentParam.BgColor.Length == 3)
  64. {
  65. radioWidget.SetWidgetBgRGBColor(currentParam.BgColor);
  66. }
  67. }
  68. radioWidget.SetBorderWidth((float)currentParam.LineWidth);
  69. radioWidget.SetWidgetBorderStyle(currentParam.BorderStyle);
  70. if(currentParam.FontColor!=null && currentParam.FontColor.Length==3)
  71. {
  72. CTextAttribute textAttr = radioWidget.GetTextAttribute();
  73. textAttr.FontColor = currentParam.FontColor;
  74. radioWidget.SetTextAttribute(textAttr);
  75. }
  76. radioWidget.SetRect(currentParam.ClientRect);
  77. radioWidget.SetFlags(currentParam.Flags);
  78. radioWidget.SetIsLocked(currentParam.Locked);
  79. radioWidget.SetIsReadOnly(currentParam.IsReadOnly);
  80. radioWidget.SetIsHidden(currentParam.IsHidden);
  81. radioWidget.SetCreationDate(PDFHelp.GetCurrentPdfTime());
  82. radioWidget.UpdateFormAp();
  83. radioWidget.ReleaseAnnot();
  84. if (CurrentParam != null)
  85. {
  86. currentParam.AnnotIndex = annotIndex;
  87. }
  88. if (PreviousParam != null)
  89. {
  90. PreviousParam.AnnotIndex = annotIndex;
  91. }
  92. return true;
  93. }
  94. return false;
  95. }
  96. internal override bool Update(bool isUndo)
  97. {
  98. if (CurrentParam as RadioButtonParam == null || PreviousParam as RadioButtonParam == null)
  99. {
  100. return false;
  101. }
  102. if (MakeAnnotValid(CurrentParam))
  103. {
  104. CPDFRadioButtonWidget radioWidget = Annot as CPDFRadioButtonWidget;
  105. if (radioWidget == null || !radioWidget.IsValid())
  106. {
  107. return false;
  108. }
  109. RadioButtonParam updateParam = (isUndo ? PreviousParam : CurrentParam) as RadioButtonParam;
  110. RadioButtonParam checkParam = (isUndo ? CurrentParam : PreviousParam) as RadioButtonParam;
  111. if(updateParam.FieldName != checkParam.FieldName)
  112. {
  113. radioWidget.SetFieldName(updateParam.FieldName);
  114. }
  115. if(updateParam.CheckStyle != checkParam.CheckStyle)
  116. {
  117. radioWidget.SetWidgetCheckStyle(updateParam.CheckStyle);
  118. }
  119. if(updateParam.IsChecked != checkParam.IsChecked)
  120. {
  121. radioWidget.SetChecked(updateParam.IsChecked);
  122. }
  123. if (!CheckArrayEqual(updateParam.LineColor, checkParam.LineColor))
  124. {
  125. if (updateParam.HasLineColor)
  126. {
  127. if (updateParam.LineColor != null && updateParam.LineColor.Length == 3)
  128. {
  129. radioWidget.SetWidgetBorderRGBColor(updateParam.LineColor);
  130. }
  131. }
  132. else
  133. {
  134. radioWidget.ClearWidgetBorderRGBColor();
  135. }
  136. }
  137. if (!CheckArrayEqual(updateParam.BgColor, checkParam.BgColor))
  138. {
  139. if (updateParam.HasBgColor)
  140. {
  141. if (updateParam.BgColor != null && updateParam.BgColor.Length == 3)
  142. {
  143. radioWidget.SetWidgetBgRGBColor(updateParam.BgColor);
  144. }
  145. }
  146. else
  147. {
  148. radioWidget.ClearWidgetBgRGBColor();
  149. }
  150. }
  151. if (updateParam.LineWidth != checkParam.LineWidth)
  152. {
  153. radioWidget.SetBorderWidth((float)updateParam.LineWidth);
  154. }
  155. if (updateParam.BorderStyle != checkParam.BorderStyle)
  156. {
  157. radioWidget.SetWidgetBorderStyle(updateParam.BorderStyle);
  158. }
  159. if(!CheckArrayEqual(updateParam.FontColor, checkParam.FontColor))
  160. {
  161. if(updateParam.FontColor!=null && updateParam.FontColor.Length==3)
  162. {
  163. CTextAttribute textAttr = radioWidget.GetTextAttribute();
  164. textAttr.FontColor = updateParam.FontColor;
  165. radioWidget.SetTextAttribute(textAttr);
  166. }
  167. }
  168. if(!updateParam.ClientRect.Equals(checkParam.ClientRect))
  169. {
  170. radioWidget.SetRect(updateParam.ClientRect);
  171. }
  172. if(updateParam.Flags!=checkParam.Flags)
  173. {
  174. radioWidget.SetFlags(updateParam.Flags);
  175. }
  176. if(updateParam.Locked!=checkParam.Locked)
  177. {
  178. radioWidget.SetIsLocked(updateParam.Locked);
  179. }
  180. if(updateParam.IsReadOnly!=checkParam.IsReadOnly)
  181. {
  182. radioWidget.SetIsReadOnly(updateParam.IsReadOnly);
  183. }
  184. if(updateParam.IsHidden!=checkParam.IsHidden)
  185. {
  186. radioWidget.SetIsHidden(updateParam.IsHidden);
  187. }
  188. radioWidget.SetModifyDate(PDFHelp.GetCurrentPdfTime());
  189. radioWidget.UpdateFormAp();
  190. return true;
  191. }
  192. return false;
  193. }
  194. internal override bool Remove()
  195. {
  196. if (MakeAnnotValid(CurrentParam))
  197. {
  198. Annot.RemoveAnnot();
  199. return true;
  200. }
  201. return false;
  202. }
  203. }
  204. }