HomePagePictureToPDFDialogViewModel.cs 28 KB

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