mirror of
https://github.com/QL-Win/QuickLook.git
synced 2025-09-12 02:09:05 +00:00
.
This commit is contained in:
73
QuickLook.Plugin/QuickLook.Plugin.PDFViewer/PdfFile.cs
Normal file
73
QuickLook.Plugin/QuickLook.Plugin.PDFViewer/PdfFile.cs
Normal file
@@ -0,0 +1,73 @@
|
||||
using System;
|
||||
using System.Drawing;
|
||||
using Size = System.Windows.Size;
|
||||
|
||||
namespace QuickLook.Plugin.PDFViewer
|
||||
{
|
||||
public class PdfFile : IDisposable
|
||||
{
|
||||
private readonly IntPtr _ctx;
|
||||
private readonly IntPtr _doc;
|
||||
private readonly IntPtr _stm;
|
||||
|
||||
public PdfFile(string path)
|
||||
{
|
||||
_ctx = LibMuPdf.NativeMethods.NewContext();
|
||||
_stm = LibMuPdf.NativeMethods.OpenFile(_ctx, path);
|
||||
_doc = LibMuPdf.NativeMethods.OpenDocumentStream(_ctx, _stm);
|
||||
|
||||
TotalPages = LibMuPdf.NativeMethods.CountPages(_doc);
|
||||
}
|
||||
|
||||
public int TotalPages { get; }
|
||||
|
||||
public void Dispose()
|
||||
{
|
||||
LibMuPdf.NativeMethods.CloseDocument(_doc);
|
||||
LibMuPdf.NativeMethods.CloseStream(_stm);
|
||||
LibMuPdf.NativeMethods.FreeContext(_ctx);
|
||||
}
|
||||
|
||||
public bool IsLastPage(int pageId)
|
||||
{
|
||||
return pageId >= TotalPages;
|
||||
}
|
||||
|
||||
public Size GetPageSize(int pageId, double zoomFactor)
|
||||
{
|
||||
if (pageId < 0 || pageId >= TotalPages)
|
||||
throw new OverflowException(
|
||||
$"Page id {pageId} should greater or equal than 0 and less than total page count {TotalPages}.");
|
||||
|
||||
var p = LibMuPdf.NativeMethods.LoadPage(_doc, pageId);
|
||||
|
||||
var realSize = new LibMuPdf.Rectangle();
|
||||
LibMuPdf.NativeMethods.BoundPage(_doc, p, ref realSize);
|
||||
|
||||
var size = new Size
|
||||
{
|
||||
Width = realSize.Right * zoomFactor,
|
||||
Height = realSize.Bottom * zoomFactor
|
||||
};
|
||||
|
||||
LibMuPdf.NativeMethods.FreePage(_doc, p);
|
||||
|
||||
return size;
|
||||
}
|
||||
|
||||
public Bitmap GetPage(int pageId, double zoomFactor)
|
||||
{
|
||||
if (pageId < 0 || pageId >= TotalPages)
|
||||
throw new OverflowException(
|
||||
$"Page id {pageId} should greater or equal than 0 and less than total page count {TotalPages}.");
|
||||
|
||||
var p = LibMuPdf.NativeMethods.LoadPage(_doc, pageId);
|
||||
|
||||
var bmp = LibMuPdf.RenderPage(_ctx, _doc, p, zoomFactor);
|
||||
|
||||
LibMuPdf.NativeMethods.FreePage(_doc, p);
|
||||
|
||||
return bmp;
|
||||
}
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user