EditToolsHelper.cs 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156
  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 string ParseRange(string str)
  83. {
  84. try
  85. {
  86. string strTemp = "";
  87. string strParse = "";
  88. for (int i = 0; i < str.Length; i++)
  89. {
  90. if (Int32.TryParse(str.Substring(i, 1), out _))
  91. {
  92. strTemp += str[i];
  93. }
  94. else
  95. {
  96. strParse += int.Parse(strTemp) - 1;
  97. strTemp = "";
  98. strParse += str.Substring(i, 1);
  99. }
  100. }
  101. strParse += int.Parse(strTemp) - 1;
  102. return strParse;
  103. }
  104. catch (Exception e)
  105. {
  106. return " ";
  107. }
  108. }
  109. public static void GetPageRange(int SelectedIndex,CPDFDocument document ,ref string PageRange, string pageRangeText)
  110. {
  111. switch (SelectedIndex)
  112. {
  113. case 0:
  114. {
  115. PageRange = "0-" + document.PageCount;
  116. }
  117. break;
  118. case 1:
  119. {
  120. PageRange = "0";
  121. for (int i = 2; i <= document.PageCount; i += 2)
  122. PageRange =PageRange + "," + i;
  123. }
  124. break;
  125. case 2:
  126. if (document.PageCount >= 2)
  127. {
  128. PageRange = "1";
  129. for (int i = 3; i <= document.PageCount; i += 2)
  130. PageRange = PageRange + "," + i;
  131. }
  132. else
  133. {
  134. MessageBox.Show("超出页面");
  135. }
  136. break;
  137. case 3:
  138. PageRange = ParseRange(pageRangeText);
  139. Trace.WriteLine("PageRange : " + PageRange);
  140. break;
  141. default:
  142. break;
  143. }
  144. }
  145. }
  146. }