ChatGPTAITranslationContent.xaml.cs 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184
  1. using ComPDFKitViewer;
  2. using PDF_Master.Helper;
  3. using System.Data;
  4. using System.Windows;
  5. using System;
  6. using System.Windows.Controls;
  7. using System.Windows.Interop;
  8. using PDF_Master.ViewModels.Dialog.HomePageToolsDialogs.HomePageBatchProcessing;
  9. using PDF_Master.ViewModels.HomePanel.ChatGPTAI;
  10. using Microsoft.Office.Core;
  11. using PDF_Master.ViewModels;
  12. using System.IO;
  13. using Point = System.Windows.Point;
  14. using Window = System.Windows.Window;
  15. using System.Threading.Tasks;
  16. using PDF_Master.Model;
  17. using PDF_Master.Properties;
  18. using Prism.Services.Dialogs;
  19. using ComPDFKit.PDFDocument;
  20. using Microsoft.Office.Interop.Word;
  21. namespace PDF_Master.Views.HomePanel.ChatGPTAI
  22. {
  23. /// <summary>
  24. /// Interaction logic for ChatGPTAITranslationContent
  25. /// </summary>
  26. public partial class ChatGPTAITranslationContent : UserControl
  27. {
  28. private ChatGPTAITranslationContentViewModel viewModel;
  29. private bool isFirst = false;
  30. private IDropTargetHelper dropHelper;
  31. public ChatGPTAITranslationContent()
  32. {
  33. InitializeComponent();
  34. viewModel = this.DataContext as ChatGPTAITranslationContentViewModel;
  35. }
  36. private async void Grid_Drop(object sender, DragEventArgs e)
  37. {
  38. DropColorBorder.Visibility = Visibility.Hidden;
  39. NormalColorBorder.Visibility = Visibility.Visible;
  40. try
  41. {
  42. Point iconPoint = e.GetPosition(this);
  43. dropHelper?.Drop((System.Runtime.InteropServices.ComTypes.IDataObject)e.Data, ref iconPoint, e.Effects);
  44. }
  45. catch
  46. {
  47. }
  48. string dropFile = "Drop";
  49. if (e.Data.GetDataPresent(System.Windows.DataFormats.FileDrop))
  50. {
  51. //BtnBlank.IsEnabled = true;
  52. int count = ((System.Array)e.Data.GetData(System.Windows.DataFormats.FileDrop)).Length;
  53. for (int i = 0; i < count; i++)
  54. {
  55. dropFile = ((System.Array)e.Data.GetData(System.Windows.DataFormats.FileDrop)).GetValue(i).ToString();
  56. if (dropFile.ToLower().EndsWith("pdf") || dropFile.ToLower().EndsWith(".doc") || dropFile.ToLower().EndsWith(".docx") || dropFile.ToLower().EndsWith(".docm") || dropFile.ToLower().EndsWith(".dot") || dropFile.ToLower().EndsWith(".dotx") || dropFile.ToLower().EndsWith(".dotm"))
  57. {
  58. if (!isFirst)
  59. {
  60. //添加付费拦截锁
  61. if (!ServiceHelper.IAPBeforeFunction())
  62. {
  63. return;
  64. }
  65. if (Settings.Default.UserDate.subscribestatus != 1)
  66. {
  67. App.mainWindowViewModel.dialogs.ShowDialog(DialogNames.IAPCompareDialog);
  68. return;
  69. }
  70. isFirst = true;
  71. viewModel.ErrorTipVisible = Visibility.Collapsed;
  72. FileInfo fileInfo = new FileInfo(dropFile);
  73. //判断文件大小
  74. if ((((float)fileInfo.Length) / 1024 / 1024) > 10)
  75. {
  76. viewModel.ErrorTipText = "The uploaded file cannot exceed 10MB";
  77. viewModel.ErrorTipVisible = Visibility.Visible;
  78. isFirst = false;
  79. return;
  80. }
  81. //判断文件页数
  82. if (fileInfo.Extension.ToLower() == ".pdf")
  83. {
  84. CPDFDocument cPDFDocument = CPDFDocument.InitWithFilePath(dropFile);
  85. if (cPDFDocument.PageCount > 30)
  86. {
  87. viewModel.ErrorTipText = "The uploaded file cannot exceed 30Pages";
  88. viewModel.ErrorTipVisible = Visibility.Visible;
  89. isFirst = false;
  90. return;
  91. }
  92. }
  93. string newfile = "";
  94. await System.Threading.Tasks.Task.Run(async delegate
  95. {
  96. newfile = await ChatGTPAIHelper.fileTranslate(dropFile, viewModel.fromlanguage, viewModel.tolanguage);
  97. });
  98. if (!string.IsNullOrEmpty(newfile))
  99. {
  100. if (File.Exists(newfile))
  101. {
  102. if (viewModel.homeContentViewModel != null)
  103. {
  104. viewModel.homeContentViewModel.OpenFile(new string[] { newfile });
  105. isFirst = false;
  106. return;
  107. }
  108. }
  109. }
  110. isFirst = false;
  111. if (newfile == "-1") { return; }
  112. viewModel.ErrorTipText = ChatGTPAIHelper.GetBaiduTranslationCode(newfile);
  113. viewModel.ErrorTipVisible = Visibility.Visible;
  114. }
  115. }
  116. }
  117. }
  118. }
  119. private void MainPage_DragEnter(object sender, DragEventArgs e)
  120. {
  121. DropColorBorder.Visibility = Visibility.Visible;
  122. NormalColorBorder.Visibility = Visibility.Hidden;
  123. //BtnBlank.IsEnabled = false;
  124. //页面编辑和缩略图
  125. if ((e.OriginalSource as Image) != null)
  126. return;
  127. try
  128. {
  129. if (dropHelper == null)
  130. {
  131. dropHelper = Activator.CreateInstance(Type.GetTypeFromCLSID(new Guid("4657278A-411B-11D2-839A-00C04FD918D0"))) as IDropTargetHelper;
  132. }
  133. Point iconPoint = e.GetPosition(this);
  134. dropHelper?.DragEnter(new WindowInteropHelper(Window.GetWindow(this)).Handle, (System.Runtime.InteropServices.ComTypes.IDataObject)e.Data, ref iconPoint, e.Effects);
  135. }
  136. catch
  137. {
  138. }
  139. }
  140. private void MainPage_DragOver(object sender, DragEventArgs e)
  141. {
  142. if ((e.OriginalSource as Image) != null)
  143. return;
  144. try
  145. {
  146. Point iconPoint = e.GetPosition(this);
  147. dropHelper?.DragOver(ref iconPoint, e.Effects);
  148. }
  149. catch
  150. {
  151. }
  152. }
  153. private void MainPage_DragLeave(object sender, DragEventArgs e)
  154. {
  155. DropColorBorder.Visibility = Visibility.Hidden;
  156. NormalColorBorder.Visibility = Visibility.Visible;
  157. // BtnBlank.IsEnabled = true;
  158. if ((e.OriginalSource as Image) != null)
  159. return;
  160. try
  161. {
  162. dropHelper?.DragLeave();
  163. }
  164. catch
  165. {
  166. }
  167. }
  168. }
  169. }