LineMeasureAnnotHistory.cs 9.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247
  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.SetLeadLength((float)currentParam.LeadLength);
  96. polygonMeasure.SetLeadExtension((float)currentParam.LeadExtension);
  97. polygonMeasure.SetLeadOffset((float)currentParam.LeadOffset);
  98. polygonMeasure.SetMeasureInfo(currentParam.measureInfo);
  99. polygonMeasure.SetMeasureScale(currentParam.measureInfo.RulerBase, currentParam.measureInfo.RulerBaseUnit,
  100. currentParam.measureInfo.RulerTranslate, currentParam.measureInfo.RulerTranslateUnit);
  101. polygonMeasure.UpdateAnnotMeasure();
  102. }
  103. }
  104. if (!string.IsNullOrEmpty(currentParam.Author))
  105. {
  106. polygonAnnot.SetAuthor(currentParam.Author);
  107. }
  108. if (!string.IsNullOrEmpty(currentParam.Content))
  109. {
  110. polygonAnnot.SetContent(currentParam.Content);
  111. }
  112. polygonAnnot.SetIsLocked(currentParam.Locked);
  113. polygonAnnot.SetCreationDate(PDFHelp.GetCurrentPdfTime());
  114. polygonAnnot.UpdateAp();
  115. polygonAnnot.ReleaseAnnot();
  116. if (currentParam != null)
  117. {
  118. currentParam.AnnotIndex = annotIndex;
  119. }
  120. if (PreviousParam != null)
  121. {
  122. PreviousParam.AnnotIndex = annotIndex;
  123. }
  124. return true;
  125. }
  126. return false;
  127. }
  128. internal override bool Update(bool isUndo)
  129. {
  130. if (CurrentParam as LineMeasureParam == null || PreviousParam as LineMeasureParam == null)
  131. {
  132. return false;
  133. }
  134. if (MakeAnnotValid(CurrentParam))
  135. {
  136. CPDFLineAnnotation polygonAnnot = Annot as CPDFLineAnnotation;
  137. if (polygonAnnot == null || !polygonAnnot.IsValid() || !polygonAnnot.IsMeasured())
  138. {
  139. return false;
  140. }
  141. LineMeasureParam updateParam = (isUndo ? PreviousParam : CurrentParam) as LineMeasureParam;
  142. LineMeasureParam checkParam = (isUndo ? CurrentParam : PreviousParam) as LineMeasureParam;
  143. if (!CheckArrayEqual(updateParam.LineColor, checkParam.LineColor))
  144. {
  145. if (updateParam.LineColor != null && updateParam.LineColor.Length == 3)
  146. {
  147. polygonAnnot.SetLineColor(updateParam.LineColor);
  148. }
  149. }
  150. if (updateParam.Transparency != checkParam.Transparency)
  151. {
  152. polygonAnnot.SetTransparency((byte)updateParam.Transparency);
  153. }
  154. if (updateParam.LineWidth != checkParam.LineWidth)
  155. {
  156. polygonAnnot.SetLineWidth((byte)updateParam.LineWidth);
  157. }
  158. if (!CheckArrayEqual(updateParam.LineDash, checkParam.LineDash))
  159. {
  160. if (updateParam.LineDash.Length == 0)
  161. {
  162. polygonAnnot.SetBorderStyle(C_BORDER_STYLE.BS_SOLID, new float[0]);
  163. }
  164. else
  165. {
  166. List<float> floatArray = new List<float>();
  167. foreach (float num in updateParam.LineDash)
  168. {
  169. floatArray.Add(num);
  170. }
  171. polygonAnnot.SetBorderStyle(C_BORDER_STYLE.BS_DASHDED, floatArray.ToArray());
  172. }
  173. }
  174. if (!updateParam.HeadPoint.Equals(checkParam.HeadPoint) || !updateParam.TailPoint.Equals(checkParam.TailPoint))
  175. {
  176. polygonAnnot.SetLinePoints(updateParam.HeadPoint, updateParam.TailPoint);
  177. }
  178. if (!updateParam.ClientRect.Equals(checkParam.ClientRect))
  179. {
  180. polygonAnnot.SetRect(updateParam.ClientRect);
  181. }
  182. if (updateParam.Author != checkParam.Author)
  183. {
  184. polygonAnnot.SetAuthor(updateParam.Author);
  185. }
  186. if (updateParam.Content != checkParam.Content)
  187. {
  188. polygonAnnot.SetContent(updateParam.Content);
  189. }
  190. if (updateParam.Locked != checkParam.Locked)
  191. {
  192. polygonAnnot.SetIsLocked(updateParam.Locked);
  193. }
  194. CPDFDistanceMeasure polygonMeasure = polygonAnnot.GetDistanceMeasure();
  195. if (polygonMeasure != null)
  196. {
  197. polygonMeasure.SetLeadLength((float)updateParam.LeadLength);
  198. polygonMeasure.SetLeadExtension((float)updateParam.LeadExtension);
  199. polygonMeasure.SetLeadOffset((float)updateParam.LeadOffset);
  200. polygonMeasure.SetMeasureInfo(updateParam.measureInfo);
  201. polygonMeasure.SetMeasureScale(updateParam.measureInfo.RulerBase, updateParam.measureInfo.RulerBaseUnit,
  202. updateParam.measureInfo.RulerTranslate, updateParam.measureInfo.RulerTranslateUnit);
  203. polygonMeasure.UpdateAnnotMeasure();
  204. }
  205. polygonAnnot.SetModifyDate(PDFHelp.GetCurrentPdfTime());
  206. polygonAnnot.UpdateAp();
  207. return true;
  208. }
  209. return false;
  210. }
  211. internal override bool Remove()
  212. {
  213. if (MakeAnnotValid(CurrentParam))
  214. {
  215. Annot.RemoveAnnot();
  216. return true;
  217. }
  218. return false;
  219. }
  220. }
  221. }