ToolMethod.cs 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186
  1. using ComPDFKit.PDFDocument;
  2. using ComPDFKit.PDFPage;
  3. using PDF_Master.CustomControl;
  4. using PDFSettings;
  5. using System;
  6. using System.Collections.Generic;
  7. using System.Drawing;
  8. using System.Drawing.Imaging;
  9. using System.IO;
  10. using System.Linq;
  11. using System.Text;
  12. using System.Threading.Tasks;
  13. using System.Windows.Media.Imaging;
  14. namespace PDF_Master.Helper
  15. {
  16. public static class ToolMethod
  17. {
  18. /// <summary>
  19. /// 返回跟DPi 无关的图片
  20. /// </summary>
  21. /// <param name="document"></param>
  22. /// <param name="width"></param>
  23. /// <param name="height"></param>
  24. /// <param name="pageIndex"></param>
  25. /// <returns></returns>
  26. public static async Task<Bitmap> RenderPageBitmap(CPDFDocument document,int width,int height,int pageIndex,bool annot,bool form)
  27. {
  28. if (width <= 0.0 || height<= 0.0)
  29. {
  30. return null;
  31. }
  32. Bitmap bitmap = new Bitmap((int)width, (int)height, PixelFormat.Format32bppArgb);
  33. BitmapData bitmapData = bitmap.LockBits(new Rectangle(0, 0, (int)width, (int)height), ImageLockMode.ReadWrite, bitmap.PixelFormat);
  34. try
  35. {
  36. CPDFPage kMPDFPage = document.PageAtIndex(pageIndex);
  37. await Task.Run(()=> kMPDFPage.RenderPageBitmap(0,0,width,height, uint.MaxValue, bitmapData.Scan0,annot?1:0,form));
  38. // kMPDFPage.RenderPageBitmapWithMatrix((float)1, new System.Windows.Rect(), uint.MaxValue, bitmapData.Scan0, 1, form: true);
  39. }
  40. catch (Exception)
  41. {
  42. return null;
  43. }
  44. bitmap.UnlockBits(bitmapData);
  45. return bitmap;
  46. }
  47. public static Bitmap RenderPageBitmapNoWait(CPDFDocument document, int width, int height, int pageIndex,bool annote,bool form,uint custombackground=0xFFFFFFFF)
  48. {
  49. if (width <= 0.0 || height <= 0.0)
  50. {
  51. return null;
  52. }
  53. Bitmap bitmap = new Bitmap(width, height, PixelFormat.Format32bppArgb);
  54. BitmapData bitmapData = bitmap.LockBits(new Rectangle(0, 0, (int)width, (int)height), ImageLockMode.ReadWrite, bitmap.PixelFormat);
  55. try
  56. {
  57. CPDFPage kMPDFPage = document.PageAtIndex(pageIndex);
  58. //1-显示注释 0-不显示注释 form-true 显示表单 form-false 不显示表单
  59. if (custombackground != 0xFFFFFFFF)
  60. {
  61. kMPDFPage.RenderPageBitmap(0, 0, width, height, custombackground, bitmapData.Scan0, annote ? 1 : 0, form);
  62. }
  63. else
  64. {
  65. kMPDFPage.RenderPageBitmap(0, 0, width, height, uint.MaxValue, bitmapData.Scan0, annote ? 1 : 0, form);
  66. }
  67. //kMPDFPage.RenderPageBitmapWithMatrix((float)1, new System.Windows.Rect(), uint.MaxValue, bitmapData.Scan0, 1, form: true);
  68. }
  69. catch (Exception)
  70. {
  71. return null;
  72. }
  73. bitmap.UnlockBits(bitmapData);
  74. return bitmap;
  75. }
  76. public static void SetFileThumbImg(string fileName)
  77. {
  78. //检查是否是加密文档 要在open前检查
  79. bool isLockedfile = false;
  80. CPDFDocument tempdoc = CPDFDocument.InitWithFilePath(fileName);
  81. if (tempdoc != null && (bool)(tempdoc.IsLocked))
  82. isLockedfile = true;
  83. OpenFileInfo fileInfo = SettingHelper.GetFileInfo(fileName);
  84. if (fileInfo != null)
  85. {
  86. try
  87. {
  88. if (string.IsNullOrWhiteSpace(fileInfo.ThumbImgPath) || isLockedfile)//未保存文档首页缩略图时,提取第一页当缩略图
  89. {
  90. if (isLockedfile)
  91. {
  92. fileInfo.ThumbImgPath = "pack://application:,,,/Resources/FilesType/ic_propertybar_file_pdf_lock.png";
  93. }
  94. else
  95. {
  96. if (tempdoc.PageCount > 0)
  97. {
  98. var size = tempdoc.GetPageSize(0);
  99. System.Drawing.Bitmap bitmap = ToolMethod.RenderPageBitmapNoWait(tempdoc, (int)size.Width, (int)size.Height, 0, true, true);
  100. string folderPath = System.IO.Path.Combine(App.CurrentPath, "CoverImage");
  101. //有可能因为其他原因存在同名文件,导致创建文件夹失败,需要先删除文件
  102. //保险措施(猜测)
  103. if (File.Exists(folderPath))
  104. File.Delete(folderPath);
  105. DirectoryInfo folder = new DirectoryInfo(folderPath);
  106. if (!folder.Exists)
  107. folder.Create();
  108. string imageName = Guid.NewGuid().ToString();
  109. string imagePath = System.IO.Path.Combine(folderPath, imageName);
  110. using (FileStream stream = new FileStream(imagePath, FileMode.Create))
  111. {
  112. bitmap.Save(stream, System.Drawing.Imaging.ImageFormat.Png);
  113. }
  114. fileInfo.ThumbImgPath = imagePath;
  115. }
  116. }
  117. }
  118. else//解锁文件覆盖了加密文件的情况 或者本地缩略图文件被删除
  119. {
  120. if (tempdoc.PageCount > 0 && (!File.Exists(fileInfo.ThumbImgPath) || (!tempdoc.IsEncrypted && fileInfo.ThumbImgPath.Equals("pack://application:,,,/Resources/Image/Home/FilesType/ic_propertybar_file_pdf_lock.png"))))
  121. {
  122. var size = tempdoc.GetPageSize(0);
  123. System.Drawing.Bitmap bitmap = ToolMethod.RenderPageBitmapNoWait(tempdoc, (int)size.Width, (int)size.Height, 0, true, true);
  124. string folderPath = System.IO.Path.Combine(App.CurrentPath, "CoverImage");
  125. if (File.Exists(folderPath))
  126. File.Delete(folderPath);
  127. DirectoryInfo folder = new DirectoryInfo(folderPath);
  128. if (!folder.Exists)
  129. folder.Create();
  130. string imageName = Guid.NewGuid().ToString();
  131. string imagePath = System.IO.Path.Combine(folderPath, imageName);
  132. using (FileStream stream = new FileStream(imagePath, FileMode.Create))
  133. {
  134. bitmap.Save(stream, System.Drawing.Imaging.ImageFormat.Png);
  135. }
  136. fileInfo.ThumbImgPath = imagePath;
  137. }
  138. }
  139. }
  140. catch
  141. {
  142. fileInfo.ThumbImgPath = null;
  143. MessageBoxEx.Show("LoadFile_GetThumbnailFailedWarning");
  144. }
  145. }
  146. if(tempdoc!=null)
  147. {
  148. tempdoc?.Release();
  149. }
  150. }
  151. public static BitmapImage GetFileThumbImg(string path)
  152. {
  153. BitmapImage bitmap = new BitmapImage();
  154. if (File.Exists(path))
  155. {
  156. bitmap.BeginInit();
  157. bitmap.CacheOption = BitmapCacheOption.OnLoad;
  158. using (Stream ms = new MemoryStream(File.ReadAllBytes(path)))
  159. {
  160. bitmap.StreamSource = ms;
  161. bitmap.EndInit();
  162. bitmap.Freeze();
  163. }
  164. }
  165. else if (path.Equals("pack://application:,,,/Resources/FilesType/ic_propertybar_file_pdf_lock.png"))
  166. bitmap = new BitmapImage(new Uri("pack://application:,,,/Resources/FilesType/ic_propertybar_file_pdf_lock.png"));
  167. else//最近文档 但是删除了缩略图的情况
  168. bitmap = new BitmapImage(new Uri("pack://application:,,,/Resources/FilesType/ic_propertybar_file_png_Large.png"));
  169. return bitmap;
  170. }
  171. }
  172. }