|
@@ -10,10 +10,14 @@ using Prism.Services.Dialogs;
|
|
|
using System;
|
|
|
using System.Collections.Generic;
|
|
|
using System.Collections.ObjectModel;
|
|
|
+using System.IO;
|
|
|
using System.Linq;
|
|
|
using System.Text;
|
|
|
using System.Threading.Tasks;
|
|
|
+using System.Windows;
|
|
|
using System.Windows.Controls;
|
|
|
+using System.Windows.Media;
|
|
|
+using System.Windows.Media.Imaging;
|
|
|
|
|
|
namespace PDF_Office.ViewModels.Dialog.ToolsDialogs
|
|
|
{
|
|
@@ -65,13 +69,35 @@ namespace PDF_Office.ViewModels.Dialog.ToolsDialogs
|
|
|
|
|
|
private PageSizeType pageSizeType = PageSizeType.kDefault;
|
|
|
|
|
|
+ private string inputWidth;
|
|
|
+
|
|
|
+ public string InputWidth
|
|
|
+ {
|
|
|
+ get { return inputWidth; }
|
|
|
+ set
|
|
|
+ {
|
|
|
+ SetProperty(ref inputWidth, value);
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ private string inputHeight;
|
|
|
+
|
|
|
+ public string InputHeight
|
|
|
+ {
|
|
|
+ get { return inputHeight; }
|
|
|
+ set
|
|
|
+ {
|
|
|
+ SetProperty(ref inputHeight, value);
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
|
|
|
public MergeDialogViewModel(IDialogService dialogService)
|
|
|
{
|
|
|
dialogs = dialogService;
|
|
|
MergeObjectlist = new ObservableCollection<MergeObject>();
|
|
|
CancelCommand = new DelegateCommand(Cancel);
|
|
|
- AddFilesCommand = new DelegateCommand(AddFiles);
|
|
|
+ AddFilesCommand = new DelegateCommand(ButtonAddFiles);
|
|
|
MergeCommand = new DelegateCommand(Merge);
|
|
|
ClearCommand = new DelegateCommand(Clear);
|
|
|
SetPageSizeTypeCommand = new DelegateCommand<object>(SetPageSizeType);
|
|
@@ -85,7 +111,12 @@ namespace PDF_Office.ViewModels.Dialog.ToolsDialogs
|
|
|
RequestClose.Invoke(new DialogResult(ButtonResult.Cancel));
|
|
|
}
|
|
|
|
|
|
- private void AddFiles()
|
|
|
+ public void ButtonAddFiles()
|
|
|
+ {
|
|
|
+ AddFiles(OpenFile());
|
|
|
+ }
|
|
|
+
|
|
|
+ private string[] OpenFile()
|
|
|
{
|
|
|
OpenFileDialog openFile = new OpenFileDialog();
|
|
|
openFile.Multiselect = true;
|
|
@@ -99,57 +130,9 @@ namespace PDF_Office.ViewModels.Dialog.ToolsDialogs
|
|
|
"(*.tiff)|*.tiff";
|
|
|
if (openFile.ShowDialog() == false)
|
|
|
{
|
|
|
- return;
|
|
|
- }
|
|
|
- bool result = true;
|
|
|
- for (int i = 0; i < openFile.FileNames.Length; i++)
|
|
|
- {
|
|
|
- MergeObject mergeObject = new MergeObject();
|
|
|
- mergeObject.FilePath = openFile.FileNames[i];
|
|
|
-
|
|
|
- //通过路径判断文件是否已添加
|
|
|
- bool IsExists = false;
|
|
|
- for (int j = 0; j < MergeObjectlist.Count; j++)
|
|
|
- {
|
|
|
- if (MergeObjectlist[i].FilePath == mergeObject.FilePath)
|
|
|
- {
|
|
|
- IsExists = true;
|
|
|
- }
|
|
|
- }
|
|
|
- if (IsExists)
|
|
|
- {
|
|
|
- continue;
|
|
|
- }
|
|
|
-
|
|
|
- CPDFDocument doc = CPDFDocument.InitWithFilePath(mergeObject.FilePath);
|
|
|
- if (doc.IsLocked)
|
|
|
- {
|
|
|
- DialogParameters value = new DialogParameters();
|
|
|
- value.Add(ParameterNames.PDFDocument, doc);
|
|
|
- dialogs.ShowDialog(DialogNames.VerifyPassWordDialog, value, e =>
|
|
|
- {
|
|
|
- if (e.Result != ButtonResult.OK)
|
|
|
- {
|
|
|
- result = false;
|
|
|
- }
|
|
|
- else
|
|
|
- {
|
|
|
- mergeObject.Password = e.Parameters.GetValue<string>(ParameterNames.PassWord);
|
|
|
- }
|
|
|
-
|
|
|
- });
|
|
|
- if (!result)
|
|
|
- {
|
|
|
- doc.Release();
|
|
|
- continue;
|
|
|
- }
|
|
|
- }
|
|
|
- mergeObject.DocName = doc.FileName;
|
|
|
- mergeObject.DocPageCount = doc.PageCount.ToString() + "页";
|
|
|
- mergeObject.DocSize = GetFileSize(mergeObject.FilePath);
|
|
|
- MergeObjectlist.Add(mergeObject);
|
|
|
- doc.Release();
|
|
|
+ return null;
|
|
|
}
|
|
|
+ return openFile.FileNames;
|
|
|
}
|
|
|
|
|
|
private string GetFileSize(string path)
|
|
@@ -197,19 +180,45 @@ namespace PDF_Office.ViewModels.Dialog.ToolsDialogs
|
|
|
CPDFDocument SaveDoc = CPDFDocument.CreateDocument();
|
|
|
for (int i = 0; i < MergeObjectlist.Count; i++)
|
|
|
{
|
|
|
- CPDFDocument tempDoc = CPDFDocument.InitWithFilePath(MergeObjectlist[i].FilePath);
|
|
|
- if (!string.IsNullOrEmpty(MergeObjectlist[i].Password))
|
|
|
+ //图片
|
|
|
+ if (Path.GetExtension(MergeObjectlist[i].FilePath).Trim().ToLower() != "pdf")
|
|
|
{
|
|
|
- tempDoc.UnlockWithPassword(MergeObjectlist[i].Password);
|
|
|
+ BitmapSource frame = MergeObjectlist[i].DocThumbnail;
|
|
|
+ byte[] imageData = new byte[frame.PixelWidth * frame.PixelHeight * 4];
|
|
|
+ if (frame.Format != PixelFormats.Bgra32)
|
|
|
+ {
|
|
|
+ FormatConvertedBitmap covert = new FormatConvertedBitmap(frame, PixelFormats.Bgra32, frame.Palette, 0);
|
|
|
+ covert.CopyPixels(imageData, frame.PixelWidth * 4, 0);
|
|
|
+ }
|
|
|
+ else
|
|
|
+ {
|
|
|
+ frame.CopyPixels(imageData, frame.PixelWidth * 4, 0);
|
|
|
+ }
|
|
|
+ frame.CopyPixels(imageData, frame.PixelWidth * 4, 0);
|
|
|
+ result = SaveDoc.InsertPage(0, frame.PixelWidth, frame.PixelHeight, imageData, CPDFDocumentImageMode.CPDFDocumentImageModeScaleToFill);
|
|
|
+ if (!result)
|
|
|
+ {
|
|
|
+ SaveDoc.Release();
|
|
|
+ return;
|
|
|
+ }
|
|
|
+ continue;
|
|
|
}
|
|
|
- result = SaveDoc.ImportPages(tempDoc, "1-1"/*MergeObjectlist[i].SetPageRange*/);
|
|
|
- if (!result)
|
|
|
+ else
|
|
|
{
|
|
|
- SaveDoc.Release();
|
|
|
+ CPDFDocument tempDoc = CPDFDocument.InitWithFilePath(MergeObjectlist[i].FilePath);
|
|
|
+ if (!string.IsNullOrEmpty(MergeObjectlist[i].Password))
|
|
|
+ {
|
|
|
+ tempDoc.UnlockWithPassword(MergeObjectlist[i].Password);
|
|
|
+ }
|
|
|
+ result = SaveDoc.ImportPages(tempDoc, MergeObjectlist[i].SetPageRange);
|
|
|
+ if (!result)
|
|
|
+ {
|
|
|
+ SaveDoc.Release();
|
|
|
+ tempDoc.Release();
|
|
|
+ return;
|
|
|
+ }
|
|
|
tempDoc.Release();
|
|
|
- return;
|
|
|
}
|
|
|
- tempDoc.Release();
|
|
|
}
|
|
|
|
|
|
string path = App.CachePath.MergeFilePath;
|
|
@@ -237,11 +246,20 @@ namespace PDF_Office.ViewModels.Dialog.ToolsDialogs
|
|
|
case PageSizeType.Legal:
|
|
|
break;
|
|
|
case PageSizeType.Customized:
|
|
|
+ if (!string.IsNullOrEmpty(InputWidth) && !string.IsNullOrEmpty(InputHeight))
|
|
|
+ {
|
|
|
+ rect.Width = Convert.ToInt32(InputWidth);
|
|
|
+ rect.Height = Convert.ToInt32(InputHeight);
|
|
|
+ }
|
|
|
+ else
|
|
|
+ {
|
|
|
+ rect.Width = 595;
|
|
|
+ rect.Height = 841;
|
|
|
+ }
|
|
|
break;
|
|
|
default:
|
|
|
break;
|
|
|
}
|
|
|
-
|
|
|
if (rect.Width > 0 && rect.Height > 0)
|
|
|
{
|
|
|
//裁剪
|
|
@@ -303,6 +321,92 @@ namespace PDF_Office.ViewModels.Dialog.ToolsDialogs
|
|
|
}
|
|
|
}
|
|
|
|
|
|
+ public void AddFiles(string[] FilePath)
|
|
|
+ {
|
|
|
+ if (FilePath == null)
|
|
|
+ {
|
|
|
+ return;
|
|
|
+ }
|
|
|
+ bool result = true;
|
|
|
+ for (int i = 0; i < FilePath.Length; i++)
|
|
|
+ {
|
|
|
+ MergeObject mergeObject = new MergeObject();
|
|
|
+ mergeObject.FilePath = FilePath[i];
|
|
|
+
|
|
|
+ //通过路径判断文件是否已添加
|
|
|
+ bool IsExists = false;
|
|
|
+ for (int j = 0; j < MergeObjectlist.Count; j++)
|
|
|
+ {
|
|
|
+ if (MergeObjectlist[j].FilePath == mergeObject.FilePath)
|
|
|
+ {
|
|
|
+ IsExists = true;
|
|
|
+ }
|
|
|
+ }
|
|
|
+ if (IsExists)
|
|
|
+ {
|
|
|
+ continue;
|
|
|
+ }
|
|
|
+ string FileType = Path.GetExtension(mergeObject.FilePath).Trim().ToLower();
|
|
|
+ if (FileType != ".pdf")
|
|
|
+ {
|
|
|
+ if (!Properties.Resources.imageex.Contains(FileType))
|
|
|
+ {
|
|
|
+ //图片格式不支持
|
|
|
+ continue;
|
|
|
+ };
|
|
|
+
|
|
|
+ mergeObject.DocName = Path.GetFileName(mergeObject.FilePath);
|
|
|
+ mergeObject.DocPageCount = 1 + "页";
|
|
|
+ mergeObject.SDKPageCount = 1;
|
|
|
+ mergeObject.DocSize = GetFileSize(mergeObject.FilePath);
|
|
|
+ mergeObject.DocThumbnail = new BitmapImage(new Uri(mergeObject.FilePath)); ;
|
|
|
+ }
|
|
|
+ else
|
|
|
+ {
|
|
|
+ CPDFDocument doc = CPDFDocument.InitWithFilePath(mergeObject.FilePath);
|
|
|
+ if (doc.IsLocked)
|
|
|
+ {
|
|
|
+ DialogParameters value = new DialogParameters();
|
|
|
+ value.Add(ParameterNames.PDFDocument, doc);
|
|
|
+ dialogs.ShowDialog(DialogNames.VerifyPassWordDialog, value, e =>
|
|
|
+ {
|
|
|
+ if (e.Result != ButtonResult.OK)
|
|
|
+ {
|
|
|
+ result = false;
|
|
|
+ }
|
|
|
+ else
|
|
|
+ {
|
|
|
+ mergeObject.Password = e.Parameters.GetValue<string>(ParameterNames.PassWord);
|
|
|
+ }
|
|
|
+
|
|
|
+ });
|
|
|
+ if (!result)
|
|
|
+ {
|
|
|
+ doc.Release();
|
|
|
+ continue;
|
|
|
+ }
|
|
|
+ }
|
|
|
+ mergeObject.DocName = doc.FileName;
|
|
|
+ mergeObject.DocPageCount = doc.PageCount.ToString() + "页";
|
|
|
+ mergeObject.SDKPageCount = doc.PageCount;
|
|
|
+ mergeObject.DocSize = GetFileSize(mergeObject.FilePath);
|
|
|
+
|
|
|
+ //获取第一页缩略图
|
|
|
+ CPDFPage page = doc.PageAtIndex(0);
|
|
|
+ Size size = doc.GetPageSize(0);
|
|
|
+
|
|
|
+ byte[] bmpData = new byte[(int)(size.Width * size.Height * 4)];
|
|
|
+ WriteableBitmap WirteBitmap = new WriteableBitmap((int)size.Width, (int)size.Height, 96, 96, PixelFormats.Bgra32, null);
|
|
|
+ page.RenderPageBitmap(0, 0, (int)size.Width, (int)size.Height, 0xFFFFFFFF, bmpData, 1);
|
|
|
+ WirteBitmap.WritePixels(new Int32Rect(0, 0, (int)size.Width, (int)size.Height), bmpData, WirteBitmap.BackBufferStride, 0);
|
|
|
+ WirteBitmap.Freeze();
|
|
|
+ mergeObject.DocThumbnail = WirteBitmap;
|
|
|
+
|
|
|
+ doc.Release();
|
|
|
+ }
|
|
|
+ MergeObjectlist.Add(mergeObject);
|
|
|
+ }
|
|
|
+ }
|
|
|
#endregion
|
|
|
}
|
|
|
}
|