-
Notifications
You must be signed in to change notification settings - Fork 62
/
SyncfusionControl.cs
161 lines (136 loc) · 5.35 KB
/
SyncfusionControl.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
using QuickLook.Plugin.PDFViewer;
using Syncfusion;
using Syncfusion.OfficeChartToImageConverter;
using Syncfusion.Presentation;
using Syncfusion.PresentationToPdfConverter;
using Syncfusion.UI.Xaml.CellGrid.Helpers;
using Syncfusion.UI.Xaml.Spreadsheet;
using Syncfusion.UI.Xaml.Spreadsheet.GraphicCells;
using Syncfusion.UI.Xaml.SpreadsheetHelper;
using Syncfusion.Windows.Controls.RichTextBoxAdv;
using System;
using System.IO;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Threading;
namespace QuickLook.Plugin.OfficeViewer;
internal static class SyncfusionControl
{
public static Control Open(string path)
{
// The Syncfusion method we are currently using does not support reading read-only file
if ((File.GetAttributes(path) & FileAttributes.ReadOnly) == FileAttributes.ReadOnly)
{
return new ContentControl()
{
Content = new StackPanel()
{
Children =
{
new Label { Content = "Read-only file is not supported." },
new Button
{
Content = "Remove read-only file attribute",
Margin = new Thickness(0, 10, 0, 0),
Command = new RelayCommand(() =>
{
try
{
FileAttributes attributes = File.GetAttributes(path);
if ((attributes & FileAttributes.ReadOnly) == FileAttributes.ReadOnly)
{
// Try to remove read-only file attribute
File.SetAttributes(path, attributes & ~FileAttributes.ReadOnly);
MessageBox.Show("Removed readonly file attribute successfully, please try to open again.", "Success", MessageBoxButton.OK, MessageBoxImage.Information);
}
}
catch (Exception ex)
{
MessageBox.Show(ex.Message, "Error", MessageBoxButton.OK, MessageBoxImage.Error);
}
}),
}
}
}
};
}
return (Path.GetExtension(path)?.ToLower()) switch
{
".doc" or ".docx" or ".docm" or ".rtf" => OpenWord(path),
".xls" or ".xlsx" or ".xlsm" => OpenExcel(path),
".pptx" or ".pptm" or ".potx" or ".potm" => OpenPowerpoint(path),
_ => new Label { Content = "File not supported." },
};
}
private static Control OpenWord(string path)
{
var editor = new SfRichTextBoxAdv
{
IsReadOnly = true,
Background = Brushes.Transparent,
EnableMiniToolBar = false,
};
editor.LoadAsync(path);
return editor;
}
private static Control OpenExcel(string path)
{
// Pre-load Syncfusion.Tools.Wpf.dll or it will throw error
var _ = new ToolsWPFAssembly();
var sheet = new SfSpreadsheet
{
AllowCellContextMenu = false,
AllowTabItemContextMenu = false,
Background = Brushes.Transparent,
DisplayAlerts = false
};
sheet.AddGraphicChartCellRenderer(new GraphicChartCellRenderer());
sheet.Open(path);
sheet.WorkbookLoaded += (sender, e) =>
{
sheet.SuspendFormulaCalculation();
sheet.Protect(true, true, string.Empty);
sheet.Workbook.Worksheets.ForEach(s => sheet.ProtectSheet(s, string.Empty));
sheet.GridCollection.ForEach(kv => kv.Value.ShowHidePopup(false));
};
return sheet;
}
private static Control OpenPowerpoint(string path)
{
var ppt = Presentation.Open(path);
ppt.ChartToImageConverter = new ChartToImageConverter();
var settings = new PresentationToPdfConverterSettings
{
OptimizeIdenticalImages = true,
ShowHiddenSlides = true
};
var pdf = PresentationToPdfConverter.Convert(ppt, settings);
var viewer = new PdfViewerControl();
var tempPdf = new MemoryStream();
pdf.Save(tempPdf);
pdf.Close(true);
pdf.Dispose();
ppt.Close();
ppt.Dispose();
viewer.Dispatcher.BeginInvoke(new Action(() =>
{
viewer.LoadPdf(tempPdf);
tempPdf.Dispose();
}), DispatcherPriority.Loaded);
return viewer;
}
}
file sealed class RelayCommand(Action execute, Func<bool> canExecute = null) : ICommand
{
private readonly Action _execute = execute ?? throw new ArgumentNullException(nameof(execute));
private readonly Func<bool> _canExecute = canExecute;
public bool CanExecute(object parameter) => _canExecute == null || _canExecute();
public void Execute(object parameter) => _execute();
public event EventHandler CanExecuteChanged
{
add => CommandManager.RequerySuggested += value;
remove => CommandManager.RequerySuggested -= value;
}
}