FreeTextAnnotHistory.cs 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252
  1. using ComPDFKit.PDFAnnotation;
  2. using ComPDFKit.PDFPage;
  3. using ComPDFKit.Tool.Help;
  4. using ComPDFKitViewer.Annot;
  5. using static ComPDFKit.PDFAnnotation.CTextAttribute.CFontNameHelper;
  6. namespace ComPDFKit.Tool.UndoManger
  7. {
  8. public class FreeTextAnnotHistory: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. FreeTextParam currentParam = CurrentParam as FreeTextParam;
  40. if (currentParam == null || PDFDoc == null || !PDFDoc.IsValid())
  41. {
  42. return false;
  43. }
  44. CPDFPage pdfPage = PDFDoc.PageAtIndex(currentParam.PageIndex);
  45. CPDFFreeTextAnnotation textAnnot = pdfPage?.CreateAnnot(C_ANNOTATION_TYPE.C_ANNOTATION_FREETEXT) as CPDFFreeTextAnnotation;
  46. if (textAnnot != null)
  47. {
  48. int annotIndex = pdfPage.GetAnnotCount() - 1;
  49. if (currentParam.LineColor!=null && currentParam.LineColor.Length==3)
  50. {
  51. textAnnot.SetLineColor(currentParam.LineColor);
  52. }
  53. if(currentParam.HasBgColor)
  54. {
  55. if (currentParam.BgColor != null && currentParam.BgColor.Length == 3)
  56. {
  57. textAnnot.SetBgColor(currentParam.BgColor);
  58. }
  59. }
  60. textAnnot.SetTransparency((byte)currentParam.Transparency);
  61. textAnnot.SetLineWidth((byte)currentParam.LineWidth);
  62. textAnnot.SetFreetextAlignment(currentParam.Alignment);
  63. CTextAttribute textAttr = new CTextAttribute();
  64. byte[] fontColor = new byte[3];
  65. if (currentParam.FontColor != null && currentParam.FontColor.Length == 3)
  66. {
  67. fontColor = currentParam.FontColor;
  68. }
  69. textAttr.FontColor = fontColor;
  70. textAttr.FontSize = (float)currentParam.FontSize;
  71. textAttr.FontName = currentParam.FontName;
  72. textAnnot.SetFreetextDa(textAttr);
  73. textAnnot.SetRect(currentParam.ClientRect);
  74. if (!string.IsNullOrEmpty(currentParam.Author))
  75. {
  76. textAnnot.SetAuthor(currentParam.Author);
  77. }
  78. if (!string.IsNullOrEmpty(currentParam.Content))
  79. {
  80. textAnnot.SetContent(currentParam.Content);
  81. }
  82. if (currentParam.Dash != null && currentParam.Dash.Length > 0)
  83. {
  84. textAnnot.SetBorderStyle(C_BORDER_STYLE.BS_DASHDED, currentParam.Dash);
  85. }
  86. textAnnot.SetIsLocked(currentParam.Locked);
  87. textAnnot.SetCreationDate(PDFHelp.GetCurrentPdfTime());
  88. textAnnot.UpdateAp();
  89. textAnnot.ReleaseAnnot();
  90. if (CurrentParam != null)
  91. {
  92. currentParam.AnnotIndex = annotIndex;
  93. }
  94. if (PreviousParam != null)
  95. {
  96. PreviousParam.AnnotIndex = annotIndex;
  97. }
  98. return true;
  99. }
  100. return false;
  101. }
  102. internal override bool Update(bool isUndo)
  103. {
  104. if (CurrentParam as FreeTextParam == null || PreviousParam as FreeTextParam == null)
  105. {
  106. return false;
  107. }
  108. if (MakeAnnotValid(CurrentParam))
  109. {
  110. CPDFFreeTextAnnotation textAnnot = Annot as CPDFFreeTextAnnotation;
  111. if (textAnnot == null || !textAnnot.IsValid())
  112. {
  113. return false;
  114. }
  115. FreeTextParam updateParam = (isUndo ? PreviousParam : CurrentParam) as FreeTextParam;
  116. FreeTextParam checkParam = (isUndo ? CurrentParam : PreviousParam) as FreeTextParam;
  117. if(!CheckArrayEqual(updateParam.LineColor,checkParam.LineColor))
  118. {
  119. if(updateParam.LineColor != null && updateParam.LineColor.Length==3)
  120. {
  121. textAnnot.SetLineColor(updateParam.LineColor);
  122. }
  123. }
  124. if (!CheckArrayEqual(updateParam.BgColor, checkParam.BgColor))
  125. {
  126. if (updateParam.HasBgColor)
  127. {
  128. if (updateParam.BgColor != null && updateParam.BgColor.Length == 3)
  129. {
  130. textAnnot.SetBgColor(updateParam.BgColor);
  131. }
  132. }
  133. else
  134. {
  135. textAnnot.ClearBgColor();
  136. }
  137. }
  138. if(updateParam.Transparency != checkParam.Transparency)
  139. {
  140. textAnnot.SetTransparency((byte)updateParam.Transparency);
  141. }
  142. if(updateParam.LineWidth != checkParam.LineWidth)
  143. {
  144. textAnnot.SetLineWidth((float)updateParam.LineWidth);
  145. }
  146. if(updateParam.Alignment != checkParam.Alignment)
  147. {
  148. textAnnot.SetFreetextAlignment(updateParam.Alignment);
  149. }
  150. if(updateParam.FontName != checkParam.FontName)
  151. {
  152. CTextAttribute textAttr = textAnnot.FreeTextDa;
  153. textAttr.FontName = updateParam.FontName;
  154. textAnnot.SetFreetextDa(textAttr);
  155. }
  156. if(updateParam.FontSize != checkParam.FontSize)
  157. {
  158. CTextAttribute textAttr = textAnnot.FreeTextDa;
  159. textAttr.FontSize = (float)updateParam.FontSize;
  160. textAnnot.SetFreetextDa(textAttr);
  161. }
  162. if(!CheckArrayEqual(updateParam.FontColor, checkParam.FontColor))
  163. {
  164. if(updateParam.FontColor != null && updateParam.FontColor.Length==3)
  165. {
  166. CTextAttribute textAttr = textAnnot.FreeTextDa;
  167. textAttr.FontColor = updateParam.FontColor;
  168. textAnnot.SetFreetextDa(textAttr);
  169. }
  170. }
  171. if (!updateParam.ClientRect.Equals(checkParam.ClientRect))
  172. {
  173. textAnnot.SetRect(updateParam.ClientRect);
  174. }
  175. if (updateParam.Author != checkParam.Author)
  176. {
  177. textAnnot.SetAuthor(updateParam.Author);
  178. }
  179. if (updateParam.Content != checkParam.Content)
  180. {
  181. textAnnot.SetContent(updateParam.Content);
  182. }
  183. if (updateParam.Locked != checkParam.Locked)
  184. {
  185. textAnnot.SetIsLocked(updateParam.Locked);
  186. }
  187. if (updateParam.Dash != null && updateParam.Dash.Length > 0)
  188. {
  189. textAnnot.SetBorderStyle(C_BORDER_STYLE.BS_DASHDED, updateParam.Dash);
  190. }
  191. else
  192. {
  193. textAnnot.SetBorderStyle(C_BORDER_STYLE.BS_SOLID, new float[0]);
  194. }
  195. textAnnot.SetModifyDate(PDFHelp.GetCurrentPdfTime());
  196. textAnnot.UpdateAp();
  197. return true;
  198. }
  199. return false;
  200. }
  201. internal override bool Remove()
  202. {
  203. if (MakeAnnotValid(CurrentParam))
  204. {
  205. Annot.RemoveAnnot();
  206. return true;
  207. }
  208. return false;
  209. }
  210. }
  211. }