|
@@ -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;
|