HomePagePictureToPDFDialogViewModel.cs 27 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755
  1. using PDF_Office.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_Office.CustomControl;
  13. using System.Windows;
  14. using PDF_Office.Helper;
  15. using PDF_Office.Model.Dialog.HomePageToolsDialogs;
  16. using ComPDFKit.PDFDocument;
  17. using System.Drawing;
  18. using DialogResult = Prism.Services.Dialogs.DialogResult;
  19. namespace PDF_Office.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. Image img = Image.FromFile(filename);
  239. if (img == null)
  240. {
  241. ImagesDataTable.Rows[fileNamesIndex]["FileState"] = "error";
  242. ImagesCurrentDataTable.Rows[fileNamesIndex]["FileState"] = "error";
  243. continue;
  244. }
  245. CPDFDocument topdfdoc = CPDFDocument.CreateDocument();
  246. FileInfo fileinfo = new FileInfo(filename);
  247. JpegInsertPage(ref topdfdoc, filename, fileinfo, img.Width, img.Height);
  248. Trace.WriteLine("Path.GetTempPath():" + Path.GetTempPath());
  249. fileName = savefilename(filename, saveSelectedPath);
  250. topdfdoc.WriteToFilePath(fileName);
  251. topdfdoc.Release();
  252. string file_size = (((float)fileinfo.Length) / 1024).ToString() + " K";
  253. ImagesDataTable.Rows[fileNamesIndex]["FileState"] = "complete";
  254. ImagesCurrentDataTable.Rows[fileNamesIndex]["FileState"] = "complete";
  255. fileNamesIndex++;
  256. //System.Diagnostics.Process.Start("Explorer", "/select," + fileName);
  257. }
  258. System.Diagnostics.Process.Start("Explorer", "/select," + fileName);
  259. }
  260. if (pictureToPDFModel.Mode == HomePagePictureToPDFDialogModel.ToPDFFileMode.OneNewFile)
  261. {
  262. CPDFDocument topdfdoc = CPDFDocument.CreateDocument();
  263. int pageindex = 0;
  264. FolderBrowserDialog folderDialog = new FolderBrowserDialog();
  265. System.Windows.Forms.SaveFileDialog sfd = new System.Windows.Forms.SaveFileDialog();
  266. sfd.FileName = "Untitle";
  267. sfd.Filter = "PDF|*.pdf;";
  268. string NewFileName = "";
  269. if (sfd.ShowDialog() == System.Windows.Forms.DialogResult.OK)
  270. {
  271. NewFileName = sfd.FileName;
  272. }
  273. else { PictureToPDFGridIsEnabled = "True"; return; }
  274. foreach (var filename in fileNames)
  275. {
  276. ImagesDataTable.Rows[fileNamesIndex]["FileState"] = "wait";
  277. ImagesCurrentDataTable.Rows[fileNamesIndex]["FileState"] = "wait";
  278. Image img = Image.FromFile(filename);
  279. if (img == null)
  280. {
  281. ImagesDataTable.Rows[fileNamesIndex]["FileState"] = "error";
  282. ImagesCurrentDataTable.Rows[fileNamesIndex]["FileState"] = "error";
  283. continue;
  284. }
  285. FileInfo fileinfo = new FileInfo(filename);
  286. JpegInsertPage(ref topdfdoc, filename, fileinfo, img.Width, img.Height, pageindex);
  287. pageindex++;
  288. string file_size = (((float)fileinfo.Length) / 1024).ToString() + " K";
  289. ImagesDataTable.Rows[fileNamesIndex]["FileState"] = "complete";
  290. ImagesCurrentDataTable.Rows[fileNamesIndex]["FileState"] = "complete";
  291. fileNamesIndex++;
  292. }
  293. System.Diagnostics.Process.Start("Explorer", "/select," + NewFileName);
  294. topdfdoc.WriteToFilePath(NewFileName);
  295. topdfdoc.Release();
  296. }
  297. if (pictureToPDFModel.Mode == HomePagePictureToPDFDialogModel.ToPDFFileMode.SelectFileName)
  298. {
  299. FolderBrowserDialog dlg = new FolderBrowserDialog();
  300. string saveSelectedPath = "";
  301. if (dlg.ShowDialog() == System.Windows.Forms.DialogResult.OK)
  302. {
  303. saveSelectedPath = dlg.SelectedPath.Trim();
  304. saveSelectedPath = saveSelectedPath + "\\";
  305. }
  306. else { PictureToPDFGridIsEnabled = "True"; return; }
  307. CPDFDocument topdfdoc = CPDFDocument.InitWithFilePath(pictureToPDFModel.FilePath);
  308. if (topdfdoc == null)
  309. {
  310. Trace.WriteLine("Document==null");
  311. //TODO
  312. MessageBoxEx.Show("文档为空");
  313. PictureToPDFGridIsEnabled = "True";
  314. return;
  315. }
  316. if (topdfdoc.IsLocked)
  317. {
  318. DialogParameters value = new DialogParameters();
  319. value.Add(ParameterNames.PDFDocument, topdfdoc);
  320. dialogs.ShowDialog(DialogNames.VerifyPassWordDialog, value, e =>
  321. {
  322. if (e.Result == ButtonResult.OK)
  323. {
  324. if (e.Parameters.ContainsKey(ParameterNames.PassWord) && e.Parameters.GetValue<string>(ParameterNames.PassWord) != null)
  325. {
  326. topdfdoc.UnlockWithPassword(e.Parameters.GetValue<string>(ParameterNames.PassWord).ToString());
  327. }
  328. }
  329. });
  330. if (topdfdoc.IsLocked)
  331. {
  332. //未成功解密文档时,释放Document对象,返回
  333. ImagesDataTable.Rows[fileNamesIndex]["FileState"] = "error";
  334. ImagesCurrentDataTable.Rows[fileNamesIndex]["FileState"] = "error";
  335. fileNamesIndex++;
  336. PictureToPDFGridIsEnabled = "True";
  337. return;
  338. }
  339. }
  340. int pageindex = topdfdoc.PageCount;
  341. foreach (var filename in fileNames)
  342. {
  343. ImagesDataTable.Rows[fileNamesIndex]["FileState"] = "wait";
  344. ImagesCurrentDataTable.Rows[fileNamesIndex]["FileState"] = "wait";
  345. Image img = Image.FromFile(filename);
  346. if (img == null)
  347. {
  348. ImagesDataTable.Rows[fileNamesIndex]["FileState"] = "error";
  349. ImagesCurrentDataTable.Rows[fileNamesIndex]["FileState"] = "error";
  350. continue;
  351. }
  352. FileInfo fileinfo = new FileInfo(filename);
  353. JpegInsertPage(ref topdfdoc, filename, fileinfo, img.Width, img.Height, pageindex);
  354. pageindex++;
  355. string file_size = (((float)fileinfo.Length) / 1024).ToString() + " K";
  356. ImagesDataTable.Rows[fileNamesIndex]["FileState"] = "complete";
  357. ImagesCurrentDataTable.Rows[fileNamesIndex]["FileState"] = "complete";
  358. fileNamesIndex++;
  359. }
  360. string saveselectedpath = CommonHelper.CreateFilePath(saveSelectedPath + topdfdoc.FileName + ".pdf");
  361. topdfdoc.WriteToFilePath(saveselectedpath);
  362. System.Diagnostics.Process.Start("Explorer", "/select," + saveselectedpath);
  363. topdfdoc.Release();
  364. }
  365. PictureToPDFGridIsEnabled = "True";
  366. fileNamesIndex = 0;
  367. }
  368. /// <summary>
  369. /// 设置保存后的文件名
  370. /// </summary>
  371. public string savefilename(string filename, string filepath)
  372. {
  373. FileInfo file = new FileInfo(filename);
  374. return CommonHelper.CreateFilePath(filepath + pictureToPDFModel.FrontFileNameLabel + file.Name.Replace(file.Extension, ".pdf") + pictureToPDFModel.RearFileNameLabel);
  375. }
  376. #endregion
  377. #region 批量处理逻辑函数
  378. /// <summary>
  379. /// 选择PDF文件
  380. /// </summary>
  381. private void selectFile()
  382. {
  383. System.Windows.Forms.OpenFileDialog dlg = new System.Windows.Forms.OpenFileDialog();
  384. dlg.Multiselect = false;
  385. dlg.Filter = "PDF|*.pdf;*.PDF;";
  386. if (dlg.ShowDialog() == System.Windows.Forms.DialogResult.OK)
  387. {
  388. SelectFileName = dlg.FileName;
  389. pictureToPDFModel.FilePath = dlg.FileName;
  390. ImageToPDFBtnIsEnable = "True";
  391. }
  392. }
  393. /// <summary>
  394. /// 添加图片文件
  395. /// </summary>
  396. private void addpicture()
  397. {
  398. FileNameNumber = fileNames.Count;
  399. System.Windows.Forms.OpenFileDialog dlg = new System.Windows.Forms.OpenFileDialog();
  400. dlg.Multiselect = true;
  401. dlg.Filter = "Picture|*.png;*.PNG;*.jpg;*.JPG;*.bmp;*.gif;*tiff;*jpeg;";
  402. if (dlg.ShowDialog() == System.Windows.Forms.DialogResult.OK)
  403. {
  404. fileNames.AddRange(dlg.FileNames.ToList());
  405. RemoveExcess(ref fileNames);
  406. SetPictureToPDFGridIsEnabled = "True";
  407. AddFileVisibility = Visibility.Hidden;
  408. RemoveIsEnable = "True";
  409. updateListview("wait");
  410. }
  411. }
  412. public void addPicture(string filename)
  413. {
  414. FileNameNumber = fileNames.Count;
  415. fileNames.Add(filename);
  416. RemoveExcess(ref fileNames);
  417. SetPictureToPDFGridIsEnabled = "True";
  418. AddFileVisibility = Visibility.Hidden;
  419. RemoveIsEnable = "True";
  420. updateListview("wait");
  421. }
  422. /// <summary>
  423. /// 添加图片文件夹
  424. /// </summary>
  425. private void addpicturefiles()
  426. {
  427. FileNameNumber = fileNames.Count;
  428. FolderBrowserDialog dialog = new FolderBrowserDialog();
  429. dialog.Description = "请选择文件路径";
  430. if (dialog.ShowDialog() == System.Windows.Forms.DialogResult.OK)
  431. {
  432. string foldPath = dialog.SelectedPath;
  433. var apps = System.IO.Directory.GetFiles(foldPath);
  434. foreach (string app in apps)
  435. {
  436. var fi = new FileInfo(app);
  437. if (fi.Extension == ".png" || fi.Extension == ".jpg" || fi.Extension == ".jpeg" || fi.Extension == ".bmp" || fi.Extension == ".gif" || fi.Extension == ".tiff")
  438. {
  439. fileNames.Add(app);
  440. }
  441. }
  442. RemoveExcess(ref fileNames);
  443. updateListview("wait");
  444. SetPictureToPDFGridIsEnabled = "True";
  445. AddFileVisibility = Visibility.Hidden;
  446. RemoveIsEnable = "True";
  447. }
  448. }
  449. /// <summary>
  450. /// 删除图片文件
  451. /// </summary>
  452. ///
  453. public void removepicturefiles()
  454. {
  455. FileNameNumber = fileNames.Count;
  456. //Trace.WriteLine(filenamesview);
  457. fileNames.Clear();
  458. if (fileNames.Count < 1)
  459. {
  460. SetPictureToPDFGridIsEnabled = "False";
  461. AddFileVisibility = Visibility.Visible;
  462. RemoveIsEnable = "False";
  463. }
  464. updateListview("wait");
  465. }
  466. public void removepicturefile()
  467. {
  468. FileNameNumber = fileNames.Count;
  469. Reverseorder(ref fileNamesView);
  470. foreach (int filenamesview in fileNamesView)
  471. {
  472. //Trace.WriteLine(filenamesview);
  473. fileNames.Remove(fileNames[filenamesview]);
  474. }
  475. if (fileNames.Count < 1)
  476. {
  477. SetPictureToPDFGridIsEnabled = "False";
  478. AddFileVisibility = Visibility.Visible;
  479. RemoveIsEnable = "False";
  480. }
  481. updateListview("wait");
  482. }
  483. public void removepdffile(int index)
  484. {
  485. ImagesCurrentDataTable.Rows.RemoveAt(index);
  486. fileNames.Remove(fileNames[index]);
  487. if (fileNames.Count < 1)
  488. {
  489. SetPictureToPDFGridIsEnabled = "False";
  490. AddFileVisibility = Visibility.Visible;
  491. RemoveIsEnable = "False";
  492. }
  493. updateListview("wait");
  494. }
  495. /// <summary>
  496. /// 打开文件图片
  497. /// </summary>
  498. public void openfiledialog()
  499. {
  500. foreach (int filenamesview in fileNamesView)
  501. {
  502. System.Diagnostics.Process.Start("Explorer", "/select," + fileNames[filenamesview]);
  503. }
  504. }
  505. /// <summary>
  506. /// 删除重复的文件
  507. /// </summary>
  508. public void RemoveExcess(ref List<string> Filenames)
  509. {
  510. List<string> filenames = new List<string>();
  511. foreach (var fileName in Filenames)
  512. {
  513. if (!filenames.Contains(fileName))
  514. {
  515. filenames.Add(fileName);
  516. }
  517. }
  518. Filenames.Clear();
  519. Filenames = filenames;
  520. }
  521. /// <summary>
  522. /// 逆序int类型集合
  523. /// </summary>
  524. public void Reverseorder(ref List<int> Numbers)
  525. {
  526. Numbers = Numbers.OrderBy(a => a).ToList();
  527. Numbers.Reverse();
  528. }
  529. /// <summary>
  530. /// 更新listview显示
  531. /// state 状态显示字符串
  532. /// </summary>
  533. public void updateListview(string state)
  534. {
  535. updateCurrentListview();
  536. DataTable imagesdatatable = new DataTable();
  537. imagesdatatable.Columns.Add("FileName");
  538. imagesdatatable.Columns.Add("FileSize");
  539. imagesdatatable.Columns.Add("FileState");
  540. int datatableindex = 0;
  541. foreach (var fileName in fileNames)
  542. {
  543. string file_all = fileName;
  544. FileInfo f = new FileInfo(file_all);
  545. if (!f.Exists) { continue; }
  546. string file_size = (((float)f.Length) / 1024).ToString() + " K";
  547. imagesdatatable.Rows.Add(f.Name, file_size, ImagesCurrentDataTable.Rows[datatableindex]["FileState"]);
  548. datatableindex++;
  549. }
  550. ImagesDataTable = imagesdatatable;
  551. }
  552. /// <summary>
  553. /// 更新Currentlistview显示
  554. /// pagerangetext 自定义页面范围字符串 pagerangeselectindex combobox下拉索引(0全部页面 1奇数页 2偶数页 3自定义页面)isevenpageisenabled 偶数页状态 state 状态显示字符串
  555. /// </summary>
  556. public void updateCurrentListview(string state = "wait")
  557. {
  558. if (fileNames.Count >= FileNameNumber)
  559. {
  560. for (int i = 0; fileNames.Count - FileNameNumber > i; i++)
  561. {
  562. ImagesCurrentDataTable.Rows.Add(state);
  563. }
  564. }
  565. else
  566. {
  567. Reverseorder(ref fileNamesView);
  568. foreach (int filenamesview in fileNamesView)
  569. {
  570. ImagesCurrentDataTable.Rows.RemoveAt(filenamesview);
  571. }
  572. }
  573. }
  574. #endregion
  575. #region 图片转换
  576. /// <summary>
  577. /// 图片转PNG
  578. /// picturefile 图片文件名
  579. /// filePath 保存路径
  580. /// </summary>
  581. public bool SavePng(string picturefile, string filePath)
  582. {
  583. Image img = Image.FromFile(picturefile);
  584. try
  585. {
  586. using (var bmp = new Bitmap(img.Width, img.Height))
  587. {
  588. bmp.SetResolution(img.HorizontalResolution, img.VerticalResolution);
  589. using (var g = Graphics.FromImage(bmp))
  590. {
  591. g.Clear(Color.White);
  592. g.DrawImageUnscaled(img, 0, 0);
  593. }
  594. bmp.Save(filePath, System.Drawing.Imaging.ImageFormat.Png);
  595. }
  596. return true;
  597. }
  598. catch
  599. {
  600. return false;
  601. }
  602. }
  603. /// <summary>
  604. /// 图片转JPeG
  605. /// picturefile 图片文件名
  606. /// filePath 保存路径
  607. /// </summary>
  608. public bool SaveJpeg(string picturefile, string filePath)
  609. {
  610. Image img = Image.FromFile(picturefile);
  611. try
  612. {
  613. using (var bmp = new Bitmap(img.Width, img.Height))
  614. {
  615. bmp.SetResolution(img.HorizontalResolution, img.VerticalResolution);
  616. using (var g = Graphics.FromImage(bmp))
  617. {
  618. g.Clear(Color.White);
  619. g.DrawImageUnscaled(img, 0, 0);
  620. }
  621. //存储各种格式
  622. //bmp.Save(filePath, System.Drawing.Imaging.ImageFormat.Gif);
  623. //bmp.Save(filePath, System.Drawing.Imaging.ImageFormat.Png);
  624. bmp.Save(filePath, System.Drawing.Imaging.ImageFormat.Jpeg);
  625. }
  626. return true;
  627. }
  628. catch
  629. {
  630. return false;
  631. }
  632. }
  633. /// <summary>
  634. /// 插入JPeG图片
  635. /// topdfdoc 所要插入的文档
  636. /// filename 图片文件名
  637. /// fileinfo 文件信息
  638. /// width page宽
  639. /// height page高
  640. /// index 插入位置
  641. /// </summary>
  642. private void JpegInsertPage(ref CPDFDocument topdfdoc, string filename, FileInfo fileinfo, int width, int height, int index = 0)
  643. {
  644. string tempFileName = "";
  645. if (fileinfo.Extension == ".jpg" || fileinfo.Extension == ".jpeg")
  646. {
  647. topdfdoc.InsertPage(index, width, height, filename);
  648. }
  649. else
  650. {
  651. tempFileName = Path.GetTempPath() + "pngtemp.jpg";
  652. if (!PictureConverter.SaveJpeg(filename, tempFileName))
  653. {
  654. MessageBoxEx.Show("图片格式有问题");
  655. }
  656. topdfdoc.InsertPage(index, width, height, tempFileName);
  657. }
  658. }
  659. public void CloseDialoge() { RequestClose?.Invoke(new DialogResult(ButtonResult.OK)); }
  660. #endregion
  661. #region 构架行为
  662. public string Title => "图片转PDF";
  663. public event Action<IDialogResult> RequestClose;
  664. public bool CanCloseDialog()
  665. {
  666. return true;
  667. }
  668. public void OnDialogClosed()
  669. {
  670. }
  671. public void OnDialogOpened(IDialogParameters parameters)
  672. {
  673. string[] filepath = null;
  674. parameters.TryGetValue<string[]>(ParameterNames.FilePath, out filepath);
  675. if (filepath != null)
  676. {
  677. fileNames = filepath.ToList();
  678. updateListview("wait");
  679. }
  680. }
  681. #endregion
  682. }
  683. }