Skip to content

Commit

Permalink
Auto resize PDF viewer window
Browse files Browse the repository at this point in the history
  • Loading branch information
emako committed Dec 13, 2024
1 parent cb59a3d commit a82a93d
Showing 1 changed file with 17 additions and 1 deletion.
18 changes: 17 additions & 1 deletion QuickLook.Plugin/QuickLook.Plugin.PDFViewer/Plugin.cs
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,11 @@
// along with this program. If not, see <http://www.gnu.org/licenses/>.

using PdfiumViewer;
using QuickLook.Common.Helpers;
using QuickLook.Common.Plugin;
using System;
using System.IO;
using System.Reflection;
using System.Runtime.ExceptionServices;
using System.Text;
using System.Windows;
Expand Down Expand Up @@ -98,9 +100,23 @@ public void View(string path, ContextObject context)
{
var desiredSize = PdfViewerControl.GetDesiredControlSizeByFirstPage(path, password);
context.SetPreferredSizeFit(desiredSize, 0.9); // Actually it is no longer effective here

context.ViewerContent = _pdfControl;

// Call the viewer window private method using reflection
// QuickLook.ViewerWindow.ResizeAndCentreExistingWindow
if (Window.GetWindow(_pdfControl) is Window window)
{
var ResizeAndCentreExistingWindow = window.GetType().GetMethod("ResizeAndCentreExistingWindow",
BindingFlags.NonPublic | BindingFlags.Instance);

if (ResizeAndCentreExistingWindow != null)
{
var newRect = (Rect)ResizeAndCentreExistingWindow.Invoke(window, [context.PreferredSize]);

window.MoveWindow(newRect.Left, newRect.Top, newRect.Width, newRect.Height);
}
}

context.IsBusy = true;
_pdfControl.LoadPdf(path, password);

Expand Down

0 comments on commit a82a93d

Please sign in to comment.