CPDFPageExtractWindow.xaml.cs 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147
  1. using Compdfkit_Tools.Helper;
  2. using Compdfkit_Tools.PDFControlUI;
  3. using System;
  4. using System.Collections.Generic;
  5. using System.Windows;
  6. using MessageBox = System.Windows.MessageBox;
  7. namespace Compdfkit_Tools.PDFControl
  8. {
  9. public partial class CPDFPageExtractWindow : Window
  10. {
  11. private string pageRangeString = string.Empty;
  12. private List<int> pageRangeList = new List<int>();
  13. public ExtractEventClass extractEventClass = new ExtractEventClass();
  14. public delegate void DialogCloseEventHandler(object sender, ExtractDialogCloseEventArgs e);
  15. public event DialogCloseEventHandler DialogClosed;
  16. public CPDFPageExtractWindow()
  17. {
  18. InitializeComponent();
  19. }
  20. public void InitPageExtractWindow(string customPageRange, int maxPage)
  21. {
  22. if (customPageRange == string.Empty)
  23. {
  24. CPDFPageExtractUI.PageRange = PageRange.AllPages;
  25. extractEventClass.PageMode = 1;
  26. }
  27. else
  28. {
  29. CPDFPageExtractUI.PageRange = PageRange.CustomPages;
  30. extractEventClass.PageMode = 4;
  31. pageRangeString = customPageRange;
  32. CommonHelper.GetPagesInRange(ref pageRangeList, pageRangeString, CPDFPageExtractUI.MaxIndex, new char[] { ',' }, new char[] { '-' });
  33. for (int i = 0; i < pageRangeList.Count; i++)
  34. {
  35. pageRangeList[i]++;
  36. }
  37. }
  38. CPDFPageExtractUI.MaxIndex = maxPage;
  39. CPDFPageExtractUI.CurrentPageRange = customPageRange;
  40. }
  41. private void CPDFPageExtractUI_Loaded(object sender, RoutedEventArgs e)
  42. {
  43. CPDFPageExtractUI.PageRangeChanged += CPDFPageExtractUI_PageRangeChanged;
  44. CPDFPageExtractUI.CustomPageRangeChanged += CPDFPageExtractUI_CustomPageRangeChanged;
  45. CPDFPageExtractUI.SeparateChanged += CPDFPageExtractUI_SeparateChanged;
  46. CPDFPageExtractUI.DeleteChanged += CPDFPageExtractUI_DeleteChanged;
  47. CPDFPageExtractUI.ExtractEvent += CPDFPageExtractUI_ExtractEvent;
  48. CPDFPageExtractUI.CancelEvent += CPDFPageExtractUI_CancelEvent;
  49. }
  50. private void CPDFPageExtractUI_CancelEvent(object sender, EventArgs e)
  51. {
  52. CloseWindow(null);
  53. }
  54. private void CPDFPageExtractUI_ExtractEvent(object sender, EventArgs e)
  55. {
  56. if (extractEventClass.PageMode != 4)
  57. {
  58. if (extractEventClass.PageMode == 1 && extractEventClass.DeleteAfterExtract)
  59. {
  60. MessageBox.Show(" Please keep at least one page.", "Alert", MessageBoxButton.OK, MessageBoxImage.Warning);
  61. return;
  62. }
  63. if (extractEventClass.PageMode == 3 && CPDFPageExtractUI.MaxIndex == 1)
  64. {
  65. MessageBox.Show(" No page will be extracted.", "Alert", MessageBoxButton.OK, MessageBoxImage.Warning);
  66. return;
  67. }
  68. CloseWindow(extractEventClass);
  69. }
  70. else if(extractEventClass.PageMode == 4 && CommonHelper.GetPagesInRange(ref pageRangeList, pageRangeString, CPDFPageExtractUI.MaxIndex, new char[] { ',' }, new char[] { '-' }))
  71. {
  72. if (pageRangeList.Count == CPDFPageExtractUI.MaxIndex && extractEventClass.DeleteAfterExtract)
  73. {
  74. MessageBox.Show(" Please keep at least one page.", "Alert", MessageBoxButton.OK, MessageBoxImage.Warning);
  75. return;
  76. }
  77. extractEventClass.PageName = pageRangeString;
  78. for (int i = 0; i < pageRangeList.Count; i++)
  79. {
  80. pageRangeList[i]++;
  81. }
  82. extractEventClass.PageParm = pageRangeList;
  83. CloseWindow(extractEventClass);
  84. }
  85. else
  86. {
  87. MessageBox.Show("Please check page range.", "Alert", MessageBoxButton.OK, MessageBoxImage.Warning);
  88. }
  89. }
  90. private void CPDFPageExtractUI_CustomPageRangeChanged(object sender, string e)
  91. {
  92. pageRangeString = e;
  93. }
  94. private void CPDFPageExtractUI_DeleteChanged(object sender, bool e)
  95. {
  96. extractEventClass.DeleteAfterExtract = e;
  97. }
  98. private void CPDFPageExtractUI_SeparateChanged(object sender, bool e)
  99. {
  100. extractEventClass.ExtractToSingleFile = e;
  101. }
  102. private void CPDFPageExtractUI_PageRangeChanged(object sender, PageRange e)
  103. {
  104. extractEventClass.PageMode = (int)e;
  105. }
  106. private void CPDFPageExtractUI_Unloaded(object sender, RoutedEventArgs e)
  107. {
  108. CPDFPageExtractUI.PageRangeChanged -= CPDFPageExtractUI_PageRangeChanged;
  109. CPDFPageExtractUI.CustomPageRangeChanged -= CPDFPageExtractUI_CustomPageRangeChanged;
  110. CPDFPageExtractUI.SeparateChanged -= CPDFPageExtractUI_SeparateChanged;
  111. CPDFPageExtractUI.DeleteChanged -= CPDFPageExtractUI_DeleteChanged;
  112. CPDFPageExtractUI.ExtractEvent -= CPDFPageExtractUI_ExtractEvent;
  113. CPDFPageExtractUI.CancelEvent -= CPDFPageExtractUI_CancelEvent;
  114. }
  115. private void CloseWindow(ExtractEventClass dialogResult)
  116. {
  117. DialogClosed?.Invoke(this, new ExtractDialogCloseEventArgs(dialogResult));
  118. Close();
  119. }
  120. }
  121. public class ExtractDialogCloseEventArgs : EventArgs
  122. {
  123. public ExtractEventClass DialogResult { get; set; }
  124. public ExtractDialogCloseEventArgs(ExtractEventClass dialogResult)
  125. {
  126. DialogResult = dialogResult;
  127. }
  128. }
  129. }