EditToolsHelper.cs 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163
  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. using System.Windows.Media;
  20. namespace PDF_Office.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.R = rgb_array[0];
  36. color.G = rgb_array[1];
  37. color.B = rgb_array[2];
  38. return color;
  39. }
  40. public static void ChooseFile(string filePath, ref BackgroundInfo backgroundInfo)
  41. {
  42. using (FileStream fileData = File.OpenRead(filePath))
  43. {
  44. string fileExt = Path.GetExtension(filePath).ToLower();
  45. BitmapFrame frame = null;
  46. BitmapDecoder decoder = BitmapDecoder.Create(fileData, BitmapCreateOptions.None, BitmapCacheOption.Default);
  47. if (decoder != null && decoder.Frames.Count > 0)
  48. {
  49. frame = decoder.Frames[0];
  50. }
  51. if (frame != null)
  52. {
  53. backgroundInfo.ImageArray = new byte[frame.PixelWidth * frame.PixelHeight * 4];
  54. backgroundInfo.ImageWidth = frame.PixelWidth;
  55. backgroundInfo.ImageHeight = frame.PixelHeight;
  56. frame.CopyPixels(backgroundInfo.ImageArray, frame.PixelWidth * 4, 0);
  57. #if DEBUG
  58. Trace.WriteLine("width :" + backgroundInfo.ImageWidth);
  59. Trace.WriteLine("height :" + backgroundInfo.ImageHeight);
  60. #endif
  61. }
  62. }
  63. }
  64. public static void ChooseFile(string filePath, ref WatermarkInfo watermarkInfo)
  65. {
  66. using (FileStream fileData = File.OpenRead(filePath))
  67. {
  68. string fileExt = Path.GetExtension(filePath).ToLower();
  69. BitmapFrame frame = null;
  70. BitmapDecoder decoder = BitmapDecoder.Create(fileData, BitmapCreateOptions.None, BitmapCacheOption.Default);
  71. if (decoder != null && decoder.Frames.Count > 0)
  72. {
  73. frame = decoder.Frames[0];
  74. }
  75. if (frame != null)
  76. {
  77. watermarkInfo.ImageArray = new byte[frame.PixelWidth * frame.PixelHeight * 4];
  78. watermarkInfo.ImageWidth = frame.PixelWidth;
  79. watermarkInfo.ImageHeight = frame.PixelHeight;
  80. frame.CopyPixels(watermarkInfo.ImageArray, frame.PixelWidth * 4, 0);
  81. #if DEBUG
  82. Trace.WriteLine("width :" + watermarkInfo.ImageWidth);
  83. Trace.WriteLine("height :" + watermarkInfo.ImageHeight);
  84. #endif
  85. }
  86. }
  87. }
  88. public static string ParseRange(string str)
  89. {
  90. try
  91. {
  92. string strTemp = "";
  93. string strParse = "";
  94. for (int i = 0; i < str.Length; i++)
  95. {
  96. if (Int32.TryParse(str.Substring(i, 1), out _))
  97. {
  98. strTemp += str[i];
  99. }
  100. else
  101. {
  102. strParse += int.Parse(strTemp) - 1;
  103. strTemp = "";
  104. strParse += str.Substring(i, 1);
  105. }
  106. }
  107. strParse += int.Parse(strTemp) - 1;
  108. return strParse;
  109. }
  110. catch (Exception e)
  111. {
  112. return " ";
  113. }
  114. }
  115. public static void GetPageRange(int SelectedIndex, CPDFDocument document, ref string PageRange, string pageRangeText)
  116. {
  117. switch (SelectedIndex)
  118. {
  119. case 0:
  120. {
  121. PageRange = "0-" + (document.PageCount - 1);
  122. }
  123. break;
  124. case 1:
  125. {
  126. PageRange = "0";
  127. for (int i = 2; i <= (document.PageCount - 1); i += 2)
  128. PageRange = PageRange + "," + i;
  129. }
  130. break;
  131. case 2:
  132. if (document.PageCount >= 2)
  133. {
  134. PageRange = "1";
  135. for (int i = 3; i <= (document.PageCount - 1); i += 2)
  136. PageRange = PageRange + "," + i;
  137. }
  138. else
  139. {
  140. MessageBox.Show("超出页面");
  141. }
  142. break;
  143. case 3:
  144. PageRange = ParseRange(pageRangeText);
  145. Trace.WriteLine("PageRange : " + PageRange);
  146. break;
  147. default:
  148. break;
  149. }
  150. }
  151. }
  152. }