浏览代码

Merge branch 'dev' of http://git.kdan.cc:8865/Windows/PDFOffice_Windows_exe into dev

ZhouJieSheng 1 年之前
父节点
当前提交
888cf778ca

+ 73 - 21
PDF Office/Helper/ChatGTPAIHelper.cs

@@ -1,6 +1,7 @@
 using ComPDFKit_Conversion.Options;
 using DryIoc;
 using ImTools;
+using Microsoft.Office.Interop.Word;
 using Newtonsoft.Json;
 using Newtonsoft.Json.Linq;
 using PDF_Master.Properties;
@@ -66,7 +67,7 @@ namespace PDF_Master.Helper
 
         #region AI服务器对接接口
         /// <summary>
-        /// 翻译指定内容,可以指定语言
+        /// 获取文件Key用来发送给服务端获取翻译文件
         /// </summary>
         /// <param name="content"></param>
         /// <param name="filename"></param>
@@ -162,12 +163,12 @@ namespace PDF_Master.Helper
                 {
                     //从网络异常信息里解析错误,后面的逻辑根据错误码判断会更准确
                     statusCode = (int)((HttpWebResponse)ex.Response).StatusCode;
-                   
+
                 }
                 Trace.WriteLine("HTTP异常:" + ex.Message);
                 if (statusCode == 401)
                 {
-                    return "Code"+"401";
+                    return "Code" + "401";
                 }
                 else
                 {
@@ -201,20 +202,25 @@ namespace PDF_Master.Helper
             return path;
         }
 
-        
+
 
         /// <summary>
         /// 翻译指定内容,可以指定语言
         /// </summary>
-        /// <param name="fileKey">文本Key</param>
+        /// <param name="content">文件</param>
         /// <param name="fromlanguage">文本语言</param>
         /// <param name="tolanguage">需要翻译的语言</param>
-        /// <returns></returns>
+        /// <param name="isprocess">是否需要阅读也进度条</param>
+        /// <returns>code与文件路径</returns>
         public static async Task<string> fileTranslate(string content, string fromlanguage, string tolanguage, bool isprocess = true)
         {
             string Code = "300";
-            intProcess(isprocess,"Translating", System.Windows.Visibility.Visible);
-            
+            intProcess(isprocess, "Translating", System.Windows.Visibility.Visible);
+            //判断word文档大小
+            if (!GetDocumentPagesWord(content)) {
+                clossProcess();
+                return "05011";
+            }
             FileInfo file = new FileInfo(content);
             string fileKey = await fileKeyTranslate(content, file.Name);
             App.mainWindowViewModel.Value = 1;
@@ -224,14 +230,14 @@ namespace PDF_Master.Helper
                 return fileKey.Replace("Code", "");
             }
             HttpWebResponse response = null;
-            ServicePointManager.DefaultConnectionLimit =200;
+            ServicePointManager.DefaultConnectionLimit = 200;
             HttpWebRequest request = (HttpWebRequest)HttpWebRequest.Create(Uri_filetranslate);
             //关闭请求
-            DocumentaryTranslationDialogViewModel.CancelProgress=()=> 
+            DocumentaryTranslationDialogViewModel.CancelProgress = () =>
             {
                 if (request != null)
                 {
-                    Code =  "-1";
+                    Code = "-1";
                     request.Abort();
                 }
             };
@@ -240,10 +246,10 @@ namespace PDF_Master.Helper
             {
                 if (request != null)
                 {
-                    Code =  "-1";
-                    request.Abort(); 
+                    Code = "-1";
+                    request.Abort();
                 }
-            }; 
+            };
             request.Method = "Post";
             request.ContentType = "application/json";
             //request.Accept = "application/vnd.api+json;version=1";
@@ -321,7 +327,7 @@ namespace PDF_Master.Helper
                     return jobject["code"].ToObject<string>().ToLower();
                 }
             }
-            catch(WebException ex)
+            catch (WebException ex)
             {
                 clossProcess();
                 int statusCode = 0;
@@ -335,7 +341,8 @@ namespace PDF_Master.Helper
                 {
                     return "401";
                 }
-                else {
+                else
+                {
                     //-1为自己取消,300为一般错误
                     return Code;
                 }
@@ -415,7 +422,7 @@ namespace PDF_Master.Helper
                     return jobject["code"].ToObject<string>().ToLower();
                 }
             }
-            catch(WebException ex)
+            catch (WebException ex)
             {
                 int statusCode = 0;
                 if (ex.Response is HttpWebResponse)
@@ -559,7 +566,7 @@ namespace PDF_Master.Helper
                 }
                 if (statusCode == 401)
                 {
-                    ChatGPTCode= "401";
+                    ChatGPTCode = "401";
                 }
                 else ChatGPTCode = "300";
                 clossProcess();
@@ -594,6 +601,49 @@ namespace PDF_Master.Helper
             App.mainWindowViewModel.IsProcessVisible = System.Windows.Visibility.Collapsed;
             App.mainWindowViewModel.ProcessCloseBtnVisible = System.Windows.Visibility.Visible;
         }
+
+        /// <summary>
+        /// 判断word文档页面大小,如果接口有问题则交给服务器判断
+        /// </summary>
+        /// <param name="filepath"></param>
+        /// <returns>返回值为是否拿到页码没拿到则为true,页面过小为true</returns>
+        public static bool GetDocumentPagesWord(string filepath)
+        {
+            try
+            {
+                FileInfo fileInfo = new FileInfo(filepath);
+                if (fileInfo.Extension.ToLower()!=".pdf")
+                {
+                    Microsoft.Office.Interop.Word.Application myWordApp = new Microsoft.Office.Interop.Word.Application();
+                    object Nothing = System.Reflection.Missing.Value;
+                    object oMissing = System.Reflection.Missing.Value;
+                    //这里是Word文件的路径
+                    object filePath = filepath;
+                    //打开文件
+                    Document myWordDoc = myWordApp.Documents.Open(
+                        ref filePath, ref oMissing, ref oMissing, ref oMissing,
+                        ref oMissing, ref oMissing, ref oMissing, ref oMissing,
+                        ref oMissing, ref oMissing, ref oMissing, ref oMissing,
+                        ref oMissing, ref oMissing, ref oMissing, ref oMissing);
+                    //下面是取得打开文件的页数
+                    int pages = myWordDoc.ComputeStatistics(WdStatistic.wdStatisticPages, ref Nothing);
+                    //关闭文件
+                    myWordDoc.Close(ref oMissing, ref oMissing, ref oMissing);
+                    //退出Word程序
+                    myWordApp.Quit(ref Nothing, ref Nothing, ref Nothing);
+                    if (pages > 30)
+                    {
+                        return false;
+                    }
+                }
+                return true;
+
+            }
+            catch
+            {
+                return true;
+            }
+        }
         #endregion
 
         #region 支持翻译的语言
@@ -739,9 +789,11 @@ namespace PDF_Master.Helper
 
         #region 错误码
 
-        public static string GetBaiduTranslationCode(string code) {
-            
-            if (!String.IsNullOrEmpty(App.HomePageLoader.GetString("BaiduTranslation" + code))) {
+        public static string GetBaiduTranslationCode(string code)
+        {
+
+            if (!String.IsNullOrEmpty(App.HomePageLoader.GetString("BaiduTranslation" + code)))
+            {
                 return App.HomePageLoader.GetString("BaiduTranslation" + code);
             }
             return code;

+ 5 - 1
PDF Office/ViewModels/Dialog/ChatGPTAIDialogs/DocumentaryTranslationDialogViewModel.cs

@@ -320,7 +320,11 @@ namespace PDF_Master.ViewModels.Dialog.ChatGPTAIDialogs
                 else
                 {
                     //newfile code
-                    if (newfile == "-1") { return; }
+                    if (newfile == "-1")
+                    {
+                        Value = 9;
+                        ProgressVisible = Visibility.Collapsed; return; 
+                    }
                     AlertsMessage alertsMessage = new AlertsMessage();
                     alertsMessage.ShowDialog("",  ChatGTPAIHelper.GetBaiduTranslationCode(newfile), App.ServiceLoader.GetString("Text_ok"));
                 }

+ 5 - 34
PDF Office/ViewModels/HomePanel/ChatGPTAI/ChatGPTAITranslationContentViewModel.cs

@@ -230,6 +230,8 @@ namespace PDF_Master.ViewModels.HomePanel.ChatGPTAI
             dispatcherTimer.Stop();
         }
 
+       
+
         /// <summary>
         /// 选择文件逻辑翻译,word pdf
         /// </summary>
@@ -260,7 +262,7 @@ namespace PDF_Master.ViewModels.HomePanel.ChatGPTAI
                 //判断文件大小
                 if ((((float)fileInfo.Length) / 1024 / 1024) > 10)
                 {
-                    ErrorTipText = "The uploaded file cannot exceed 10MB";
+                    ErrorTipText = "File translation file size cannot exceed 10MB";
                     ErrorTipVisible = Visibility.Visible;
                     return;
                 }
@@ -270,43 +272,12 @@ namespace PDF_Master.ViewModels.HomePanel.ChatGPTAI
                     CPDFDocument cPDFDocument = CPDFDocument.InitWithFilePath(dialog.FileName);
                     if (cPDFDocument.PageCount > 30)
                     {
-                        ErrorTipText = "The uploaded file cannot exceed 10MB";
+                        ErrorTipText = "Documents over 30 pages";
                         ErrorTipVisible = Visibility.Visible;
                         return;
                     }
                 }
-                else
-                {
-                    try
-                    {
-                        Microsoft.Office.Interop.Word.Application myWordApp = new Microsoft.Office.Interop.Word.Application();
-                        object Nothing = System.Reflection.Missing.Value;
-                        object oMissing = System.Reflection.Missing.Value;
-                        object filePath = dialog.FileName; //这里是Word文件的路径
-                                                           //打开文件
-                        Document myWordDoc = myWordApp.Documents.Open(
-                            ref filePath, ref oMissing, ref oMissing, ref oMissing,
-                            ref oMissing, ref oMissing, ref oMissing, ref oMissing,
-                            ref oMissing, ref oMissing, ref oMissing, ref oMissing,
-                            ref oMissing, ref oMissing, ref oMissing, ref oMissing);
-                        //下面是取得打开文件的页数
-                        int pages = myWordDoc.ComputeStatistics(WdStatistic.wdStatisticPages, ref Nothing);
-                        //关闭文件
-                        myWordDoc.Close(ref oMissing, ref oMissing, ref oMissing);
-                        //退出Word程序
-                        myWordApp.Quit(ref Nothing, ref Nothing, ref Nothing);
-                        if (pages > 30)
-                        {
-                            ErrorTipText = "The uploaded file cannot exceed 30pages";
-                            ErrorTipVisible = Visibility.Visible;
-                            return;
-                        }
-                    }
-                    catch
-                    {
-
-                    }
-                }
+                
                 string newfile = "";
                 await Task.Run(async delegate
                 {

+ 11 - 10
PDF Office/ViewModels/Tools/AnnotToolContentViewModel.Command.cs

@@ -1452,16 +1452,17 @@ namespace PDF_Master.ViewModels.Tools
                             break;
 
                         case ActionType.Modify:
-                            //if (string.IsNullOrEmpty(StrAnnotToolChecked) == false)
-                            //{
-                            //    List<AnnotHandlerEventArgs> selectedArgs = new List<AnnotHandlerEventArgs>() { annot };
-                            //    switch (annot.EventType)
-                            //    {
-                            //        case AnnotArgsType.AnnotFreeText://文本
-                            //            GetFreetext(selectedArgs);
-                            //            break;
-                            //    }
-                            //}
+                            //undo/redo,字体大小联动,刷新属性面板数据
+                            if (string.IsNullOrEmpty(StrAnnotToolChecked) == false)
+                            {
+                                List<AnnotHandlerEventArgs> selectedArgs = new List<AnnotHandlerEventArgs>() { annot };
+                                switch (annot.EventType)
+                                {
+                                    case AnnotArgsType.AnnotFreeText://文本
+                                        GetFreetext(selectedArgs);
+                                        break;
+                                }
+                            }
                             if (viewModel != null)
                             {
                                 int pageindex = editEvent.PageIndex;

+ 3 - 1
PDF Office/Views/BOTA/BookmarkContent.xaml.cs

@@ -435,7 +435,9 @@ namespace PDF_Master.Views.BOTA
                 MenuItem rename = contextMenu.Items[0] as MenuItem;
                 MenuItem editPageIndex = contextMenu.Items[1] as MenuItem;
                 MenuItem del = contextMenu.Items[2] as MenuItem;
-                if (BookMarkListView.SelectedItems.Count >= 2)
+                rename.IsEnabled = true;
+                editPageIndex.IsEnabled = true;
+                if (BookMarkListView.SelectedItems.Count > 1)
                 {
                     rename.IsEnabled = false;
                     editPageIndex.IsEnabled = false;

+ 0 - 33
PDF Office/Views/HomePanel/ChatGPTAI/ChatGPTAITranslationContent.xaml.cs

@@ -94,39 +94,6 @@ namespace PDF_Master.Views.HomePanel.ChatGPTAI
                                     return;
                                 }
                             }
-                            else
-                            {
-                                try
-                                {
-                                    Microsoft.Office.Interop.Word.Application myWordApp = new Microsoft.Office.Interop.Word.Application();
-                                    object Nothing = System.Reflection.Missing.Value;
-                                    object oMissing = System.Reflection.Missing.Value;
-                                    object filePath = dropFile; //这里是Word文件的路径
-                                                                //打开文件
-                                    Document myWordDoc = myWordApp.Documents.Open(
-                                        ref filePath, ref oMissing, ref oMissing, ref oMissing,
-                                        ref oMissing, ref oMissing, ref oMissing, ref oMissing,
-                                        ref oMissing, ref oMissing, ref oMissing, ref oMissing,
-                                        ref oMissing, ref oMissing, ref oMissing, ref oMissing);
-                                    //下面是取得打开文件的页数
-                                    int pages = myWordDoc.ComputeStatistics(WdStatistic.wdStatisticPages, ref Nothing);
-                                    //关闭文件
-                                    myWordDoc.Close(ref oMissing, ref oMissing, ref oMissing);
-                                    //退出Word程序
-                                    myWordApp.Quit(ref Nothing, ref Nothing, ref Nothing);
-                                    if (pages > 30)
-                                    {
-                                        viewModel.ErrorTipText = "The uploaded file cannot exceed 30Pages";
-                                        viewModel.ErrorTipVisible = Visibility.Visible;
-                                        isFirst = false;
-                                        return;
-                                    }
-                                }
-                                catch
-                                {
-
-                                }
-                            }
                             string newfile = "";
                             await System.Threading.Tasks.Task.Run(async delegate
                             {