123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808 |
- using PDF_Master.Model;
- using Prism.Commands;
- using System.Data;
- using Prism.Mvvm;
- using Prism.Services.Dialogs;
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Windows.Forms;
- using System.IO;
- using System.Diagnostics;
- using PDF_Master.CustomControl;
- using System.Windows;
- using PDF_Master.Helper;
- using PDF_Master.Model.Dialog.HomePageToolsDialogs;
- using ComPDFKit.PDFDocument;
- using System.Drawing;
- using DialogResult = Prism.Services.Dialogs.DialogResult;
- namespace PDF_Master.ViewModels.Dialog.HomePageToolsDialogs
- {
- public class HomePagePictureToPDFDialogViewModel : BindableBase, IDialogAware
- {
- #region 文案
- private string T_title;
- public string T_Title
- {
- get { return T_title; }
- set
- {
- SetProperty(ref T_title, value);
- }
- }
- private string T_addFolder;
- public string T_AddFolder
- {
- get { return T_addFolder; }
- set
- {
- SetProperty(ref T_addFolder, value);
- }
- }
- private string T_addFile;
- public string T_AddFile
- {
- get { return T_addFile; }
- set
- {
- SetProperty(ref T_addFile, value);
- }
- }
- private string T_emptyContext;
- public string T_EmptyContext
- {
- get { return T_emptyContext; }
- set
- {
- SetProperty(ref T_emptyContext, value);
- }
- }
- private string T_settings;
- public string T_Settings
- {
- get { return T_settings; }
- set
- {
- SetProperty(ref T_settings, value);
- }
- }
- private string T_exportTitle;
- public string T_ExportTitle
- {
- get { return T_exportTitle; }
- set
- {
- SetProperty(ref T_exportTitle, value);
- }
- }
- private string T_create;
- public string T_Create
- {
- get { return T_create; }
- set
- {
- SetProperty(ref T_create, value);
- }
- }
- private string T_merge;
- public string T_Merge
- {
- get { return T_merge; }
- set
- {
- SetProperty(ref T_merge, value);
- }
- }
- private string T_emptySelectFiles;
- public string T_EmptySelectFiles
- {
- get { return T_emptySelectFiles; }
- set
- {
- SetProperty(ref T_emptySelectFiles, value);
- }
- }
- private string T_export;
- public string T_Export
- {
- get { return T_export; }
- set
- {
- SetProperty(ref T_export, value);
- }
- }
- private string T_saveAsPDF;
- public string T_SaveAsPDF
- {
- get { return T_saveAsPDF; }
- set
- {
- SetProperty(ref T_saveAsPDF, value);
- }
- }
-
- private void InitString()
- {
- T_Title = App.MainPageLoader.GetString("ImageToPDF_Title");
- T_AddFolder = App.MainPageLoader.GetString("ImageToPDF_AddFolder");
- T_AddFile = App.MainPageLoader.GetString("ImageToPDF_AddFile");
- T_EmptySelectFiles = App.MainPageLoader.GetString("ImageToPDF_SelectFiles");
- T_EmptyContext = App.MainPageLoader.GetString("ImageToPDF_EmptyContext");
- T_Settings = App.MainPageLoader.GetString("ImageToPDF_Settings");
- T_ExportTitle = App.MainPageLoader.GetString("ImageToPDF_ExportTitle");
- T_Create = App.MainPageLoader.GetString("ImageToPDF_Create");
- T_Merge = App.MainPageLoader.GetString("ImageToPDF_Merge");
- T_SaveAsPDF= App.MainPageLoader.GetString("ImageToPDF_SaveAsPDF");
- }
- #endregion
- #region 参数和属性
- private List<string> fileNames;
- private int fileNamesIndex = 0;
- private int FileNameNumber = 0;
- public IDialogService dialogs;
- public List<int> fileNamesView = new List<int>();
- public HomePagePictureToPDFDialogModel pictureToPDFModel = new HomePagePictureToPDFDialogModel();
- private string pictureToPDFGridIsEnabled = "True";
- public string PictureToPDFGridIsEnabled
- {
- get
- {
- return pictureToPDFGridIsEnabled;
- }
- set
- {
- SetProperty(ref pictureToPDFGridIsEnabled, value);
- }
- }
- private string setPictureToPDFGridIsEnabled = "True";
- public string SetPictureToPDFGridIsEnabled
- {
- get
- {
- return setPictureToPDFGridIsEnabled;
- }
- set
- {
- SetProperty(ref setPictureToPDFGridIsEnabled, value);
- }
- }
- private DataTable imagesDataTable = new DataTable();
- public DataTable ImagesDataTable
- {
- get { return imagesDataTable; }
- set
- {
- SetProperty(ref imagesDataTable, value);
- }
- }
- private DataTable imagesCurrentDataTable = new DataTable();
- public DataTable ImagesCurrentDataTable
- {
- get { return imagesCurrentDataTable; }
- set
- {
- SetProperty(ref imagesCurrentDataTable, value);
- }
- }
- private string removeIsEnable = "True";
- public string RemoveIsEnable
- {
- get { return removeIsEnable; }
- set
- {
- SetProperty(ref removeIsEnable, value);
- }
- }
- private string imageToPDFBtnIsEnable = "True";
- public string ImageToPDFBtnIsEnable
- {
- get { return imageToPDFBtnIsEnable; }
- set
- {
- SetProperty(ref imageToPDFBtnIsEnable, value);
- }
- }
- private string selectFileName = "File";
- public string SelectFileName
- {
- get { return selectFileName; }
- set
- {
- SetProperty(ref selectFileName, value);
- }
- }
- private Visibility addFileVisibility = Visibility.Hidden;
- public Visibility AddFileVisibility
- {
- get { return addFileVisibility; }
- set
- {
- SetProperty(ref addFileVisibility, value);
- RaisePropertyChanged();
- }
- }
- #endregion
- #region 委托声明
- public DelegateCommand ToPDFCommand { get; set; }
- public DelegateCommand ADDPictureCommand { get; set; }
- public DelegateCommand ADDPictureFilesCommand { get; set; }
- public DelegateCommand RemovePictureFileCommand { get; set; }
- public DelegateCommand SelectFileCommand { get; set; }
- #endregion
- public HomePagePictureToPDFDialogViewModel(IDialogService dialogs)
- {
- InitString();
- this.dialogs = dialogs;
- ToPDFCommand = new DelegateCommand(topdf);
- ADDPictureCommand = new DelegateCommand(addpicture);
- ADDPictureFilesCommand = new DelegateCommand(addpicturefiles);
- RemovePictureFileCommand = new DelegateCommand(removepicturefiles);
- SelectFileCommand = new DelegateCommand(selectFile);
- ImagesDataTable.Columns.Add("FileName");
- ImagesDataTable.Columns.Add("FileSize");
- ImagesDataTable.Columns.Add("FileState");
- ImagesCurrentDataTable.Columns.Add("FileState");
- this.dialogs = dialogs;
- }
- #region 逻辑函数
- private void topdf()
- {
- PictureToPDFGridIsEnabled = "False";
- if (pictureToPDFModel.Mode == HomePagePictureToPDFDialogModel.ToPDFFileMode.NewFiles)
- {
- FolderBrowserDialog dlg = new FolderBrowserDialog();
- string saveSelectedPath = "";
- string SaveSelectedPath = "";
- if (dlg.ShowDialog() == System.Windows.Forms.DialogResult.OK)
- {
- SaveSelectedPath = dlg.SelectedPath.Trim();
- saveSelectedPath = SaveSelectedPath + "\\";
- }
- else { PictureToPDFGridIsEnabled = "True"; return; }
- fileNamesIndex = 0;
- string fileName = "";
- foreach (var filename in fileNames)
- {
- ImagesDataTable.Rows[fileNamesIndex]["FileState"] = "wait";
- ImagesCurrentDataTable.Rows[fileNamesIndex]["FileState"] = "wait";
- try
- {
- Image img = Image.FromFile(filename);
- if (img == null)
- {
- ImagesDataTable.Rows[fileNamesIndex]["FileState"] = "error";
- ImagesCurrentDataTable.Rows[fileNamesIndex]["FileState"] = "error";
- fileNamesIndex++;
- continue;
- }
-
-
-
- CPDFDocument topdfdoc = CPDFDocument.CreateDocument();
- FileInfo fileinfo = new FileInfo(filename);
- JpegInsertPage(ref topdfdoc, filename, fileinfo, img.Width, img.Height);
- Trace.WriteLine("Path.GetTempPath():" + Path.GetTempPath());
- fileName = savefilename(filename, saveSelectedPath);
- topdfdoc.WriteToFilePath(fileName);
- topdfdoc.Release();
- string file_size = (((float)fileinfo.Length) / 1024).ToString() + " K";
- ImagesDataTable.Rows[fileNamesIndex]["FileState"] = "complete";
- ImagesCurrentDataTable.Rows[fileNamesIndex]["FileState"] = "complete";
- fileNamesIndex++;
- }
- catch
- {
- ImagesDataTable.Rows[fileNamesIndex]["FileState"] = "error";
- ImagesCurrentDataTable.Rows[fileNamesIndex]["FileState"] = "error";
- fileNamesIndex++;
- continue;
- }
-
- //System.Diagnostics.Process.Start("Explorer", "/select," + fileName);
- }
- System.Diagnostics.Process.Start("Explorer", "/select," + fileName);
- }
- if (pictureToPDFModel.Mode == HomePagePictureToPDFDialogModel.ToPDFFileMode.OneNewFile)
- {
- CPDFDocument topdfdoc = CPDFDocument.CreateDocument();
- int pageindex = 0;
- FolderBrowserDialog folderDialog = new FolderBrowserDialog();
- System.Windows.Forms.SaveFileDialog sfd = new System.Windows.Forms.SaveFileDialog();
- sfd.FileName = "Untitle";
- sfd.Filter = "PDF|*.pdf;";
- string NewFileName = "";
- if (sfd.ShowDialog() == System.Windows.Forms.DialogResult.OK)
- {
- NewFileName = sfd.FileName;
- }
- else { PictureToPDFGridIsEnabled = "True"; return; }
- foreach (var filename in fileNames)
- {
- ImagesDataTable.Rows[fileNamesIndex]["FileState"] = "wait";
- ImagesCurrentDataTable.Rows[fileNamesIndex]["FileState"] = "wait";
- Image img = Image.FromFile(filename);
- if (img == null)
- {
- ImagesDataTable.Rows[fileNamesIndex]["FileState"] = "error";
- ImagesCurrentDataTable.Rows[fileNamesIndex]["FileState"] = "error";
- continue;
- }
- FileInfo fileinfo = new FileInfo(filename);
- JpegInsertPage(ref topdfdoc, filename, fileinfo, img.Width, img.Height, pageindex);
- pageindex++;
- string file_size = (((float)fileinfo.Length) / 1024).ToString() + " K";
- ImagesDataTable.Rows[fileNamesIndex]["FileState"] = "complete";
- ImagesCurrentDataTable.Rows[fileNamesIndex]["FileState"] = "complete";
- fileNamesIndex++;
- }
- System.Diagnostics.Process.Start("Explorer", "/select," + NewFileName);
- topdfdoc.WriteToFilePath(NewFileName);
- topdfdoc.Release();
- }
- if (pictureToPDFModel.Mode == HomePagePictureToPDFDialogModel.ToPDFFileMode.SelectFileName)
- {
- FolderBrowserDialog dlg = new FolderBrowserDialog();
- string saveSelectedPath = "";
- if (dlg.ShowDialog() == System.Windows.Forms.DialogResult.OK)
- {
- saveSelectedPath = dlg.SelectedPath.Trim();
- saveSelectedPath = saveSelectedPath + "\\";
- }
- else { PictureToPDFGridIsEnabled = "True"; return; }
- CPDFDocument topdfdoc = CPDFDocument.InitWithFilePath(pictureToPDFModel.FilePath);
- if (topdfdoc == null)
- {
- Trace.WriteLine("Document==null");
- //TODO
- MessageBoxEx.Show("文档为空");
- PictureToPDFGridIsEnabled = "True";
- return;
- }
- if (topdfdoc.IsLocked)
- {
- DialogParameters value = new DialogParameters();
- value.Add(ParameterNames.PDFDocument, topdfdoc);
- dialogs.ShowDialog(DialogNames.VerifyPassWordDialog, value, e =>
- {
- if (e.Result == ButtonResult.OK)
- {
- if (e.Parameters.ContainsKey(ParameterNames.PassWord) && e.Parameters.GetValue<string>(ParameterNames.PassWord) != null)
- {
- topdfdoc.UnlockWithPassword(e.Parameters.GetValue<string>(ParameterNames.PassWord).ToString());
- }
- }
- });
- if (topdfdoc.IsLocked)
- {
- //未成功解密文档时,释放Document对象,返回
- ImagesDataTable.Rows[fileNamesIndex]["FileState"] = "error";
- ImagesCurrentDataTable.Rows[fileNamesIndex]["FileState"] = "error";
- fileNamesIndex++;
- PictureToPDFGridIsEnabled = "True";
- return;
- }
- }
- int pageindex = topdfdoc.PageCount;
- foreach (var filename in fileNames)
- {
- ImagesDataTable.Rows[fileNamesIndex]["FileState"] = "wait";
- ImagesCurrentDataTable.Rows[fileNamesIndex]["FileState"] = "wait";
- Image img = Image.FromFile(filename);
- if (img == null)
- {
- ImagesDataTable.Rows[fileNamesIndex]["FileState"] = "error";
- ImagesCurrentDataTable.Rows[fileNamesIndex]["FileState"] = "error";
- continue;
- }
- FileInfo fileinfo = new FileInfo(filename);
- JpegInsertPage(ref topdfdoc, filename, fileinfo, img.Width, img.Height, pageindex);
- pageindex++;
- string file_size = (((float)fileinfo.Length) / 1024).ToString() + " K";
- ImagesDataTable.Rows[fileNamesIndex]["FileState"] = "complete";
- ImagesCurrentDataTable.Rows[fileNamesIndex]["FileState"] = "complete";
- fileNamesIndex++;
- }
- string saveselectedpath = CommonHelper.CreateFilePath(saveSelectedPath + topdfdoc.FileName + ".pdf");
- topdfdoc.WriteToFilePath(saveselectedpath);
- System.Diagnostics.Process.Start("Explorer", "/select," + saveselectedpath);
- topdfdoc.Release();
- }
- PictureToPDFGridIsEnabled = "True";
- fileNamesIndex = 0;
- }
- /// <summary>
- /// 设置保存后的文件名
- /// </summary>
- public string savefilename(string filename, string filepath)
- {
- FileInfo file = new FileInfo(filename);
- return CommonHelper.CreateFilePath(filepath + pictureToPDFModel.FrontFileNameLabel + file.Name.Replace(file.Extension, ".pdf") + pictureToPDFModel.RearFileNameLabel);
- }
- #endregion
- #region 批量处理逻辑函数
- /// <summary>
- /// 选择PDF文件
- /// </summary>
- private void selectFile()
- {
- System.Windows.Forms.OpenFileDialog dlg = new System.Windows.Forms.OpenFileDialog();
- dlg.Multiselect = false;
- dlg.Filter = "PDF|*.pdf;";
- if (dlg.ShowDialog() == System.Windows.Forms.DialogResult.OK)
- {
- SelectFileName = dlg.FileName;
- pictureToPDFModel.FilePath = dlg.FileName;
- ImageToPDFBtnIsEnable = "True";
- }
- }
- /// <summary>
- /// 添加图片文件
- /// </summary>
- private void addpicture()
- {
- FileNameNumber = fileNames.Count;
- System.Windows.Forms.OpenFileDialog dlg = new System.Windows.Forms.OpenFileDialog();
- dlg.Multiselect = true;
- dlg.Filter = "Picture|*.png;*.jpg;*.bmp;*.gif;*tiff;*jpeg;";
- if (dlg.ShowDialog() == System.Windows.Forms.DialogResult.OK)
- {
- fileNames.AddRange(dlg.FileNames.ToList());
- RemoveExcess(ref fileNames);
- SetPictureToPDFGridIsEnabled = "True";
- AddFileVisibility = Visibility.Hidden;
- RemoveIsEnable = "True";
- updateListview("wait");
- }
- }
- public void addPicture(string filename)
- {
- FileNameNumber = fileNames.Count;
- fileNames.Add(filename);
- RemoveExcess(ref fileNames);
- SetPictureToPDFGridIsEnabled = "True";
- AddFileVisibility = Visibility.Hidden;
- RemoveIsEnable = "True";
- updateListview("wait");
- }
- /// <summary>
- /// 添加图片文件夹
- /// </summary>
- private void addpicturefiles()
- {
- FileNameNumber = fileNames.Count;
- FolderBrowserDialog dialog = new FolderBrowserDialog();
- dialog.Description = "请选择文件路径";
- if (dialog.ShowDialog() == System.Windows.Forms.DialogResult.OK)
- {
- string foldPath = dialog.SelectedPath;
- var apps = System.IO.Directory.GetFiles(foldPath);
- foreach (string app in apps)
- {
- var fi = new FileInfo(app);
- if (fi.Extension == ".png" || fi.Extension == ".jpg" || fi.Extension == ".jpeg" || fi.Extension == ".bmp" || fi.Extension == ".gif" || fi.Extension == ".tiff")
- {
- fileNames.Add(app);
- }
- }
- RemoveExcess(ref fileNames);
- updateListview("wait");
- SetPictureToPDFGridIsEnabled = "True";
- AddFileVisibility = Visibility.Hidden;
- RemoveIsEnable = "True";
- }
- }
- /// <summary>
- /// 删除图片文件
- /// </summary>
- ///
- public void removepicturefiles()
- {if (fileNamesView.Count > 0)
- { removepicturefile(); }
- else
- {
- FileNameNumber = fileNames.Count;
- //Trace.WriteLine(filenamesview);
- fileNames.Clear();
- if (fileNames.Count < 1)
- {
- SetPictureToPDFGridIsEnabled = "False";
- AddFileVisibility = Visibility.Visible;
- RemoveIsEnable = "False";
- }
- updateListview("wait");
- }
-
- }
- public void removepicturefile()
- {
- FileNameNumber = fileNames.Count;
- Reverseorder(ref fileNamesView);
- foreach (int filenamesview in fileNamesView)
- {
- //Trace.WriteLine(filenamesview);
- fileNames.Remove(fileNames[filenamesview]);
- }
- if (fileNames.Count < 1)
- {
- SetPictureToPDFGridIsEnabled = "False";
- AddFileVisibility = Visibility.Visible;
- RemoveIsEnable = "False";
- }
- updateListview("wait");
- }
- public void removepdffile(int index)
- {
- ImagesCurrentDataTable.Rows.RemoveAt(index);
- fileNames.Remove(fileNames[index]);
- if (fileNames.Count < 1)
- {
- SetPictureToPDFGridIsEnabled = "False";
- AddFileVisibility = Visibility.Visible;
- RemoveIsEnable = "False";
- }
- updateListview("wait");
- }
- /// <summary>
- /// 打开文件图片
- /// </summary>
- public void openfiledialog()
- {
- foreach (int filenamesview in fileNamesView)
- {
- System.Diagnostics.Process.Start("Explorer", "/select," + fileNames[filenamesview]);
- }
- }
- /// <summary>
- /// 删除重复的文件
- /// </summary>
- public void RemoveExcess(ref List<string> Filenames)
- {
- List<string> filenames = new List<string>();
- foreach (var fileName in Filenames)
- {
- if (!filenames.Contains(fileName))
- {
- filenames.Add(fileName);
- }
- }
- Filenames.Clear();
- Filenames = filenames;
- }
- /// <summary>
- /// 逆序int类型集合
- /// </summary>
- public void Reverseorder(ref List<int> Numbers)
- {
- Numbers = Numbers.OrderBy(a => a).ToList();
- Numbers.Reverse();
- }
- /// <summary>
- /// 更新listview显示
- /// state 状态显示字符串
- /// </summary>
- public void updateListview(string state)
- {
- updateCurrentListview();
- DataTable imagesdatatable = new DataTable();
- imagesdatatable.Columns.Add("FileName");
- imagesdatatable.Columns.Add("FileSize");
- imagesdatatable.Columns.Add("FileState");
- int datatableindex = 0;
- foreach (var fileName in fileNames)
- {
- string file_all = fileName;
- FileInfo f = new FileInfo(file_all);
- if (!f.Exists) { continue; }
- string file_size = (((float)f.Length) / 1024).ToString() + " K";
- imagesdatatable.Rows.Add(f.Name, file_size, ImagesCurrentDataTable.Rows[datatableindex]["FileState"]);
- datatableindex++;
- }
- ImagesDataTable = imagesdatatable;
- }
- /// <summary>
- /// 更新Currentlistview显示
- /// pagerangetext 自定义页面范围字符串 pagerangeselectindex combobox下拉索引(0全部页面 1奇数页 2偶数页 3自定义页面)isevenpageisenabled 偶数页状态 state 状态显示字符串
- /// </summary>
- public void updateCurrentListview(string state = "wait")
- {
- if (fileNames.Count >= FileNameNumber)
- {
- for (int i = 0; fileNames.Count - FileNameNumber > i; i++)
- {
- ImagesCurrentDataTable.Rows.Add(state);
- }
- }
- else
- {
- Reverseorder(ref fileNamesView);
- foreach (int filenamesview in fileNamesView)
- {
- ImagesCurrentDataTable.Rows.RemoveAt(filenamesview);
- }
- }
- }
- #endregion
- #region 图片转换
- /// <summary>
- /// 图片转PNG
- /// picturefile 图片文件名
- /// filePath 保存路径
- /// </summary>
- public bool SavePng(string picturefile, string filePath)
- {
- Image img = Image.FromFile(picturefile);
- try
- {
- using (var bmp = new Bitmap(img.Width, img.Height))
- {
- bmp.SetResolution(img.HorizontalResolution, img.VerticalResolution);
- using (var g = Graphics.FromImage(bmp))
- {
- g.Clear(Color.White);
- g.DrawImageUnscaled(img, 0, 0);
- }
- bmp.Save(filePath, System.Drawing.Imaging.ImageFormat.Png);
- }
- return true;
- }
- catch
- {
- return false;
- }
- }
- /// <summary>
- /// 图片转JPeG
- /// picturefile 图片文件名
- /// filePath 保存路径
- /// </summary>
- public bool SaveJpeg(string picturefile, string filePath)
- {
- Image img = Image.FromFile(picturefile);
- try
- {
- using (var bmp = new Bitmap(img.Width, img.Height))
- {
- bmp.SetResolution(img.HorizontalResolution, img.VerticalResolution);
- using (var g = Graphics.FromImage(bmp))
- {
- g.Clear(Color.White);
- g.DrawImageUnscaled(img, 0, 0);
- }
- //存储各种格式
- //bmp.Save(filePath, System.Drawing.Imaging.ImageFormat.Gif);
- //bmp.Save(filePath, System.Drawing.Imaging.ImageFormat.Png);
- bmp.Save(filePath, System.Drawing.Imaging.ImageFormat.Jpeg);
- }
- return true;
- }
- catch
- {
- return false;
- }
- }
- /// <summary>
- /// 插入JPeG图片
- /// topdfdoc 所要插入的文档
- /// filename 图片文件名
- /// fileinfo 文件信息
- /// width page宽
- /// height page高
- /// index 插入位置
- /// </summary>
- private void JpegInsertPage(ref CPDFDocument topdfdoc, string filename, FileInfo fileinfo, int width, int height, int index = 0)
- {
- string tempFileName = "";
- if (fileinfo.Extension == ".jpg" || fileinfo.Extension == ".jpeg")
- {
- topdfdoc.InsertPage(index, width, height, filename);
- }
- else
- {
- tempFileName = Path.GetTempPath() + "pngtemp.jpg";
- if (!PictureConverter.SaveJpeg(filename, tempFileName))
- {
- MessageBoxEx.Show("图片格式有问题");
- }
- topdfdoc.InsertPage(index, width, height, tempFileName);
- }
- }
- public void CloseDialoge() { RequestClose?.Invoke(new DialogResult(ButtonResult.OK)); }
- #endregion
- #region 构架行为
- public string Title => "图片转PDF";
- public event Action<IDialogResult> RequestClose;
- public bool CanCloseDialog()
- {
- return true;
- }
- public void OnDialogClosed()
- {
- }
- public void OnDialogOpened(IDialogParameters parameters)
- {
- string[] filepath = null;
- parameters.TryGetValue<string[]>(ParameterNames.FilePath, out filepath);
- if (filepath != null)
- {
- fileNames = filepath.ToList();
- updateListview("wait");
- }
- }
- #endregion
- }
- }
|