using ComPDFKit.PDFAnnotation;
using ComPDFKit.PDFDocument;
using ComPDFKitViewer;
using ComPDFKitViewer.AnnotEvent;
using ComPDFKitViewer.PdfViewer;
using Microsoft.Office.Core;
using Microsoft.Win32;
using PDF_Office.CustomControl;
using PDF_Office.Helper;
using PDF_Office.Model;
using PDF_Office.Model.BOTA;
using PDF_Office.Properties;
using PDF_Office.ViewModels.BOTA;
using PDF_Office.ViewModels.PropertyPanel.AnnotPanel;
using PDF_Office.Views.BOTA;
using PDF_Office.Views.PropertyPanel.AnnotPanel;
using PDFSettings;
using Prism.Mvvm;
using Prism.Regions;
using Prism.Services.Dialogs;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Forms;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using static Dropbox.Api.Sharing.ListFileMembersIndividualResult;
using ContextMenu = System.Windows.Controls.ContextMenu;
using HorizontalAlignment = System.Windows.HorizontalAlignment;
using MenuItem = System.Windows.Controls.MenuItem;
using OpenFileDialog = Microsoft.Win32.OpenFileDialog;
namespace PDF_Office.ViewModels.Tools
{
//文件说明:初始化注释工具的数据,注释工具,菜单响应事件
public sealed partial class AnnotToolContentViewModel : BindableBase, INavigationAware
{
#region 初始化
#region 初始化数据
//初始化注释的属性值,并存在本地缓存数据集里
private void InitDefaultValue()
{
InitAnnotHighlight();
InitAnnotUnderline();
InitAnnotSquiggly();
InitAnnotStrikeout();
}
private void InitAnnotHighlight()
{
var annotate = Settings.Default.AppProperties.Annotate;
if (annotate != null)
{
HighLightColor = new SolidColorBrush(annotate.HighLightColor);
HighLightOpacity = 1;
}
else
{
HighLightColor = new SolidColorBrush(Colors.Red);
HighLightOpacity = 1;
}
}
private void InitAnnotUnderline()
{
var annotate = Settings.Default.AppProperties.Annotate;
if (annotate != null)
{
UnderLineColor = new SolidColorBrush(annotate.UnderLineColor);
HighLightOpacity = 1;
}
else
{
UnderLineColor = new SolidColorBrush(Colors.Red);
UnderLineOpacity = 1;
}
}
private void InitAnnotSquiggly()
{
var annotate = Settings.Default.AppProperties.Annotate;
if (annotate != null)
{
SquigglyColor = new SolidColorBrush(annotate.StrikethroughColor);
SquigglyOpacity = 1;
}
else
{
SquigglyColor = new SolidColorBrush(Colors.Red);
SquigglyOpacity = 1;
}
}
private void InitAnnotStrikeout()
{
var annotate = Settings.Default.AppProperties.Annotate;
if (annotate != null)
{
StrikeoutColor = new SolidColorBrush(annotate.StrikethroughColor);
StrikeoutOpacity = 1;
}
else
{
StrikeoutColor = new SolidColorBrush(Colors.Red);
StrikeoutOpacity = 1;
}
}
//用来记录选中和创建注释的标识
private void InitToolDict()
{
ToolExpandDict.Add("SnapshotEdit", AnnotArgsType.SnapshotWithEditTool);
ToolExpandDict.Add("HighLight", AnnotArgsType.AnnotHighlight);
ToolExpandDict.Add("UnderLine", AnnotArgsType.AnnotUnderline);
ToolExpandDict.Add("Squiggly", AnnotArgsType.AnnotSquiggly);
ToolExpandDict.Add("Strikeout", AnnotArgsType.AnnotStrikeout);
ToolExpandDict.Add("Freehand", AnnotArgsType.AnnotFreehand);
ToolExpandDict.Add("Freetext", AnnotArgsType.AnnotFreeText);
ToolExpandDict.Add("StickyNote", AnnotArgsType.AnnotSticky);
ToolExpandDict.Add("Rect", AnnotArgsType.AnnotSquare);
ToolExpandDict.Add("Circle", AnnotArgsType.AnnotCircle);
ToolExpandDict.Add("Arrow", AnnotArgsType.AnnotLine);
ToolExpandDict.Add("Line", AnnotArgsType.AnnotLine);
ToolExpandDict.Add("Link", AnnotArgsType.AnnotLink);
}
#endregion 初始化数据
#endregion 初始化
#region 注释工具
//注释工具
private void FindAnnotTypeKey(string tag, ref AnnotHandlerEventArgs annotArgs, bool isRightMenuAdd = false)
{
switch (tag)
{
case "SnapshotEdit"://内容选择
annotArgs = GetSnapshotEdit();
break;
case "HighLight"://字体高亮
annotArgs = GetHighLight();
break;
case "UnderLine"://下划线
annotArgs = GetUnderLine();
break;
case "Squiggly"://波浪线
annotArgs = GetSquiggly();
break;
case "Strikeout"://删除线
annotArgs = GetStrikeout();
break;
case "Freehand"://手绘
annotArgs = GetFreehand();
break;
case "Freetext"://文本
annotArgs = GetFreetext();
break;
case "StickyNote"://便签
annotArgs = GetStickyNote();
break;
case "Rect"://矩形
annotArgs = GetRect();
break;
case "Circle"://圆
annotArgs = GetCircle();
break;
case "Arrow"://箭头
case "Line"://线
annotArgs = GetArrowLine(tag);
break;
case "Stamp"://图章
annotArgs = GetStamp();
break;
case "Image":
annotArgs = GetImage();
break;
case "Signature"://签名
annotArgs = GetSignature();
PDFViewer.SetMouseMode(MouseModes.PanTool);//清空其他注释
break;
case "Link"://链接
viewContentViewModel.IsRightMenuCreateLink = isRightMenuAdd;
annotArgs = GetLink();
break;
}
}
///
/// 高亮注释
///
private AnnotHandlerEventArgs GetHighLight(List selectedArgs = null)
{
TextHighlightAnnotArgs highlightArgs = null;
if (selectedArgs == null || selectedArgs.Count == 0)
{
highlightArgs = new TextHighlightAnnotArgs();
highlightArgs.Transparency = highLightOpacity;
highlightArgs.Color = (highLightColor as SolidColorBrush).Color;
if (highlightArgs != null)
{
selectedArgs = new List();
selectedArgs.Add(highlightArgs);
}
}
else
{
highlightArgs = selectedArgs[0] as TextHighlightAnnotArgs;
}
AddToPropertyPanel("TextAnnotProperty", "HighLight", selectedArgs);
return highlightArgs;
}
///
/// 链接
///
/// 所选中的注释
/// 注释属性列表
public AnnotHandlerEventArgs GetLink(List selectedArgs = null, AnnotAttribEvent annotAttribEvent = null)
{
LinkAnnotArgs linkArgs = new LinkAnnotArgs();
if (selectedArgs != null && selectedArgs.Count > 0)
{
linkArgs = selectedArgs[0] as LinkAnnotArgs;
}
else
{
linkArgs.URI = string.Empty;
linkArgs.LinkType = LINK_TYPE.GOTO;
linkArgs.DestIndex = -1;
if (linkArgs != null)
{
selectedArgs = new List();
selectedArgs.Add(linkArgs);
}
}
AddToPropertyPanel("LinkAnnotProperty", "Link", selectedArgs, annotAttribEvent);
return linkArgs;
}
///
/// 下划线
///
///
private AnnotHandlerEventArgs GetUnderLine(List selectedArgs = null)
{
TextUnderlineAnnotArgs underlineArgs = null;
if (selectedArgs == null || selectedArgs.Count == 0)
{
underlineArgs = new TextUnderlineAnnotArgs();
underlineArgs.Transparency = UnderLineOpacity;
underlineArgs.Color = (UnderLineColor as SolidColorBrush).Color;
if (underlineArgs != null)
{
selectedArgs = new List();
selectedArgs.Add(underlineArgs);
}
}
else
{
underlineArgs = selectedArgs[0] as TextUnderlineAnnotArgs;
}
AddToPropertyPanel("TextAnnotProperty", "UnderLine", selectedArgs);
return underlineArgs;
}
///
/// 波浪线
///
///
///
private AnnotHandlerEventArgs GetSquiggly(List selectedArgs = null)
{
TextSquigglyAnnotArgs squigglyArgs = null;
if (selectedArgs == null || selectedArgs.Count == 0)
{
squigglyArgs = new TextSquigglyAnnotArgs();
squigglyArgs.Transparency = SquigglyOpacity;
squigglyArgs.Color = (squigglyColor as SolidColorBrush).Color;
if (squigglyArgs != null)
{
selectedArgs = new List();
selectedArgs.Add(squigglyArgs);
}
}
else
{
squigglyArgs = selectedArgs[0] as TextSquigglyAnnotArgs;
}
AddToPropertyPanel("TextAnnotProperty", "Squiggly", selectedArgs);
return squigglyArgs;
}
///
/// 删除线
///
///
///
private AnnotHandlerEventArgs GetStrikeout(List selectedArgs = null)
{
TextStrikeoutAnnotArgs strikeoutArgs = null;
if (selectedArgs == null || selectedArgs.Count == 0)
{
strikeoutArgs = new TextStrikeoutAnnotArgs();
strikeoutArgs.Transparency = strikeoutOpacity;
strikeoutArgs.Color = (strikeoutColor as SolidColorBrush).Color;
if (strikeoutArgs != null)
{
selectedArgs = new List();
selectedArgs.Add(strikeoutArgs);
}
}
else
{
strikeoutArgs = selectedArgs[0] as TextStrikeoutAnnotArgs;
}
AddToPropertyPanel("TextAnnotProperty", "Strikeout", selectedArgs);
return strikeoutArgs;
}
///
/// 手绘
///
///
///
private AnnotHandlerEventArgs GetFreehand(List selectedArgs = null)
{
FreehandAnnotArgs freehandArgs = null;
if (selectedArgs == null || selectedArgs.Count == 0)
{
freehandArgs = new FreehandAnnotArgs();
var annotate = Settings.Default.AppProperties.Annotate;
if (annotate != null)
{
freehandArgs.InkColor = annotate.FreeHandColor;
}
else
{
freehandArgs.InkColor = Color.FromRgb(0x38, 0xE0, 0x2E);
}
freehandArgs.Transparency = 1;
freehandArgs.LineWidth = 2;
if (freehandArgs != null)
{
selectedArgs = new List();
selectedArgs.Add(freehandArgs);
}
}
else
{
freehandArgs = selectedArgs[0] as FreehandAnnotArgs;
}
AddToPropertyPanel("FreehandAnnotProperty", "Freehand", selectedArgs);
return freehandArgs;
}
///
/// 文本
///
///
///
private AnnotHandlerEventArgs GetFreetext(List selectedArgs = null)
{
FreeTextAnnotArgs freetextArgs = null;
TextAlignment textAlignment;
if (selectedArgs == null || selectedArgs.Count == 0)
{
freetextArgs = new FreeTextAnnotArgs();
var annotate = Settings.Default.AppProperties.Annotate;
if (annotate != null)
{
freetextArgs.FontColor = annotate.TextAnnoteColor;
}
else
{
freetextArgs.FontColor = Colors.Black;
}
freetextArgs.Align = TextAlignment.Left;
freetextArgs.BgColor = Colors.Transparent;
freetextArgs.FontFamily = new FontFamily(Settings.Default.AppProperties.Annotate.TextFontFamaily);
freetextArgs.FontColor = Colors.Black;
freetextArgs.FontSize = 14;
freetextArgs.Transparency = 1;
freetextArgs.LineColor = Colors.Black;
freetextArgs.LineWidth = 0;
freetextArgs.TextContent = string.Empty;
int align = (int)Settings.Default.AppProperties.Annotate.TextAlign;
if (align == 0)
textAlignment = TextAlignment.Left;
else if (align == 1)
textAlignment = TextAlignment.Center;
else
textAlignment = TextAlignment.Right;
if (freetextArgs != null)
{
selectedArgs = new List();
selectedArgs.Add(freetextArgs);
}
}
else
{
freetextArgs = selectedArgs[0] as FreeTextAnnotArgs;
textAlignment = freetextArgs.Align;
}
AddToPropertyPanel("FreetextAnnotProperty", "Freetext", selectedArgs);
return freetextArgs;
}
///
/// 便签
///
///
///
private AnnotHandlerEventArgs GetStickyNote(List selectedArgs = null)
{
StickyAnnotArgs stickyAnnotArgs = new StickyAnnotArgs();
if (selectedArgs == null || selectedArgs.Count == 0)
{
var annotate = Settings.Default.AppProperties.Annotate;
if (annotate != null)
{
stickyAnnotArgs.Color = annotate.NoteAnnoteColor;
}
else
{
stickyAnnotArgs.Color = Color.FromRgb(0xFF, 0x81, 0x33);
}
stickyAnnotArgs.StickyNote = string.Empty;
stickyAnnotArgs.Transparency = 1;
if (stickyAnnotArgs != null)
{
selectedArgs = new List();
selectedArgs.Add(stickyAnnotArgs);
}
}
else
{
stickyAnnotArgs = selectedArgs[0] as StickyAnnotArgs;
}
List stickyAnnotArgsList = new List();
if (stickyAnnotArgs != null)
stickyAnnotArgsList.Add(stickyAnnotArgs);
AddToPropertyPanel("StickyNoteProperty", "StickyNote", selectedArgs);
return stickyAnnotArgs;
}
///
/// 矩形
///
///
///
private AnnotHandlerEventArgs GetRect(List selectedArgs = null)
{
SquareAnnotArgs squareArgs = null;
if (selectedArgs == null || selectedArgs.Count == 0)
{
squareArgs = new SquareAnnotArgs();
var annotate = Settings.Default.AppProperties.Annotate;
if (annotate != null)
{
squareArgs.BgColor = annotate.RectangleFillColor;
squareArgs.LineColor = annotate.RectangleBorderColor;
}
else
{
squareArgs.LineColor = Colors.Red;
squareArgs.BgColor = Colors.Red;
}
squareArgs.LineWidth = 1;
squareArgs.Transparency = 1;
squareArgs.LineDash = DashStyles.Solid;
squareArgs.Content = string.Empty;
if (squareArgs != null)
{
selectedArgs = new List();
selectedArgs.Add(squareArgs);
}
}
else
{
squareArgs = selectedArgs[0] as SquareAnnotArgs;
}
AddToPropertyPanel("SharpsAnnotProperty", "Rect", selectedArgs);
return squareArgs;
}
///
/// 圆
///
///
///
private AnnotHandlerEventArgs GetCircle(List selectedArgs = null)
{
CircleAnnotArgs circleAnnotArgs = null;
if (selectedArgs == null || selectedArgs.Count == 0)
{
circleAnnotArgs = new CircleAnnotArgs();
var annotate = Settings.Default.AppProperties.Annotate;
if (annotate != null)
{
circleAnnotArgs.LineColor = annotate.CircleBorderColor;
circleAnnotArgs.BgColor = annotate.CircleFillColor;
}
else
{
circleAnnotArgs.LineColor = Colors.Red;
circleAnnotArgs.BgColor = Colors.Red;
}
circleAnnotArgs.LineWidth = 1;
circleAnnotArgs.Transparency = 1;
circleAnnotArgs.LineDash = DashStyles.Solid;
circleAnnotArgs.Content = string.Empty;
if (circleAnnotArgs != null)
{
selectedArgs = new List();
selectedArgs.Add(circleAnnotArgs);
}
}
else
{
circleAnnotArgs = selectedArgs[0] as CircleAnnotArgs;
}
AddToPropertyPanel("SharpsAnnotProperty", "Circle", selectedArgs);
return circleAnnotArgs;
}
///
/// 箭头 线
///
///
///
///
private AnnotHandlerEventArgs GetArrowLine(string TagStr, List selectedArgs = null)
{
LineAnnotArgs lineArgs = new LineAnnotArgs();
if (selectedArgs == null || selectedArgs.Count == 0)
{
var annotate = Settings.Default.AppProperties.Annotate;
if (annotate != null)
{
lineArgs.LineColor = annotate.LineColor;
}
else
{
lineArgs.LineColor = Colors.Red;
}
lineArgs.LineColor = Colors.Red;
lineArgs.HeadLineType = C_LINE_TYPE.LINETYPE_NONE;
if (TagStr == "Line")
{
lineArgs.TailLineType = C_LINE_TYPE.LINETYPE_NONE;
}
else
{
lineArgs.TailLineType = C_LINE_TYPE.LINETYPE_ARROW;
}
lineArgs.LineDash = DashStyles.Solid;
lineArgs.LineWidth = 1;
lineArgs.Transparency = 1;
lineArgs.Content = string.Empty;
if (lineArgs != null)
{
selectedArgs = new List();
selectedArgs.Add(lineArgs);
}
}
else
{
lineArgs = selectedArgs[0] as LineAnnotArgs;
}
AddToPropertyPanel("SharpsAnnotProperty", TagStr, selectedArgs);
return lineArgs;
}
///
/// 图章
///
///
private AnnotHandlerEventArgs GetStamp()
{
StampAnnotArgs stampAnnotArgs = new StampAnnotArgs();
stampAnnotArgs.Opacity = 1;
stampAnnotArgs.StampText = "APPROVED";
stampAnnotArgs.Type = StampType.STANDARD_STAMP;
List stampAnnotArgsList = new List();
if (stampAnnotArgs != null)
stampAnnotArgsList.Add(stampAnnotArgs);
AddToPropertyPanel("StampAnnotProperty", null, stampAnnotArgsList);
return stampAnnotArgs;
}
///
/// 签名
///
///
private AnnotHandlerEventArgs GetSignature()
{
AddToPropertyPanel("SignatureAnnotProperty");
return null;
}
private AnnotHandlerEventArgs GetImage()
{
StampAnnotArgs stampArgs = new StampAnnotArgs();
stampArgs.Opacity = 1;
stampArgs.Type = StampType.IMAGE_STAMP;
OpenFileDialog openFileDialog = new OpenFileDialog();
openFileDialog.Filter = "Image Files(*.jpg;*.jpeg;*.png;*.bmp)|*.jpg;*.jpeg;*.png;*.bmp;";
if (openFileDialog.ShowDialog() == true)
{
stampArgs.ImagePath = openFileDialog.FileName;
}
List stampArgsList = new List();
if (stampArgs != null)
stampArgsList.Add(stampArgs);
AddToPropertyPanel("ImageAnnotProperty", null, stampArgsList);
return stampArgs;
}
///
/// 内容选择
///
///
///
private AnnotHandlerEventArgs GetSnapshotEdit()
{
SnapshotEditToolArgs snapshotArgs = new SnapshotEditToolArgs();
//SnapshotEditMenuViewModel snapshotEditMenuViewModel = new SnapshotEditMenuViewModel();
snapshotArgs.ControlPointColor = Colors.White;
snapshotArgs.BgColor = Color.FromArgb(0x99, 0x00, 0x00, 0x00);
snapshotArgs.LineColor = Color.FromArgb(0xFF, 0x47, 0x7E, 0xDE);
SnapshotEditMenuViewModel.SnapToolArgs = snapshotArgs;
SnapshotEditMenuViewModel.PDFViewer = PDFViewer;
SnapshotEditMenuViewModel.SnapToolEvent -= SnapshotEditMenuViewModel_SnapToolEvent;
SnapshotEditMenuViewModel.SnapToolEvent += SnapshotEditMenuViewModel_SnapToolEvent;
#region to do
//SnapshotEditMenu snapMenu = new SnapshotEditMenu();
//snapshotArgs.ToolPanel = snapMenu;
//SnapshotEditMenuViewModel snapshotEditMenuViewModel = (SnapshotEditMenuViewModel)snapMenu.DataContext;
//snapshotEditMenuViewModel.SnapToolArgs = snapshotArgs;
//snapshotEditMenuViewModel.PDFViewer = PDFViewer;
//snapshotEditMenuViewModel.ToggleBtn = annotBtn;
//snapshotEditMenuViewModel.SnapToolEvent += SnapshotEditMenuViewModel_SnapToolEvent;
//SnapshotEditMenuViewModel = snapshotEditMenuViewModel;
#endregion to do
return snapshotArgs;
}
#endregion 注释工具
#region 菜单
///
/// 选择文本、图片,右键菜单
///
///
///
private void AnnotToolMenu_Click(object sender, RoutedEventArgs e)
{
if (sender is MenuItem clickMenu && clickMenu.CommandParameter is AnnotCommandArgs)
{
AnnotCommandArgs annotCommand = clickMenu.CommandParameter as AnnotCommandArgs;
switch (clickMenu.Tag.ToString())
{
case "HighLight"://字体高亮
TextHighlightAnnotArgs highlightArgs = (TextHighlightAnnotArgs)GetHighLight();
highlightArgs.ClientRect = annotCommand.TextRect;
highlightArgs.Content = annotCommand.Text;
highlightArgs.Author = Settings.Default.AppProperties.Description.Author;
PDFViewer.SetMouseMode(MouseModes.AnnotCreate);
PDFViewer.SetToolParam(highlightArgs);
PDFViewer.SetMouseMode(MouseModes.PanTool);
break;
case "UnderLine"://下划线
TextUnderlineAnnotArgs underlineArgs = (TextUnderlineAnnotArgs)GetUnderLine();
underlineArgs.ClientRect = annotCommand.TextRect;
underlineArgs.Content = annotCommand.Text;
underlineArgs.Author = Settings.Default.AppProperties.Description.Author;
PDFViewer.SetMouseMode(MouseModes.AnnotCreate);
PDFViewer.SetToolParam(underlineArgs);
PDFViewer.SetMouseMode(MouseModes.PanTool);
break;
case "Strikeout"://删除线
TextStrikeoutAnnotArgs strikeoutArgs = (TextStrikeoutAnnotArgs)GetStrikeout();
strikeoutArgs.ClientRect = annotCommand.TextRect;
strikeoutArgs.Content = annotCommand.Text;
strikeoutArgs.Author = Settings.Default.AppProperties.Description.Author;
PDFViewer.SetMouseMode(MouseModes.AnnotCreate);
PDFViewer.SetToolParam(strikeoutArgs);
PDFViewer.SetMouseMode(MouseModes.PanTool);
break;
case "Freetext"://文本
FreeTextAnnotArgs textArgs = (FreeTextAnnotArgs)GetFreetext();
textArgs.ClientRect = annotCommand.TextRect;
//textArgs.Transparency = 1;
//textArgs.BgColor = Colors.Transparent;
//textArgs.LineColor = Colors.Red;
//textArgs.LineWidth = 2;
textArgs.TextContent = annotCommand.Text;
PDFViewer.CreatePageAnnot(annotCommand.PageIndex, textArgs);
break;
case "StickyNote"://便签
StickyAnnotArgs stickyAnnotArgs = (StickyAnnotArgs)GetStickyNote();
stickyAnnotArgs.ClientRect = annotCommand.TextRect;
PDFViewer.CreatePageAnnot(annotCommand.PageIndex, stickyAnnotArgs);
break;
case "Rect"://矩形
SquareAnnotArgs squareAnnotArgs = (SquareAnnotArgs)GetRect();
squareAnnotArgs.ClientRect = annotCommand.TextRect;
PDFViewer.CreatePageAnnot(annotCommand.PageIndex, squareAnnotArgs);
break;
case "Circle"://圆
CircleAnnotArgs circleAnnotArgs = (CircleAnnotArgs)GetCircle();
circleAnnotArgs.ClientRect = annotCommand.TextRect;
PDFViewer.CreatePageAnnot(annotCommand.PageIndex, circleAnnotArgs);
break;
case "Line"://线
LineAnnotArgs lineAnnotArgs = (LineAnnotArgs)GetArrowLine(clickMenu.Tag.ToString());
lineAnnotArgs.ClientRect = annotCommand.TextRect;
lineAnnotArgs.LinePoints.Add(new Point(annotCommand.TextRect.Left, annotCommand.TextRect.Top));
lineAnnotArgs.LinePoints.Add(new Point(annotCommand.TextRect.Right, annotCommand.TextRect.Bottom));
PDFViewer.CreatePageAnnot(annotCommand.PageIndex, lineAnnotArgs);
break;
case "Link"://链接
LinkAnnotArgs linkArgs = new LinkAnnotArgs();
linkArgs.ClientRect = annotCommand.TextRect;
linkArgs.URI = string.Empty;
linkArgs.LinkType = LINK_TYPE.GOTO;
linkArgs.DestIndex = annotCommand.PageIndex;
PDFViewer.CreatePageAnnot(annotCommand.PageIndex, linkArgs);
BtnLinkIsChecked = true;
List lists = new List();
lists.Add(linkArgs);
AnnotHandlerEventArgs annotArgs = GetLink(lists);
PDFViewer.SetMouseMode(MouseModes.AnnotCreate);
PDFViewer.SetToolParam(annotArgs);
viewContentViewModel.IsPropertyOpen = true;
break;
case "OutLine":
bool isTabItemAnnotation = IsBOTATabItemShow(out BOTAContentViewModel bOTAContentViewModel, out BOTAContent bOTAContent, "TabItemOutLine");
if (isTabItemAnnotation == false)
{
bOTAContent.TabItemOutLine.IsSelected = true;
}
OutLineControlViewModel outLineControlViewModel = GetOutLineControlViewModel(bOTAContentViewModel, out OutLineControl outLineControl);
if (outLineControlViewModel != null && outLineControl != null)
{
string str = null;
if (string.IsNullOrEmpty(annotCommand.Text))
{
str = (annotCommand.PageIndex + 1).ToString();
}
else
{
str = annotCommand.Text;
}
OutlineNode outline = outLineControl.AddOutlineNode(str);
CPDFOutline result = outline.Outline;
if (result != null)
{
outLineControlViewModel.SetTitle(result, str);
outLineControlViewModel.Updata(false);
//outLineControl.IsReName = true;
//if (outLineControl.OutlineView.SelectedItem is OutlineNode selectOutLine)
//{
// outLineControl.ReNameOutlineNode = outline;
//}
}
}
break;
case "ExportPicture":
ExportPicture_MenuItemClick();
break;
}
//ShowPropertyPanel(false);
}
}
private void ExportPicture_MenuItemClick()
{
//图片提取
try
{
List imageList = new List();
Dictionary> imageDict = PDFViewer?.GetSelectedImages();
foreach (int pageIndex in imageDict.Keys)
{
imageList = imageDict[pageIndex];
}
string path = PDFViewer.Document.FileName;
System.Windows.Forms.FolderBrowserDialog folderDialog = new FolderBrowserDialog();
if (folderDialog.ShowDialog() == System.Windows.Forms.DialogResult.OK)
{
int i = 1;
int pagenum = 1;
string choosePath = folderDialog.SelectedPath;
string sPath = choosePath + "\\" + path + "_Extract Image";
sPath = GetExportPathName(sPath);
if (!Directory.Exists(sPath))
{
Directory.CreateDirectory(sPath);
}
if (Directory.Exists(sPath))
{
foreach (System.Drawing.Bitmap image in imageList)
{
foreach (var item in imageDict.Keys)
{
pagenum = item;
}
string filename = path + "_" + "Page" + (pagenum + 1).ToString() + "_" + GetNum(i);
string savePath = System.IO.Path.Combine(sPath, filename + ".jpg");
image.Save(savePath, System.Drawing.Imaging.ImageFormat.Jpeg);
i++;
}
System.Diagnostics.Process.Start("explorer", "/select,\"" + sPath + "\"");
}
}
}
catch (Exception ex) { }
}
public string GetExportPathName(string path)
{
int i = 1;
string outpath = path;
while (Directory.Exists(outpath))
{
outpath = path + $"({i.ToString()})";
i++;
}
return outpath;
}
private string GetNum(int num)
{
if (num % 10 < 1)
{
return "00" + num.ToString();
}
if (num % 10 >= 1 && num % 10 < 10)
{
return "0" + num.ToString();
}
else
{
return num.ToString();
}
}
#region 注释右键菜单事件
//高亮、下划线、删除
private void HightAnnotCopyText_Menu(object obj)
{
if (obj as CusMenuItem != null)
{
var menu = obj as CusMenuItem;
var annot = menu.Parameter as AnnotHandlerEventArgs;
if (annot != null)
{
System.Windows.Clipboard.SetText(annot.Content);
}
}
}
//更改为当前注释属性默认值
private void AnnotDefaultValues_Menu(object obj)
{
if (obj as CusMenuItem != null)
{
var menu = obj as CusMenuItem;
var annot = menu.Parameter as AnnotHandlerEventArgs;
if (annot != null)
{
if (annot is TextHighlightAnnotArgs)
{
var color = (annot as TextHighlightAnnotArgs).Color;
Settings.Default.AppProperties.Annotate.HighLightColor = color;
HighLightColor = new SolidColorBrush(color);
}
else if (annot is TextUnderlineAnnotArgs)
{
var color = (annot as TextHighlightAnnotArgs).Color;
Settings.Default.AppProperties.Annotate.UnderLineColor = color;
UnderLineColor = new SolidColorBrush(color);
}
else if (annot is TextStrikeoutAnnotArgs)
{
var color = (annot as TextHighlightAnnotArgs).Color;
Settings.Default.AppProperties.Annotate.StrikethroughColor = color;
StrikeoutColor = new SolidColorBrush(color);
}
else if (annot is FreehandAnnotArgs)
{
var color = (annot as FreehandAnnotArgs).InkColor;
Settings.Default.AppProperties.Annotate.FreeHandColor = color;
}
else if (annot is FreeTextAnnotArgs)
{
var freeText = annot as FreeTextAnnotArgs;
Settings.Default.AppProperties.Annotate.TextAnnoteColor = freeText.FontColor;
Settings.Default.AppProperties.Annotate.TextFontFamaily = freeText.FontFamily.ToString();
Settings.Default.AppProperties.Annotate.TextAlign = freeText.Align;
}
else if (annot is StickyAnnotArgs)
{
var color = (annot as StickyAnnotArgs).Color;
Settings.Default.AppProperties.Annotate.NoteAnnoteColor = color;
}
else if (annot is SquareAnnotArgs)
{
var bgColor = (annot as SquareAnnotArgs).BgColor;
Settings.Default.AppProperties.Annotate.RectangleFillColor = bgColor;
var borderColor = (annot as SquareAnnotArgs).LineColor;
Settings.Default.AppProperties.Annotate.RectangleBorderColor = borderColor;
}
else if (annot is CircleAnnotArgs)
{
var bgColor = (annot as CircleAnnotArgs).BgColor;
Settings.Default.AppProperties.Annotate.CircleFillColor = bgColor;
var borderColor = (annot as CircleAnnotArgs).LineColor;
Settings.Default.AppProperties.Annotate.CircleBorderColor = borderColor;
}
else if (annot is LineAnnotArgs)
{
var color = (annot as LineAnnotArgs).LineColor;
Settings.Default.AppProperties.Annotate.LineColor = color;
}
Settings.Default.Save();
}
}
}
//更改颜色
private void AnnotColorPalette_Menu(object obj)
{
if (obj as CusMenuItem != null)
{
var menu = obj as CusMenuItem;
var annot = menu.Parameter as AnnotHandlerEventArgs;
if (annot != null)
{
var item = new ColorDropBoxPop();
item.DataContext = annot;
item.ColorSelected -= AnnotMenu_ColorSelected;
item.ColorSelected += AnnotMenu_ColorSelected;
System.Windows.Controls.Primitives.Popup popup = new System.Windows.Controls.Primitives.Popup();
popup.Child = item;
popup.PlacementRectangle = new Rect(Mouse.GetPosition(App.Current.MainWindow), new Size(item.Width, item.Height));
popup.Placement = System.Windows.Controls.Primitives.PlacementMode.Bottom;
popup.IsOpen = true;
}
}
}
private void AnnotMenu_ColorSelected(object sender, Color e)
{
if (sender != null)
{
var annot = (sender as FrameworkElement).DataContext as AnnotHandlerEventArgs;
if (annot != null)
{
if (annot is FreehandAnnotArgs || annot is StickyAnnotArgs || annot is LineAnnotArgs)
{
var AnnotEvent = AnnotAttribEvent.GetAnnotAttribEvent(annot, annot.GetAnnotAttrib());
AnnotEvent?.UpdateAttrib(AnnotAttrib.Color, e);
AnnotEvent?.UpdateAnnot();
}
else if (annot is FreeTextAnnotArgs)
{
var AnnotEvent = AnnotAttribEvent.GetAnnotAttribEvent(annot, annot.GetAnnotAttrib());
AnnotEvent?.UpdateAttrib(AnnotAttrib.FontColor, e);
AnnotEvent?.UpdateAnnot();
}
else if (annot is SquareAnnotArgs || annot is CircleAnnotArgs)
{
var AnnotEvent = AnnotAttribEvent.GetAnnotAttribEvent(annot, annot.GetAnnotAttrib());
AnnotEvent?.UpdateAttrib(AnnotAttrib.FillColor, e);
AnnotEvent?.UpdateAnnot();
}
}
}
}
//添加笔记
private void AnnotAddNoteText_Menu(object obj)
{
if (obj as CusMenuItem != null)
{
var menu = obj as CusMenuItem;
var annot = menu.Parameter as AnnotHandlerEventArgs;
if (annot != null)
{
AnnotationHandlerEventArgs args = new AnnotationHandlerEventArgs();
args.ClientRect = annot.ClientRect;
args.AnnotHandlerEventArgs = annot;
args.PageIndex = annot.PageIndex;
args.AnnotIndex = annot.AnnotIndex;
args.EventType = annot.EventType;
args.CreateTime = annot.CreateTime;
args.UpdateTime = annot.UpdateTime;
args.Content = annot.Content;
args.MarkupContent = annot.MarkupContent;
args.Author = annot.Author;
args.Locked = annot.Locked;
args.ReadOnly = annot.ReadOnly;
args.FormField = annot.FormField;
args.Document = PDFViewer.Document;
DialogParameters value = new DialogParameters();
value.Add(ParameterNames.Annotation, args);
dialogs.ShowDialog(DialogNames.AddAnnotationDialog, value, e =>
{
if (e.Result == ButtonResult.OK && e.Parameters != null)
{
PDFViewer.UndoManager.CanSave = true;
if (e.Parameters.ContainsKey(ParameterNames.Annotation) && e.Parameters.ContainsKey(ParameterNames.AnnotEvent))
{
}
}
});
}
}
}
//手绘
private void FreeHandLineStyle_Menu(object obj)
{
if (obj as CusMenuItem != null)
{
var menu = obj as CusMenuItem;
var annot = menu.Parameter as FreehandAnnotArgs;
if (annot != null)
{
var AnnotEvent = AnnotAttribEvent.GetAnnotAttribEvent(annot, annot.GetAnnotAttrib());
if (menu.tag.ToString() == "Solid")
{
AnnotEvent?.UpdateAttrib(AnnotAttrib.LineStyle, AnnotPropertyPanel.GetLineDashStyle(true));
}
else
{
AnnotEvent?.UpdateAttrib(AnnotAttrib.LineStyle, AnnotPropertyPanel.GetLineDashStyle(false));
}
AnnotEvent?.UpdateAnnot();
}
}
}
//文本
private void FreeTextFontFamily_Menu(object obj)
{
if (obj as CusMenuItem != null)
{
var menu = obj as CusMenuItem;
var annot = menu.Parameter as AnnotHandlerEventArgs;
if (annot != null)
{
var AnnotEvent = AnnotAttribEvent.GetAnnotAttribEvent(annot, annot.GetAnnotAttrib());
AnnotEvent?.UpdateAttrib(AnnotAttrib.FontFamily, new FontFamily(menu.tag.ToString()));
AnnotEvent?.UpdateAnnot();
}
}
}
private void FreeTextAglin_Menu(object obj)
{
if (obj as CusMenuItem != null)
{
var menu = obj as CusMenuItem;
var annot = menu.Parameter as AnnotHandlerEventArgs;
var tag = menu.control.Tag;
if (annot != null && tag != null)
{
var strTag = tag.ToString();
var AnnotEvent = AnnotAttribEvent.GetAnnotAttribEvent(annot, annot.GetAnnotAttrib());
if (strTag == "Left")
{
AnnotEvent?.UpdateAttrib(AnnotAttrib.TextAlign, TextAlignment.Left);
}
else if (strTag == "Center")
{
AnnotEvent?.UpdateAttrib(AnnotAttrib.TextAlign, TextAlignment.Center);
}
else if (strTag == "Right")
{
AnnotEvent?.UpdateAttrib(AnnotAttrib.TextAlign, TextAlignment.Right);
}
AnnotEvent?.UpdateAnnot();
}
}
}
//便签
private void StrikeNoteEditStrike_Menu(object obj)
{
if (obj as CusMenuItem != null)
{
var menu = obj as CusMenuItem;
var annot = menu.Parameter as AnnotHandlerEventArgs;
if (annot != null)
{
List list = new List();
list.Add(annot);
GetStickyNote(list);
annot.Author = Settings.Default.AppProperties.Description.Author;
PDFViewer.SetMouseMode(MouseModes.AnnotCreate);
PDFViewer.SetToolParam(annot);
#region 设计重新调整,阅读页空白处,右键菜单,添加链接需要显示,其他和pro mac一样的效果,不显示属性栏
//isRightMenuAddAnnot = false;
#endregion 设计重新调整,阅读页空白处,右键菜单,添加链接需要显示,其他和pro mac一样的效果,不显示属性栏
}
}
}
//形状
private void ShapeLineStyle_Menu(object obj)
{
if (obj as CusMenuItem != null)
{
var menu = obj as CusMenuItem;
var annot = menu.Parameter as AnnotHandlerEventArgs;
var tag = menu.control.Tag;
if (annot != null && tag != null)
{
var strTag = tag.ToString();
var AnnotEvent = AnnotAttribEvent.GetAnnotAttribEvent(annot, annot.GetAnnotAttrib());
if (strTag == "Solid")
{
var dashStyle = AnnotPropertyPanel.GetLineDashStyle(true);
AnnotEvent?.UpdateAttrib(AnnotAttrib.LineStyle, dashStyle);
}
else
{
var dashStyle = AnnotPropertyPanel.GetLineDashStyle(false);
AnnotEvent?.UpdateAttrib(AnnotAttrib.LineStyle, dashStyle);
}
AnnotEvent?.UpdateAnnot();
}
}
}
private void ShapeLineDirect_Menu(object obj)
{
if (obj as CusMenuItem != null)
{
var menu = obj as CusMenuItem;
var annot = menu.Parameter as LineAnnotArgs;
var tag = menu.control.Tag;
if (annot != null && tag != null)
{
var strTag = tag.ToString();
var AnnotEvent = AnnotAttribEvent.GetAnnotAttribEvent(annot, annot.GetAnnotAttrib());
if (strTag == "ver")
{
annot.SetLineVertical();
}
else
{
annot.SetLineHorizontal();
}
}
}
}
//图章
private void StampExportPicture_Menu(object obj)
{
if (obj as CusMenuItem != null)
{
var menu = obj as CusMenuItem;
var annot = menu.Parameter as StampAnnotArgs;
if (annot != null)
{
Microsoft.Win32.SaveFileDialog dlg = new Microsoft.Win32.SaveFileDialog();
var format = menu.tag.ToString();
if (format == "PDF")
dlg.Filter = "PDF|*.pdf";
else
dlg.Filter = format + "|*." + format.ToLower();
dlg.DefaultExt = "." + menu.tag.ToString().ToLower();
dlg.FileName = PDFViewer.Document.FileName;
if (dlg.ShowDialog() == true)
{
var stampPicture = dlg.FileName;
if (menu.tag.ToString() == "PDF")
{
var writeStamp = annot.GetStampDrawing();
var tempDoc = new CPDFViewer();
tempDoc.CreateDocument();
if (tempDoc.Document == null)
{
AlertsMessage alertsMessage = new AlertsMessage();
alertsMessage.ShowDialog("", "创建文件失败.", "OK");
return;
}
byte[] buffer;
var encoder = new PngBitmapEncoder();
encoder.Frames.Add(BitmapFrame.Create(writeStamp));
using (var memoryStream = new MemoryStream())
{
encoder.Save(memoryStream);
buffer = memoryStream.ToArray();
}
tempDoc.Document.InsertPage(0, writeStamp.PixelWidth, writeStamp.PixelHeight, buffer, CPDFDocumentImageMode.CPDFDocumentImageModeScaleAspectFit);
var result = tempDoc.Document.WriteToFilePath(dlg.FileName);
if (result)
{
}
}
else
{
var writeStamp = annot.GetStampDrawing();
using (FileStream stream5 = new FileStream(stampPicture, FileMode.Create))
{
PngBitmapEncoder encoder5 = new PngBitmapEncoder();
encoder5.Frames.Add(BitmapFrame.Create(writeStamp));
encoder5.Save(stream5);
}
}
}
}
}
}
#endregion 注释右键菜单事件
#endregion 菜单
}
}