12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454 |
- using ComPDFKit.PDFDocument;
- using ComPDFKit.PDFPage;
- using ComPDFKitViewer.AnnotEvent;
- using ComPDFKitViewer.PdfViewer;
- using PDF_Office.Model;
- using PDF_Office.ViewModels.Tools;
- using Prism.Mvvm;
- using Prism.Regions;
- using System;
- using System.Collections.Generic;
- using System.Drawing;
- using System.Linq;
- using System.Text;
- using System.Threading.Tasks;
- using System.Windows.Media;
- using System.Windows;
- using System.Windows.Media.Imaging;
- using Prism.Commands;
- using PDF_Office.Helper;
- using Microsoft.Office.Interop.Word;
- using static Dropbox.Api.Files.FileCategory;
- using PDF_Office.Views.PropertyPanel.AnnotPanel;
- using System.Windows.Controls;
- using PDF_Office.CustomControl;
- using System.Windows.Markup;
- using ComPDFKitViewer;
- using System.Runtime.InteropServices;
- using System.Text.RegularExpressions;
- using System.Net;
- using System.Diagnostics;
- using static Dropbox.Api.TeamLog.TrustedTeamsRequestState;
- using System.Windows.Input;
- using Prism.Events;
- using Prism.Services.Dialogs;
- using PDF_Office.Views.BOTA;
- using ImTools;
- using PDF_Office.ViewModels.Tools.AnnotManager;
- namespace PDF_Office.ViewModels.PropertyPanel.AnnotPanel
- {
- internal class LinkAnnotPropertyViewModel : BindableBase, INavigationAware
- {
- #region 文案
- private string T_title;
- public string T_Title
- {
- get { return T_title; }
- set
- {
- SetProperty(ref T_title, value);
- }
- }
- private string T_page;
- public string T_Page
- {
- get { return T_page; }
- set
- {
- SetProperty(ref T_page, value);
- }
- }
- private void InitString()
- {
- T_Title = App.MainPageLoader.GetString("Link_Title");
- T_Page = App.MainPageLoader.GetString("Link_Page");
- }
- #endregion 文案
- public AnnotAttribEvent AnnotAttribEvent { get; set; }
- private LinkAnnotArgs linkAnnotArgs;
- public LinkAnnotArgs LinkAnnotArgs
- {
- get
- {
- return linkAnnotArgs;
- }
- set
- {
- if (linkAnnotArgs != null)
- {
- linkAnnotArgs.LinkDrawFinished -= linkAnnotArgs_LinkDrawFinished;
- }
- linkAnnotArgs = value;
- if (linkAnnotArgs != null)
- {
- linkAnnotArgs.LinkDrawFinished += linkAnnotArgs_LinkDrawFinished;
- }
- }
- }
- private AnnotArgsType annotType;
- public AnnotArgsType AnnotType
- {
- get { return annotType; }
- set
- {
- SetProperty(ref annotType, value);
- }
- }
- #region 输入框的验证、设置
- private string pageNumTextContent;
- public string PageNumTextContent
- {
- get
- {
- if (CheckPageNumVaild(out int pageNum, pageNumTextContent))
- {
- if (LinkAnnotArgs != null)
- {
- LinkAnnotArgs.DestIndex = pageNum - 1;
- LinkAnnotArgs.Content = string.Format($"To Page {LinkAnnotArgs.DestIndex}");
- //isTextChanged = (int)AnnotAttribEvent.Attribs[AnnotAttrib.LinkDestIndx] != AnnotArgs.DestIndex ? true : false;
- SetImagePreview(LinkAnnotArgs.DestIndex);
- }
- }
- else
- {
- ImagePreviewVisibility = Visibility.Collapsed;
- BtnGOorBackVisibility = Visibility.Collapsed;
- BtnLocationIsEnabled = false;
- }
- return pageNumTextContent;
- }
- set
- {
- SetProperty(ref pageNumTextContent, value);
- }
- }
- private string pageWebTextContent;
- public string PageWebTextContent
- {
- get
- {
- if (CheckPageWebVaild(pageWebTextContent))
- {
- pageWebTextContent = pageWebTextContent.Trim().ToLower();
- if (!pageWebTextContent.StartsWith("http://") && !pageWebTextContent.StartsWith("https://"))
- {
- pageWebTextContent = "http://" + pageWebTextContent;
- }
- if (LinkAnnotArgs != null)
- {
- LinkAnnotArgs.URI = pageWebTextContent;
- LinkAnnotArgs.Content = LinkAnnotArgs.URI;
- //isTextChanged = (string)AnnotAttribEvent.Attribs[AnnotAttrib.LinkUri] != AnnotArgs.URI ? true : false;
- }
- }
- return pageWebTextContent;
- }
- set
- {
- SetProperty(ref pageWebTextContent, value);
- }
- }
- private string pageMailTextContent;
- public string PageMailTextContent
- {
- get
- {
- if (CheckPageMailVaild(pageMailTextContent))
- {
- if (!string.IsNullOrEmpty(pageMailTextContent) && LinkAnnotArgs != null)
- {
- LinkAnnotArgs.URI = "mailto:" + pageMailTextContent.Trim();
- LinkAnnotArgs.Content = LinkAnnotArgs.URI;
- //isTextChanged = (string)AnnotAttribEvent.Attribs[AnnotAttrib.LinkUri] != AnnotArgs.URI ? true : false;
- }
- }
- return pageMailTextContent;
- }
- set
- {
- SetProperty(ref pageMailTextContent, value);
- }
- }
- /// <summary>
- /// 设置预文本内容
- /// </summary>
- private string pageNumPlaceHoldText = "输入目标页面";
- public string PageNumPlaceHoldText
- {
- get { return pageNumPlaceHoldText; }
- set
- {
- SetProperty(ref pageNumPlaceHoldText, value);
- }
- }
- /// <summary>
- /// 是否显示输入框下方提示文案
- /// </summary>
- private Visibility showPageNumTip = Visibility.Collapsed;
- public Visibility ShowPageNumTip
- {
- get { return showPageNumTip; }
- set
- {
- SetProperty(ref showPageNumTip, value);
- }
- }
- /// <summary>
- /// 输入框下方提示文案
- /// </summary>
- private string pageNumTipText = "Page number error.";
- public string PageNumTipText
- {
- get { return pageNumTipText; }
- set
- {
- SetProperty(ref pageNumTipText, value);
- }
- }
- private bool pageNumTextIsEnabled = false;
- public bool PageNumTextIsEnabled
- {
- get { return pageNumTextIsEnabled; }
- set
- {
- SetProperty(ref pageNumTextIsEnabled, value);
- }
- }
- private bool pageWebTextIsEnabled = false;
- public bool PageWebTextIsEnabled
- {
- get { return pageWebTextIsEnabled; }
- set
- {
- SetProperty(ref pageWebTextIsEnabled, value);
- }
- }
- private string pageWebPlaceHoldText = "输入您要跳转的链接";
- public string PageWebPlaceHoldText
- {
- get { return pageWebPlaceHoldText; }
- set
- {
- SetProperty(ref pageWebPlaceHoldText, value);
- }
- }
- private Visibility showPageWebTip = Visibility.Collapsed;
- public Visibility ShowPageWebTip
- {
- get { return showPageWebTip; }
- set
- {
- SetProperty(ref showPageWebTip, value);
- }
- }
- private string pageWebTipText = "Invalid link.";
- public string PageWebTipText
- {
- get { return pageWebTipText; }
- set
- {
- SetProperty(ref pageWebTipText, value);
- }
- }
- private bool pageMailTextIsEnabled = false;
- public bool PageMailTextIsEnabled
- {
- get { return pageMailTextIsEnabled; }
- set
- {
- SetProperty(ref pageMailTextIsEnabled, value);
- }
- }
- private string pageMailPlaceHoldText = "输入您要跳转邮件地址";
- public string PageMailPlaceHoldText
- {
- get { return pageMailPlaceHoldText; }
- set
- {
- SetProperty(ref pageMailPlaceHoldText, value);
- }
- }
- private Visibility showPageMailTip = Visibility.Collapsed;
- public Visibility ShowPageMailTip
- {
- get { return showPageMailTip; }
- set
- {
- SetProperty(ref showPageMailTip, value);
- }
- }
- private string pageMailTipText = "The email is invalid.Please enter a valid email address.";
- public string PageMailTipText
- {
- get { return pageMailTipText; }
- set
- {
- SetProperty(ref pageMailTipText, value);
- }
- }
- #endregion 输入框的验证、设置
- #region 按钮、图片的设置
- private bool btnLocationIsEnabled = false;
- public bool BtnLocationIsEnabled
- {
- get { return btnLocationIsEnabled; }
- set
- {
- SetProperty(ref btnLocationIsEnabled, value);
- }
- }
- private bool btnLocationIsChecked = false;
- public bool BtnLocationIsChecked
- {
- get { return btnLocationIsChecked; }
- set
- {
- SetProperty(ref btnLocationIsChecked, value);
- }
- }
- private WriteableBitmap previewImage;
- public WriteableBitmap PreviewImage
- {
- get { return previewImage; }
- set
- {
- SetProperty(ref previewImage, value);
- }
- }
- private int imagePreviewWidth = 196;
- public int ImagePreviewWidth
- {
- get { return imagePreviewWidth; }
- set
- {
- SetProperty(ref imagePreviewWidth, value);
- }
- }
- private int imagePreviewHeight = 256;
- public int ImagePreviewHeight
- {
- get { return imagePreviewHeight; }
- set
- {
- SetProperty(ref imagePreviewHeight, value);
- }
- }
- private Visibility imagePreviewVisibility = Visibility.Collapsed;
- public Visibility ImagePreviewVisibility
- {
- get { return imagePreviewVisibility; }
- set
- {
- SetProperty(ref imagePreviewVisibility, value);
- }
- }
- private Visibility btnGOorBackVisibility = Visibility.Collapsed;
- public Visibility BtnGOorBackVisibility
- {
- get { return btnGOorBackVisibility; }
- set
- {
- SetProperty(ref btnGOorBackVisibility, value);
- }
- }
- private string btnGOorBackContent = "GO";
- public string BtnGOorBackContent
- {
- get { return btnGOorBackContent; }
- set
- {
- SetProperty(ref btnGOorBackContent, value);
- }
- }
- private bool btnLinkPageIsChecked = true;
- public bool BtnLinkPageIsChecked
- {
- get { return btnLinkPageIsChecked; }
- set
- {
- SetProperty(ref btnLinkPageIsChecked, value);
- }
- }
- private bool btnLinkWebIsChecked = false;
- public bool BtnLinkWebIsChecked
- {
- get { return btnLinkWebIsChecked; }
- set
- {
- SetProperty(ref btnLinkWebIsChecked, value);
- }
- }
- private bool btnLinkMailIsChecked = false;
- public bool BtnLinkMailIsChecked
- {
- get { return btnLinkMailIsChecked; }
- set
- {
- SetProperty(ref btnLinkMailIsChecked, value);
- }
- }
- #endregion 按钮、图片的设置
- private bool isLoaded = false;
- private bool isMail = false;
- private bool isSelected = false;
- private bool IsAnnotSelect = true;
- private string historyBtnGOorBackTag = string.Empty;
- private int totalPage = 0;
- private int backPageIndex = 0;
- private int historyPageIndex = 0;
- private CPDFViewer pdfViewer;
- private CPDFDocument document;
- private Button btnGOorBack = null;
- private LinkAnnotProperty linkAnnot;
- private AnnotTransfer propertyPanel;
- private ViewContentViewModel viewContentViewModel;
- public IRegionManager region;
- public IDialogService dialogs;
- public IEventAggregator events;
- #region 命令
- public DelegateCommand<object> LoadedCommand { get; set; }
- public DelegateCommand<object> PageNumTextLostFocusCommand { get; set; }
- public DelegateCommand<object> PageNumTextKeyDownCommand { get; set; }
- public DelegateCommand<object> PageMailTextLostFocusCommand { get; set; }
- public DelegateCommand<object> PageMailTextKeyDownCommand { get; set; }
- public DelegateCommand<object> PageWebTextLostFocusCommand { get; set; }
- public DelegateCommand<object> PageWebTextKeyDownCommand { get; set; }
- public DelegateCommand<object> BtnGOorBackPageCommand { get; set; }
- public DelegateCommand<object> LocationCommand { get; set; }
- public DelegateCommand<object> ToggleButtonTabCommand { get; set; }
- #endregion 命令
- public LinkAnnotPropertyViewModel(IRegionManager regionManager, IDialogService dialogService, IEventAggregator eventAggregator)
- {
- region = regionManager;
- dialogs = dialogService;
- events = eventAggregator;
- LoadedCommand = new DelegateCommand<object>(Loaded);
- PageNumTextLostFocusCommand = new DelegateCommand<object>(PageNumTextLostFocus);
- PageNumTextKeyDownCommand = new DelegateCommand<object>(PageNumTextKeyDown);
- PageMailTextLostFocusCommand = new DelegateCommand<object>(PageMailTextLostFocus);
- PageMailTextKeyDownCommand = new DelegateCommand<object>(PageMailTextKeyDown);
- PageWebTextLostFocusCommand = new DelegateCommand<object>(PageWebTextLostFocus);
- PageWebTextKeyDownCommand = new DelegateCommand<object>(PageWebTextKeyDown);
- BtnGOorBackPageCommand = new DelegateCommand<object>(BtnGOorBackPageEvent);
- LocationCommand = new DelegateCommand<object>(LocationPage);
- ToggleButtonTabCommand = new DelegateCommand<object>(ToggleButtonTabSelected);
- isLoaded = true;
- InitString();
- }
- private void PageWebTextKeyDown(object obj)
- {
- if (obj is KeyEventArgs keyEventArgs)
- {
- if (keyEventArgs.Key == Key.Enter)
- {
- PageWebTextLostFocus(obj);
- }
- }
- }
- private void PageWebTextLostFocus(object obj)
- {
- if (!string.IsNullOrEmpty(PageWebTextContent))
- {
- if (CheckPageWebVaild(PageWebTextContent))
- {
- AnnotAttribEvent?.UpdateAttrib(AnnotAttrib.LinkType, LINK_TYPE.URI);
- PageWebTextContent = PageWebTextContent.Trim().ToLower();
- if (!PageWebTextContent.StartsWith("http://") && !PageWebTextContent.StartsWith("https://"))
- {
- PageWebTextContent = "http://" + PageWebTextContent;
- }
- AnnotAttribEvent?.UpdateAttrib(AnnotAttrib.LinkUri, PageWebTextContent);
- AnnotAttribEvent?.UpdateAnnot();
- LinkAnnotArgs?.InvokeLinkSaveCalled(this, EventArgs.Empty);
- }
- }
- }
- private void PageMailTextKeyDown(object obj)
- {
- if (obj is KeyEventArgs keyEventArgs)
- {
- if (keyEventArgs.Key == Key.Enter)
- {
- PageMailTextLostFocus(obj);
- }
- }
- }
- private void PageMailTextLostFocus(object obj)
- {
- if (!string.IsNullOrEmpty(PageMailTextContent))
- {
- if (CheckPageMailVaild(PageMailTextContent))
- {
- AnnotAttribEvent?.UpdateAttrib(AnnotAttrib.LinkType, LINK_TYPE.URI);
- AnnotAttribEvent?.UpdateAttrib(AnnotAttrib.LinkUri, "mailto:" + PageMailTextContent.Trim());
- AnnotAttribEvent?.UpdateAnnot();
- LinkAnnotArgs?.InvokeLinkSaveCalled(this, EventArgs.Empty);
- }
- }
- }
- /// <summary>
- /// 页码文本框 Enter键
- /// </summary>
- /// <param name="obj"></param>
- private void PageNumTextKeyDown(object obj)
- {
- if (obj is KeyEventArgs keyEventArgs)
- {
- if (keyEventArgs.Key == Key.Enter)
- {
- PageNumTextLostFocus(obj);
- }
- }
- }
- /// <summary>
- /// 页码文本框失去焦点
- /// </summary>
- /// <param name="obj"></param>
- private void PageNumTextLostFocus(object obj)
- {
- if (!string.IsNullOrEmpty(PageNumTextContent))
- {
- if (CheckPageNumVaild(out int pageNum, PageNumTextContent))
- {
- //isSelected = true;
- AnnotAttribEvent?.UpdateAttrib(AnnotAttrib.LinkType, LINK_TYPE.GOTO);
- AnnotAttribEvent?.UpdateAttrib(AnnotAttrib.LinkDestIndx, pageNum - 1);
- AnnotAttribEvent?.UpdateAnnot();
- //isTextChanged = false;
- SetImagePreview(LinkAnnotArgs.DestIndex);
- //ImagePreviewVisibility = Visibility.Collapsed;
- //BtnGOorBackVisibility= Visibility.Collapsed;
- BtnLocationIsEnabled = false;
- LinkAnnotArgs?.InvokeLinkSaveCalled(this, EventArgs.Empty);
- SelectAnnotation();
- }
- else
- {
- BtnLocationIsEnabled = true;
- ImagePreviewVisibility = Visibility.Collapsed;
- BtnGOorBackVisibility = Visibility.Collapsed;
- isSelected = false;
- }
- }
- else
- {
- BtnLocationIsEnabled = true;
- }
- }
- /// <summary>
- /// 模块选择
- /// </summary>
- /// <param name="obj"></param>
- private void ToggleButtonTabSelected(object obj)
- {
- if (obj is RadioButton radioButton)
- {
- switch (radioButton.Tag)
- {
- case "Page":
- LinkAnnotArgs.LinkType = LINK_TYPE.GOTO;
- LinkAnnotArgs.URI = string.Empty;
- AnnotAttribEvent.Attribs[AnnotAttrib.LinkType] = LINK_TYPE.GOTO;
- AnnotAttribEvent.Attribs[AnnotAttrib.LinkUri] = string.Empty;
- isMail = false;
- if (btnGOorBack != null)
- {
- if (!string.IsNullOrEmpty(historyBtnGOorBackTag))
- {
- ChangeBtnGOorBack(historyBtnGOorBackTag, btnGOorBack);
- }
- else
- {
- ChangeBtnGOorBack("GO", btnGOorBack);
- }
- }
- BtnGOorBackVisibility = string.IsNullOrEmpty(PageNumTextContent) ? Visibility.Collapsed : Visibility.Visible;
- break;
- case "Web":
- isMail = false;
- SetLinkType_URIState("Web");
- BtnGOorBackVisibility = string.IsNullOrEmpty(PageWebTextContent) ? Visibility.Collapsed : Visibility.Visible;
- break;
- case "Mail":
- isMail = true;
- SetLinkType_URIState("Mail");
- BtnGOorBackVisibility = string.IsNullOrEmpty(PageMailTextContent) ? Visibility.Collapsed : Visibility.Visible;
- break;
- }
- }
- }
- /// <summary>
- /// 设置BtnGOorBack状态
- /// </summary>
- /// <param name="uri"></param>
- private void SetLinkType_URIState(string uri)
- {
- LinkAnnotArgs.LinkType = LINK_TYPE.URI;
- LinkAnnotArgs.URI = string.Empty;
- AnnotAttribEvent.Attribs[AnnotAttrib.LinkType] = LINK_TYPE.URI;
- AnnotAttribEvent.Attribs[AnnotAttrib.LinkUri] = string.Empty;
- if (btnGOorBack != null)
- {
- historyBtnGOorBackTag = btnGOorBack.Tag.ToString();
- //ChangeBtnGOorBack("GO", btnGOorBack);
- switch (uri)
- {
- case "Web":
- if (!string.IsNullOrEmpty(PageWebTextContent))
- {
- ChangeBtnGOorBack("GO", btnGOorBack);
- }
- //BtnGOorBackVisibility = string.IsNullOrEmpty(PageWebTextContent) ? Visibility.Collapsed : Visibility.Visible;
- break;
- case "Mail":
- if (!string.IsNullOrEmpty(PageMailTextContent))
- {
- ChangeBtnGOorBack("GO", btnGOorBack);
- }
- //BtnGOorBackVisibility = string.IsNullOrEmpty(PageMailTextContent) ? Visibility.Collapsed : Visibility.Visible;
- break;
- }
- }
- }
- /// <summary>
- /// 定位目标范围
- /// </summary>
- /// <param name="obj"></param>
- private void LocationPage(object obj)
- {
- //没有选择注释工具、也不是右键添加注释
- //在阅读页,选择注释后,点击按钮
- //需要设置相关参数
- if (AnnotAttribEvent.IsAnnotCreateReset == false && propertyPanel.IsAddLink == false)
- {
- propertyPanel.IsLocationLink = true;
- pdfViewer.SetMouseMode(MouseModes.AnnotCreate);
- pdfViewer.SetToolParam(LinkAnnotArgs);
- }
- pdfViewer.EnableDrawSelectArea(true);
- historyPageIndex = pdfViewer.CurrentIndex;
- PageNumTextIsEnabled = false;
- isSelected = false;
- BtnLocationIsChecked = true;
- bool isExist = false;
- if (region.Regions.ContainsRegionWithName(viewContentViewModel.TipContentRegionName))
- {
- var views = region.Regions[viewContentViewModel.TipContentRegionName].Views;
- var isHas = views.FindFirst(q => q is Views.TipContent.LinkAnnotTip);
- if (isHas != null)
- {
- isExist = true;
- }
- }
- if (isExist == false)
- {
- region.RequestNavigate(viewContentViewModel.TipContentRegionName, "LinkAnnotTip");
- }
- viewContentViewModel.ShowTip(true);
- //AnnotAttribEvent.IsAnnotCreateReset = true;
- }
- /// <summary>
- /// 定位目标范围完成时,触发
- /// </summary>
- /// <param name="sender"></param>
- /// <param name="e"></param>
- private void PDFViewer_CustomNotifyHandler(object sender, CustomNotityData e)
- {
- if (e.NotifyType == CustomNotifyType.LinkAreaLocate)
- {
- if (e.NotifyData is AreaLocate areaInfo)
- {
- if (pdfViewer.ToolManager.CurrentAnnotArgs == null)
- {
- CleanLocationState();
- return;
- }
- if (pdfViewer.ToolManager.CurrentAnnotArgs.EventType == AnnotArgsType.AnnotLink && pdfViewer.MouseMode == MouseModes.AnnotCreate)
- {
- System.Windows.Size pageSize = pdfViewer.Document.GetPageSize(areaInfo.PageIndex);
- historyPageIndex = LinkAnnotArgs.PageIndex;
- AnnotAttribEvent = propertyPanel.AnnotEvent;
- LinkAnnotArgs = (LinkAnnotArgs)propertyPanel.annot;
- LinkAnnotArgs.IsPositionSet = true;
- LinkAnnotArgs.DestIndex = areaInfo.PageIndex;
- LinkAnnotArgs.DestinationPos = new System.Windows.Point(areaInfo.Area.X, pageSize.Height - areaInfo.Area.Y);
- AnnotAttribEvent?.UpdateAttrib(AnnotAttrib.LinkType, LINK_TYPE.GOTO);
- AnnotAttribEvent?.UpdateAttrib(AnnotAttrib.LinkDestIndx, areaInfo.PageIndex);
- AnnotAttribEvent?.UpdateAnnot();
- LinkAnnotArgs.InvokeLinkSaveCalled(this, EventArgs.Empty);
- pdfViewer.GoToPage(historyPageIndex);
- if (string.IsNullOrEmpty(PageNumTextContent))
- {
- pdfViewer.SelectAnnotation(historyPageIndex, LinkAnnotArgs.AnnotIndex);
- }
- BtnLocationIsChecked = false;
- PageNumTextContent = (areaInfo.PageIndex + 1).ToString();
- SetImagePreview(areaInfo.PageIndex);
- viewContentViewModel.TipVisible = Visibility.Collapsed;
- //没有选择注释工具、也不是右键添加注释
- //在阅读页,选择注释后,点击按钮
- //需要设置相关参数
- if (propertyPanel.IsLocationLink)
- {
- pdfViewer.SetMouseMode(MouseModes.PanTool);
- propertyPanel.IsLocationLink = false;
- pdfViewer.SelectAnnotation(historyPageIndex, LinkAnnotArgs.AnnotIndex);
- }
- }
- }
- }
- }
- private void CleanLocationState()
- {
- viewContentViewModel.TipVisible = Visibility.Collapsed;
- BtnLocationIsChecked = false;
- propertyPanel.IsLocationLink = false;
- //没有选择注释工具、也不是右键添加注释
- //在阅读页,选择注释后,点击按钮
- //需要设置相关参数
- if (propertyPanel.IsAddLink == false)
- {
- pdfViewer.SetMouseMode(MouseModes.PanTool);
- }
- }
- /// <summary>
- /// 前往/返回按钮
- /// </summary>
- /// <param name="obj"></param>
- private void BtnGOorBackPageEvent(object obj)
- {
- if (obj is object[] arry)
- {
- btnGOorBack = arry[0] as Button;
- RadioButton btnLinkPage = arry[1] as RadioButton;
- RadioButton btnLinkWeb = arry[2] as RadioButton;
- RadioButton btnLinkMail = arry[3] as RadioButton;
- if (btnLinkPage != null && btnGOorBack != null && btnLinkWeb != null && btnLinkMail != null)
- {
- if (btnLinkPage.IsChecked == true)
- {
- LinkPageGoOrBack();
- }
- if (btnLinkWeb.IsChecked == true)
- {
- LinkWebSave();
- }
- if (btnLinkMail.IsChecked == true)
- {
- LinkMailSave();
- }
- }
- }
- }
- /// <summary>
- /// 保存邮件链接
- /// </summary>
- private void LinkMailSave()
- {
- if (CheckPageMailVaild(PageMailTextContent))
- {
- //AnnotAttribEvent?.UpdateAttrib(AnnotAttrib.LinkType, LINK_TYPE.URI);
- //AnnotAttribEvent?.UpdateAttrib(AnnotAttrib.LinkUri, "mailto:" + PageMailTextContent.Trim());
- //AnnotAttribEvent?.UpdateAnnot();
- //LinkAnnotArgs?.InvokeLinkSaveCalled(this, EventArgs.Empty);
- Process.Start("mailto:?to=" + PageMailTextContent.Trim());
- }
- }
- /// <summary>
- /// 保存web链接
- /// </summary>
- private void LinkWebSave()
- {
- if (CheckPageWebVaild(PageWebTextContent))
- {
- //AnnotAttribEvent?.UpdateAttrib(AnnotAttrib.LinkType, LINK_TYPE.URI);
- //PageWebTextContent = PageWebTextContent.Trim().ToLower();
- //if (!PageWebTextContent.StartsWith("http://") && !PageWebTextContent.StartsWith("https://"))
- //{
- // PageWebTextContent = "http://" + PageWebTextContent;
- //}
- //AnnotAttribEvent?.UpdateAttrib(AnnotAttrib.LinkUri, PageWebTextContent);
- //AnnotAttribEvent?.UpdateAnnot();
- //LinkAnnotArgs?.InvokeLinkSaveCalled(this, EventArgs.Empty);
- if (PageWebTextContent.StartsWith("http://") || PageWebTextContent.StartsWith("https://"))
- {
- Process.Start(PageWebTextContent);
- }
- }
- else
- {
- BtnGOorBackVisibility = Visibility.Collapsed;
- }
- }
- /// <summary>
- /// 页码链接 GoOrBack
- /// </summary>
- private void LinkPageGoOrBack()
- {
- int pageIndex = Convert.ToInt32(PageNumTextContent) - 1;
- if (btnGOorBack.Tag.ToString() == "GO")
- {
- //AnnotAttribEvent?.UpdateAttrib(AnnotAttrib.LinkType, LINK_TYPE.GOTO);
- //AnnotAttribEvent?.UpdateAttrib(AnnotAttrib.LinkDestIndx, pageIndex);
- //AnnotAttribEvent?.UpdateAnnot();
- historyPageIndex = LinkAnnotArgs != null ? LinkAnnotArgs.PageIndex : backPageIndex;
- pdfViewer.GoToPage(pageIndex);
- ChangeBtnGOorBack("BACK", btnGOorBack);
- }
- else
- {
- if (historyPageIndex != -1)
- {
- SelectAnnotation();
- pdfViewer.GoToPage(historyPageIndex);
- ChangeBtnGOorBack("GO", btnGOorBack);
- }
- }
- }
- /// <summary>
- /// 选择注释
- /// </summary>
- private void SelectAnnotation()
- {
- if (LinkAnnotArgs != null && !isSelected && LinkAnnotArgs.AnnotIndex >= 0)
- {
- pdfViewer.SelectAnnotation(LinkAnnotArgs.PageIndex, LinkAnnotArgs.AnnotIndex);
- isSelected = true;
- }
- else
- {
- isSelected = false;
- }
- }
- /// <summary>
- /// 设置前往/返回按钮的文本
- /// </summary>
- /// <param name="flag"></param>
- /// <param name="button"></param>
- private void ChangeBtnGOorBack(string flag, Button button)
- {
- switch (flag)
- {
- case "BACK":
- button.Tag = "BACK";
- button.Content = "BACK";
- break;
- case "GO":
- button.Tag = "GO";
- button.Content = "GO";
- break;
- default:
- break;
- }
- }
- /// <summary>
- /// 页码格式验证
- /// </summary>
- /// <param name="pageNum"></param>
- /// <param name="text"></param>
- /// <returns></returns>
- private bool CheckPageNumVaild(out int pageNum, string text)
- {
- pageNum = -1;
- if (string.IsNullOrEmpty(text))
- {
- return false;
- }
- if (text.Trim() != string.Empty)
- {
- if (!int.TryParse(text.Trim(), out pageNum))
- {
- ShowPageNumTip = Visibility.Visible;
- PageNumTipText = " Page number error.";
- return false;
- }
- }
- if (pageNum < 1 || pageNum > totalPage)
- {
- ShowPageNumTip = Visibility.Visible;
- PageNumTipText = " Page number out of range.";
- return false;
- }
- else
- {
- ShowPageNumTip = Visibility.Collapsed;
- }
- //BtnGOorBackVisibility = Visibility.Visible;
- return true;
- }
- /// <summary>
- /// web链接格式验证
- /// </summary>
- /// <param name="text"></param>
- /// <returns></returns>
- private bool CheckPageWebVaild(string text)
- {
- BtnGOorBackVisibility = Visibility.Collapsed;
- if (string.IsNullOrEmpty(text))
- {
- return false;
- }
- string checkUrl = text.ToLower().TrimStart("http://".ToCharArray()).TrimStart("https://".ToCharArray());
- if (!Regex.IsMatch(checkUrl, "([a-zA-Z0-9/\\-%\\?#&=]+[./\\-%\\?#&=]?)+"))
- {
- ShowPageWebTip = Visibility.Visible;
- PageWebTipText = "Invalid link.";
- return false;
- }
- string matchText = Regex.Match(checkUrl, "([a-zA-Z0-9/\\-%\\?#&=]+[./\\-%\\?#&=]?)+").Value;
- if (matchText.Length != checkUrl.Length)
- {
- ShowPageWebTip = Visibility.Visible;
- PageWebTipText = "Invalid link.";
- return false;
- }
- else
- {
- ShowPageWebTip = Visibility.Collapsed;
- }
- BtnGOorBackVisibility = Visibility.Visible;
- return true;
- }
- /// <summary>
- /// 邮箱链接格式验证
- /// </summary>
- /// <param name="text"></param>
- /// <returns></returns>
- private bool CheckPageMailVaild(string text)
- {
- BtnGOorBackVisibility = Visibility.Collapsed;
- if (string.IsNullOrEmpty(text))
- {
- return false;
- }
- if (!Regex.IsMatch(text, "^[A-Za-z0-9\u4e00-\u9fa5_\\-\\.]+@[a-zA-Z0-9_-]+(\\.[a-zA-Z0-9_-]+)+$"))
- {
- ShowPageMailTip = Visibility.Visible;
- PageMailTipText = "The email is invalid.Please enter a valid email address.";
- return false;
- }
- else
- {
- ShowPageMailTip = Visibility.Collapsed;
- }
- BtnGOorBackVisibility = Visibility.Visible;
- return true;
- }
- /// <summary>
- /// 设置图像
- /// </summary>
- /// <param name="pageIndex"></param>
- private void SetImagePreview(int pageIndex)
- {
- ImagePreviewVisibility = Visibility.Visible;
- BtnGOorBackVisibility = Visibility.Visible;
- BtnLocationIsEnabled = true;
- int dpiHeight = (int)ImagePreviewHeight;
- int dpiWidth = (int)ImagePreviewWidth;
- if (dpiHeight <= 0 || dpiWidth <= 0)
- {
- return;
- }
- //int pageIndex = AnnotArgs.DestIndex;
- Rect MediaRect = GetPageRect(document, pageIndex, CPDFDisplayBox.MediaBox);
- System.Windows.Size pageSize = GetPageSize(document, pageIndex);
- double scale = Math.Min(dpiWidth / MediaRect.Width, dpiHeight / MediaRect.Height);
- scale = Math.Min(scale, 1);
- int cropWidth = (int)(scale * pageSize.Width);
- int cropHeight = (int)(scale * pageSize.Height);
- byte[] imageDatas = RenderPDFPageToArray(document, pageIndex, new Rect(0, 0, cropWidth, cropHeight), scale, true, true);
- WriteableBitmap WirteBitmap = new WriteableBitmap(cropWidth, cropHeight, 96, 96, PixelFormats.Bgra32, null);
- WirteBitmap.WritePixels(new Int32Rect(0, 0, cropWidth, cropHeight), imageDatas, WirteBitmap.BackBufferStride, 0);
- PreviewImage = WirteBitmap;
- }
- /// <summary>
- /// 链接画框完成时触发事件
- /// </summary>
- /// <param name="sender"></param>
- /// <param name="e"></param>
- private void linkAnnotArgs_LinkDrawFinished(object sender, bool e)
- {
- SetTextBoxEnableOrNot(e, totalPage);
- }
- private void Loaded(object obj)
- {
- if (obj is CompositeCommandParameter composite)
- {
- if (composite.Parameter is LinkAnnotProperty linkAnnotProperty)
- {
- linkAnnot = linkAnnotProperty;
- }
- }
- }
- public bool IsNavigationTarget(NavigationContext navigationContext)
- {
- return true;
- }
- public void OnNavigatedFrom(NavigationContext navigationContext)
- {
- }
- public void OnNavigatedTo(NavigationContext navigationContext)
- {
- navigationContext.Parameters.TryGetValue<AnnotTransfer>(ParameterNames.PropertyPanelContentViewModel, out propertyPanel);
- navigationContext.Parameters.TryGetValue<CPDFViewer>(ParameterNames.PDFViewer, out pdfViewer);
- navigationContext.Parameters.TryGetValue<ViewContentViewModel>(ParameterNames.ViewContentViewModel, out viewContentViewModel);
- if (propertyPanel != null && pdfViewer != null)
- {
- pdfViewer.CustomNotifyHandler -= PDFViewer_CustomNotifyHandler;
- pdfViewer.CustomNotifyHandler += PDFViewer_CustomNotifyHandler;
- AnnotAttribEvent = propertyPanel.AnnotEvent;
- AnnotType = propertyPanel.annot.EventType;
- document = pdfViewer.Document;
- totalPage = pdfViewer.Document.PageCount;
- if (viewContentViewModel.IsRightMenuCreateLink)
- {
- SetTextBoxEnableOrNot(true, totalPage);
- }
- else
- {
- SetTextBoxEnableOrNot(false, totalPage);
- }
- if (AnnotAttribEvent.IsAnnotCreateReset == false)
- {
- //根据选择的,链接,获取对应的信息
- GetAnnotAttribute();
- }
- if (isLoaded)
- {
- if (pdfViewer != null && pdfViewer.ToolManager != null && pdfViewer.ToolManager.CurrentAnnotArgs?.EventType == AnnotArgsType.AnnotLink)
- {
- LinkAnnotArgs = pdfViewer.ToolManager.CurrentAnnotArgs as LinkAnnotArgs;
- if (LinkAnnotArgs.AnnotIndex == -1)
- {
- LinkAnnotArgs = (LinkAnnotArgs)propertyPanel.annot;
- }
- if (btnGOorBack != null)
- {
- ChangeBtnGOorBack("GO", btnGOorBack);
- }
- if (AnnotAttribEvent.IsAnnotCreateReset)
- {
- LinkAnnotArgs = pdfViewer.ToolManager.CurrentAnnotArgs as LinkAnnotArgs;
- if (LinkAnnotArgs == null)
- {
- backPageIndex = pdfViewer.CurrentIndex;
- }
- else
- {
- LinkAnnotArgs.PageIndex = pdfViewer.CurrentIndex;
- }
- }
- }
- }
- if (LinkAnnotArgs == null)
- {
- LinkAnnotArgs = (LinkAnnotArgs)propertyPanel.annot;
- }
- }
- }
- /// <summary>
- /// 选择链接后,显示参数
- /// </summary>
- private void GetAnnotAttribute()
- {
- if (AnnotAttribEvent.Attribs.ContainsKey(AnnotAttrib.LinkType))
- {
- //设置显示的链接模块
- SetLinkType((LINK_TYPE)AnnotAttribEvent.Attribs[AnnotAttrib.LinkType]);
- }
- if (AnnotAttribEvent.Attribs.ContainsKey(AnnotAttrib.LinkDestIndx) && !AnnotAttribEvent.IsAnnotCreateReset)
- {
- SetLinkPageNum((int)AnnotAttribEvent.Attribs[AnnotAttrib.LinkDestIndx] + 1);
- if (LinkAnnotArgs == null)
- {
- backPageIndex = pdfViewer.CurrentIndex;
- }
- else
- {
- LinkAnnotArgs.PageIndex = pdfViewer.CurrentIndex;
- }
- }
- //暂定 第一版 不显示邮箱、web链接模块
- //if (AnnotAttribEvent.Attribs.ContainsKey(AnnotAttrib.LinkUri))
- //{
- // string linkUrl = (string)AnnotAttribEvent.Attribs[AnnotAttrib.LinkUri];
- // if (!string.IsNullOrEmpty(linkUrl))
- // {
- // if (linkUrl.StartsWith("mailto:", StringComparison.OrdinalIgnoreCase))
- // {
- // SetLinkType(LINK_TYPE.URI, true);
- // if (AnnotAttribEvent.IsAnnotCreateReset)
- // {
- // SetLinkEmail("");
- // }
- // else
- // {
- // SetLinkEmail(linkUrl.ToLower().TrimStart("mailto:".ToCharArray()));
- // }
- // }
- // else
- // {
- // if (AnnotAttribEvent.IsAnnotCreateReset)
- // {
- // SetLinkUrl("");
- // }
- // else
- // {
- // SetLinkUrl(linkUrl);
- // }
- // }
- // }
- //}
- }
- private void SetLinkUrl(string url)
- {
- PageWebTextContent = url;
- PageWebTextIsEnabled = true;
- }
- private void SetLinkEmail(string email)
- {
- PageMailTextContent = email;
- PageMailTextIsEnabled = true;
- }
- /// <summary>
- /// 设置页码的数据
- /// </summary>
- /// <param name="pageNum"></param>
- private void SetLinkPageNum(int pageNum)
- {
- if (pageNum > 0 && pageNum <= totalPage)
- {
- PageNumTextContent = pageNum.ToString();
- BtnLocationIsEnabled = true;
- PageNumTextIsEnabled = true;
- ImagePreviewVisibility = Visibility.Visible;
- SetImagePreview(pageNum - 1);
- BtnLocationIsEnabled = true;
- //if (btnGOorBack != null)
- //{
- // ChangeBtnGOorBack("GO", btnGOorBack);
- //}
- }
- }
- /// <summary>
- /// 根据链接类型显示模块
- /// </summary>
- /// <param name="linkType"></param>
- /// <param name="isMail"></param>
- public void SetLinkType(LINK_TYPE linkType, bool isMail = false)
- {
- BtnLinkPageIsChecked = false;
- BtnLinkMailIsChecked = false;
- BtnLinkWebIsChecked = false;
- //暂定 第一版 不显示邮箱、web链接模块
- if (linkType == LINK_TYPE.URI)
- {
- BtnLinkPageIsChecked = true;
- BtnGOorBackVisibility = Visibility.Collapsed;
- viewContentViewModel.SelectedPrpoertyPanel("PropertyPanelContent", null);
- return;
- }
- switch (linkType)
- {
- case LINK_TYPE.GOTO:
- BtnLinkPageIsChecked = true;
- break;
- case LINK_TYPE.URI:
- if (this.isMail || isMail)
- {
- BtnLinkMailIsChecked = true;
- }
- else
- {
- BtnLinkWebIsChecked = true;
- }
- break;
- default:
- BtnLinkPageIsChecked = true;
- break;
- }
- }
- /// <summary>
- /// 设置文本框和按钮的属性
- /// </summary>
- /// <param name="enable"></param>
- /// <param name="totalPage"></param>
- private void SetTextBoxEnableOrNot(bool enable, int totalPage)
- {
- this.totalPage = totalPage;
- PageNumTextContent = string.Empty;
- PageWebTextContent = string.Empty;
- PageMailTextContent = string.Empty;
- PageNumTextIsEnabled = enable;
- PageWebTextIsEnabled = enable;
- PageMailTextIsEnabled = enable;
- PageNumPlaceHoldText = PageNumTextIsEnabled ? string.Format($"1-{totalPage}页") : "输入目标页面";
- PageWebPlaceHoldText = PageWebTextIsEnabled ? "https://www.pdfreaderpro.com" : "输入您要跳转的链接";
- PageMailPlaceHoldText = PageMailTextIsEnabled ? "support@pdfreaderpro.com" : "输入您要跳转邮件地址";
- ShowPageNumTip = Visibility.Collapsed;
- ShowPageWebTip = Visibility.Collapsed;
- ShowPageMailTip = Visibility.Collapsed;
- BtnLocationIsEnabled = enable;
- }
- /// <summary>
- /// 将PDF的页面转换成数据流
- /// </summary>
- /// <param name="document"></param>
- /// <param name="pageIndex"></param>
- /// <param name="renderRect"></param>
- /// <param name="currentZoom"></param>
- /// <param name="renderAnnot"></param>
- /// <param name="renderForm"></param>
- /// <returns></returns>
- public byte[] RenderPDFPageToArray(CPDFDocument document, int pageIndex, Rect renderRect, double currentZoom, bool renderAnnot = false, bool renderForm = false)
- {
- if (renderRect.Width <= 0 || renderRect.Height <= 0 || document == null)
- {
- return null;
- }
- try
- {
- byte[] bmpData = new byte[(int)renderRect.Width * (int)renderRect.Height * 4];
- currentZoom = currentZoom * 96 / 72;
- int flag = 0;
- if (renderAnnot)
- {
- flag = 1;
- }
- uint bgColor = 0xFFFFFFFF;
- CPDFPage page = document.PageAtIndex(pageIndex);
- if (page.IsValid())
- {
- page.RenderPageBitmapWithMatrix((float)currentZoom, renderRect, bgColor, bmpData, flag, renderForm);
- }
- return bmpData;
- }
- catch (Exception ex)
- {
- return null;
- }
- }
- /// <summary>
- /// 获取页面的矩形
- /// </summary>
- /// <param name="document"></param>
- /// <param name="pageIndex"></param>
- /// <param name="displayBox"></param>
- /// <returns></returns>
- private Rect GetPageRect(CPDFDocument document, int pageIndex, CPDFDisplayBox displayBox)
- {
- CPDFPage currentPage = document.PageAtIndex(pageIndex);
- Rect boundBox = currentPage.GetBoundsForBox(displayBox);
- Rect boundRect = new Rect((int)(boundBox.Left / 72.0 * 96.0), (int)(boundBox.Top / 72.0 * 96.0),
- (int)(boundBox.Width / 72.0 * 96.0), (int)(boundBox.Height / 72.0 * 96.0));
- return boundRect;
- }
- /// <summary>
- /// 获取页面的尺寸
- /// </summary>
- /// <param name="document"></param>
- /// <param name="pageIndex"></param>
- /// <returns></returns>
- private System.Windows.Size GetPageSize(CPDFDocument document, int pageIndex)
- {
- System.Windows.Size pageSize = document.GetPageSize(pageIndex);
- pageSize.Width = pageSize.Width / 72.0 * 96;
- pageSize.Height = pageSize.Height / 72.0 * 96;
- return pageSize;
- }
- }
- }
|