CheckBoxHistory.cs 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239
  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 CheckBoxHistory: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. CheckBoxParam currentParam = CurrentParam as CheckBoxParam;
  39. if (currentParam == null || PDFDoc == null || !PDFDoc.IsValid())
  40. {
  41. return false;
  42. }
  43. CPDFPage pdfPage = PDFDoc.PageAtIndex(currentParam.PageIndex);
  44. CPDFCheckBoxWidget checkboxWidget = pdfPage.CreateWidget(C_WIDGET_TYPE.WIDGET_CHECKBOX) as CPDFCheckBoxWidget;
  45. if (checkboxWidget != null)
  46. {
  47. int annotIndex = pdfPage.GetAnnotCount() - 1;
  48. if (!string.IsNullOrEmpty(currentParam.FieldName))
  49. {
  50. checkboxWidget.SetFieldName(currentParam.FieldName);
  51. }
  52. checkboxWidget.SetWidgetCheckStyle(currentParam.CheckStyle);
  53. checkboxWidget.SetChecked(currentParam.IsChecked);
  54. if(currentParam.HasLineColor)
  55. {
  56. if (currentParam.LineColor != null && currentParam.LineColor.Length == 3)
  57. {
  58. checkboxWidget.SetWidgetBorderRGBColor(currentParam.LineColor);
  59. }
  60. }
  61. if (currentParam.HasBgColor)
  62. {
  63. if (currentParam.BgColor != null && currentParam.BgColor.Length == 3)
  64. {
  65. checkboxWidget.SetWidgetBgRGBColor(currentParam.BgColor);
  66. }
  67. }
  68. checkboxWidget.SetBorderWidth((float)currentParam.LineWidth);
  69. checkboxWidget.SetWidgetBorderStyle(currentParam.BorderStyle);
  70. if (currentParam.FontColor != null && currentParam.FontColor.Length == 3)
  71. {
  72. CTextAttribute textAttr = checkboxWidget.GetTextAttribute();
  73. textAttr.FontColor = currentParam.FontColor;
  74. checkboxWidget.SetTextAttribute(textAttr);
  75. }
  76. checkboxWidget.SetRect(currentParam.ClientRect);
  77. checkboxWidget.SetFlags(currentParam.Flags);
  78. checkboxWidget.SetIsLocked(currentParam.Locked);
  79. checkboxWidget.SetIsReadOnly(currentParam.IsReadOnly);
  80. checkboxWidget.SetIsHidden(currentParam.IsHidden);
  81. checkboxWidget.SetCreationDate(PDFHelp.GetCurrentPdfTime());
  82. checkboxWidget.UpdateFormAp();
  83. checkboxWidget.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 CheckBoxParam == null || PreviousParam as CheckBoxParam == null)
  99. {
  100. return false;
  101. }
  102. if (MakeAnnotValid(CurrentParam))
  103. {
  104. CPDFCheckBoxWidget checkboxWidget = Annot as CPDFCheckBoxWidget;
  105. if (checkboxWidget == null || !checkboxWidget.IsValid())
  106. {
  107. return false;
  108. }
  109. CheckBoxParam updateParam = (isUndo ? PreviousParam : CurrentParam) as CheckBoxParam;
  110. CheckBoxParam checkParam = (isUndo ? CurrentParam : PreviousParam) as CheckBoxParam;
  111. if (updateParam.FieldName != checkParam.FieldName)
  112. {
  113. checkboxWidget.SetFieldName(updateParam.FieldName);
  114. }
  115. if (updateParam.CheckStyle != checkParam.CheckStyle)
  116. {
  117. checkboxWidget.SetWidgetCheckStyle(updateParam.CheckStyle);
  118. }
  119. if (updateParam.IsChecked != checkParam.IsChecked)
  120. {
  121. checkboxWidget.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. checkboxWidget.SetWidgetBorderRGBColor(updateParam.LineColor);
  130. }
  131. }
  132. else
  133. {
  134. checkboxWidget.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. checkboxWidget.SetWidgetBgRGBColor(updateParam.BgColor);
  144. }
  145. }
  146. else
  147. {
  148. checkboxWidget.ClearWidgetBgRGBColor();
  149. }
  150. }
  151. if (updateParam.LineWidth != checkParam.LineWidth)
  152. {
  153. checkboxWidget.SetBorderWidth((float)updateParam.LineWidth);
  154. }
  155. if (updateParam.BorderStyle != checkParam.BorderStyle)
  156. {
  157. checkboxWidget.SetWidgetBorderStyle(updateParam.BorderStyle);
  158. }
  159. if (!CheckArrayEqual(updateParam.FontColor, checkParam.FontColor))
  160. {
  161. CTextAttribute textAttr = checkboxWidget.GetTextAttribute();
  162. textAttr.FontColor = updateParam.FontColor;
  163. checkboxWidget.SetTextAttribute(textAttr);
  164. }
  165. if (!updateParam.ClientRect.Equals(checkParam.ClientRect))
  166. {
  167. checkboxWidget.SetRect(updateParam.ClientRect);
  168. }
  169. if (updateParam.Flags != checkParam.Flags)
  170. {
  171. checkboxWidget.SetFlags(updateParam.Flags);
  172. }
  173. if (updateParam.Locked != checkParam.Locked)
  174. {
  175. checkboxWidget.SetIsLocked(updateParam.Locked);
  176. }
  177. if (updateParam.IsReadOnly != checkParam.IsReadOnly)
  178. {
  179. checkboxWidget.SetIsReadOnly(updateParam.IsReadOnly);
  180. }
  181. if (updateParam.IsHidden != checkParam.IsHidden)
  182. {
  183. checkboxWidget.SetIsHidden(updateParam.IsHidden);
  184. }
  185. checkboxWidget.SetModifyDate(PDFHelp.GetCurrentPdfTime());
  186. checkboxWidget.UpdateFormAp();
  187. return true;
  188. }
  189. return false;
  190. }
  191. internal override bool Remove()
  192. {
  193. if (MakeAnnotValid(CurrentParam))
  194. {
  195. Annot.RemoveAnnot();
  196. return true;
  197. }
  198. return false;
  199. }
  200. }
  201. }