RedactAnnotHistory.cs 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248
  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 RedactAnnotHistory: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. RedactParam currentParam = CurrentParam as RedactParam;
  39. if (currentParam == null || PDFDoc == null || !PDFDoc.IsValid())
  40. {
  41. return false;
  42. }
  43. CPDFPage pdfPage = PDFDoc.PageAtIndex(currentParam.PageIndex);
  44. CPDFRedactAnnotation redactAnnot = pdfPage?.CreateAnnot(C_ANNOTATION_TYPE.C_ANNOTATION_REDACT) as CPDFRedactAnnotation;
  45. if (redactAnnot != null)
  46. {
  47. int annotIndex = pdfPage.GetAnnotCount() - 1;
  48. if (currentParam.LineColor != null && currentParam.LineColor.Length == 3)
  49. {
  50. redactAnnot.SetOutlineColor(currentParam.LineColor);
  51. }
  52. if (currentParam.BgColor != null && currentParam.BgColor.Length == 3)
  53. {
  54. redactAnnot.SetFillColor(currentParam.BgColor);
  55. }
  56. redactAnnot.SetTextAlignment(currentParam.Alignment);
  57. CTextAttribute textAttr = new CTextAttribute();
  58. byte[] fontColor = new byte[3];
  59. if (currentParam.FontColor != null && currentParam.FontColor.Length == 3)
  60. {
  61. fontColor = currentParam.FontColor;
  62. }
  63. textAttr.FontColor = fontColor;
  64. textAttr.FontSize = (float)currentParam.FontSize;
  65. textAttr.FontName = ObtainFontName(
  66. GetFontType(currentParam.FontName),
  67. false,
  68. false);
  69. redactAnnot.SetTextAttribute(textAttr);
  70. if(currentParam.QuardRects != null)
  71. {
  72. redactAnnot.SetQuardRects(currentParam.QuardRects);
  73. }
  74. redactAnnot.SetRect(currentParam.ClientRect);
  75. redactAnnot.SetBorderWidth(1);
  76. redactAnnot.SetTransparency((byte)currentParam.Transparency);
  77. if(!string.IsNullOrEmpty(currentParam.OverlayText))
  78. {
  79. redactAnnot.SetOverlayText(currentParam.OverlayText);
  80. }
  81. if (!string.IsNullOrEmpty(currentParam.Author))
  82. {
  83. redactAnnot.SetAuthor(currentParam.Author);
  84. }
  85. if (!string.IsNullOrEmpty(currentParam.Content))
  86. {
  87. redactAnnot.SetContent(currentParam.Content);
  88. }
  89. redactAnnot.SetIsLocked(currentParam.Locked);
  90. redactAnnot.SetCreationDate(PDFHelp.GetCurrentPdfTime());
  91. redactAnnot.UpdateAp();
  92. redactAnnot.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 RedactParam == null || PreviousParam as RedactParam == null)
  108. {
  109. return false;
  110. }
  111. if (MakeAnnotValid(CurrentParam))
  112. {
  113. CPDFRedactAnnotation redactAnnot = Annot as CPDFRedactAnnotation;
  114. if (redactAnnot == null || !redactAnnot.IsValid())
  115. {
  116. return false;
  117. }
  118. RedactParam updateParam = (isUndo ? PreviousParam : CurrentParam) as RedactParam;
  119. RedactParam checkParam = (isUndo ? CurrentParam : PreviousParam) as RedactParam;
  120. if (!CheckArrayEqual(updateParam.LineColor, checkParam.LineColor))
  121. {
  122. if (updateParam.LineColor != null && updateParam.LineColor.Length == 3)
  123. {
  124. redactAnnot.SetOutlineColor(updateParam.LineColor);
  125. }
  126. }
  127. if (!CheckArrayEqual(updateParam.BgColor, checkParam.BgColor))
  128. {
  129. if (updateParam.BgColor != null && updateParam.BgColor.Length == 3)
  130. {
  131. redactAnnot.SetFillColor(updateParam.BgColor);
  132. }
  133. }
  134. if (updateParam.Alignment != checkParam.Alignment)
  135. {
  136. redactAnnot.SetTextAlignment(updateParam.Alignment);
  137. }
  138. if (updateParam.FontName != checkParam.FontName)
  139. {
  140. CTextAttribute textAttr = redactAnnot.GetTextAttribute();
  141. bool isBold = IsBold(textAttr.FontName);
  142. bool isItalic = IsItalic(textAttr.FontName);
  143. FontType fontType = GetFontType(updateParam.FontName);
  144. textAttr.FontName = ObtainFontName(fontType, isBold, isItalic);
  145. redactAnnot.SetTextAttribute(textAttr);
  146. }
  147. if (updateParam.FontSize != checkParam.FontSize)
  148. {
  149. CTextAttribute textAttr = redactAnnot.GetTextAttribute();
  150. textAttr.FontSize = (float)updateParam.FontSize;
  151. redactAnnot.SetTextAttribute(textAttr);
  152. }
  153. if (!CheckArrayEqual(updateParam.FontColor, checkParam.FontColor))
  154. {
  155. if (updateParam.FontColor != null && updateParam.FontColor.Length == 3)
  156. {
  157. CTextAttribute textAttr = redactAnnot.GetTextAttribute();
  158. textAttr.FontColor = updateParam.FontColor;
  159. redactAnnot.SetTextAttribute(textAttr);
  160. }
  161. }
  162. if (updateParam.QuardRects != null)
  163. {
  164. redactAnnot.SetQuardRects(updateParam.QuardRects);
  165. }
  166. if (!updateParam.ClientRect.Equals(checkParam.ClientRect))
  167. {
  168. redactAnnot.SetRect(updateParam.ClientRect);
  169. }
  170. if (updateParam.Transparency != checkParam.Transparency)
  171. {
  172. redactAnnot.SetTransparency((byte)updateParam.Transparency);
  173. }
  174. if(updateParam.OverlayText != checkParam.OverlayText)
  175. {
  176. redactAnnot.SetOverlayText(updateParam.OverlayText);
  177. }
  178. if (updateParam.Author != checkParam.Author)
  179. {
  180. redactAnnot.SetAuthor(updateParam.Author);
  181. }
  182. if (updateParam.Content != checkParam.Content)
  183. {
  184. redactAnnot.SetContent(updateParam.Content);
  185. }
  186. if (updateParam.Locked != checkParam.Locked)
  187. {
  188. redactAnnot.SetIsLocked(updateParam.Locked);
  189. }
  190. redactAnnot.SetModifyDate(PDFHelp.GetCurrentPdfTime());
  191. redactAnnot.UpdateAp();
  192. return true;
  193. }
  194. return false;
  195. }
  196. internal override bool Remove()
  197. {
  198. if (MakeAnnotValid(CurrentParam))
  199. {
  200. Annot.RemoveAnnot();
  201. return true;
  202. }
  203. return false;
  204. }
  205. }
  206. }