LinkAnnotHistory.cs 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200
  1. using ComPDFKit.PDFAnnotation;
  2. using ComPDFKit.PDFDocument;
  3. using ComPDFKit.PDFDocument.Action;
  4. using ComPDFKit.PDFPage;
  5. using ComPDFKit.Tool.Help;
  6. namespace ComPDFKit.Tool.UndoManger
  7. {
  8. public class LinkAnnotHistory:AnnotHistory
  9. {
  10. public override int GetAnnotIndex()
  11. {
  12. if (CurrentParam != null)
  13. {
  14. return CurrentParam.AnnotIndex;
  15. }
  16. return base.GetAnnotIndex();
  17. }
  18. public override int GetPageIndex()
  19. {
  20. if (CurrentParam != null)
  21. {
  22. return CurrentParam.PageIndex;
  23. }
  24. return base.GetPageIndex();
  25. }
  26. public override void SetAnnotIndex(int newIndex)
  27. {
  28. if (CurrentParam != null)
  29. {
  30. CurrentParam.AnnotIndex = newIndex;
  31. }
  32. if (PreviousParam != null)
  33. {
  34. PreviousParam.AnnotIndex = newIndex;
  35. }
  36. }
  37. internal override bool Add()
  38. {
  39. LinkParam currentParam = CurrentParam as LinkParam;
  40. if (currentParam == null || PDFDoc == null || !PDFDoc.IsValid())
  41. {
  42. return false;
  43. }
  44. CPDFPage pdfPage = PDFDoc.PageAtIndex(currentParam.PageIndex);
  45. CPDFLinkAnnotation linkAnnot = pdfPage?.CreateAnnot(C_ANNOTATION_TYPE.C_ANNOTATION_LINK) as CPDFLinkAnnotation;
  46. if (linkAnnot != null)
  47. {
  48. int annotIndex = pdfPage.GetAnnotCount() - 1;
  49. switch(currentParam.Action)
  50. {
  51. case C_ACTION_TYPE.ACTION_TYPE_GOTO:
  52. {
  53. CPDFGoToAction gotoAction = new CPDFGoToAction();
  54. CPDFDestination destination = new CPDFDestination();
  55. destination.Position_X = currentParam.DestinationPosition.x;
  56. destination.Position_Y = currentParam.DestinationPosition.y;
  57. destination.PageIndex = currentParam.DestinationPageIndex;
  58. gotoAction.SetDestination(PDFDoc,destination);
  59. linkAnnot.SetLinkAction(gotoAction);
  60. }
  61. break;
  62. case C_ACTION_TYPE.ACTION_TYPE_URI:
  63. {
  64. CPDFUriAction uriAction = new CPDFUriAction();
  65. if(!string.IsNullOrEmpty(currentParam.Uri))
  66. {
  67. uriAction.SetUri(currentParam.Uri);
  68. }
  69. linkAnnot.SetLinkAction(uriAction);
  70. }
  71. break;
  72. default:
  73. break;
  74. }
  75. linkAnnot.SetRect(currentParam.ClientRect);
  76. if (!string.IsNullOrEmpty(currentParam.Author))
  77. {
  78. linkAnnot.SetAuthor(currentParam.Author);
  79. }
  80. if (!string.IsNullOrEmpty(currentParam.Content))
  81. {
  82. linkAnnot.SetContent(currentParam.Content);
  83. }
  84. linkAnnot.SetIsLocked(currentParam.Locked);
  85. linkAnnot.SetCreationDate(PDFHelp.GetCurrentPdfTime());
  86. linkAnnot.UpdateAp();
  87. linkAnnot.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 LinkParam == null || PreviousParam as LinkParam == null)
  103. {
  104. return false;
  105. }
  106. if (MakeAnnotValid(CurrentParam))
  107. {
  108. CPDFLinkAnnotation linkAnnot = Annot as CPDFLinkAnnotation;
  109. if (linkAnnot == null || !linkAnnot.IsValid())
  110. {
  111. return false;
  112. }
  113. LinkParam updateParam = (isUndo ? PreviousParam : CurrentParam) as LinkParam;
  114. LinkParam checkParam = (isUndo ? CurrentParam : PreviousParam) as LinkParam;
  115. switch (updateParam.Action)
  116. {
  117. case C_ACTION_TYPE.ACTION_TYPE_GOTO:
  118. {
  119. CPDFGoToAction gotoAction = new CPDFGoToAction();
  120. CPDFDestination destination = new CPDFDestination();
  121. destination.Position_X = updateParam.DestinationPosition.x;
  122. destination.Position_Y = updateParam.DestinationPosition.y;
  123. destination.PageIndex = updateParam.DestinationPageIndex;
  124. gotoAction.SetDestination(PDFDoc, destination);
  125. linkAnnot.SetLinkAction(gotoAction);
  126. }
  127. break;
  128. case C_ACTION_TYPE.ACTION_TYPE_URI:
  129. {
  130. CPDFUriAction uriAction = new CPDFUriAction();
  131. if(!string.IsNullOrEmpty(updateParam.Uri))
  132. {
  133. uriAction.SetUri(updateParam.Uri);
  134. }
  135. linkAnnot.SetLinkAction(uriAction);
  136. }
  137. break;
  138. default:
  139. break;
  140. }
  141. if (!updateParam.ClientRect.Equals(checkParam.ClientRect))
  142. {
  143. linkAnnot.SetRect(updateParam.ClientRect);
  144. }
  145. if (updateParam.Author != checkParam.Author)
  146. {
  147. linkAnnot.SetAuthor(updateParam.Author);
  148. }
  149. if (updateParam.Content != checkParam.Content)
  150. {
  151. linkAnnot.SetContent(updateParam.Content);
  152. }
  153. if (updateParam.Locked != checkParam.Locked)
  154. {
  155. linkAnnot.SetIsLocked(updateParam.Locked);
  156. }
  157. linkAnnot.SetModifyDate(PDFHelp.GetCurrentPdfTime());
  158. linkAnnot.UpdateAp();
  159. return true;
  160. }
  161. return false;
  162. }
  163. internal override bool Remove()
  164. {
  165. if (MakeAnnotValid(CurrentParam))
  166. {
  167. Annot.RemoveAnnot();
  168. return true;
  169. }
  170. return false;
  171. }
  172. }
  173. }