123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237 |
- using ComPDFKit.Import;
- using ComPDFKit.PDFAnnotation;
- using ComPDFKit.PDFAnnotation.Form;
- using ComPDFKit.PDFDocument;
- using ComPDFKit.PDFDocument.Action;
- using ComPDFKit.PDFPage;
- using ComPDFKitViewer.AnnotEvent;
- using ComPDFKitViewer.PdfViewer;
- using Microsoft.Win32;
- using System;
- using System.Collections.Generic;
- using System.Diagnostics;
- using System.IO;
- using System.Linq;
- using System.Windows;
- using System.Windows.Controls;
- using System.Windows.Media.Imaging;
- namespace Compdfkit_Tools.PDFControl
- {
- public partial class PDFViewControl : UserControl
- {
- public CPDFViewer PDFView { get; set; }
- public bool CustomSignHandle { get; set; }
- private double[] zoomLevelList = { 1f, 8f, 12f, 25, 33f, 50, 66f, 75, 100, 125, 150, 200, 300, 400, 600, 800, 1000 };
- public PDFViewControl()
- {
- InitializeComponent();
- PDFView = new CPDFViewer();
- Content = PDFView;
- PDFView.MouseWheelZoomHandler += PDFView_MouseWheelZoomHandler;
- PDFView.PDFActionHandler += PDFView_PDFActionHandler;
- PDFView.WidgetClickHandler += PDFView_WidgetClickHandler;
- }
- /// <summary>
- /// set CustomSignhandle = ture if dont need this default function.
- /// </summary>
- /// <param name="sender"></param>
- /// <param name="e"></param>
- private void PDFView_WidgetClickHandler(object sender, WidgetArgs e)
- {
- if ((e is WidgetSignArgs) && CustomSignHandle == false)
- {
- try
- {
- WidgetSignArgs signArgs = (WidgetSignArgs)e;
- OpenFileDialog openFileDialog = new OpenFileDialog();
- openFileDialog.Filter = "Image Files(*.jpg;*.jpeg;*.png;*.bmp)|*.jpg;*.jpeg;*.png;*.bmp;";
- if (openFileDialog.ShowDialog() == true)
- {
- string ImagePath = openFileDialog.FileName;
- using (FileStream fileData = File.OpenRead(ImagePath))
- {
- string fileExt = System.IO.Path.GetExtension(ImagePath).ToLower();
- BitmapFrame frame = null;
- BitmapDecoder decoder = BitmapDecoder.Create(fileData, BitmapCreateOptions.None, BitmapCacheOption.Default);
- if (decoder != null && decoder.Frames.Count > 0)
- {
- frame = decoder.Frames[0];
- }
- if (frame != null)
- {
- byte[] ImageArray = new byte[frame.PixelWidth * frame.PixelHeight * 4];
- int ImageWidth = frame.PixelWidth;
- int ImageHeight = frame.PixelHeight;
- frame.CopyPixels(ImageArray, frame.PixelWidth * 4, 0);
- signArgs.UpdateApWithImage(ImageArray, ImageWidth, ImageHeight, C_Scale_Type.fitCenter, 0);
- }
- }
- }
- }
- catch (Exception ex)
- {
- }
- }
- }
- private void PDFView_PDFActionHandler(object sender, CPDFAction pdfAction)
- {
- if (pdfAction != null)
- {
- switch (pdfAction.ActionType)
- {
- case C_ACTION_TYPE.ACTION_TYPE_NAMED:
- {
- CPDFNamedAction namedAction = pdfAction as CPDFNamedAction;
- string namedStr = namedAction.GetName();
- switch (namedStr)
- {
- case "FirstPage":
- {
- PDFView?.GoToPage(0);
- break;
- }
- case "LastPage":
- {
- PDFView?.GoToPage(PDFView.Document.PageCount - 1);
- break;
- }
- case "NextPage":
- if (PDFView != null)
- {
- int nextIndex = PDFView.CurrentIndex + 1;
- if (nextIndex < PDFView.Document.PageCount)
- {
- PDFView.GoToPage(nextIndex);
- }
- }
- break;
- case "PrevPage":
- if (PDFView != null)
- {
- int prevIndex = PDFView.CurrentIndex - 1;
- if (prevIndex >= 0)
- {
- PDFView.GoToPage(prevIndex);
- }
- }
- break;
- default:
- break;
- }
- break;
- }
- case C_ACTION_TYPE.ACTION_TYPE_GOTO:
- if (PDFView != null)
- {
- CPDFGoToAction gotoAction = pdfAction as CPDFGoToAction;
- CPDFDestination dest = gotoAction.GetDestination(PDFView.Document);
- if (dest != null)
- PDFView.GoToPage(dest.PageIndex, new Point(0, 0));
- }
- break;
- case C_ACTION_TYPE.ACTION_TYPE_GOTOR:
- if (PDFView != null)
- {
- CPDFGoToRAction gotorAction = pdfAction as CPDFGoToRAction;
- CPDFDestination dest = gotorAction.GetDestination(PDFView.Document);
- if (dest != null)
- {
- PDFView.GoToPage(dest.PageIndex, new Point(0, 0));
- }
- }
- break;
- case C_ACTION_TYPE.ACTION_TYPE_URI:
- {
- CPDFUriAction uriAction = pdfAction as CPDFUriAction;
- string uri = uriAction.GetUri();
- try
- {
- if (!string.IsNullOrEmpty(uri))
- {
- Process.Start(uri);
- }
- }
- catch (Exception ex)
- {
- }
- }
- break;
- default:
- break;
- }
- }
- }
- private void PDFView_MouseWheelZoomHandler(object sender, bool e)
- {
- double newZoom = CheckZoomLevel(PDFView.ZoomFactor + (e ? 0.01 : -0.01), e);
- PDFView.Zoom(newZoom);
- }
- private double CheckZoomLevel(double zoom, bool IsGrowth)
- {
- double standardZoom = 100;
- if (zoom <= 0.01)
- {
- return 0.01;
- }
- if (zoom >= 10)
- {
- return 10;
- }
- zoom *= 100;
- for (int i = 0; i < zoomLevelList.Length - 1; i++)
- {
- if (zoom > zoomLevelList[i] && zoom <= zoomLevelList[i + 1] && IsGrowth)
- {
- standardZoom = zoomLevelList[i + 1];
- break;
- }
- if (zoom >= zoomLevelList[i] && zoom < zoomLevelList[i + 1] && !IsGrowth)
- {
- standardZoom = zoomLevelList[i];
- break;
- }
- }
- return standardZoom / 100;
- }
- public bool CheckHasForm()
- {
- if (PDFView == null || PDFView.Document == null)
- return false;
- var document = PDFView.Document;
- for (int i = 0; i < document.PageCount; i++)
- {
- CPDFPage page = document.PageAtIndex(i, false);
- List<CPDFAnnotation> annotList = page.GetAnnotations();
- if (annotList == null || annotList.Count < 1)
- continue;
- List<CPDFWidget> formList = annotList.AsEnumerable().
- Where(x => x.Type == C_ANNOTATION_TYPE.C_ANNOTATION_WIDGET)
- .Cast<CPDFWidget>()
- .ToList();
- if (formList.Count > 0)
- return true;
- }
- return false;
- }
- }
- }
|