HomePagePictureToPDFDialogViewModel.cs 27 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760
  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. {if (fileNamesView.Count > 0)
  455. { removepicturefile(); }
  456. else
  457. {
  458. FileNameNumber = fileNames.Count;
  459. //Trace.WriteLine(filenamesview);
  460. fileNames.Clear();
  461. if (fileNames.Count < 1)
  462. {
  463. SetPictureToPDFGridIsEnabled = "False";
  464. AddFileVisibility = Visibility.Visible;
  465. RemoveIsEnable = "False";
  466. }
  467. updateListview("wait");
  468. }
  469. }
  470. public void removepicturefile()
  471. {
  472. FileNameNumber = fileNames.Count;
  473. Reverseorder(ref fileNamesView);
  474. foreach (int filenamesview in fileNamesView)
  475. {
  476. //Trace.WriteLine(filenamesview);
  477. fileNames.Remove(fileNames[filenamesview]);
  478. }
  479. if (fileNames.Count < 1)
  480. {
  481. SetPictureToPDFGridIsEnabled = "False";
  482. AddFileVisibility = Visibility.Visible;
  483. RemoveIsEnable = "False";
  484. }
  485. updateListview("wait");
  486. }
  487. public void removepdffile(int index)
  488. {
  489. ImagesCurrentDataTable.Rows.RemoveAt(index);
  490. fileNames.Remove(fileNames[index]);
  491. if (fileNames.Count < 1)
  492. {
  493. SetPictureToPDFGridIsEnabled = "False";
  494. AddFileVisibility = Visibility.Visible;
  495. RemoveIsEnable = "False";
  496. }
  497. updateListview("wait");
  498. }
  499. /// <summary>
  500. /// 打开文件图片
  501. /// </summary>
  502. public void openfiledialog()
  503. {
  504. foreach (int filenamesview in fileNamesView)
  505. {
  506. System.Diagnostics.Process.Start("Explorer", "/select," + fileNames[filenamesview]);
  507. }
  508. }
  509. /// <summary>
  510. /// 删除重复的文件
  511. /// </summary>
  512. public void RemoveExcess(ref List<string> Filenames)
  513. {
  514. List<string> filenames = new List<string>();
  515. foreach (var fileName in Filenames)
  516. {
  517. if (!filenames.Contains(fileName))
  518. {
  519. filenames.Add(fileName);
  520. }
  521. }
  522. Filenames.Clear();
  523. Filenames = filenames;
  524. }
  525. /// <summary>
  526. /// 逆序int类型集合
  527. /// </summary>
  528. public void Reverseorder(ref List<int> Numbers)
  529. {
  530. Numbers = Numbers.OrderBy(a => a).ToList();
  531. Numbers.Reverse();
  532. }
  533. /// <summary>
  534. /// 更新listview显示
  535. /// state 状态显示字符串
  536. /// </summary>
  537. public void updateListview(string state)
  538. {
  539. updateCurrentListview();
  540. DataTable imagesdatatable = new DataTable();
  541. imagesdatatable.Columns.Add("FileName");
  542. imagesdatatable.Columns.Add("FileSize");
  543. imagesdatatable.Columns.Add("FileState");
  544. int datatableindex = 0;
  545. foreach (var fileName in fileNames)
  546. {
  547. string file_all = fileName;
  548. FileInfo f = new FileInfo(file_all);
  549. if (!f.Exists) { continue; }
  550. string file_size = (((float)f.Length) / 1024).ToString() + " K";
  551. imagesdatatable.Rows.Add(f.Name, file_size, ImagesCurrentDataTable.Rows[datatableindex]["FileState"]);
  552. datatableindex++;
  553. }
  554. ImagesDataTable = imagesdatatable;
  555. }
  556. /// <summary>
  557. /// 更新Currentlistview显示
  558. /// pagerangetext 自定义页面范围字符串 pagerangeselectindex combobox下拉索引(0全部页面 1奇数页 2偶数页 3自定义页面)isevenpageisenabled 偶数页状态 state 状态显示字符串
  559. /// </summary>
  560. public void updateCurrentListview(string state = "wait")
  561. {
  562. if (fileNames.Count >= FileNameNumber)
  563. {
  564. for (int i = 0; fileNames.Count - FileNameNumber > i; i++)
  565. {
  566. ImagesCurrentDataTable.Rows.Add(state);
  567. }
  568. }
  569. else
  570. {
  571. Reverseorder(ref fileNamesView);
  572. foreach (int filenamesview in fileNamesView)
  573. {
  574. ImagesCurrentDataTable.Rows.RemoveAt(filenamesview);
  575. }
  576. }
  577. }
  578. #endregion
  579. #region 图片转换
  580. /// <summary>
  581. /// 图片转PNG
  582. /// picturefile 图片文件名
  583. /// filePath 保存路径
  584. /// </summary>
  585. public bool SavePng(string picturefile, string filePath)
  586. {
  587. Image img = Image.FromFile(picturefile);
  588. try
  589. {
  590. using (var bmp = new Bitmap(img.Width, img.Height))
  591. {
  592. bmp.SetResolution(img.HorizontalResolution, img.VerticalResolution);
  593. using (var g = Graphics.FromImage(bmp))
  594. {
  595. g.Clear(Color.White);
  596. g.DrawImageUnscaled(img, 0, 0);
  597. }
  598. bmp.Save(filePath, System.Drawing.Imaging.ImageFormat.Png);
  599. }
  600. return true;
  601. }
  602. catch
  603. {
  604. return false;
  605. }
  606. }
  607. /// <summary>
  608. /// 图片转JPeG
  609. /// picturefile 图片文件名
  610. /// filePath 保存路径
  611. /// </summary>
  612. public bool SaveJpeg(string picturefile, string filePath)
  613. {
  614. Image img = Image.FromFile(picturefile);
  615. try
  616. {
  617. using (var bmp = new Bitmap(img.Width, img.Height))
  618. {
  619. bmp.SetResolution(img.HorizontalResolution, img.VerticalResolution);
  620. using (var g = Graphics.FromImage(bmp))
  621. {
  622. g.Clear(Color.White);
  623. g.DrawImageUnscaled(img, 0, 0);
  624. }
  625. //存储各种格式
  626. //bmp.Save(filePath, System.Drawing.Imaging.ImageFormat.Gif);
  627. //bmp.Save(filePath, System.Drawing.Imaging.ImageFormat.Png);
  628. bmp.Save(filePath, System.Drawing.Imaging.ImageFormat.Jpeg);
  629. }
  630. return true;
  631. }
  632. catch
  633. {
  634. return false;
  635. }
  636. }
  637. /// <summary>
  638. /// 插入JPeG图片
  639. /// topdfdoc 所要插入的文档
  640. /// filename 图片文件名
  641. /// fileinfo 文件信息
  642. /// width page宽
  643. /// height page高
  644. /// index 插入位置
  645. /// </summary>
  646. private void JpegInsertPage(ref CPDFDocument topdfdoc, string filename, FileInfo fileinfo, int width, int height, int index = 0)
  647. {
  648. string tempFileName = "";
  649. if (fileinfo.Extension == ".jpg" || fileinfo.Extension == ".jpeg")
  650. {
  651. topdfdoc.InsertPage(index, width, height, filename);
  652. }
  653. else
  654. {
  655. tempFileName = Path.GetTempPath() + "pngtemp.jpg";
  656. if (!PictureConverter.SaveJpeg(filename, tempFileName))
  657. {
  658. MessageBoxEx.Show("图片格式有问题");
  659. }
  660. topdfdoc.InsertPage(index, width, height, tempFileName);
  661. }
  662. }
  663. public void CloseDialoge() { RequestClose?.Invoke(new DialogResult(ButtonResult.OK)); }
  664. #endregion
  665. #region 构架行为
  666. public string Title => "图片转PDF";
  667. public event Action<IDialogResult> RequestClose;
  668. public bool CanCloseDialog()
  669. {
  670. return true;
  671. }
  672. public void OnDialogClosed()
  673. {
  674. }
  675. public void OnDialogOpened(IDialogParameters parameters)
  676. {
  677. string[] filepath = null;
  678. parameters.TryGetValue<string[]>(ParameterNames.FilePath, out filepath);
  679. if (filepath != null)
  680. {
  681. fileNames = filepath.ToList();
  682. updateListview("wait");
  683. }
  684. }
  685. #endregion
  686. }
  687. }