FreeTextAnnotHistory.cs 9.1 KB

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