ToolMethod.cs 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193
  1. using ComPDFKit.PDFDocument;
  2. using ComPDFKit.PDFPage;
  3. using PDF_Office.CustomControl;
  4. using PDFSettings.Settings;
  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_Office.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. bool isNewDocument = false;
  79. //检查是否是新文档
  80. OpenFileInfo isnew = SettingHelper.GetFileInfo(fileName);
  81. if (isnew == null)
  82. isNewDocument = true;
  83. //检查是否是加密文档 要在open前检查
  84. bool isLockedfile = false;
  85. CPDFDocument tempdoc = CPDFDocument.InitWithFilePath(fileName);
  86. if (tempdoc != null && (bool)(tempdoc.IsLocked))
  87. isLockedfile = true;
  88. OpenFileInfo fileInfo = SettingHelper.GetFileInfo(fileName);
  89. if (fileInfo != null)
  90. {
  91. try
  92. {
  93. if (string.IsNullOrWhiteSpace(fileInfo.ThumbImgPath) || isLockedfile)//未保存文档首页缩略图时,提取第一页当缩略图
  94. {
  95. if (isLockedfile)
  96. {
  97. fileInfo.ThumbImgPath = "pack://application:,,,/Resources/FilesType/ic_propertybar_file_pdf_lock.png";
  98. }
  99. else
  100. {
  101. if (tempdoc.PageCount > 0)
  102. {
  103. var size = tempdoc.GetPageSize(0);
  104. System.Drawing.Bitmap bitmap = ToolMethod.RenderPageBitmapNoWait(tempdoc, (int)size.Width, (int)size.Height, 0, true, true);
  105. string folderPath = System.IO.Path.Combine(App.CurrentPath, "CoverImage");
  106. //有可能因为其他原因存在同名文件,导致创建文件夹失败,需要先删除文件
  107. //保险措施(猜测)
  108. if (File.Exists(folderPath))
  109. File.Delete(folderPath);
  110. DirectoryInfo folder = new DirectoryInfo(folderPath);
  111. if (!folder.Exists)
  112. folder.Create();
  113. string imageName = Guid.NewGuid().ToString();
  114. string imagePath = System.IO.Path.Combine(folderPath, imageName);
  115. using (FileStream stream = new FileStream(imagePath, FileMode.Create))
  116. {
  117. bitmap.Save(stream, System.Drawing.Imaging.ImageFormat.Png);
  118. }
  119. fileInfo.ThumbImgPath = imagePath;
  120. }
  121. }
  122. }
  123. else//解锁文件覆盖了加密文件的情况 或者本地缩略图文件被删除
  124. {
  125. 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"))))
  126. {
  127. var size = tempdoc.GetPageSize(0);
  128. System.Drawing.Bitmap bitmap = ToolMethod.RenderPageBitmapNoWait(tempdoc, (int)size.Width, (int)size.Height, 0, true, true);
  129. string folderPath = System.IO.Path.Combine(App.CurrentPath, "CoverImage");
  130. if (File.Exists(folderPath))
  131. File.Delete(folderPath);
  132. DirectoryInfo folder = new DirectoryInfo(folderPath);
  133. if (!folder.Exists)
  134. folder.Create();
  135. string imageName = Guid.NewGuid().ToString();
  136. string imagePath = System.IO.Path.Combine(folderPath, imageName);
  137. using (FileStream stream = new FileStream(imagePath, FileMode.Create))
  138. {
  139. bitmap.Save(stream, System.Drawing.Imaging.ImageFormat.Png);
  140. }
  141. fileInfo.ThumbImgPath = imagePath;
  142. }
  143. }
  144. }
  145. catch
  146. {
  147. fileInfo.ThumbImgPath = null;
  148. MessageBoxEx.Show("LoadFile_GetThumbnailFailedWarning");
  149. }
  150. }
  151. if(tempdoc!=null)
  152. {
  153. tempdoc?.Release();
  154. }
  155. }
  156. public static BitmapImage GetFileThumbImg(string path)
  157. {
  158. BitmapImage bitmap = new BitmapImage();
  159. if (File.Exists(path))
  160. {
  161. bitmap.BeginInit();
  162. bitmap.CacheOption = BitmapCacheOption.OnLoad;
  163. using (Stream ms = new MemoryStream(File.ReadAllBytes(path)))
  164. {
  165. bitmap.StreamSource = ms;
  166. bitmap.EndInit();
  167. bitmap.Freeze();
  168. }
  169. }
  170. else if (path.Equals("pack://application:,,,/Resources/FilesType/ic_propertybar_file_pdf_lock.png"))
  171. bitmap = new BitmapImage(new Uri("pack://application:,,,/Resources/FilesType/ic_propertybar_file_pdf_lock.png"));
  172. else//最近文档 但是删除了缩略图的情况
  173. bitmap = new BitmapImage(new Uri("pack://application:,,,/Resources/FilesType/ic_propertybar_file_png_Large.png"));
  174. return bitmap;
  175. }
  176. }
  177. }