LineMeasureAnnotHistory.cs 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243
  1. using ComPDFKit.Measure;
  2. using ComPDFKit.PDFAnnotation;
  3. using ComPDFKit.PDFPage;
  4. using ComPDFKit.Tool.Help;
  5. using ComPDFKitViewer.Annot;
  6. using System;
  7. using System.Collections.Generic;
  8. using System.Linq;
  9. using System.Text;
  10. using System.Threading.Tasks;
  11. using static ComPDFKit.PDFAnnotation.CTextAttribute;
  12. namespace ComPDFKit.Tool.UndoManger
  13. {
  14. internal class LineMeasureAnnotHistory : AnnotHistory
  15. {
  16. public override int GetAnnotIndex()
  17. {
  18. if (CurrentParam != null)
  19. {
  20. return CurrentParam.AnnotIndex;
  21. }
  22. return base.GetAnnotIndex();
  23. }
  24. public override int GetPageIndex()
  25. {
  26. if (CurrentParam != null)
  27. {
  28. return CurrentParam.PageIndex;
  29. }
  30. return base.GetPageIndex();
  31. }
  32. public override void SetAnnotIndex(int newIndex)
  33. {
  34. if (CurrentParam != null)
  35. {
  36. CurrentParam.AnnotIndex = newIndex;
  37. }
  38. if (PreviousParam != null)
  39. {
  40. PreviousParam.AnnotIndex = newIndex;
  41. }
  42. }
  43. internal override bool Add()
  44. {
  45. LineMeasureParam currentParam = CurrentParam as LineMeasureParam;
  46. if (CurrentParam == null || PDFDoc == null || !PDFDoc.IsValid())
  47. {
  48. return false;
  49. }
  50. CPDFPage pdfPage = PDFDoc.PageAtIndex(currentParam.PageIndex);
  51. CPDFLineAnnotation polygonAnnot = pdfPage?.CreateAnnot(C_ANNOTATION_TYPE.C_ANNOTATION_LINE) as CPDFLineAnnotation;
  52. if (polygonAnnot != null)
  53. {
  54. int annotIndex = pdfPage.GetAnnotCount() - 1;
  55. if (currentParam.HeadLineType != C_LINE_TYPE.LINETYPE_NONE || currentParam.TailLineType != C_LINE_TYPE.LINETYPE_NONE)
  56. {
  57. polygonAnnot.SetLineType(currentParam.HeadLineType, currentParam.TailLineType);
  58. }
  59. if (currentParam.LineColor != null && currentParam.LineColor.Length == 3)
  60. {
  61. polygonAnnot.SetLineColor(currentParam.LineColor);
  62. }
  63. polygonAnnot.SetTransparency((byte)currentParam.Transparency);
  64. polygonAnnot.SetLineWidth(currentParam.LineWidth);
  65. polygonAnnot.SetLinePoints(currentParam.HeadPoint, currentParam.TailPoint);
  66. polygonAnnot.SetRect(currentParam.ClientRect);
  67. if (currentParam.LineDash != null)
  68. {
  69. if (currentParam.LineDash.Length == 0)
  70. {
  71. polygonAnnot.SetBorderStyle(C_BORDER_STYLE.BS_SOLID, new float[0]);
  72. }
  73. else
  74. {
  75. List<float> floatArray = new List<float>();
  76. foreach (float num in currentParam.LineDash)
  77. {
  78. floatArray.Add(num);
  79. }
  80. polygonAnnot.SetBorderStyle(C_BORDER_STYLE.BS_DASHDED, floatArray.ToArray());
  81. }
  82. }
  83. CTextAttribute textAttribute = new CTextAttribute();
  84. textAttribute.FontColor = currentParam.FontColor;
  85. textAttribute.FontSize = (float)currentParam.FontSize;
  86. textAttribute.FontName = CFontNameHelper.ObtainFontName(CFontNameHelper.GetFontType(currentParam.FontName),
  87. currentParam.IsBold,
  88. currentParam.IsItalic);
  89. polygonAnnot.SetTextAttribute(textAttribute);
  90. if (currentParam.measureInfo != null)
  91. {
  92. CPDFDistanceMeasure polygonMeasure = polygonAnnot.GetDistanceMeasure();
  93. if (polygonMeasure != null)
  94. {
  95. polygonMeasure.SetMeasureInfo(currentParam.measureInfo);
  96. polygonMeasure.SetMeasureScale(currentParam.measureInfo.RulerBase, currentParam.measureInfo.RulerBaseUnit,
  97. currentParam.measureInfo.RulerTranslate, currentParam.measureInfo.RulerTranslateUnit);
  98. polygonMeasure.UpdateAnnotMeasure();
  99. }
  100. }
  101. if (!string.IsNullOrEmpty(currentParam.Author))
  102. {
  103. polygonAnnot.SetAuthor(currentParam.Author);
  104. }
  105. if (!string.IsNullOrEmpty(currentParam.Content))
  106. {
  107. polygonAnnot.SetContent(currentParam.Content);
  108. }
  109. polygonAnnot.SetIsLocked(currentParam.Locked);
  110. polygonAnnot.SetCreationDate(PDFHelp.GetCurrentPdfTime());
  111. polygonAnnot.UpdateAp();
  112. polygonAnnot.ReleaseAnnot();
  113. if (currentParam != null)
  114. {
  115. currentParam.AnnotIndex = annotIndex;
  116. }
  117. if (PreviousParam != null)
  118. {
  119. PreviousParam.AnnotIndex = annotIndex;
  120. }
  121. return true;
  122. }
  123. return false;
  124. }
  125. internal override bool Update(bool isUndo)
  126. {
  127. if (CurrentParam as LineMeasureParam == null || PreviousParam as LineMeasureParam == null)
  128. {
  129. return false;
  130. }
  131. if (MakeAnnotValid(CurrentParam))
  132. {
  133. CPDFLineAnnotation polygonAnnot = Annot as CPDFLineAnnotation;
  134. if (polygonAnnot == null || !polygonAnnot.IsValid() || !polygonAnnot.IsMeasured())
  135. {
  136. return false;
  137. }
  138. LineMeasureParam updateParam = (isUndo ? PreviousParam : CurrentParam) as LineMeasureParam;
  139. LineMeasureParam checkParam = (isUndo ? CurrentParam : PreviousParam) as LineMeasureParam;
  140. if (!CheckArrayEqual(updateParam.LineColor, checkParam.LineColor))
  141. {
  142. if (updateParam.LineColor != null && updateParam.LineColor.Length == 3)
  143. {
  144. polygonAnnot.SetLineColor(updateParam.LineColor);
  145. }
  146. }
  147. if (updateParam.Transparency != checkParam.Transparency)
  148. {
  149. polygonAnnot.SetTransparency((byte)updateParam.Transparency);
  150. }
  151. if (updateParam.LineWidth != checkParam.LineWidth)
  152. {
  153. polygonAnnot.SetLineWidth((byte)updateParam.LineWidth);
  154. }
  155. if (!CheckArrayEqual(updateParam.LineDash, checkParam.LineDash))
  156. {
  157. if (updateParam.LineDash.Length == 0)
  158. {
  159. polygonAnnot.SetBorderStyle(C_BORDER_STYLE.BS_SOLID, new float[0]);
  160. }
  161. else
  162. {
  163. List<float> floatArray = new List<float>();
  164. foreach (float num in updateParam.LineDash)
  165. {
  166. floatArray.Add(num);
  167. }
  168. polygonAnnot.SetBorderStyle(C_BORDER_STYLE.BS_DASHDED, floatArray.ToArray());
  169. }
  170. }
  171. if (!updateParam.HeadPoint.Equals(checkParam.HeadPoint) || !updateParam.TailPoint.Equals(checkParam.TailPoint))
  172. {
  173. polygonAnnot.SetLinePoints(updateParam.HeadPoint, updateParam.TailPoint);
  174. }
  175. if (!updateParam.ClientRect.Equals(checkParam.ClientRect))
  176. {
  177. polygonAnnot.SetRect(updateParam.ClientRect);
  178. }
  179. if (updateParam.Author != checkParam.Author)
  180. {
  181. polygonAnnot.SetAuthor(updateParam.Author);
  182. }
  183. if (updateParam.Content != checkParam.Content)
  184. {
  185. polygonAnnot.SetContent(updateParam.Content);
  186. }
  187. if (updateParam.Locked != checkParam.Locked)
  188. {
  189. polygonAnnot.SetIsLocked(updateParam.Locked);
  190. }
  191. if (updateParam.measureInfo != checkParam.measureInfo)
  192. {
  193. CPDFDistanceMeasure polygonMeasure = polygonAnnot.GetDistanceMeasure();
  194. if (polygonMeasure != null)
  195. {
  196. polygonMeasure.SetMeasureInfo(updateParam.measureInfo);
  197. polygonMeasure.SetMeasureScale(updateParam.measureInfo.RulerBase, updateParam.measureInfo.RulerBaseUnit,
  198. updateParam.measureInfo.RulerTranslate, updateParam.measureInfo.RulerTranslateUnit);
  199. polygonMeasure.UpdateAnnotMeasure();
  200. }
  201. }
  202. polygonAnnot.SetModifyDate(PDFHelp.GetCurrentPdfTime());
  203. polygonAnnot.UpdateAp();
  204. return true;
  205. }
  206. return false;
  207. }
  208. internal override bool Remove()
  209. {
  210. if (MakeAnnotValid(CurrentParam))
  211. {
  212. Annot.RemoveAnnot();
  213. return true;
  214. }
  215. return false;
  216. }
  217. }
  218. }