EditToolsHelper.cs 5.4 KB

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