HomePagePictureToPDFDialogViewModel.cs 29 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808
  1. using PDF_Master.Model;
  2. using Prism.Commands;
  3. using System.Data;
  4. using Prism.Mvvm;
  5. using Prism.Services.Dialogs;
  6. using System;
  7. using System.Collections.Generic;
  8. using System.Linq;
  9. using System.Windows.Forms;
  10. using System.IO;
  11. using System.Diagnostics;
  12. using PDF_Master.CustomControl;
  13. using System.Windows;
  14. using PDF_Master.Helper;
  15. using PDF_Master.Model.Dialog.HomePageToolsDialogs;
  16. using ComPDFKit.PDFDocument;
  17. using System.Drawing;
  18. using DialogResult = Prism.Services.Dialogs.DialogResult;
  19. namespace PDF_Master.ViewModels.Dialog.HomePageToolsDialogs
  20. {
  21. public class HomePagePictureToPDFDialogViewModel : BindableBase, IDialogAware
  22. {
  23. #region 文案
  24. private string T_title;
  25. public string T_Title
  26. {
  27. get { return T_title; }
  28. set
  29. {
  30. SetProperty(ref T_title, value);
  31. }
  32. }
  33. private string T_addFolder;
  34. public string T_AddFolder
  35. {
  36. get { return T_addFolder; }
  37. set
  38. {
  39. SetProperty(ref T_addFolder, value);
  40. }
  41. }
  42. private string T_addFile;
  43. public string T_AddFile
  44. {
  45. get { return T_addFile; }
  46. set
  47. {
  48. SetProperty(ref T_addFile, value);
  49. }
  50. }
  51. private string T_emptyContext;
  52. public string T_EmptyContext
  53. {
  54. get { return T_emptyContext; }
  55. set
  56. {
  57. SetProperty(ref T_emptyContext, value);
  58. }
  59. }
  60. private string T_settings;
  61. public string T_Settings
  62. {
  63. get { return T_settings; }
  64. set
  65. {
  66. SetProperty(ref T_settings, value);
  67. }
  68. }
  69. private string T_exportTitle;
  70. public string T_ExportTitle
  71. {
  72. get { return T_exportTitle; }
  73. set
  74. {
  75. SetProperty(ref T_exportTitle, value);
  76. }
  77. }
  78. private string T_create;
  79. public string T_Create
  80. {
  81. get { return T_create; }
  82. set
  83. {
  84. SetProperty(ref T_create, value);
  85. }
  86. }
  87. private string T_merge;
  88. public string T_Merge
  89. {
  90. get { return T_merge; }
  91. set
  92. {
  93. SetProperty(ref T_merge, value);
  94. }
  95. }
  96. private string T_emptySelectFiles;
  97. public string T_EmptySelectFiles
  98. {
  99. get { return T_emptySelectFiles; }
  100. set
  101. {
  102. SetProperty(ref T_emptySelectFiles, value);
  103. }
  104. }
  105. private string T_export;
  106. public string T_Export
  107. {
  108. get { return T_export; }
  109. set
  110. {
  111. SetProperty(ref T_export, value);
  112. }
  113. }
  114. private string T_saveAsPDF;
  115. public string T_SaveAsPDF
  116. {
  117. get { return T_saveAsPDF; }
  118. set
  119. {
  120. SetProperty(ref T_saveAsPDF, value);
  121. }
  122. }
  123. private void InitString()
  124. {
  125. T_Title = App.MainPageLoader.GetString("ImageToPDF_Title");
  126. T_AddFolder = App.MainPageLoader.GetString("ImageToPDF_AddFolder");
  127. T_AddFile = App.MainPageLoader.GetString("ImageToPDF_AddFile");
  128. T_EmptySelectFiles = App.MainPageLoader.GetString("ImageToPDF_SelectFiles");
  129. T_EmptyContext = App.MainPageLoader.GetString("ImageToPDF_EmptyContext");
  130. T_Settings = App.MainPageLoader.GetString("ImageToPDF_Settings");
  131. T_ExportTitle = App.MainPageLoader.GetString("ImageToPDF_ExportTitle");
  132. T_Create = App.MainPageLoader.GetString("ImageToPDF_Create");
  133. T_Merge = App.MainPageLoader.GetString("ImageToPDF_Merge");
  134. T_SaveAsPDF= App.MainPageLoader.GetString("ImageToPDF_SaveAsPDF");
  135. }
  136. #endregion
  137. #region 参数和属性
  138. private List<string> fileNames;
  139. private int fileNamesIndex = 0;
  140. private int FileNameNumber = 0;
  141. public IDialogService dialogs;
  142. public List<int> fileNamesView = new List<int>();
  143. public HomePagePictureToPDFDialogModel pictureToPDFModel = new HomePagePictureToPDFDialogModel();
  144. private string pictureToPDFGridIsEnabled = "True";
  145. public string PictureToPDFGridIsEnabled
  146. {
  147. get
  148. {
  149. return pictureToPDFGridIsEnabled;
  150. }
  151. set
  152. {
  153. SetProperty(ref pictureToPDFGridIsEnabled, value);
  154. }
  155. }
  156. private string setPictureToPDFGridIsEnabled = "True";
  157. public string SetPictureToPDFGridIsEnabled
  158. {
  159. get
  160. {
  161. return setPictureToPDFGridIsEnabled;
  162. }
  163. set
  164. {
  165. SetProperty(ref setPictureToPDFGridIsEnabled, value);
  166. }
  167. }
  168. private DataTable imagesDataTable = new DataTable();
  169. public DataTable ImagesDataTable
  170. {
  171. get { return imagesDataTable; }
  172. set
  173. {
  174. SetProperty(ref imagesDataTable, value);
  175. }
  176. }
  177. private DataTable imagesCurrentDataTable = new DataTable();
  178. public DataTable ImagesCurrentDataTable
  179. {
  180. get { return imagesCurrentDataTable; }
  181. set
  182. {
  183. SetProperty(ref imagesCurrentDataTable, value);
  184. }
  185. }
  186. private string removeIsEnable = "True";
  187. public string RemoveIsEnable
  188. {
  189. get { return removeIsEnable; }
  190. set
  191. {
  192. SetProperty(ref removeIsEnable, value);
  193. }
  194. }
  195. private string imageToPDFBtnIsEnable = "True";
  196. public string ImageToPDFBtnIsEnable
  197. {
  198. get { return imageToPDFBtnIsEnable; }
  199. set
  200. {
  201. SetProperty(ref imageToPDFBtnIsEnable, value);
  202. }
  203. }
  204. private string selectFileName = "File";
  205. public string SelectFileName
  206. {
  207. get { return selectFileName; }
  208. set
  209. {
  210. SetProperty(ref selectFileName, value);
  211. }
  212. }
  213. private Visibility addFileVisibility = Visibility.Hidden;
  214. public Visibility AddFileVisibility
  215. {
  216. get { return addFileVisibility; }
  217. set
  218. {
  219. SetProperty(ref addFileVisibility, value);
  220. RaisePropertyChanged();
  221. }
  222. }
  223. #endregion
  224. #region 委托声明
  225. public DelegateCommand ToPDFCommand { get; set; }
  226. public DelegateCommand ADDPictureCommand { get; set; }
  227. public DelegateCommand ADDPictureFilesCommand { get; set; }
  228. public DelegateCommand RemovePictureFileCommand { get; set; }
  229. public DelegateCommand SelectFileCommand { get; set; }
  230. #endregion
  231. public HomePagePictureToPDFDialogViewModel(IDialogService dialogs)
  232. {
  233. InitString();
  234. this.dialogs = dialogs;
  235. ToPDFCommand = new DelegateCommand(topdf);
  236. ADDPictureCommand = new DelegateCommand(addpicture);
  237. ADDPictureFilesCommand = new DelegateCommand(addpicturefiles);
  238. RemovePictureFileCommand = new DelegateCommand(removepicturefiles);
  239. SelectFileCommand = new DelegateCommand(selectFile);
  240. ImagesDataTable.Columns.Add("FileName");
  241. ImagesDataTable.Columns.Add("FileSize");
  242. ImagesDataTable.Columns.Add("FileState");
  243. ImagesCurrentDataTable.Columns.Add("FileState");
  244. this.dialogs = dialogs;
  245. }
  246. #region 逻辑函数
  247. private void topdf()
  248. {
  249. PictureToPDFGridIsEnabled = "False";
  250. if (pictureToPDFModel.Mode == HomePagePictureToPDFDialogModel.ToPDFFileMode.NewFiles)
  251. {
  252. FolderBrowserDialog dlg = new FolderBrowserDialog();
  253. string saveSelectedPath = "";
  254. string SaveSelectedPath = "";
  255. if (dlg.ShowDialog() == System.Windows.Forms.DialogResult.OK)
  256. {
  257. SaveSelectedPath = dlg.SelectedPath.Trim();
  258. saveSelectedPath = SaveSelectedPath + "\\";
  259. }
  260. else { PictureToPDFGridIsEnabled = "True"; return; }
  261. fileNamesIndex = 0;
  262. string fileName = "";
  263. foreach (var filename in fileNames)
  264. {
  265. ImagesDataTable.Rows[fileNamesIndex]["FileState"] = "wait";
  266. ImagesCurrentDataTable.Rows[fileNamesIndex]["FileState"] = "wait";
  267. try
  268. {
  269. Image img = Image.FromFile(filename);
  270. if (img == null)
  271. {
  272. ImagesDataTable.Rows[fileNamesIndex]["FileState"] = "error";
  273. ImagesCurrentDataTable.Rows[fileNamesIndex]["FileState"] = "error";
  274. fileNamesIndex++;
  275. continue;
  276. }
  277. CPDFDocument topdfdoc = CPDFDocument.CreateDocument();
  278. FileInfo fileinfo = new FileInfo(filename);
  279. JpegInsertPage(ref topdfdoc, filename, fileinfo, img.Width, img.Height);
  280. Trace.WriteLine("Path.GetTempPath():" + Path.GetTempPath());
  281. fileName = savefilename(filename, saveSelectedPath);
  282. topdfdoc.WriteToFilePath(fileName);
  283. topdfdoc.Release();
  284. string file_size = (((float)fileinfo.Length) / 1024).ToString() + " K";
  285. ImagesDataTable.Rows[fileNamesIndex]["FileState"] = "complete";
  286. ImagesCurrentDataTable.Rows[fileNamesIndex]["FileState"] = "complete";
  287. fileNamesIndex++;
  288. }
  289. catch
  290. {
  291. ImagesDataTable.Rows[fileNamesIndex]["FileState"] = "error";
  292. ImagesCurrentDataTable.Rows[fileNamesIndex]["FileState"] = "error";
  293. fileNamesIndex++;
  294. continue;
  295. }
  296. //System.Diagnostics.Process.Start("Explorer", "/select," + fileName);
  297. }
  298. System.Diagnostics.Process.Start("Explorer", "/select," + fileName);
  299. }
  300. if (pictureToPDFModel.Mode == HomePagePictureToPDFDialogModel.ToPDFFileMode.OneNewFile)
  301. {
  302. CPDFDocument topdfdoc = CPDFDocument.CreateDocument();
  303. int pageindex = 0;
  304. FolderBrowserDialog folderDialog = new FolderBrowserDialog();
  305. System.Windows.Forms.SaveFileDialog sfd = new System.Windows.Forms.SaveFileDialog();
  306. sfd.FileName = "Untitle";
  307. sfd.Filter = "PDF|*.pdf;";
  308. string NewFileName = "";
  309. if (sfd.ShowDialog() == System.Windows.Forms.DialogResult.OK)
  310. {
  311. NewFileName = sfd.FileName;
  312. }
  313. else { PictureToPDFGridIsEnabled = "True"; return; }
  314. foreach (var filename in fileNames)
  315. {
  316. ImagesDataTable.Rows[fileNamesIndex]["FileState"] = "wait";
  317. ImagesCurrentDataTable.Rows[fileNamesIndex]["FileState"] = "wait";
  318. Image img = Image.FromFile(filename);
  319. if (img == null)
  320. {
  321. ImagesDataTable.Rows[fileNamesIndex]["FileState"] = "error";
  322. ImagesCurrentDataTable.Rows[fileNamesIndex]["FileState"] = "error";
  323. continue;
  324. }
  325. FileInfo fileinfo = new FileInfo(filename);
  326. JpegInsertPage(ref topdfdoc, filename, fileinfo, img.Width, img.Height, pageindex);
  327. pageindex++;
  328. string file_size = (((float)fileinfo.Length) / 1024).ToString() + " K";
  329. ImagesDataTable.Rows[fileNamesIndex]["FileState"] = "complete";
  330. ImagesCurrentDataTable.Rows[fileNamesIndex]["FileState"] = "complete";
  331. fileNamesIndex++;
  332. }
  333. System.Diagnostics.Process.Start("Explorer", "/select," + NewFileName);
  334. topdfdoc.WriteToFilePath(NewFileName);
  335. topdfdoc.Release();
  336. }
  337. if (pictureToPDFModel.Mode == HomePagePictureToPDFDialogModel.ToPDFFileMode.SelectFileName)
  338. {
  339. FolderBrowserDialog dlg = new FolderBrowserDialog();
  340. string saveSelectedPath = "";
  341. if (dlg.ShowDialog() == System.Windows.Forms.DialogResult.OK)
  342. {
  343. saveSelectedPath = dlg.SelectedPath.Trim();
  344. saveSelectedPath = saveSelectedPath + "\\";
  345. }
  346. else { PictureToPDFGridIsEnabled = "True"; return; }
  347. CPDFDocument topdfdoc = CPDFDocument.InitWithFilePath(pictureToPDFModel.FilePath);
  348. if (topdfdoc == null)
  349. {
  350. Trace.WriteLine("Document==null");
  351. //TODO
  352. MessageBoxEx.Show("文档为空");
  353. PictureToPDFGridIsEnabled = "True";
  354. return;
  355. }
  356. if (topdfdoc.IsLocked)
  357. {
  358. DialogParameters value = new DialogParameters();
  359. value.Add(ParameterNames.PDFDocument, topdfdoc);
  360. dialogs.ShowDialog(DialogNames.VerifyPassWordDialog, value, e =>
  361. {
  362. if (e.Result == ButtonResult.OK)
  363. {
  364. if (e.Parameters.ContainsKey(ParameterNames.PassWord) && e.Parameters.GetValue<string>(ParameterNames.PassWord) != null)
  365. {
  366. topdfdoc.UnlockWithPassword(e.Parameters.GetValue<string>(ParameterNames.PassWord).ToString());
  367. }
  368. }
  369. });
  370. if (topdfdoc.IsLocked)
  371. {
  372. //未成功解密文档时,释放Document对象,返回
  373. ImagesDataTable.Rows[fileNamesIndex]["FileState"] = "error";
  374. ImagesCurrentDataTable.Rows[fileNamesIndex]["FileState"] = "error";
  375. fileNamesIndex++;
  376. PictureToPDFGridIsEnabled = "True";
  377. return;
  378. }
  379. }
  380. int pageindex = topdfdoc.PageCount;
  381. foreach (var filename in fileNames)
  382. {
  383. ImagesDataTable.Rows[fileNamesIndex]["FileState"] = "wait";
  384. ImagesCurrentDataTable.Rows[fileNamesIndex]["FileState"] = "wait";
  385. Image img = Image.FromFile(filename);
  386. if (img == null)
  387. {
  388. ImagesDataTable.Rows[fileNamesIndex]["FileState"] = "error";
  389. ImagesCurrentDataTable.Rows[fileNamesIndex]["FileState"] = "error";
  390. continue;
  391. }
  392. FileInfo fileinfo = new FileInfo(filename);
  393. JpegInsertPage(ref topdfdoc, filename, fileinfo, img.Width, img.Height, pageindex);
  394. pageindex++;
  395. string file_size = (((float)fileinfo.Length) / 1024).ToString() + " K";
  396. ImagesDataTable.Rows[fileNamesIndex]["FileState"] = "complete";
  397. ImagesCurrentDataTable.Rows[fileNamesIndex]["FileState"] = "complete";
  398. fileNamesIndex++;
  399. }
  400. string saveselectedpath = CommonHelper.CreateFilePath(saveSelectedPath + topdfdoc.FileName + ".pdf");
  401. topdfdoc.WriteToFilePath(saveselectedpath);
  402. System.Diagnostics.Process.Start("Explorer", "/select," + saveselectedpath);
  403. topdfdoc.Release();
  404. }
  405. PictureToPDFGridIsEnabled = "True";
  406. fileNamesIndex = 0;
  407. }
  408. /// <summary>
  409. /// 设置保存后的文件名
  410. /// </summary>
  411. public string savefilename(string filename, string filepath)
  412. {
  413. FileInfo file = new FileInfo(filename);
  414. return CommonHelper.CreateFilePath(filepath + pictureToPDFModel.FrontFileNameLabel + file.Name.Replace(file.Extension, ".pdf") + pictureToPDFModel.RearFileNameLabel);
  415. }
  416. #endregion
  417. #region 批量处理逻辑函数
  418. /// <summary>
  419. /// 选择PDF文件
  420. /// </summary>
  421. private void selectFile()
  422. {
  423. System.Windows.Forms.OpenFileDialog dlg = new System.Windows.Forms.OpenFileDialog();
  424. dlg.Multiselect = false;
  425. dlg.Filter = "PDF|*.pdf;";
  426. if (dlg.ShowDialog() == System.Windows.Forms.DialogResult.OK)
  427. {
  428. SelectFileName = dlg.FileName;
  429. pictureToPDFModel.FilePath = dlg.FileName;
  430. ImageToPDFBtnIsEnable = "True";
  431. }
  432. }
  433. /// <summary>
  434. /// 添加图片文件
  435. /// </summary>
  436. private void addpicture()
  437. {
  438. FileNameNumber = fileNames.Count;
  439. System.Windows.Forms.OpenFileDialog dlg = new System.Windows.Forms.OpenFileDialog();
  440. dlg.Multiselect = true;
  441. dlg.Filter = "Picture|*.png;*.jpg;*.bmp;*.gif;*tiff;*jpeg;";
  442. if (dlg.ShowDialog() == System.Windows.Forms.DialogResult.OK)
  443. {
  444. fileNames.AddRange(dlg.FileNames.ToList());
  445. RemoveExcess(ref fileNames);
  446. SetPictureToPDFGridIsEnabled = "True";
  447. AddFileVisibility = Visibility.Hidden;
  448. RemoveIsEnable = "True";
  449. updateListview("wait");
  450. }
  451. }
  452. public void addPicture(string filename)
  453. {
  454. FileNameNumber = fileNames.Count;
  455. fileNames.Add(filename);
  456. RemoveExcess(ref fileNames);
  457. SetPictureToPDFGridIsEnabled = "True";
  458. AddFileVisibility = Visibility.Hidden;
  459. RemoveIsEnable = "True";
  460. updateListview("wait");
  461. }
  462. /// <summary>
  463. /// 添加图片文件夹
  464. /// </summary>
  465. private void addpicturefiles()
  466. {
  467. FileNameNumber = fileNames.Count;
  468. FolderBrowserDialog dialog = new FolderBrowserDialog();
  469. dialog.Description = "请选择文件路径";
  470. if (dialog.ShowDialog() == System.Windows.Forms.DialogResult.OK)
  471. {
  472. string foldPath = dialog.SelectedPath;
  473. var apps = System.IO.Directory.GetFiles(foldPath);
  474. foreach (string app in apps)
  475. {
  476. var fi = new FileInfo(app);
  477. if (fi.Extension == ".png" || fi.Extension == ".jpg" || fi.Extension == ".jpeg" || fi.Extension == ".bmp" || fi.Extension == ".gif" || fi.Extension == ".tiff")
  478. {
  479. fileNames.Add(app);
  480. }
  481. }
  482. RemoveExcess(ref fileNames);
  483. updateListview("wait");
  484. SetPictureToPDFGridIsEnabled = "True";
  485. AddFileVisibility = Visibility.Hidden;
  486. RemoveIsEnable = "True";
  487. }
  488. }
  489. /// <summary>
  490. /// 删除图片文件
  491. /// </summary>
  492. ///
  493. public void removepicturefiles()
  494. {if (fileNamesView.Count > 0)
  495. { removepicturefile(); }
  496. else
  497. {
  498. FileNameNumber = fileNames.Count;
  499. //Trace.WriteLine(filenamesview);
  500. fileNames.Clear();
  501. if (fileNames.Count < 1)
  502. {
  503. SetPictureToPDFGridIsEnabled = "False";
  504. AddFileVisibility = Visibility.Visible;
  505. RemoveIsEnable = "False";
  506. }
  507. updateListview("wait");
  508. }
  509. }
  510. public void removepicturefile()
  511. {
  512. FileNameNumber = fileNames.Count;
  513. Reverseorder(ref fileNamesView);
  514. foreach (int filenamesview in fileNamesView)
  515. {
  516. //Trace.WriteLine(filenamesview);
  517. fileNames.Remove(fileNames[filenamesview]);
  518. }
  519. if (fileNames.Count < 1)
  520. {
  521. SetPictureToPDFGridIsEnabled = "False";
  522. AddFileVisibility = Visibility.Visible;
  523. RemoveIsEnable = "False";
  524. }
  525. updateListview("wait");
  526. }
  527. public void removepdffile(int index)
  528. {
  529. ImagesCurrentDataTable.Rows.RemoveAt(index);
  530. fileNames.Remove(fileNames[index]);
  531. if (fileNames.Count < 1)
  532. {
  533. SetPictureToPDFGridIsEnabled = "False";
  534. AddFileVisibility = Visibility.Visible;
  535. RemoveIsEnable = "False";
  536. }
  537. updateListview("wait");
  538. }
  539. /// <summary>
  540. /// 打开文件图片
  541. /// </summary>
  542. public void openfiledialog()
  543. {
  544. foreach (int filenamesview in fileNamesView)
  545. {
  546. System.Diagnostics.Process.Start("Explorer", "/select," + fileNames[filenamesview]);
  547. }
  548. }
  549. /// <summary>
  550. /// 删除重复的文件
  551. /// </summary>
  552. public void RemoveExcess(ref List<string> Filenames)
  553. {
  554. List<string> filenames = new List<string>();
  555. foreach (var fileName in Filenames)
  556. {
  557. if (!filenames.Contains(fileName))
  558. {
  559. filenames.Add(fileName);
  560. }
  561. }
  562. Filenames.Clear();
  563. Filenames = filenames;
  564. }
  565. /// <summary>
  566. /// 逆序int类型集合
  567. /// </summary>
  568. public void Reverseorder(ref List<int> Numbers)
  569. {
  570. Numbers = Numbers.OrderBy(a => a).ToList();
  571. Numbers.Reverse();
  572. }
  573. /// <summary>
  574. /// 更新listview显示
  575. /// state 状态显示字符串
  576. /// </summary>
  577. public void updateListview(string state)
  578. {
  579. updateCurrentListview();
  580. DataTable imagesdatatable = new DataTable();
  581. imagesdatatable.Columns.Add("FileName");
  582. imagesdatatable.Columns.Add("FileSize");
  583. imagesdatatable.Columns.Add("FileState");
  584. int datatableindex = 0;
  585. foreach (var fileName in fileNames)
  586. {
  587. string file_all = fileName;
  588. FileInfo f = new FileInfo(file_all);
  589. if (!f.Exists) { continue; }
  590. string file_size = (((float)f.Length) / 1024).ToString() + " K";
  591. imagesdatatable.Rows.Add(f.Name, file_size, ImagesCurrentDataTable.Rows[datatableindex]["FileState"]);
  592. datatableindex++;
  593. }
  594. ImagesDataTable = imagesdatatable;
  595. }
  596. /// <summary>
  597. /// 更新Currentlistview显示
  598. /// pagerangetext 自定义页面范围字符串 pagerangeselectindex combobox下拉索引(0全部页面 1奇数页 2偶数页 3自定义页面)isevenpageisenabled 偶数页状态 state 状态显示字符串
  599. /// </summary>
  600. public void updateCurrentListview(string state = "wait")
  601. {
  602. if (fileNames.Count >= FileNameNumber)
  603. {
  604. for (int i = 0; fileNames.Count - FileNameNumber > i; i++)
  605. {
  606. ImagesCurrentDataTable.Rows.Add(state);
  607. }
  608. }
  609. else
  610. {
  611. Reverseorder(ref fileNamesView);
  612. foreach (int filenamesview in fileNamesView)
  613. {
  614. ImagesCurrentDataTable.Rows.RemoveAt(filenamesview);
  615. }
  616. }
  617. }
  618. #endregion
  619. #region 图片转换
  620. /// <summary>
  621. /// 图片转PNG
  622. /// picturefile 图片文件名
  623. /// filePath 保存路径
  624. /// </summary>
  625. public bool SavePng(string picturefile, string filePath)
  626. {
  627. Image img = Image.FromFile(picturefile);
  628. try
  629. {
  630. using (var bmp = new Bitmap(img.Width, img.Height))
  631. {
  632. bmp.SetResolution(img.HorizontalResolution, img.VerticalResolution);
  633. using (var g = Graphics.FromImage(bmp))
  634. {
  635. g.Clear(Color.White);
  636. g.DrawImageUnscaled(img, 0, 0);
  637. }
  638. bmp.Save(filePath, System.Drawing.Imaging.ImageFormat.Png);
  639. }
  640. return true;
  641. }
  642. catch
  643. {
  644. return false;
  645. }
  646. }
  647. /// <summary>
  648. /// 图片转JPeG
  649. /// picturefile 图片文件名
  650. /// filePath 保存路径
  651. /// </summary>
  652. public bool SaveJpeg(string picturefile, string filePath)
  653. {
  654. Image img = Image.FromFile(picturefile);
  655. try
  656. {
  657. using (var bmp = new Bitmap(img.Width, img.Height))
  658. {
  659. bmp.SetResolution(img.HorizontalResolution, img.VerticalResolution);
  660. using (var g = Graphics.FromImage(bmp))
  661. {
  662. g.Clear(Color.White);
  663. g.DrawImageUnscaled(img, 0, 0);
  664. }
  665. //存储各种格式
  666. //bmp.Save(filePath, System.Drawing.Imaging.ImageFormat.Gif);
  667. //bmp.Save(filePath, System.Drawing.Imaging.ImageFormat.Png);
  668. bmp.Save(filePath, System.Drawing.Imaging.ImageFormat.Jpeg);
  669. }
  670. return true;
  671. }
  672. catch
  673. {
  674. return false;
  675. }
  676. }
  677. /// <summary>
  678. /// 插入JPeG图片
  679. /// topdfdoc 所要插入的文档
  680. /// filename 图片文件名
  681. /// fileinfo 文件信息
  682. /// width page宽
  683. /// height page高
  684. /// index 插入位置
  685. /// </summary>
  686. private void JpegInsertPage(ref CPDFDocument topdfdoc, string filename, FileInfo fileinfo, int width, int height, int index = 0)
  687. {
  688. string tempFileName = "";
  689. if (fileinfo.Extension == ".jpg" || fileinfo.Extension == ".jpeg")
  690. {
  691. topdfdoc.InsertPage(index, width, height, filename);
  692. }
  693. else
  694. {
  695. tempFileName = Path.GetTempPath() + "pngtemp.jpg";
  696. if (!PictureConverter.SaveJpeg(filename, tempFileName))
  697. {
  698. MessageBoxEx.Show("图片格式有问题");
  699. }
  700. topdfdoc.InsertPage(index, width, height, tempFileName);
  701. }
  702. }
  703. public void CloseDialoge() { RequestClose?.Invoke(new DialogResult(ButtonResult.OK)); }
  704. #endregion
  705. #region 构架行为
  706. public string Title => "图片转PDF";
  707. public event Action<IDialogResult> RequestClose;
  708. public bool CanCloseDialog()
  709. {
  710. return true;
  711. }
  712. public void OnDialogClosed()
  713. {
  714. }
  715. public void OnDialogOpened(IDialogParameters parameters)
  716. {
  717. string[] filepath = null;
  718. parameters.TryGetValue<string[]>(ParameterNames.FilePath, out filepath);
  719. if (filepath != null)
  720. {
  721. fileNames = filepath.ToList();
  722. updateListview("wait");
  723. }
  724. }
  725. #endregion
  726. }
  727. }