forked from QL-Win/QuickLook.Plugin.OfficeViewer
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSyncfusionControl.cs
More file actions
118 lines (100 loc) · 3.45 KB
/
SyncfusionControl.cs
File metadata and controls
118 lines (100 loc) · 3.45 KB
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
using System;
using System.IO;
using System.Windows.Controls;
using System.Windows.Media;
using System.Windows.Threading;
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;
namespace QuickLook.Plugin.OfficeViewer
{
internal static class SyncfusionControl
{
public static Control Open(string path)
{
switch (Path.GetExtension(path)?.ToLower())
{
case ".doc":
case ".docx":
case ".docm":
case ".rtf":
return OpenWord(path);
case ".xls":
case ".xlsx":
case ".xlsm":
return OpenExcel(path);
case ".pptx":
case ".pptm":
case ".potx":
case ".potm":
return OpenPowerpoint(path);
default:
return 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, "");
sheet.Workbook.Worksheets.ForEach(s => sheet.ProtectSheet(s, ""));
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;
}
}
}