forked from QL-Win/QuickLook
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
30 changed files
with
807 additions
and
48 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
181 changes: 181 additions & 0 deletions
181
QuickLook.Plugin/QuickLook.Plugin.OfficeViewer/OfficeInteropWrapper.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,181 @@ | ||
using System; | ||
using System.Diagnostics; | ||
using System.IO; | ||
using Microsoft.Office.Interop.Excel; | ||
using Microsoft.Office.Interop.PowerPoint; | ||
using Microsoft.Office.Interop.Word; | ||
using Application = Microsoft.Office.Interop.Excel.Application; | ||
using Task = System.Threading.Tasks.Task; | ||
|
||
namespace QuickLook.Plugin.OfficeViewer | ||
{ | ||
internal class OfficeInteropWrapper : IDisposable | ||
{ | ||
public enum FileTypeEnum | ||
{ | ||
Word, | ||
Excel, | ||
PowerPoint | ||
} | ||
|
||
private readonly string _path; | ||
private readonly string _tempPdf = Path.GetTempFileName(); | ||
|
||
private Application _excelApp; | ||
private Microsoft.Office.Interop.PowerPoint.Application _powerpointApp; | ||
private Microsoft.Office.Interop.Word.Application _wordApp; | ||
|
||
public OfficeInteropWrapper(string path) | ||
{ | ||
_path = path; | ||
|
||
switch (Path.GetExtension(path).ToLower()) | ||
{ | ||
case ".doc": | ||
case ".docx": | ||
FileType = FileTypeEnum.Word; | ||
break; | ||
case ".xls": | ||
case ".xlsx": | ||
FileType = FileTypeEnum.Excel; | ||
break; | ||
case ".ppt": | ||
case ".pptx": | ||
FileType = FileTypeEnum.PowerPoint; | ||
break; | ||
default: | ||
throw new NotSupportedException($"{path} is not supported."); | ||
} | ||
|
||
LoadApplication(); | ||
} | ||
|
||
public FileTypeEnum FileType { get; } | ||
|
||
public void Dispose() | ||
{ | ||
GC.SuppressFinalize(this); | ||
|
||
// communicate with COM in a separate thread | ||
Task.Run(() => | ||
{ | ||
try | ||
{ | ||
//_wordApp?.Documents.Close(false); | ||
_wordApp?.Quit(false); | ||
_wordApp = null; | ||
|
||
_excelApp?.Workbooks.Close(); | ||
_excelApp?.Quit(); | ||
_excelApp = null; | ||
|
||
_powerpointApp?.Quit(); | ||
_powerpointApp = null; | ||
} | ||
catch (Exception e) | ||
{ | ||
Debug.WriteLine(e.Message); | ||
Debug.WriteLine(e.StackTrace); | ||
} | ||
}) | ||
.Wait(); | ||
} | ||
|
||
public string SaveAsPdf() | ||
{ | ||
if (_wordApp == null && _excelApp == null && _powerpointApp == null) | ||
throw new Exception("Office application launch failed."); | ||
|
||
var succeeded = false; | ||
|
||
// communicate with COM in a separate thread | ||
Task.Run(() => | ||
{ | ||
try | ||
{ | ||
switch (FileType) | ||
{ | ||
case FileTypeEnum.Word: | ||
_wordApp.ActiveDocument.ExportAsFixedFormat(_tempPdf, | ||
WdExportFormat.wdExportFormatPDF); | ||
succeeded = true; | ||
break; | ||
case FileTypeEnum.Excel: | ||
_excelApp.ActiveWorkbook.ExportAsFixedFormat(XlFixedFormatType.xlTypePDF, | ||
_tempPdf); | ||
succeeded = true; | ||
break; | ||
case FileTypeEnum.PowerPoint: | ||
_powerpointApp.ActivePresentation.ExportAsFixedFormat(_tempPdf, | ||
PpFixedFormatType.ppFixedFormatTypePDF); | ||
succeeded = true; | ||
break; | ||
default: | ||
throw new NotSupportedException($"{_path} is not supported."); | ||
} | ||
} | ||
catch (Exception e) | ||
{ | ||
Debug.WriteLine(e.Message); | ||
Debug.WriteLine(e.StackTrace); | ||
} | ||
}) | ||
.Wait(); | ||
|
||
if (succeeded) | ||
return FileType == FileTypeEnum.Excel | ||
? _tempPdf + ".pdf" | ||
: _tempPdf; // Excel will add ".pdf" to our filename | ||
|
||
Dispose(); | ||
return string.Empty; | ||
} | ||
|
||
private void LoadApplication() | ||
{ | ||
var succeeded = false; | ||
|
||
// communicate with COM in a separate thread | ||
Task.Run(() => | ||
{ | ||
try | ||
{ | ||
switch (FileType) | ||
{ | ||
case FileTypeEnum.Word: | ||
_wordApp = new Microsoft.Office.Interop.Word.Application(); | ||
_wordApp.Documents.Add(_path); | ||
succeeded = true; | ||
break; | ||
case FileTypeEnum.Excel: | ||
_excelApp = new Application(); | ||
_excelApp.Workbooks.Add(_path); | ||
succeeded = true; | ||
break; | ||
case FileTypeEnum.PowerPoint: | ||
_powerpointApp = new Microsoft.Office.Interop.PowerPoint.Application(); | ||
_powerpointApp.Presentations.Open(_path); | ||
succeeded = true; | ||
break; | ||
default: | ||
throw new NotSupportedException($"{_path} is not supported."); | ||
} | ||
} | ||
catch (Exception e) | ||
{ | ||
Debug.WriteLine(e.Message); | ||
Debug.WriteLine(e.StackTrace); | ||
} | ||
}) | ||
.Wait(); | ||
|
||
if (!succeeded) | ||
Dispose(); | ||
} | ||
|
||
~OfficeInteropWrapper() | ||
{ | ||
Dispose(); | ||
} | ||
} | ||
} |
93 changes: 93 additions & 0 deletions
93
QuickLook.Plugin/QuickLook.Plugin.OfficeViewer/PluginInterface.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,93 @@ | ||
using System; | ||
using System.IO; | ||
using System.Windows; | ||
using QuickLook.Plugin.PDFViewer; | ||
|
||
namespace QuickLook.Plugin.OfficeViewer | ||
{ | ||
public class PluginInterface : IViewer | ||
{ | ||
private string _pdfPath = ""; | ||
private PdfViewerControl _pdfViewer; | ||
|
||
public int Priority => int.MaxValue; | ||
|
||
public bool CanHandle(string path) | ||
{ | ||
if (Directory.Exists(path)) | ||
return false; | ||
|
||
switch (Path.GetExtension(path).ToLower()) | ||
{ | ||
case ".doc": | ||
case ".docx": | ||
case ".xls": | ||
case ".xlsx": | ||
case ".ppt": | ||
case ".pptx": | ||
return true; | ||
} | ||
|
||
return false; | ||
} | ||
|
||
public void Prepare(string path, ViewContentContainer container) | ||
{ | ||
container.SetPreferedSizeFit(new Size {Width = 800, Height = 600}, 0.8); | ||
} | ||
|
||
public void View(string path, ViewContentContainer container) | ||
{ | ||
using (var officeApp = new OfficeInteropWrapper(path)) | ||
{ | ||
_pdfPath = officeApp.SaveAsPdf(); | ||
} | ||
|
||
if (string.IsNullOrEmpty(_pdfPath)) | ||
throw new Exception("COM failed."); | ||
|
||
_pdfViewer = new PdfViewerControl(); | ||
|
||
_pdfViewer.Loaded += (sender, e) => | ||
{ | ||
try | ||
{ | ||
_pdfViewer.LoadPdf(_pdfPath); | ||
} | ||
catch (Exception ex) | ||
{ | ||
throw ex; | ||
} | ||
|
||
container.Title = $"{Path.GetFileName(path)} (1 / {_pdfViewer.TotalPages})"; | ||
}; | ||
_pdfViewer.CurrentPageChanged += (sender, e) => container.Title = | ||
$"{Path.GetFileName(path)} ({_pdfViewer.CurrectPage + 1} / {_pdfViewer.TotalPages})"; | ||
|
||
container.SetContent(_pdfViewer); | ||
} | ||
|
||
public void Dispose() | ||
{ | ||
GC.SuppressFinalize(this); | ||
|
||
// release the Pdf file first | ||
_pdfViewer?.Dispose(); | ||
_pdfViewer = null; | ||
|
||
try | ||
{ | ||
File.Delete(_pdfPath); | ||
} | ||
catch (Exception) | ||
{ | ||
// ignored | ||
} | ||
} | ||
|
||
~PluginInterface() | ||
{ | ||
Dispose(); | ||
} | ||
} | ||
} |
Oops, something went wrong.