123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222 |
- 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;
- namespace PDF_Office.ViewModels.PropertyPanel.AnnotPanel
- {
- internal class LinkAnnotPropertyViewModel : BindableBase, INavigationAware
- {
- public AnnotAttribEvent AnnotAttribEvent { get; set; }
- private AnnotHandlerEventArgs Annot;
- private AnnotPropertyPanel PropertyPanel;
- private AnnotArgsType annotType;
- private LinkAnnotArgs _annotArgs;
- public LinkAnnotArgs AnnotArgs
- {
- get
- {
- return _annotArgs;
- }
- set
- {
- if (_annotArgs != null)
- {
- _annotArgs.LinkDrawFinished -= _annotArgs_LinkDrawFinished;
- }
- _annotArgs = value;
- if (_annotArgs != null)
- {
- _annotArgs.LinkDrawFinished += _annotArgs_LinkDrawFinished;
- }
- }
- }
- public AnnotArgsType AnnotType
- {
- get { return annotType; }
- set
- {
- SetProperty(ref annotType, value);
- }
- }
- private WriteableBitmap previewImage;
- public WriteableBitmap PreviewImage
- {
- get { return previewImage; }
- set
- {
- SetProperty(ref previewImage, value);
- }
- }
- private CPDFViewer pdfViewer;
- private CPDFDocument document;
- private LinkAnnotProperty linkAnnot;
- public DelegateCommand<object> LoadedCommand { get; set; }
- public LinkAnnotPropertyViewModel()
- {
- LoadedCommand = new DelegateCommand<object>(Loaded);
- }
- private void _annotArgs_LinkDrawFinished(object sender, bool e)
- {
- linkAnnot.SetTextBoxEnableOrNot(e);
- //AnnotArgs = (LinkAnnotArgs)PropertyPanel.annot;
- }
- private void Loaded(object obj)
- {
- if (obj is CompositeCommandParameter composite)
- {
- if (composite.Parameter is LinkAnnotProperty linkAnnotProperty)
- {
- linkAnnot = linkAnnotProperty;
- //AnnotArgs = (LinkAnnotArgs)PropertyPanel.annot;
- int dpiHeight = (int)linkAnnot.ImagePreview.Height;
- int dpiWidth = (int)linkAnnot.ImagePreview.Width;
- if (dpiHeight <= 0 || dpiWidth <= 0)
- {
- return;
- }
- int pageIndex = pdfViewer.CurrentIndex;
- 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;
- }
- }
- }
- 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);
- if (PropertyPanel != null && pdfViewer != null)
- {
- AnnotAttribEvent = PropertyPanel.AnnotEvent;
- AnnotType = PropertyPanel.annot.EventType;
- //AnnotArgs = (LinkAnnotArgs)PropertyPanel.annot;
- //if (Annot is ComPDFKitViewer.AnnotEvent.LineAnnotArgs lineAnnotArgs)
- //{
- // AnnotArgs = lineAnnotArgs;
- //}
- //GetAnnotProperty();
- //LoadPropertyHandler?.Invoke(null, Annot);
- document = pdfViewer.Document;
- if (linkAnnot != null)
- {
- if (pdfViewer != null && pdfViewer.ToolManager != null && pdfViewer.ToolManager.CurrentAnnotArgs?.EventType == AnnotArgsType.AnnotLink)
- {
- if (AnnotAttribEvent.IsAnnotCreateReset)
- {
- AnnotArgs = pdfViewer.ToolManager.CurrentAnnotArgs as LinkAnnotArgs;
- }
- }
- linkAnnot.SetTextBoxEnableOrNot(!AnnotAttribEvent.IsAnnotCreateReset);
- }
- else
- {
- AnnotArgs = (LinkAnnotArgs)PropertyPanel.annot;
- }
- }
- }
- 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;
- }
- }
- 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;
- }
- 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;
- }
- }
- }
|