123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473 |
- using PDF_Office.Model.PDFTool;
- using PDF_Office.Properties;
- using PDFSettings;
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Text;
- using System.Threading.Tasks;
- namespace PDF_Office.Helper
- {
- /// <summary>
- /// 预设需求:远程控制对PDF工具进行排序
- /// </summary>
- public enum PDFToolType
- {
- /// <summary>
- /// 普通工具
- /// </summary>
- Normal,
- /// <summary>
- /// 常用工具
- /// </summary>
- Common,
- /// <summary>
- ///付费工具
- /// </summary>
- Payment,
- /// <summary>
- /// 优势工具
- /// </summary>
- Advantage,
- /// <summary>
- /// 无需打开文档
- /// </summary>
- NoNeedToOpenDoc
- }
- public enum PDFFnType
- {
- Split,
- Extract,
- Insert,
- Compress,
- Merge,
- Print,
- Security,
- ConvertPDF,
- PDFToWord,
- PDFToExcel,
- PDFToPPT,
- ImageToPDF,
- OCR,
- WaterMark,
- HeaderFooter,
- BatesNumbers,
- Batch,
- Background,
- CompareDoc,
- Redact,
- PageEdit,
- BatchRemove,
- FormFiledRecognition,
- PDFToImage,
- }
- /// <summary>
- /// PDF工具初始化类型
- /// </summary>
- internal class PDFToolsInit
- {
- public ToolItem toolItem { get; private set; }
- public PDFToolsInit()
- {
- toolItem = new ToolItem();
- }
- /// <summary>
- /// 图标
- /// </summary>
- public void SetImagePath(string path)
- {
- toolItem.FnImg = path;
- }
- /// <summary>
- /// 初始化UI显示内容
- /// </summary>
- public void SetContent(string fnName,string fnInfo)
- {
- toolItem.FnName = fnName;
- toolItem.FnInfo = fnInfo;
- }
- /// <summary>
- /// 初始化功能
- /// </summary>
- public void SetFnType(PDFFnType fnType)
- {
- toolItem.FnType = fnType;
- }
- }
- public class PDFToolsHelper
- {
- public List<ToolItem> AllTools = null;
- public List<ToolItem> QuickTools = null;
- public List<ToolItem> MoreTools = null;
- private static PDFToolsHelper pDFToolsHelper;
- public const int QuickToolCount = 8;//快捷工具显示的个数
- public static PDFToolsHelper GetInstance()
- {
- if (pDFToolsHelper == null)
- pDFToolsHelper = new PDFToolsHelper();
- return pDFToolsHelper;
- }
- private PDFToolsHelper()
- {
- InitPDFTools();
- }
- private void InitPDFTools()
- {
- AllTools = new List<ToolItem>();
- QuickTools = new List<ToolItem>();
- MoreTools = new List<ToolItem>();
- //测试代码
- //Settings.Default.AllPDFToolsList.Clear();
- //Settings.Default.QuickPDFToolsList.Clear();
- //Settings.Default.Save();
- InitAllTools();
- InitQuickTools();
- InitMoreTools();
- // ContianNewTools();
- }
- //用来测试的
- public void ClearData()
- {
- Settings.Default.AllPDFToolsList.Clear();
- Settings.Default.QuickPDFToolsList.Clear();
- Settings.Default.Save();
- AllTools.Clear();
- QuickTools.Clear();
- MoreTools.Clear();
- InitAllTools();
- InitQuickTools();
- InitMoreTools();
- }
- private void InitAllTools()
- {
- //批量处理
- var batch = new PDFToolsInit();
- batch.SetFnType(PDFFnType.Batch);
- batch.SetImagePath(@"pack://application:,,,/Resources/HomeIcon/PDFTools/batch.png");
- batch.SetContent("批量处理", "Batch convert, compress, secure, watermark PDFs.");
- //OCR
- var oCR = new PDFToolsInit();
- oCR.SetFnType(PDFFnType.OCR);
- oCR.SetImagePath(@"pack://application:,,,/Resources/HomeIcon/PDFTools/ocr.png");
- oCR.SetContent("OCR", "Batch convert, compress, secure, watermark PDFs.");
- //转档PDF
- var convertPDF = new PDFToolsInit();
- convertPDF.SetFnType(PDFFnType.ConvertPDF);
- convertPDF.SetImagePath(@"pack://application:,,,/Resources/HomeIcon/PDFTools/convert.png");
- convertPDF.SetContent("转档PDF", "Batch convert, compress, secure, watermark PDFs.");
- //图片转PDF
- var imageToPDF = new PDFToolsInit();
- imageToPDF.SetFnType(PDFFnType.ImageToPDF);
- imageToPDF.SetImagePath(@"pack://application:,,,/Resources/HomeIcon/PDFTools/image.png");
- imageToPDF.SetContent("Image To PDF", "Batch convert, compress, secure, watermark PDFs.");
- //合并
- var merge = new PDFToolsInit();
- merge.SetFnType(PDFFnType.Merge);
- merge.SetImagePath(@"pack://application:,,,/Resources/HomeIcon/PDFTools/merge.png");
- merge.SetContent("Merge", "Batch convert, compress, secure, watermark PDFs.");
- //压缩
- var compress = new PDFToolsInit();
- compress.SetFnType(PDFFnType.Compress);
- compress.SetImagePath(@"pack://application:,,,/Resources/HomeIcon/PDFTools/compress.png");
- compress.SetContent("Compress", "Batch convert, compress, secure, watermark PDFs.");
- //安全
- var security = new PDFToolsInit();
- security.SetFnType(PDFFnType.Security);
- security.SetImagePath(@"pack://application:,,,/Resources/HomeIcon/PDFTools/security.png");
- security.SetContent("Security", "Batch convert, compress, secure, watermark PDFs.");
- //文件对比
- var compareDoc = new PDFToolsInit();
- compareDoc.SetFnType(PDFFnType.CompareDoc);
- compareDoc.SetImagePath(@"pack://application:,,,/Resources/HomeIcon/PDFTools/compress.png");
- compareDoc.SetContent("文件对比", "Batch convert, compress, secure, watermark PDFs.");
- //PDF转PPT
- var pDFToPPT = new PDFToolsInit();
- pDFToPPT.SetFnType(PDFFnType.PDFToPPT);
- pDFToPPT.SetImagePath(@"pack://application:,,,/Resources/HomeIcon/PDFTools/pdf-to-ppt.png");
- pDFToPPT.SetContent("To PPt", "Batch convert, compress, secure, watermark PDFs.");
- //PDF转Excel
- var pDFToExcel = new PDFToolsInit();
- pDFToExcel.SetFnType(PDFFnType.PDFToExcel);
- pDFToExcel.SetImagePath(@"pack://application:,,,/Resources/HomeIcon/PDFTools/pdf-to-excel.png");
- pDFToExcel.SetContent("To Excel", "Batch convert, compress, secure, watermark PDFs.");
- //PDF转Word
- var pDFToWord = new PDFToolsInit();
- pDFToWord.SetFnType(PDFFnType.PDFToWord);
- pDFToWord.SetImagePath(@"pack://application:,,,/Resources/HomeIcon/PDFTools/pdf-to-word.png");
- pDFToWord.SetContent("To Word", "Batch convert, compress, secure, watermark PDFs.");
- //PDF转图片
- var pDFToImage = new PDFToolsInit();
- pDFToImage.SetFnType(PDFFnType.PDFToImage);
- pDFToImage.SetImagePath(@"pack://application:,,,/Resources/HomeIcon/PDFTools/pdf-to-image.png");
- pDFToImage.SetContent("To Image", "Batch convert, compress, secure, watermark PDFs.");
- //水印
- var waterMark = new PDFToolsInit();
- waterMark.SetFnType(PDFFnType.WaterMark);
- waterMark.SetImagePath(@"pack://application:,,,/Resources/HomeIcon/PDFTools/watermark.png");
- waterMark.SetContent("水印", "Batch convert, compress, secure, watermark PDFs.");
- //背景
- var background = new PDFToolsInit();
- background.SetFnType(PDFFnType.Background);
- background.SetImagePath(@"pack://application:,,,/Resources/HomeIcon/PDFTools/background.png");
- background.SetContent("背景", "Batch convert, compress, secure, watermark PDFs.");
- //页眉页脚
- var headerFooter = new PDFToolsInit();
- headerFooter.SetFnType(PDFFnType.HeaderFooter);
- headerFooter.SetImagePath(@"pack://application:,,,/Resources/HomeIcon/PDFTools/header&footer.png");
- headerFooter.SetContent("页眉页脚", "Batch convert, compress, secure, watermark PDFs.");
- //贝茨Bates码
- var batesNumbers = new PDFToolsInit();
- batesNumbers.SetFnType(PDFFnType.BatesNumbers);
- batesNumbers.SetImagePath(@"pack://application:,,,/Resources/HomeIcon/PDFTools/batesnumbers.png");
- batesNumbers.SetContent("贝茨Bates码", "Batch convert, compress, secure, watermark PDFs.");
- //批量移除
- var batchRemove = new PDFToolsInit();
- batchRemove.SetFnType(PDFFnType.BatchRemove);
- batchRemove.SetImagePath(@"pack://application:,,,/Resources/HomeIcon/PDFTools/batch-remove.png");
- batchRemove.SetContent("批量移除", "Batch convert, compress, secure, watermark PDFs.");
- //编辑
- var redact = new PDFToolsInit();
- redact.SetFnType(PDFFnType.Redact);
- redact.SetImagePath(@"pack://application:,,,/Resources/HomeIcon/PDFTools/reduct.png");
- redact.SetContent("编辑", "Batch convert, compress, secure, watermark PDFs.");
- //表单存档识别
- var formFiledRecognition = new PDFToolsInit();
- formFiledRecognition.SetFnType(PDFFnType.FormFiledRecognition);
- formFiledRecognition.SetImagePath(@"pack://application:,,,/Resources/HomeIcon/PDFTools/form-filed-recognition.png");
- formFiledRecognition.SetContent("表单存档识别", "Batch convert, compress, secure, watermark PDFs.");
- //打印
- var print = new PDFToolsInit();
- print.SetFnType(PDFFnType.Print);
- print.SetImagePath(@"pack://application:,,,/Resources/HomeIcon/PDFTools/print.png");
- print.SetContent("打印", "Batch convert, compress, secure, watermark PDFs.");
- //页面编辑
- var pageEdit = new PDFToolsInit();
- pageEdit.SetFnType(PDFFnType.PageEdit);
- pageEdit.SetImagePath(@"pack://application:,,,/Resources/HomeIcon/PDFTools/page-edit.png");
- pageEdit.SetContent("页面编辑", "Batch convert, compress, secure, watermark PDFs.");
- //插入
- var insert = new PDFToolsInit();
- insert.SetFnType(PDFFnType.Insert);
- insert.SetImagePath(@"pack://application:,,,/Resources/HomeIcon/PDFTools/insert.png");
- insert.SetContent("插入", "Batch convert, compress, secure, watermark PDFs.");
- //拆分
- var split = new PDFToolsInit();
- split.SetFnType(PDFFnType.Split);
- split.SetImagePath(@"pack://application:,,,/Resources/HomeIcon/PDFTools/split.png");
- split.SetContent("拆分", "Batch convert, compress, secure, watermark PDFs.");
- //提取
- var extract = new PDFToolsInit();
- extract.SetFnType(PDFFnType.Extract);
- extract.SetImagePath(@"pack://application:,,,/Resources/HomeIcon/PDFTools/extract.png");
- extract.SetContent("提取", "Batch convert, compress, secure, watermark PDFs.");
- ///设置快捷工具默认初始顺序
- AllTools.Add(pDFToWord.toolItem);
- AllTools.Add(pDFToExcel.toolItem);
- AllTools.Add(pDFToPPT.toolItem);
- AllTools.Add(pDFToImage.toolItem);
- AllTools.Add(security.toolItem);
- AllTools.Add(imageToPDF.toolItem);
- AllTools.Add(merge.toolItem);
- AllTools.Add(compress.toolItem);
- //更多工具默认初始顺序
- //AllTools.Add(split.toolItem);
- //AllTools.Add(extract.toolItem);
- //AllTools.Add(insert.toolItem);
- //AllTools.Add(merge.toolItem);
- //AllTools.Add(print.toolItem);
- //AllTools.Add(convertPDF.toolItem);
- //AllTools.Add(imageToPDF.toolItem);
- //AllTools.Add(oCR.toolItem);
- //AllTools.Add(waterMark.toolItem);
- //AllTools.Add(headerFooter.toolItem);
- //AllTools.Add(batesNumbers.toolItem);
- //AllTools.Add(batch.toolItem);
- //AllTools.Add(background.toolItem);
- //AllTools.Add(compareDoc.toolItem);
- //AllTools.Add(redact.toolItem);
- //AllTools.Add(pageEdit.toolItem);
- //AllTools.Add(batchRemove.toolItem);
- //AllTools.Add(formFiledRecognition.toolItem);
- //AllTools.Add(pDFToImage.toolItem);
- }
- /// <summary>
- /// 检查是否为新增工具
- /// </summary>
- private void ContianNewTools()
- {
- var cacheList = Settings.Default.AllPDFToolsList;
- foreach (var item in AllTools)
- {
- ToolItem toolItem = null;
- foreach (var cacheItem in cacheList)
- {
- var strFnTye = Enum.GetName(typeof(PDFFnType), item.FnType);
- if (cacheItem.FnTypeStr != strFnTye && cacheItem.IsNewTool == true)
- {
- toolItem = item;
- break;
- }
- }
- if(toolItem != null && toolItem.IsNewTool == true)
- {
- CacheToolItem cacheTool = new CacheToolItem();
- cacheTool.IsNewTool = true;
- cacheTool.ToolTypeId = toolItem.ToolTypeId;
- cacheTool.ToolTypeStr = Enum.GetName(typeof(PDFToolType), toolItem.ToolType);
- cacheTool.FnTypeStr = Enum.GetName(typeof(PDFFnType), toolItem.FnType);
- Settings.Default.AllPDFToolsList.Add(cacheTool);
- }
-
- }
- Settings.Default.Save();
- }
- /// <summary>
- /// 快捷工具
- /// </summary>
- private void InitQuickTools()
- {
- //读取缓存的形式
- //var cacheList = Settings.Default.AllPDFToolsList;
- //if (cacheList == null)
- //暂时不用支持记录到缓存,且原方案有一定bug,先采用简单的形式加载处理
- var cacheList = new AllPDFToolsList();
- if(cacheList.Count > 0)
- {
- int currentCount = 0;
- foreach (var cacheItem in cacheList)
- {
- if (cacheItem.ToolLayOutType == 1 && currentCount <= QuickToolCount)
- {
- foreach (var allItem in AllTools)
- {
- if (cacheItem.FnTypeStr == Enum.GetName(typeof(PDFFnType), allItem.FnType))
- {
- allItem.IsQuickTool = true;
- var index = QuickTools.FindIndex(t => t.ToolTypeId > allItem.ToolTypeId);
- if (index == -1)
- {
- allItem.ToolTypeId = cacheItem.ToolTypeId;
- QuickTools.Add(allItem);
- }
- else
- {
- allItem.ToolTypeId = index;
- QuickTools.Insert(index, allItem);
- }
- currentCount++;
- break;
- }
- }
- }
- }
- }
- else
- {
- //当所有工具大于等于快捷工具显示的个数时
- if (AllTools.Count >= QuickToolCount)
- {
- for (int i = 0; i < QuickToolCount; i++)
- {
- AllTools[i].ToolTypeId = i;
- AllTools[i].IsQuickTool = true;
- QuickTools.Add(AllTools[i]);
- }
- }
- else
- {
- for (int i = 0; i < AllTools.Count; i++)
- {
- AllTools[i].ToolTypeId = i;
- AllTools[i].IsQuickTool = true;
- QuickTools.Add(AllTools[i]);
- }
- }
- SaveAllTools();
- }
- }
- private void SaveAllTools()
- {
- var cacheList = Settings.Default.AllPDFToolsList;
- foreach (var item in AllTools)
- {
- CacheToolItem cacheItem = new CacheToolItem();
- cacheItem.ToolLayOutType = (item.IsQuickTool == true ? 1 : 0);
- cacheItem.IsNewTool = item.IsNewTool;
- cacheItem.ToolTypeId = item.ToolTypeId;
- cacheItem.ToolTypeStr = Enum.GetName(typeof(PDFToolType), item.ToolType);
- cacheItem.FnTypeStr = Enum.GetName(typeof(PDFFnType), item.FnType);
- cacheList.Add(cacheItem);
- }
- Settings.Default.Save();
- }
- private void InitMoreTools()
- {
- int currentCount = 0;
- foreach (var allItem in AllTools)
- {
- if (allItem.IsQuickTool == false)
- {
- allItem.ToolTypeId = currentCount;
- MoreTools.Add(allItem);
- currentCount++;
- }
- }
- }
- public void SaveCacheList()
- {
- var cacheLists = Settings.Default.AllPDFToolsList;
- CacheToolItem cacheItem = null;
- foreach (var item in AllTools)
- {
- cacheItem = cacheLists.Find(t => t.FnTypeStr == Enum.GetName(typeof(PDFFnType), item.FnType));
- if (cacheItem != null)
- {
- cacheItem.ToolTypeId = item.ToolTypeId;
- cacheItem.ToolLayOutType = (item.IsQuickTool == true ? 1 : 0);
- }
- }
- Settings.Default.Save();
- }
- }
- }
|