|
@@ -16,11 +16,14 @@ using System.Linq;
|
|
|
using System.Text;
|
|
|
using System.Threading.Tasks;
|
|
|
using System.Windows;
|
|
|
-using System.Windows.Controls;
|
|
|
using PDF_Office.Model;
|
|
|
using System.ComponentModel;
|
|
|
using PDF_Office.Helper;
|
|
|
using PDFSettings.Settings;
|
|
|
+using System.Drawing;
|
|
|
+using System.IO;
|
|
|
+using System.Drawing.Imaging;
|
|
|
+using ComPDFKit.PDFDocument;
|
|
|
|
|
|
namespace PDF_Office.ViewModels
|
|
|
{
|
|
@@ -58,6 +61,8 @@ namespace PDF_Office.ViewModels
|
|
|
set { SetProperty(ref fileChanged, value); }
|
|
|
}
|
|
|
|
|
|
+ private bool isNewDocument = false;
|
|
|
+
|
|
|
public CPDFViewer PDFViewer { get; set; }
|
|
|
|
|
|
|
|
@@ -113,6 +118,10 @@ namespace PDF_Office.ViewModels
|
|
|
}
|
|
|
|
|
|
|
|
|
+ /// <summary>
|
|
|
+ /// 打开指定路径的PDF文件
|
|
|
+ /// </summary>
|
|
|
+ /// <param name="filePath"></param>
|
|
|
public void OpenFile(string filePath)
|
|
|
{
|
|
|
var result = LoadFileFormPath(filePath);
|
|
@@ -122,28 +131,40 @@ namespace PDF_Office.ViewModels
|
|
|
}
|
|
|
FilePath = filePath;
|
|
|
|
|
|
- NavigationParameters parameters = new NavigationParameters {
|
|
|
- { ParameterNames.MainViewModel, this },
|
|
|
- { ParameterNames.PDFViewer,PDFViewer}
|
|
|
- };
|
|
|
- System.Windows.Application.Current.Dispatcher.BeginInvoke(System.Windows.Threading.DispatcherPriority.Send, new Action(() =>
|
|
|
- {
|
|
|
- if (toolregion.Regions.ContainsRegionWithName(MainContentRegionName))
|
|
|
- toolregion.RequestNavigate(MainContentRegionName, "ViewContent", parameters);
|
|
|
- }));
|
|
|
+ NavigateToViewContent();
|
|
|
|
|
|
//检查是否是新文档
|
|
|
OpenFileInfo isnew = SettingHelper.GetFileInfo(filePath);
|
|
|
if (isnew == null)
|
|
|
{
|
|
|
isNewDocument = true;
|
|
|
- if(App.OpenedFileList.Contains(filePath) == false)
|
|
|
+ if (App.OpenedFileList.Contains(filePath) == false)
|
|
|
+ {
|
|
|
App.OpenedFileList.Add(filePath);
|
|
|
- SettingHelper.SortRecentOpenFiles(filePath);
|
|
|
- }
|
|
|
+ }
|
|
|
+
|
|
|
+ }
|
|
|
+ //打开文件后,不管是新文件还是旧文件都需要更新排序
|
|
|
+ SettingHelper.SortRecentOpenFiles(filePath);
|
|
|
+ }
|
|
|
+
|
|
|
+ /// <summary>
|
|
|
+ /// 创建PDFviewer对象后导航到ViewContent
|
|
|
+ /// </summary>
|
|
|
+ private void NavigateToViewContent()
|
|
|
+ {
|
|
|
+ NavigationParameters parameters = new NavigationParameters {
|
|
|
+ { ParameterNames.MainViewModel, this },
|
|
|
+ { ParameterNames.PDFViewer,PDFViewer}
|
|
|
+ };
|
|
|
+ System.Windows.Application.Current.Dispatcher.BeginInvoke(System.Windows.Threading.DispatcherPriority.Send, new Action(() =>
|
|
|
+ {
|
|
|
+ if (toolregion.Regions.ContainsRegionWithName(MainContentRegionName))
|
|
|
+ toolregion.RequestNavigate(MainContentRegionName, "ViewContent", parameters);
|
|
|
+ }));
|
|
|
|
|
|
}
|
|
|
- private bool isNewDocument = false;
|
|
|
+
|
|
|
/// <summary>
|
|
|
/// 从文件路径创建PDFViewer对象,已包含文档解密逻辑
|
|
|
/// </summary>
|
|
@@ -195,6 +216,213 @@ namespace PDF_Office.ViewModels
|
|
|
return true;
|
|
|
}
|
|
|
|
|
|
+ /// <summary>
|
|
|
+ /// 创建文件,路径为空时表示创建空白文档
|
|
|
+ /// 否则表示从图片路径创建PDf
|
|
|
+ /// </summary>
|
|
|
+ /// <param name="imagePath"></param>
|
|
|
+ /// <returns></returns>
|
|
|
+ public bool CreateFile(string imagePath = null)
|
|
|
+ {
|
|
|
+ PDFViewer = new CPDFViewer();
|
|
|
+ PDFViewer.CreateDocument();
|
|
|
+ PDFViewer.UndoManager.PropertyChanged += UndoManager_PropertyChanged;
|
|
|
+ if (PDFViewer.Document == null)
|
|
|
+ {
|
|
|
+ AlertsMessage alertsMessage = new AlertsMessage();
|
|
|
+ alertsMessage.ShowDialog("","创建文件失败.","OK");
|
|
|
+ return false;
|
|
|
+ }
|
|
|
+
|
|
|
+ if (string.IsNullOrEmpty(imagePath))
|
|
|
+ {
|
|
|
+ FileName = "Blank Page.pdf";
|
|
|
+ //默认插入595*842 大小的页面
|
|
|
+ PDFViewer.Document.InsertPage(0, 595, 842, null);
|
|
|
+ }
|
|
|
+ else
|
|
|
+ {
|
|
|
+ FileName = imagePath.Substring(imagePath.LastIndexOf("\\") + 1, imagePath.LastIndexOf(".") - imagePath.LastIndexOf("\\") - 1) + ".pdf";
|
|
|
+ Bitmap pic = new Bitmap(imagePath);
|
|
|
+ int width = pic.Size.Width;
|
|
|
+ int height = pic.Size.Height;
|
|
|
+
|
|
|
+ string ex = System.IO.Path.GetExtension(imagePath);
|
|
|
+ //其他格式或者名称中含空格的图片 在本地先转存一下
|
|
|
+ if ((ex != ".jpg" && ex != ".jpeg") || !Uri.IsWellFormedUriString(imagePath, UriKind.Absolute))
|
|
|
+ {
|
|
|
+ //将其他格式图片转换成jpg
|
|
|
+ string folderPath = Path.GetTempPath();
|
|
|
+ if (File.Exists(folderPath))
|
|
|
+ {
|
|
|
+ File.Delete(folderPath);
|
|
|
+ }
|
|
|
+ DirectoryInfo tempfolder = new DirectoryInfo(folderPath);
|
|
|
+ if (!tempfolder.Exists)
|
|
|
+ {
|
|
|
+ tempfolder.Create();
|
|
|
+ }
|
|
|
+ string targetPath = System.IO.Path.Combine(folderPath, Guid.NewGuid().ToString());
|
|
|
+ imagePath = targetPath;
|
|
|
+ pic.Save(targetPath, ImageFormat.Jpeg);
|
|
|
+ }
|
|
|
+ pic.Dispose();
|
|
|
+
|
|
|
+ var result = PDFViewer.Document.InsertPage(0, width, height, imagePath);
|
|
|
+ if (!result)
|
|
|
+ {
|
|
|
+ AlertsMessage alertsMessage = new AlertsMessage();
|
|
|
+ alertsMessage.ShowDialog("", "创建文件失败.", "OK");
|
|
|
+ return false;
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ //设置背景色
|
|
|
+ ////PDFViewer.SetBackgroundBrush(new SolidColorBrush((Color)System.Windows.Media.ColorConverter.ConvertFromString(Settings.Default.AppProperties.InitialVIew.Background)));
|
|
|
+ PDFViewer.Load();
|
|
|
+ PDFViewer.UndoManager.CanSave = true;
|
|
|
+ FileChanged = Visibility.Visible;
|
|
|
+ PDFViewer.SetFormFieldHighlight(true);
|
|
|
+
|
|
|
+ NavigateToViewContent();
|
|
|
+ return true;
|
|
|
+ }
|
|
|
+
|
|
|
+ /// <summary>
|
|
|
+ /// 从office文件转换成PDF
|
|
|
+ /// </summary>
|
|
|
+ /// <param name="sourcepath"></param>
|
|
|
+ /// <returns></returns>
|
|
|
+ public async Task<bool> CreateFileFromOffice(string sourcepath)
|
|
|
+ {
|
|
|
+ try
|
|
|
+ {
|
|
|
+ string folderPath = Path.GetTempPath();
|
|
|
+ if (File.Exists(folderPath))
|
|
|
+ {
|
|
|
+ File.Delete(folderPath);
|
|
|
+ }
|
|
|
+ DirectoryInfo tempfolder = new DirectoryInfo(folderPath);
|
|
|
+ if (!tempfolder.Exists)
|
|
|
+ {
|
|
|
+ tempfolder.Create();
|
|
|
+ }
|
|
|
+ string targetPath = System.IO.Path.Combine(folderPath, Guid.NewGuid().ToString() + ".pdf");
|
|
|
+
|
|
|
+ string ex = System.IO.Path.GetExtension(sourcepath);
|
|
|
+
|
|
|
+ switch (ex)
|
|
|
+ {
|
|
|
+
|
|
|
+ case ".doc":
|
|
|
+ case ".docx":
|
|
|
+ case "docm":
|
|
|
+ case ".dot":
|
|
|
+ case ".dotx":
|
|
|
+ case ".dotm":
|
|
|
+ case ".txt":
|
|
|
+ await Task.Run(() =>
|
|
|
+ {
|
|
|
+
|
|
|
+ Microsoft.Office.Interop.Word.Application word = new Microsoft.Office.Interop.Word.Application();
|
|
|
+ Microsoft.Office.Interop.Word.Document document = null;
|
|
|
+
|
|
|
+ word.Visible = false;
|
|
|
+ word.ShowWindowsInTaskbar = true;
|
|
|
+ document = word.Documents.Open(sourcepath);
|
|
|
+ document?.ExportAsFixedFormat(targetPath, Microsoft.Office.Interop.Word.WdExportFormat.wdExportFormatPDF);
|
|
|
+ document?.Close();
|
|
|
+ word?.Quit();
|
|
|
+ });
|
|
|
+ break;
|
|
|
+ case ".xls":
|
|
|
+ case ".xlsx":
|
|
|
+ case ".xlsm":
|
|
|
+ case ".xlsb":
|
|
|
+ case ".xlam":
|
|
|
+ case ".xltx":
|
|
|
+ case ".xlt":
|
|
|
+ await Task.Run(() =>
|
|
|
+ {
|
|
|
+ Microsoft.Office.Interop.Excel.Application excele = new Microsoft.Office.Interop.Excel.Application();
|
|
|
+ Microsoft.Office.Interop.Excel.Workbook workbook = null;
|
|
|
+
|
|
|
+ excele.Visible = false;
|
|
|
+ try
|
|
|
+ {
|
|
|
+ workbook = excele.Workbooks.Open(sourcepath);
|
|
|
+ }
|
|
|
+ catch (Exception e)
|
|
|
+ {
|
|
|
+ workbook = excele.Workbooks.Open(sourcepath, 0, true, 5, "", "", true, Microsoft.Office.Interop.Excel.XlPlatform.xlWindows, "t", false, false, 0, true, 1, Microsoft.Office.Interop.Excel.XlCorruptLoad.xlRepairFile);
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+ workbook?.ExportAsFixedFormat(Microsoft.Office.Interop.Excel.XlFixedFormatType.xlTypePDF, targetPath);
|
|
|
+ workbook?.Close();
|
|
|
+ excele?.Quit();
|
|
|
+ });
|
|
|
+ break;
|
|
|
+ case ".ppt":
|
|
|
+ case ".pptx":
|
|
|
+ case ".pptm":
|
|
|
+ case ".pptsx":
|
|
|
+ case ".pps":
|
|
|
+ case ".pptsm":
|
|
|
+ case ".pot":
|
|
|
+ case ".potm":
|
|
|
+ await Task.Run(() =>
|
|
|
+ {
|
|
|
+ Microsoft.Office.Interop.PowerPoint.Application ppt = new Microsoft.Office.Interop.PowerPoint.Application();
|
|
|
+ Microsoft.Office.Interop.PowerPoint.Presentation presentation = null;
|
|
|
+
|
|
|
+ ppt.Visible = Microsoft.Office.Core.MsoTriState.msoCTrue;
|
|
|
+ presentation = ppt.Presentations.Open(sourcepath);
|
|
|
+
|
|
|
+ presentation.ExportAsFixedFormat(targetPath, Microsoft.Office.Interop.PowerPoint.PpFixedFormatType.ppFixedFormatTypePDF);
|
|
|
+ presentation?.Close();
|
|
|
+ ppt?.Quit();
|
|
|
+ });
|
|
|
+ break;
|
|
|
+ }
|
|
|
+
|
|
|
+ PDFViewer = new CPDFViewer();
|
|
|
+ PDFViewer.CreateDocument();
|
|
|
+
|
|
|
+ if (PDFViewer.Document == null)
|
|
|
+ {
|
|
|
+ AlertsMessage alertsMessage = new AlertsMessage();
|
|
|
+ alertsMessage.ShowDialog("","创建PDF失败","OK");
|
|
|
+ return false;
|
|
|
+ }
|
|
|
+
|
|
|
+ FileName = sourcepath.Substring(sourcepath.LastIndexOf("\\") + 1, sourcepath.LastIndexOf(".") - sourcepath.LastIndexOf("\\") - 1) + ".pdf";
|
|
|
+
|
|
|
+ var tempdoc = CPDFDocument.InitWithFilePath(targetPath);
|
|
|
+ if (tempdoc == null)
|
|
|
+ {
|
|
|
+ AlertsMessage alertsMessage = new AlertsMessage();
|
|
|
+ alertsMessage.ShowDialog("", "创建PDF失败", "OK");
|
|
|
+ return false;
|
|
|
+ }
|
|
|
+
|
|
|
+ PDFViewer.Document.ImportPages(tempdoc, "");
|
|
|
+
|
|
|
+ //PDFViewer.SetBackgroundBrush(new SolidColorBrush((Color)System.Windows.Media.ColorConverter.ConvertFromString(Settings.Default.AppProperties.InitialVIew.Background)));
|
|
|
+ PDFViewer.Load();
|
|
|
+ PDFViewer.UndoManager.CanSave = true;
|
|
|
+
|
|
|
+
|
|
|
+ return true;
|
|
|
+ }
|
|
|
+ catch (Exception ex)
|
|
|
+ {
|
|
|
+ AlertsMessage alertsMessage = new AlertsMessage();
|
|
|
+ alertsMessage.ShowDialog("", "没有安装Office", "OK");
|
|
|
+ return false;
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
private void UndoManager_PropertyChanged(object sender, PropertyChangedEventArgs e)
|
|
|
{
|
|
|
if (e.PropertyName == "CanSave")
|
|
@@ -207,9 +435,15 @@ namespace PDF_Office.ViewModels
|
|
|
{
|
|
|
FileChanged = Visibility.Collapsed;
|
|
|
}
|
|
|
+
|
|
|
+ if (!string.IsNullOrEmpty(PDFViewer.Document.FileName))
|
|
|
+ {
|
|
|
+ FileName = PDFViewer.Document.FileName;
|
|
|
+ }
|
|
|
}
|
|
|
}
|
|
|
|
|
|
+ #region Navigation
|
|
|
public void OnNavigatedTo(NavigationContext navigationContext)
|
|
|
{
|
|
|
if (navigationContext.Parameters.Count <= 0)
|
|
@@ -229,5 +463,6 @@ namespace PDF_Office.ViewModels
|
|
|
public void OnNavigatedFrom(NavigationContext navigationContext)
|
|
|
{
|
|
|
}
|
|
|
+ #endregion
|
|
|
}
|
|
|
}
|