123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540 |
- using PDF_Office.Model.PageEdit;
- using PDF_Office.ViewModels.Dialog.BOTA;
- using System;
- using System.Collections.Generic;
- using System.Diagnostics;
- using System.IO;
- using System.Linq;
- using System.Reflection;
- using System.Runtime.InteropServices;
- using System.Text;
- using System.Text.RegularExpressions;
- using System.Threading.Tasks;
- using System.Windows;
- using System.Windows.Media;
- namespace PDF_Office.Helper
- {
- public class Compare<T, C> : IEqualityComparer<T>
- {
- private Func<T, C> _getField;
- public Compare(Func<T, C> getfield)
- {
- this._getField = getfield;
- }
- public bool Equals(T x, T y)
- {
- return EqualityComparer<C>.Default.Equals(_getField(x), _getField(y));
- }
- public int GetHashCode(T obj)
- {
- return EqualityComparer<C>.Default.GetHashCode(this._getField(obj));
- }
- }
- public enum FileExtension
- {
- JPG = 255216,
- GIF = 7173,
- PNG = 13780,
- SWF = 6787,
- RAR = 8297,
- ZIP = 8075,
- _7Z = 55122,
- VALIDFILE = 9999999
- }
- /// <summary>
- /// 1 FindVisualParent 查找目标类型的父类控件
- /// 2 FindVisualChild 查找目标类型的子类控件
- /// 3 GetPageParmFromList 从页码集合获取页码字符串,如1,2,3 转换成1-3
- /// 4 GetPagesInRange 校验PageRange 输入是否合法,且可返回List<int> Pages 存放的索引值
- /// 5.ShowFileBrowser 显示系统文件浏览器,可以根据传入的路径参数,自动选中对应的文件
- /// 6.CreateFilePath 检查对应路径是否有重名,有重名的情况追加尾号
- /// 7.CreateFolder 检查对应文件夹是否有重名,有重名的情况追加尾号
- /// 8.GetUnitsFromPageSize 将PDF页面宽高转换成对应的单位
- /// 9.GetDpi() 返回当前设备DPI
- /// 10.GetFileSize() 获取文件大小
- /// </summary>
- public static class CommonHelper
- {
- [DllImport("user32.dll")]
- private static extern IntPtr SetCapture(long hWnd);
- [DllImport("user32.dll")]
- private static extern long ReleaseCapture();
- /// <summary>
- /// 打开路径并定位文件...对于@"h:\Bleacher Report - Hardaway with the safe call ??.mp4"这样的,explorer.exe /select,d:xxx不认,用API整它
- /// </summary>
- /// <param name="filePath">文件绝对路径</param>
- [DllImport("shell32.dll", ExactSpelling = true)]
- private static extern void ILFree(IntPtr pidlList);
- [DllImport("shell32.dll", CharSet = CharSet.Unicode, ExactSpelling = true)]
- private static extern IntPtr ILCreateFromPathW(string pszPath);
- [DllImport("shell32.dll", ExactSpelling = true)]
- private static extern int SHOpenFolderAndSelectItems(IntPtr pidlList, uint cild, IntPtr children, uint dwFlags);
- /// <summary>
- /// 打开文件夹浏览,并选中一个文件(对于文件名称比较不规范的,可以用这个)
- /// </summary>
- /// <param name="filePath"></param>
- public static void ExplorerFile(string filePath)
- {
- try
- {
- if (!File.Exists(filePath) && !Directory.Exists(filePath))
- return;
- if (Directory.Exists(filePath))
- Process.Start(@"explorer.exe", "/select,\"" + filePath + "\"");
- else
- {
- IntPtr pidlList = ILCreateFromPathW(filePath);
- if (pidlList != IntPtr.Zero)
- {
- try
- {
- Marshal.ThrowExceptionForHR(SHOpenFolderAndSelectItems(pidlList, 0, IntPtr.Zero, 0));
- }
- finally
- {
- ILFree(pidlList);
- }
- }
- }
- }
- catch { }
- }
- /// <summary>
- /// 自定义Distinct扩展方法
- /// </summary>
- /// <typeparam name="T">要去重的对象类</typeparam>
- /// <typeparam name="C">自定义去重的字段类型</typeparam>
- /// <param name="source">要去重的对象</param>
- /// <param name="getfield">获取自定义去重字段的委托</param>
- /// <returns></returns>
- public static IEnumerable<T> DistinctHelper<T, C>(this IEnumerable<T> source, Func<T, C> getfield)
- {
- return source.Distinct(new Compare<T, C>(getfield));
- }
- /// <summary>
- /// 查找对象目标类型的父类控件
- /// </summary>
- /// <typeparam name="T"></typeparam>
- /// <param name="obj"></param>
- /// <returns></returns>
- public static T FindVisualParent<T>(DependencyObject obj) where T : class
- {
- try
- {
- while (obj != null)
- {
- if (obj is T)
- return obj as T;
- obj = VisualTreeHelper.GetParent(obj);
- }
- return null;
- }
- catch { return null; }
- }
- /// <summary>
- /// 根据对象查找目标类型的子类
- /// </summary>
- /// <typeparam name="childItem"></typeparam>
- /// <param name="obj"></param>
- /// <returns></returns>
- public static childItem FindVisualChild<childItem>(DependencyObject obj)
- where childItem : DependencyObject
- {
- try
- {
- for (int i = 0; i < VisualTreeHelper.GetChildrenCount(obj); i++)
- {
- DependencyObject child = VisualTreeHelper.GetChild(obj, i);
- if (child != null && child is childItem)
- return (childItem)child;
- else
- {
- childItem childOfChild = FindVisualChild<childItem>(child);
- if (childOfChild != null)
- return childOfChild;
- }
- }
- return null;
- }
- catch { return null; }
- }
- public static T FindVisualChildByName<T>(DependencyObject parent, string name) where T : DependencyObject
- {
- for (int i = 0; i < VisualTreeHelper.GetChildrenCount(parent); i++)
- {
- var child = VisualTreeHelper.GetChild(parent, i);
- string controlName = child.GetValue(System.Windows.Controls.Control.NameProperty) as string;
- if (controlName == name)
- {
- return child as T;
- }
- else
- {
- T result = FindVisualChildByName<T>(child, name);
- if (result != null)
- return result;
- }
- }
- return null;
- }
- /// <summary>
- /// 从页码集合获取页码字符串,如1,2,3 转换成1-3
- /// </summary>
- /// <param name="pagesList"></param>
- /// <returns></returns>
- public static string GetPageParmFromList(List<int> pagesList)
- {
- string pageParam = "";
- if (pagesList.Count != 0)
- {
- pagesList.Sort();//先对页码排序
- for (int i = 0; i < pagesList.Count; i++)
- {
- if (i == 0)
- {
- pageParam += pagesList[0].ToString();
- }
- else
- {
- if (pagesList[i] == pagesList[i - 1] + 1)//页码连续
- {
- if (i >= 2)
- {
- if (pagesList[i - 1] != pagesList[i - 2] + 1)
- pageParam += "-";
- }
- else
- pageParam += "-";
- if (i == pagesList.Count - 1)
- {
- pageParam += pagesList[i].ToString();
- }
- }
- else//页码不连续时
- {
- if (i >= 2)
- {
- if (pagesList[i - 1] == pagesList[i - 2] + 1)
- pageParam += pagesList[i - 1].ToString();
- }
- pageParam += "," + pagesList[i].ToString();
- }
- }
- }
- }
- return pageParam;
- }
- /// <summary>
- /// 校验PageRange 输入是否合法,且可返回List<int> Pages 存放的索引值
- /// </summary>
- /// <param name="pageIndexList">返回的页面索引集合</param>
- /// <param name="pageRange">需要判断的文本</param>
- /// <param name="count">页面总数</param>
- /// <param name="enumerationSeparator">例 new char[] { ',' }</param>
- /// <param name="rangeSeparator">例 new char[] { '-' }</param>
- /// <param name="inittag"></param>
- /// <returns></returns>
- public static bool GetPagesInRange(ref List<int> pageIndexList, string pageRange, int count, char[] enumerationSeparator, char[] rangeSeparator, bool inittag = false)
- {
- string[] rangeSplit = pageRange.Split(enumerationSeparator);//根据分隔符 拆分字符串
- pageIndexList.Clear();
- foreach (string range in rangeSplit)
- {
- int starttag = 1;
- if (inittag)
- {
- starttag = 0;
- }
- if (range.Contains("-"))//连续页
- {
- try
- {
- string[] limits = range.Split(rangeSeparator);//对子字符串再根据”-“ 拆分
- if (limits.Length >= 2 && !string.IsNullOrWhiteSpace(limits[0]) && !string.IsNullOrWhiteSpace(limits[1]))
- {
- int start = int.Parse(limits[0]);
- int end = int.Parse(limits[1]);
- if ((start < starttag) || (end > count) || (start > end))
- {
- return false;
- }
- for (int i = start; i <= end; ++i)
- {
- if (pageIndexList.Contains(i))
- {
- return false;
- }
- pageIndexList.Add(i - 1);
- }
- continue;
- }
- }
- catch
- {
- return false;
- }
- }
- int pageNr;
- try
- {
- // Single page
- pageNr = int.Parse(range);//单页
- }
- catch//格式不正确时
- {
- return false;
- }
- if (pageNr < starttag || pageNr > count)
- {
- return false;
- }
- if (pageIndexList.Contains(pageNr))
- {
- return false;
- }
- pageIndexList.Add(pageNr - 1);
- }
- return true;
- }
- /// <summary>
- /// 显示系统文件浏览器
- /// </summary>
- /// <param name="selectedFile">要选中的文件路径</param>
- public static void ShowFileBrowser(string selectedFile = null)
- {
- if (string.IsNullOrEmpty(selectedFile))
- {
- Process.Start(@"explorer.exe");
- }
- else
- {
- Process.Start(@"explorer.exe", "/select,\"" + selectedFile + "\"");
- }
- }
- /// <summary>
- /// 检测文件是否重复 追加尾号
- /// </summary>
- /// <param name="path"></param>
- /// <returns></returns>
- public static string CreateFilePath(string path)
- {
- int i = 1;
- string oldDestName = path;
- do
- {
- if (File.Exists(path))
- {
- int lastDot = oldDestName.LastIndexOf('.');
- string fileExtension = string.Empty;
- string fileName = oldDestName;
- if (lastDot > 0)
- {
- fileExtension = fileName.Substring(lastDot);
- fileName = fileName.Substring(0, lastDot);
- }
- path = fileName + string.Format(@"({0})", i) + fileExtension;
- }
- ++i;
- } while (File.Exists(path));
- return path;
- }
- /// <summary>
- /// 检查文件夹是否重复 追加尾号
- /// </summary>
- /// <param name="path"></param>
- /// <returns></returns>
- public static string CreateFolder(string folder)
- {
- int i = 1;
- string oldDestName = folder;
- DirectoryInfo info = new DirectoryInfo(folder);
- do
- {
- info = new DirectoryInfo(folder);
- if (info.Exists)
- {
- string fileName = oldDestName;
- folder = fileName + string.Format(@"({0})", i);
- }
- ++i;
- } while (info.Exists);
- info.Create();
- return folder;
- }
- /// <summary>
- /// 将Document 返回的PageSize 转换成对应单位
- /// </summary>
- /// <param name="size"></param>
- /// <param name="unit"></param>
- /// <returns></returns>
- public static double GetUnitsFromPageSize(double size, PageItemUnits unit = PageItemUnits.MM)
- {
- double sizeWithUnit = 0;
- switch (unit)
- {
- case PageItemUnits.MM:
- sizeWithUnit = size / 72 * 25.4;
- break;
- case PageItemUnits.CM:
- sizeWithUnit = size / 72 * 25.4 / 10.0;
- break;
- case PageItemUnits.IN:
- sizeWithUnit = size / 72;
- break;
- default:
- break;
- }
- return sizeWithUnit;
- }
- /// <summary>
- /// 将mm单位转换成pdf文件尺寸
- /// </summary>
- /// <param name="size"></param>
- /// <returns></returns>
- public static double GetPageSizeFomrUnit(double size)
- {
- double pagesize = 0;
- pagesize = size / 25.4 * 72.0;
- return pagesize;
- }
- /// <summary>
- /// 返回设备DPI
- /// </summary>
- /// <returns></returns>
- public static double GetDpi()
- {
- BindingFlags bindingAttr = BindingFlags.Static | BindingFlags.NonPublic;
- PropertyInfo property = typeof(SystemParameters).GetProperty("Dpi", bindingAttr);
- return (int)property.GetValue(null, null);
- }
- /// <summary>
- /// 根据路径计算文件大小
- /// </summary>
- /// <param name="path"></param>
- /// <returns></returns>
- public static string GetFileSize(string path)
- {
- System.IO.FileInfo fileInfo = null;
- try
- {
- fileInfo = new System.IO.FileInfo(path);
- }
- catch
- {
- return "0KB";
- }
- if (fileInfo != null && fileInfo.Exists)
- {
- var size = Math.Round(fileInfo.Length / 1024.0, 0);
- if (size > 1024)
- {
- var sizeDouble = Math.Round(size / 1024.0, 2);
- if (sizeDouble > 1024)
- {
- sizeDouble = Math.Round(sizeDouble / 1024.0, 2);
- return sizeDouble + "G";
- }
- else
- return sizeDouble + "M";
- }
- else
- {
- return (int)System.Math.Ceiling(size) + "KB";
- }
- }
- else
- {
- return "0KB";
- }
- }
- /// <summary>
- /// 将sdk里的日期转化为标准日期格式
- /// </summary>
- /// <param name="sdkDate"></param>
- /// <returns></returns>
- public static string GetDate(string sdkDate)
- {
- try
- {
- if (sdkDate == null || string.IsNullOrEmpty(sdkDate))
- {
- return "";
- }
- string dateStr = Regex.Match(sdkDate, "(?<=D\\:)[0-9]+(?=[\\+\\-])").Value;
- if (string.IsNullOrEmpty(dateStr))
- {
- return "";
- }
- string text = dateStr.Substring(0, 4) + "-" + dateStr.Substring(4, 2) + "-" + dateStr.Substring(6, 2) + " " + dateStr.Substring(8, 2) + ":" + dateStr.Substring(10, 2) + ":" + dateStr.Substring(12, 2);
- return text;
- }
- catch { return ""; }
- }
- }
- }
|