FreeTextAnnotHistory.cs 9.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276
  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 = ObtainFontName(
  72. GetFontType(currentParam.FontName),
  73. currentParam.IsBold,
  74. currentParam.IsItalic);
  75. textAnnot.SetFreetextDa(textAttr);
  76. textAnnot.SetRect(currentParam.ClientRect);
  77. if (!string.IsNullOrEmpty(currentParam.Author))
  78. {
  79. textAnnot.SetAuthor(currentParam.Author);
  80. }
  81. if (!string.IsNullOrEmpty(currentParam.Content))
  82. {
  83. textAnnot.SetContent(currentParam.Content);
  84. }
  85. if (currentParam.Dash != null && currentParam.Dash.Length > 0)
  86. {
  87. textAnnot.SetBorderStyle(C_BORDER_STYLE.BS_DASHDED, currentParam.Dash);
  88. }
  89. textAnnot.SetIsLocked(currentParam.Locked);
  90. textAnnot.SetCreationDate(PDFHelp.GetCurrentPdfTime());
  91. textAnnot.UpdateAp();
  92. textAnnot.ReleaseAnnot();
  93. if (CurrentParam != null)
  94. {
  95. currentParam.AnnotIndex = annotIndex;
  96. }
  97. if (PreviousParam != null)
  98. {
  99. PreviousParam.AnnotIndex = annotIndex;
  100. }
  101. return true;
  102. }
  103. return false;
  104. }
  105. internal override bool Update(bool isUndo)
  106. {
  107. if (CurrentParam as FreeTextParam == null || PreviousParam as FreeTextParam == null)
  108. {
  109. return false;
  110. }
  111. if (MakeAnnotValid(CurrentParam))
  112. {
  113. CPDFFreeTextAnnotation textAnnot = Annot as CPDFFreeTextAnnotation;
  114. if (textAnnot == null || !textAnnot.IsValid())
  115. {
  116. return false;
  117. }
  118. FreeTextParam updateParam = (isUndo ? PreviousParam : CurrentParam) as FreeTextParam;
  119. FreeTextParam checkParam = (isUndo ? CurrentParam : PreviousParam) as FreeTextParam;
  120. if(!CheckArrayEqual(updateParam.LineColor,checkParam.LineColor))
  121. {
  122. if(updateParam.LineColor != null && updateParam.LineColor.Length==3)
  123. {
  124. textAnnot.SetLineColor(updateParam.LineColor);
  125. }
  126. }
  127. if (!CheckArrayEqual(updateParam.BgColor, checkParam.BgColor))
  128. {
  129. if (updateParam.HasBgColor)
  130. {
  131. if (updateParam.BgColor != null && updateParam.BgColor.Length == 3)
  132. {
  133. textAnnot.SetBgColor(updateParam.BgColor);
  134. }
  135. }
  136. else
  137. {
  138. textAnnot.ClearBgColor();
  139. }
  140. }
  141. if(updateParam.Transparency != checkParam.Transparency)
  142. {
  143. textAnnot.SetTransparency((byte)updateParam.Transparency);
  144. }
  145. if(updateParam.LineWidth != checkParam.LineWidth)
  146. {
  147. textAnnot.SetLineWidth((float)updateParam.LineWidth);
  148. }
  149. if(updateParam.Alignment != checkParam.Alignment)
  150. {
  151. textAnnot.SetFreetextAlignment(updateParam.Alignment);
  152. }
  153. if(updateParam.FontName != checkParam.FontName)
  154. {
  155. CTextAttribute textAttr = textAnnot.FreeTextDa;
  156. bool isBold = IsBold(textAttr.FontName);
  157. bool isItalic = IsItalic(textAttr.FontName);
  158. FontType fontType = GetFontType(updateParam.FontName);
  159. textAttr.FontName = ObtainFontName(fontType, isBold, isItalic);
  160. textAnnot.SetFreetextDa(textAttr);
  161. }
  162. if(updateParam.FontSize != checkParam.FontSize)
  163. {
  164. CTextAttribute textAttr = textAnnot.FreeTextDa;
  165. textAttr.FontSize = (float)updateParam.FontSize;
  166. textAnnot.SetFreetextDa(textAttr);
  167. }
  168. if(!CheckArrayEqual(updateParam.FontColor, checkParam.FontColor))
  169. {
  170. if(updateParam.FontColor != null && updateParam.FontColor.Length==3)
  171. {
  172. CTextAttribute textAttr = textAnnot.FreeTextDa;
  173. textAttr.FontColor = updateParam.FontColor;
  174. textAnnot.SetFreetextDa(textAttr);
  175. }
  176. }
  177. if(updateParam.IsBold != checkParam.IsBold)
  178. {
  179. CTextAttribute textAttr = textAnnot.FreeTextDa;
  180. bool isItalic = IsItalic(textAttr.FontName);
  181. FontType fontType = GetFontType(textAttr.FontName);
  182. textAttr.FontName = ObtainFontName(fontType, updateParam.IsBold, isItalic);
  183. textAnnot.SetFreetextDa(textAttr);
  184. }
  185. if(updateParam.IsItalic != checkParam.IsItalic)
  186. {
  187. CTextAttribute textAttr = textAnnot.FreeTextDa;
  188. bool isBold = IsBold(textAttr.FontName);
  189. FontType fontType = GetFontType(textAttr.FontName);
  190. textAttr.FontName = ObtainFontName(fontType, isBold, updateParam.IsItalic);
  191. textAnnot.SetFreetextDa(textAttr);
  192. }
  193. if (!updateParam.ClientRect.Equals(checkParam.ClientRect))
  194. {
  195. textAnnot.SetRect(updateParam.ClientRect);
  196. }
  197. if (updateParam.Author != checkParam.Author)
  198. {
  199. textAnnot.SetAuthor(updateParam.Author);
  200. }
  201. if (updateParam.Content != checkParam.Content)
  202. {
  203. textAnnot.SetContent(updateParam.Content);
  204. }
  205. if (updateParam.Locked != checkParam.Locked)
  206. {
  207. textAnnot.SetIsLocked(updateParam.Locked);
  208. }
  209. if (updateParam.Dash != null && updateParam.Dash.Length > 0)
  210. {
  211. textAnnot.SetBorderStyle(C_BORDER_STYLE.BS_DASHDED, updateParam.Dash);
  212. }
  213. else
  214. {
  215. textAnnot.SetBorderStyle(C_BORDER_STYLE.BS_SOLID, new float[0]);
  216. }
  217. textAnnot.SetModifyDate(PDFHelp.GetCurrentPdfTime());
  218. textAnnot.UpdateAp();
  219. return true;
  220. }
  221. return false;
  222. }
  223. internal override bool Remove()
  224. {
  225. if (MakeAnnotValid(CurrentParam))
  226. {
  227. Annot.RemoveAnnot();
  228. return true;
  229. }
  230. return false;
  231. }
  232. }
  233. }