PolyLineMeasureAnnotHistory.cs 9.0 KB

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