EditToolsHelper.cs 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195
  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_Master.Model.EditTools.Background;
  11. using PDF_Master.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. using System.Windows.Media;
  20. namespace PDF_Master.Helper
  21. {
  22. public static class EditToolsHelper
  23. {
  24. public static byte[] ConvertColor(Color color)
  25. {
  26. byte[] rgb_array = new byte[] { 0, 0, 0 };
  27. rgb_array[0] = color.R;
  28. rgb_array[1] = color.G;
  29. rgb_array[2] = color.B;
  30. return rgb_array;
  31. }
  32. public static Color ConvertColor(byte[] rgb_array)
  33. {
  34. Color color = new Color();
  35. color.A = 0xFF;
  36. color.R = rgb_array[0];
  37. color.G = rgb_array[1];
  38. color.B = rgb_array[2];
  39. return color;
  40. }
  41. public static void ChooseFile(string filePath, ref BackgroundInfo backgroundInfo)
  42. {
  43. using (FileStream fileData = File.OpenRead(filePath))
  44. {
  45. string fileExt = Path.GetExtension(filePath).ToLower();
  46. BitmapFrame frame = null;
  47. BitmapDecoder decoder = BitmapDecoder.Create(fileData, BitmapCreateOptions.None, BitmapCacheOption.Default);
  48. if (decoder != null && decoder.Frames.Count > 0)
  49. {
  50. frame = decoder.Frames[0];
  51. }
  52. if (frame != null)
  53. {
  54. backgroundInfo.ImageArray = new byte[frame.PixelWidth * frame.PixelHeight * 4];
  55. backgroundInfo.ImageWidth = frame.PixelWidth;
  56. backgroundInfo.ImageHeight = frame.PixelHeight;
  57. frame.CopyPixels(backgroundInfo.ImageArray, frame.PixelWidth * 4, 0);
  58. #if DEBUG
  59. Trace.WriteLine("width :" + backgroundInfo.ImageWidth);
  60. Trace.WriteLine("height :" + backgroundInfo.ImageHeight);
  61. #endif
  62. }
  63. }
  64. }
  65. public static void ChooseFile(string filePath, ref WatermarkInfo watermarkInfo)
  66. {
  67. using (FileStream fileData = File.OpenRead(filePath))
  68. {
  69. string fileExt = Path.GetExtension(filePath).ToLower();
  70. BitmapFrame frame = null;
  71. BitmapDecoder decoder = BitmapDecoder.Create(fileData, BitmapCreateOptions.None, BitmapCacheOption.Default);
  72. if (decoder != null && decoder.Frames.Count > 0)
  73. {
  74. frame = decoder.Frames[0];
  75. }
  76. if (frame != null)
  77. {
  78. watermarkInfo.ImageArray = new byte[frame.PixelWidth * frame.PixelHeight * 4];
  79. watermarkInfo.ImageWidth = frame.PixelWidth;
  80. watermarkInfo.ImageHeight = frame.PixelHeight;
  81. frame.CopyPixels(watermarkInfo.ImageArray, frame.PixelWidth * 4, 0);
  82. #if DEBUG
  83. Trace.WriteLine("width :" + watermarkInfo.ImageWidth);
  84. Trace.WriteLine("height :" + watermarkInfo.ImageHeight);
  85. #endif
  86. }
  87. }
  88. }
  89. public static string ParseRange(string str)
  90. {
  91. try
  92. {
  93. string strTemp = "";
  94. string strParse = "";
  95. for (int i = 0; i < str.Length; i++)
  96. {
  97. if (Int32.TryParse(str.Substring(i, 1), out _))
  98. {
  99. strTemp += str[i];
  100. }
  101. else
  102. {
  103. strParse += int.Parse(strTemp) - 1;
  104. strTemp = "";
  105. strParse += str.Substring(i, 1);
  106. }
  107. }
  108. strParse += int.Parse(strTemp) - 1;
  109. return strParse;
  110. }
  111. catch
  112. {
  113. return " ";
  114. }
  115. }
  116. public static void GetPageRange(int SelectedIndex, CPDFDocument document, ref string PageRange, string pageRangeText,bool isOne=false)
  117. {
  118. switch (SelectedIndex)
  119. {
  120. case 0:
  121. {
  122. if (isOne)
  123. {
  124. PageRange = "1-" + (document.PageCount);
  125. }
  126. else { PageRange = "0-" + (document.PageCount - 1); }
  127. }
  128. break;
  129. case 1:
  130. {
  131. if (isOne)
  132. {
  133. PageRange = "1";
  134. for (int i = 3; i <= (document.PageCount); i += 2)
  135. PageRange = PageRange + "," + i;
  136. }
  137. else {
  138. PageRange = "0";
  139. for (int i = 2; i <= (document.PageCount - 1); i += 2)
  140. PageRange = PageRange + "," + i;
  141. }
  142. }
  143. break;
  144. case 2:
  145. if (document.PageCount >= 2)
  146. {
  147. if (isOne)
  148. {
  149. PageRange = "2";
  150. for (int i = 4; i <= (document.PageCount); i += 2)
  151. PageRange = PageRange + "," + i;
  152. }
  153. else
  154. {
  155. PageRange = "1";
  156. for (int i = 3; i <= (document.PageCount - 1); i += 2)
  157. PageRange = PageRange + "," + i;
  158. }
  159. }
  160. else
  161. {
  162. PageRange="1";
  163. }
  164. break;
  165. case 3:
  166. if (isOne)
  167. {
  168. PageRange = pageRangeText;
  169. Trace.WriteLine("PageRange : " + PageRange);
  170. }
  171. else
  172. {
  173. PageRange = ParseRange(pageRangeText);
  174. Trace.WriteLine("PageRange : " + PageRange);
  175. }
  176. break;
  177. default:
  178. break;
  179. }
  180. }
  181. }
  182. }