123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188 |
- 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;
- namespace PDF_Office.Helper
- {
- public static class EditToolsHelper
- {
- private static byte[] BitmapData = null;
- private static int Width=0;
- private static int Height=0;
- public static byte[] ConvertColor(string color)
- {
- byte[] rgb_array = new byte[] { 0, 0, 0 };
- rgb_array[0] = (byte)(color[1] * 15 + color[2]);
- rgb_array[1] = (byte)(color[3] * 15 + color[4]);
- rgb_array[2] = (byte)(color[5] * 15 + color[6]);
- return rgb_array;
- }
- 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 void ChooseFile(string filePath, ref WatermarkInfo watermarkInfo, CPDFDocument document) {
- GetBitmapFromDocment(filePath, document);
- watermarkInfo.ImageArray = BitmapData;
- watermarkInfo.ImageWidth = Width;
- watermarkInfo.ImageHeight = Height;
- }
- public static void ChooseFile(string filePath, ref BackgroundInfo backgroundInfo, CPDFDocument document)
- {
- GetBitmapFromDocment(filePath, document);
- backgroundInfo.ImageArray = BitmapData;
- backgroundInfo.ImageWidth = Width;
- backgroundInfo.ImageHeight = Height;
- }
- public static async void GetBitmapFromDocment(string filePath,CPDFDocument document)
- {
- CPDFPage page = document.PageAtIndex(0);
- byte[] bmp_data = new byte[(int)page.PageSize.Width * (int)page.PageSize.Height * 4];
- await Task.Run(delegate
- {
- page.RenderPageBitmap(0, 0, (int)page.PageSize.Width, (int)page.PageSize.Height, 0xffffffff, bmp_data, 1);
- });
- BitmapData = bmp_data;
- Width = (int)page.PageSize.Width;
- Height = (int)page.PageSize.Height;
- }
- 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)
- {
-
- switch (SelectedIndex)
- {
- case 0:
- {
- PageRange = "0-" + document.PageCount;
- }
- break;
- case 1:
- {
- PageRange = "0";
- for (int i = 2; i <= document.PageCount; i += 2)
- PageRange =PageRange + "," + i;
- }
- break;
- case 2:
- if (document.PageCount >= 2)
- {
- PageRange = "1";
- for (int i = 3; i <= document.PageCount; i += 2)
- PageRange = PageRange + "," + i;
- }
- else
- {
- MessageBox.Show("超出页面");
- }
- break;
- case 3:
- PageRange = ParseRange(pageRangeText);
- Trace.WriteLine("PageRange : " + PageRange);
- break;
- default:
- break;
- }
- }
- }
- }
|