123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195 |
- using System;
- using System.Collections.Generic;
- using System.Diagnostics;
- using System.IO;
- using System.Linq;
- using System.Text;
- using System.Threading.Tasks;
- using System.Windows.Media.Imaging;
- using System.Windows;
- using PDF_Office.Model.EditTools.Background;
- using PDF_Office.Model.EditTools.Watermark;
- using ComPDFKit.PDFDocument;
- using System.Windows.Controls;
- using ComPDFKit.PDFPage;
- using static Dropbox.Api.TeamLog.PaperDownloadFormat;
- using Microsoft.Office.Interop.Word;
- using Task = System.Threading.Tasks.Task;
- using System.Drawing.Imaging;
- using System.Windows.Media;
- namespace PDF_Office.Helper
- {
- public static class EditToolsHelper
- {
- public static byte[] ConvertColor(Color color)
- {
- byte[] rgb_array = new byte[] { 0, 0, 0 };
- rgb_array[0] = color.R;
- rgb_array[1] = color.G;
- rgb_array[2] = color.B;
- return rgb_array;
- }
- public static Color ConvertColor(byte[] rgb_array)
- {
- Color color = new Color();
- color.A = 0xFF;
- color.R = rgb_array[0];
- color.G = rgb_array[1];
- color.B = rgb_array[2];
- return color;
- }
- public static void ChooseFile(string filePath, ref BackgroundInfo backgroundInfo)
- {
- using (FileStream fileData = File.OpenRead(filePath))
- {
- string fileExt = Path.GetExtension(filePath).ToLower();
- BitmapFrame frame = null;
- BitmapDecoder decoder = BitmapDecoder.Create(fileData, BitmapCreateOptions.None, BitmapCacheOption.Default);
- if (decoder != null && decoder.Frames.Count > 0)
- {
- frame = decoder.Frames[0];
- }
- if (frame != null)
- {
- backgroundInfo.ImageArray = new byte[frame.PixelWidth * frame.PixelHeight * 4];
- backgroundInfo.ImageWidth = frame.PixelWidth;
- backgroundInfo.ImageHeight = frame.PixelHeight;
- frame.CopyPixels(backgroundInfo.ImageArray, frame.PixelWidth * 4, 0);
- #if DEBUG
- Trace.WriteLine("width :" + backgroundInfo.ImageWidth);
- Trace.WriteLine("height :" + backgroundInfo.ImageHeight);
- #endif
- }
- }
- }
- public static void ChooseFile(string filePath, ref WatermarkInfo watermarkInfo)
- {
- using (FileStream fileData = File.OpenRead(filePath))
- {
- string fileExt = Path.GetExtension(filePath).ToLower();
- BitmapFrame frame = null;
- BitmapDecoder decoder = BitmapDecoder.Create(fileData, BitmapCreateOptions.None, BitmapCacheOption.Default);
- if (decoder != null && decoder.Frames.Count > 0)
- {
- frame = decoder.Frames[0];
- }
- if (frame != null)
- {
- watermarkInfo.ImageArray = new byte[frame.PixelWidth * frame.PixelHeight * 4];
- watermarkInfo.ImageWidth = frame.PixelWidth;
- watermarkInfo.ImageHeight = frame.PixelHeight;
- frame.CopyPixels(watermarkInfo.ImageArray, frame.PixelWidth * 4, 0);
- #if DEBUG
- Trace.WriteLine("width :" + watermarkInfo.ImageWidth);
- Trace.WriteLine("height :" + watermarkInfo.ImageHeight);
- #endif
- }
- }
- }
- public static string ParseRange(string str)
- {
- try
- {
- string strTemp = "";
- string strParse = "";
- for (int i = 0; i < str.Length; i++)
- {
- if (Int32.TryParse(str.Substring(i, 1), out _))
- {
- strTemp += str[i];
- }
- else
- {
- strParse += int.Parse(strTemp) - 1;
- strTemp = "";
- strParse += str.Substring(i, 1);
- }
- }
- strParse += int.Parse(strTemp) - 1;
- return strParse;
- }
- catch (Exception e)
- {
- return " ";
- }
- }
- public static void GetPageRange(int SelectedIndex, CPDFDocument document, ref string PageRange, string pageRangeText,bool isOne=false)
- {
- switch (SelectedIndex)
- {
- case 0:
- {
- if (isOne)
- {
- PageRange = "1-" + (document.PageCount);
- }
- else { PageRange = "0-" + (document.PageCount - 1); }
-
- }
- break;
- case 1:
- {
- if (isOne)
- {
- PageRange = "1";
- for (int i = 3; i <= (document.PageCount); i += 2)
- PageRange = PageRange + "," + i;
- }
- else {
- PageRange = "0";
- for (int i = 2; i <= (document.PageCount - 1); i += 2)
- PageRange = PageRange + "," + i;
- }
-
- }
- break;
- case 2:
- if (document.PageCount >= 2)
- {
- if (isOne)
- {
- PageRange = "2";
- for (int i = 4; i <= (document.PageCount); i += 2)
- PageRange = PageRange + "," + i;
- }
- else
- {
- PageRange = "1";
- for (int i = 3; i <= (document.PageCount - 1); i += 2)
- PageRange = PageRange + "," + i;
- }
- }
- else
- {
- PageRange="1";
- }
- break;
- case 3:
- if (isOne)
- {
- PageRange = pageRangeText;
- Trace.WriteLine("PageRange : " + PageRange);
- }
- else
- {
- PageRange = ParseRange(pageRangeText);
- Trace.WriteLine("PageRange : " + PageRange);
- }
-
- break;
- default:
- break;
- }
- }
- }
- }
|