EditToolsHelper.cs 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Diagnostics;
  4. using System.IO;
  5. using System.Linq;
  6. using System.Text;
  7. using System.Threading.Tasks;
  8. using System.Windows.Media.Imaging;
  9. using System.Windows;
  10. using PDF_Office.Model.EditTools.Background;
  11. using PDF_Office.Model.EditTools.Watermark;
  12. using ComPDFKit.PDFDocument;
  13. using System.Windows.Controls;
  14. using ComPDFKit.PDFPage;
  15. using static Dropbox.Api.TeamLog.PaperDownloadFormat;
  16. using Microsoft.Office.Interop.Word;
  17. using Task = System.Threading.Tasks.Task;
  18. using System.Drawing.Imaging;
  19. namespace PDF_Office.Helper
  20. {
  21. public static class EditToolsHelper
  22. {
  23. private static byte[] BitmapData = null;
  24. private static int Width=0;
  25. private static int Height=0;
  26. public static byte[] ConvertColor(string color)
  27. {
  28. byte[] rgb_array = new byte[] { 0, 0, 0 };
  29. rgb_array[0] = (byte)(color[1] * 15 + color[2]);
  30. rgb_array[1] = (byte)(color[3] * 15 + color[4]);
  31. rgb_array[2] = (byte)(color[5] * 15 + color[6]);
  32. return rgb_array;
  33. }
  34. public static void ChooseFile(string filePath,ref BackgroundInfo backgroundInfo)
  35. {
  36. using (FileStream fileData = File.OpenRead(filePath))
  37. {
  38. string fileExt = Path.GetExtension(filePath).ToLower();
  39. BitmapFrame frame = null;
  40. BitmapDecoder decoder = BitmapDecoder.Create(fileData, BitmapCreateOptions.None, BitmapCacheOption.Default);
  41. if (decoder != null && decoder.Frames.Count > 0)
  42. {
  43. frame = decoder.Frames[0];
  44. }
  45. if (frame != null)
  46. {
  47. backgroundInfo.ImageArray = new byte[frame.PixelWidth * frame.PixelHeight * 4];
  48. backgroundInfo.ImageWidth = frame.PixelWidth;
  49. backgroundInfo.ImageHeight = frame.PixelHeight;
  50. frame.CopyPixels(backgroundInfo.ImageArray, frame.PixelWidth * 4, 0);
  51. #if DEBUG
  52. Trace.WriteLine("width :" + backgroundInfo.ImageWidth);
  53. Trace.WriteLine("height :" + backgroundInfo.ImageHeight);
  54. #endif
  55. }
  56. }
  57. }
  58. public static void ChooseFile(string filePath, ref WatermarkInfo watermarkInfo)
  59. {
  60. using (FileStream fileData = File.OpenRead(filePath))
  61. {
  62. string fileExt = Path.GetExtension(filePath).ToLower();
  63. BitmapFrame frame = null;
  64. BitmapDecoder decoder = BitmapDecoder.Create(fileData, BitmapCreateOptions.None, BitmapCacheOption.Default);
  65. if (decoder != null && decoder.Frames.Count > 0)
  66. {
  67. frame = decoder.Frames[0];
  68. }
  69. if (frame != null)
  70. {
  71. watermarkInfo.ImageArray = new byte[frame.PixelWidth * frame.PixelHeight * 4];
  72. watermarkInfo.ImageWidth = frame.PixelWidth;
  73. watermarkInfo.ImageHeight = frame.PixelHeight;
  74. frame.CopyPixels(watermarkInfo.ImageArray, frame.PixelWidth * 4, 0);
  75. #if DEBUG
  76. Trace.WriteLine("width :" + watermarkInfo.ImageWidth);
  77. Trace.WriteLine("height :" + watermarkInfo.ImageHeight);
  78. #endif
  79. }
  80. }
  81. }
  82. public static void ChooseFile(string filePath, ref WatermarkInfo watermarkInfo, CPDFDocument document) {
  83. GetBitmapFromDocment(filePath, document);
  84. watermarkInfo.ImageArray = BitmapData;
  85. watermarkInfo.ImageWidth = Width;
  86. watermarkInfo.ImageHeight = Height;
  87. }
  88. public static void ChooseFile(string filePath, ref BackgroundInfo backgroundInfo, CPDFDocument document)
  89. {
  90. GetBitmapFromDocment(filePath, document);
  91. backgroundInfo.ImageArray = BitmapData;
  92. backgroundInfo.ImageWidth = Width;
  93. backgroundInfo.ImageHeight = Height;
  94. }
  95. public static async void GetBitmapFromDocment(string filePath,CPDFDocument document)
  96. {
  97. CPDFPage page = document.PageAtIndex(0);
  98. byte[] bmp_data = new byte[(int)page.PageSize.Width * (int)page.PageSize.Height * 4];
  99. await Task.Run(delegate
  100. {
  101. page.RenderPageBitmap(0, 0, (int)page.PageSize.Width, (int)page.PageSize.Height, 0xffffffff, bmp_data, 1);
  102. });
  103. BitmapData = bmp_data;
  104. Width = (int)page.PageSize.Width;
  105. Height = (int)page.PageSize.Height;
  106. }
  107. public static string ParseRange(string str)
  108. {
  109. try
  110. {
  111. string strTemp = "";
  112. string strParse = "";
  113. for (int i = 0; i < str.Length; i++)
  114. {
  115. if (Int32.TryParse(str.Substring(i, 1), out _))
  116. {
  117. strTemp += str[i];
  118. }
  119. else
  120. {
  121. strParse += int.Parse(strTemp) - 1;
  122. strTemp = "";
  123. strParse += str.Substring(i, 1);
  124. }
  125. }
  126. strParse += int.Parse(strTemp) - 1;
  127. return strParse;
  128. }
  129. catch (Exception e)
  130. {
  131. return " ";
  132. }
  133. }
  134. public static void GetPageRange(int SelectedIndex,CPDFDocument document ,ref string PageRange, string pageRangeText)
  135. {
  136. switch (SelectedIndex)
  137. {
  138. case 0:
  139. {
  140. PageRange = "0-" + document.PageCount;
  141. }
  142. break;
  143. case 1:
  144. {
  145. PageRange = "0";
  146. for (int i = 2; i <= document.PageCount; i += 2)
  147. PageRange =PageRange + "," + i;
  148. }
  149. break;
  150. case 2:
  151. if (document.PageCount >= 2)
  152. {
  153. PageRange = "1";
  154. for (int i = 3; i <= document.PageCount; i += 2)
  155. PageRange = PageRange + "," + i;
  156. }
  157. else
  158. {
  159. MessageBox.Show("超出页面");
  160. }
  161. break;
  162. case 3:
  163. PageRange = ParseRange(pageRangeText);
  164. Trace.WriteLine("PageRange : " + PageRange);
  165. break;
  166. default:
  167. break;
  168. }
  169. }
  170. }
  171. }