ChatGPTAITranslationContent.xaml.cs 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180
  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. try
  39. {
  40. Point iconPoint = e.GetPosition(this);
  41. dropHelper?.Drop((System.Runtime.InteropServices.ComTypes.IDataObject)e.Data, ref iconPoint, e.Effects);
  42. }
  43. catch
  44. {
  45. }
  46. string dropFile = "Drop";
  47. if (e.Data.GetDataPresent(System.Windows.DataFormats.FileDrop))
  48. {
  49. //BtnBlank.IsEnabled = true;
  50. int count = ((System.Array)e.Data.GetData(System.Windows.DataFormats.FileDrop)).Length;
  51. for (int i = 0; i < count; i++)
  52. {
  53. dropFile = ((System.Array)e.Data.GetData(System.Windows.DataFormats.FileDrop)).GetValue(i).ToString();
  54. 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"))
  55. {
  56. if (!isFirst)
  57. {
  58. //添加付费拦截锁
  59. if (!ServiceHelper.IAPBeforeFunction())
  60. {
  61. return;
  62. }
  63. if (Settings.Default.UserDate.subscribestatus != 1)
  64. {
  65. App.mainWindowViewModel.dialogs.ShowDialog(DialogNames.IAPCompareDialog);
  66. return;
  67. }
  68. isFirst = true;
  69. viewModel.ErrorTipVisible = Visibility.Collapsed;
  70. FileInfo fileInfo = new FileInfo(dropFile);
  71. //判断文件大小
  72. if ((((float)fileInfo.Length) / 1024 / 1024) > 10)
  73. {
  74. viewModel.ErrorTipText = "The uploaded file cannot exceed 10MB";
  75. viewModel.ErrorTipVisible = Visibility.Visible;
  76. isFirst = false;
  77. return;
  78. }
  79. //判断文件页数
  80. if (fileInfo.Extension.ToLower() == ".pdf")
  81. {
  82. CPDFDocument cPDFDocument = CPDFDocument.InitWithFilePath(dropFile);
  83. if (cPDFDocument.PageCount > 30)
  84. {
  85. viewModel.ErrorTipText = "The uploaded file cannot exceed 30Pages";
  86. viewModel.ErrorTipVisible = Visibility.Visible;
  87. isFirst = false;
  88. return;
  89. }
  90. }
  91. string newfile = "";
  92. await System.Threading.Tasks.Task.Run(async delegate
  93. {
  94. newfile = await ChatGTPAIHelper.fileTranslate(dropFile, viewModel.fromlanguage, viewModel.tolanguage);
  95. });
  96. if (!string.IsNullOrEmpty(newfile))
  97. {
  98. if (File.Exists(newfile))
  99. {
  100. if (viewModel.homeContentViewModel != null)
  101. {
  102. viewModel.homeContentViewModel.OpenFile(new string[] { newfile });
  103. isFirst = false;
  104. return;
  105. }
  106. }
  107. }
  108. isFirst = false;
  109. viewModel.ErrorTipText = newfile;
  110. viewModel.ErrorTipVisible = Visibility.Visible;
  111. }
  112. }
  113. }
  114. }
  115. }
  116. private void MainPage_DragEnter(object sender, DragEventArgs e)
  117. {
  118. DropColorBorder.Visibility = Visibility.Visible;
  119. //BtnBlank.IsEnabled = false;
  120. //页面编辑和缩略图
  121. if ((e.OriginalSource as Image) != null)
  122. return;
  123. try
  124. {
  125. if (dropHelper == null)
  126. {
  127. dropHelper = Activator.CreateInstance(Type.GetTypeFromCLSID(new Guid("4657278A-411B-11D2-839A-00C04FD918D0"))) as IDropTargetHelper;
  128. }
  129. Point iconPoint = e.GetPosition(this);
  130. dropHelper?.DragEnter(new WindowInteropHelper(Window.GetWindow(this)).Handle, (System.Runtime.InteropServices.ComTypes.IDataObject)e.Data, ref iconPoint, e.Effects);
  131. }
  132. catch
  133. {
  134. }
  135. }
  136. private void MainPage_DragOver(object sender, DragEventArgs e)
  137. {
  138. if ((e.OriginalSource as Image) != null)
  139. return;
  140. try
  141. {
  142. Point iconPoint = e.GetPosition(this);
  143. dropHelper?.DragOver(ref iconPoint, e.Effects);
  144. }
  145. catch
  146. {
  147. }
  148. }
  149. private void MainPage_DragLeave(object sender, DragEventArgs e)
  150. {
  151. DropColorBorder.Visibility = Visibility.Collapsed;
  152. // BtnBlank.IsEnabled = true;
  153. if ((e.OriginalSource as Image) != null)
  154. return;
  155. try
  156. {
  157. dropHelper?.DragLeave();
  158. }
  159. catch
  160. {
  161. }
  162. }
  163. }
  164. }