123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350 |
- 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;
- namespace PDF_Office.ViewModels.PropertyPanel.AnnotPanel
- {
- internal class LinkAnnotPropertyViewModel : BindableBase, INavigationAware
- {
- 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 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 AnnotPropertyPanel 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;
- }
- 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
- {
- ImagePreviewVisibility = Visibility.Collapsed;
- BtnGOorBackVisibility = Visibility.Collapsed;
- isSelected = false;
- }
- }
- }
- /// <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)
- {
- 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;
- foreach (var item in views)
- {
- if (item is Views.TipContent.LinkAnnotTip linkAnnotTip)
- {
- isExist = true;
- break;
- }
- }
- }
- if (isExist == false)
- {
- region.RequestNavigate(viewContentViewModel.TipContentRegionName, "LinkAnnotTip");
- }
- //if(string.IsNullOrEmpty(PageNumTextContent))
- viewContentViewModel.ShowTip(true);
- }
- /// <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<AnnotPropertyPanel>(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)
- {
- 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>
- /// <param name="sender"></param>
- /// <param name="e"></param>
- private async void PDFViewer_CustomNotifyHandler(object sender, CustomNotityData e)
- {
- if (e.NotifyType == CustomNotifyType.LinkAreaLocate)
- {
- if (e.NotifyData is AreaLocate areaInfo)
- {
- if (pdfViewer.ToolManager.CurrentAnnotArgs.EventType == AnnotArgsType.AnnotLink && pdfViewer.MouseMode == MouseModes.AnnotCreate)
- {
- LinkAnnotArgs linkArgs = pdfViewer.ToolManager.CurrentAnnotArgs as LinkAnnotArgs;
- System.Windows.Size pageSize = pdfViewer.Document.GetPageSize(areaInfo.PageIndex);
- linkArgs.IsPositionSet = true;
- linkArgs.DestIndex = areaInfo.PageIndex;
- linkArgs.DestinationPos = new System.Windows.Point(areaInfo.Area.X, pageSize.Height - areaInfo.Area.Y);
- LinkAnnotArgs = linkArgs;
- LinkAnnotArgs.PageIndex = historyPageIndex;
- AnnotAttribEvent?.UpdateAttrib(AnnotAttrib.LinkType, LINK_TYPE.GOTO);
- AnnotAttribEvent?.UpdateAttrib(AnnotAttrib.LinkDestIndx, areaInfo.PageIndex);
- AnnotAttribEvent?.UpdateAnnot();
- LinkAnnotArgs.InvokeLinkSaveCalled(this, EventArgs.Empty);
- PageNumTextContent = (areaInfo.PageIndex + 1).ToString();
- SetImagePreview(areaInfo.PageIndex);
- await System.Threading.Tasks.Task.Delay(2);
- SelectAnnotation();
- pdfViewer.GoToPage(historyPageIndex);
- //viewContentViewModel.LinkAnnotTipVisibility = Visibility.Collapsed;
- viewContentViewModel.TipVisible = Visibility.Collapsed;
- BtnLocationIsChecked = false;
- }
- }
- }
- }
- /// <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;
- }
- }
- 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;
- 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;
- }
- }
- }
|