2021-09-14 18:01:45 +02:00
|
|
|
|
// Copyright (c) Microsoft Corporation
|
|
|
|
|
|
// The Microsoft Corporation licenses this file to you under the MIT license.
|
|
|
|
|
|
// See the LICENSE file in the project root for more information.
|
|
|
|
|
|
using System;
|
|
|
|
|
|
using System.Drawing;
|
|
|
|
|
|
using System.IO;
|
|
|
|
|
|
using System.Runtime.InteropServices;
|
|
|
|
|
|
using System.Runtime.InteropServices.ComTypes;
|
|
|
|
|
|
using Common.ComInterlop;
|
|
|
|
|
|
using Common.Utilities;
|
|
|
|
|
|
using Windows.Data.Pdf;
|
|
|
|
|
|
using Windows.Storage.Streams;
|
|
|
|
|
|
|
|
|
|
|
|
namespace Microsoft.PowerToys.ThumbnailHandler.Pdf
|
|
|
|
|
|
{
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// PDF Thumbnail Provider.
|
|
|
|
|
|
/// </summary>
|
2022-12-14 13:37:23 +01:00
|
|
|
|
public class PdfThumbnailProvider
|
2021-09-14 18:01:45 +02:00
|
|
|
|
{
|
2022-12-14 13:37:23 +01:00
|
|
|
|
public PdfThumbnailProvider(string filePath)
|
|
|
|
|
|
{
|
|
|
|
|
|
FilePath = filePath;
|
|
|
|
|
|
Stream = new FileStream(filePath, FileMode.Open, FileAccess.Read);
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// Gets the file path to the file creating thumbnail for.
|
|
|
|
|
|
/// </summary>
|
|
|
|
|
|
public string FilePath { get; private set; }
|
|
|
|
|
|
|
2021-09-14 18:01:45 +02:00
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// Gets the stream object to access file.
|
|
|
|
|
|
/// </summary>
|
2022-12-14 13:37:23 +01:00
|
|
|
|
public Stream Stream { get; private set; }
|
2021-09-14 18:01:45 +02:00
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// The maximum dimension (width or height) thumbnail we will generate.
|
|
|
|
|
|
/// </summary>
|
|
|
|
|
|
private const uint MaxThumbnailSize = 10000;
|
|
|
|
|
|
|
2022-12-14 13:37:23 +01:00
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// Generate thumbnail bitmap for provided Pdf file/stream.
|
|
|
|
|
|
/// </summary>
|
|
|
|
|
|
/// <param name="cx">Maximum thumbnail size, in pixels.</param>
|
|
|
|
|
|
/// <returns>Generated bitmap</returns>
|
|
|
|
|
|
public Bitmap GetThumbnail(uint cx)
|
2021-09-14 18:01:45 +02:00
|
|
|
|
{
|
|
|
|
|
|
if (cx == 0 || cx > MaxThumbnailSize)
|
|
|
|
|
|
{
|
2022-12-14 13:37:23 +01:00
|
|
|
|
return null;
|
2021-09-14 18:01:45 +02:00
|
|
|
|
}
|
|
|
|
|
|
|
2022-10-26 14:02:31 +01:00
|
|
|
|
if (global::PowerToys.GPOWrapper.GPOWrapper.GetConfiguredPdfThumbnailsEnabledValue() == global::PowerToys.GPOWrapper.GpoRuleConfigured.Disabled)
|
|
|
|
|
|
{
|
|
|
|
|
|
// GPO is disabling this utility.
|
2022-12-14 13:37:23 +01:00
|
|
|
|
return null;
|
2022-10-26 14:02:31 +01:00
|
|
|
|
}
|
|
|
|
|
|
|
2021-09-14 18:01:45 +02:00
|
|
|
|
using var memStream = new MemoryStream();
|
|
|
|
|
|
|
2022-12-14 13:37:23 +01:00
|
|
|
|
this.Stream.CopyTo(memStream);
|
2021-09-14 18:01:45 +02:00
|
|
|
|
memStream.Position = 0;
|
|
|
|
|
|
|
|
|
|
|
|
// AsRandomAccessStream() extension method from System.Runtime.WindowsRuntime
|
|
|
|
|
|
var pdf = PdfDocument.LoadFromStreamAsync(memStream.AsRandomAccessStream()).GetAwaiter().GetResult();
|
|
|
|
|
|
|
|
|
|
|
|
if (pdf.PageCount > 0)
|
|
|
|
|
|
{
|
|
|
|
|
|
using var page = pdf.GetPage(0);
|
|
|
|
|
|
|
|
|
|
|
|
var image = PageToImage(page, cx);
|
|
|
|
|
|
|
|
|
|
|
|
using Bitmap thumbnail = new Bitmap(image);
|
|
|
|
|
|
|
2022-12-14 13:37:23 +01:00
|
|
|
|
return (Bitmap)thumbnail.Clone();
|
2021-09-14 18:01:45 +02:00
|
|
|
|
}
|
2022-12-14 13:37:23 +01:00
|
|
|
|
|
|
|
|
|
|
return null;
|
2021-09-14 18:01:45 +02:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// Transform the PdfPage to an Image.
|
|
|
|
|
|
/// </summary>
|
|
|
|
|
|
/// <param name="page">The page to transform to an Image.</param>
|
|
|
|
|
|
/// <param name="height">The height of the page.</param>
|
|
|
|
|
|
/// <returns>An object of type <see cref="Image"/></returns>
|
|
|
|
|
|
private static Image PageToImage(PdfPage page, uint height)
|
|
|
|
|
|
{
|
|
|
|
|
|
Image imageOfPage;
|
|
|
|
|
|
|
|
|
|
|
|
using var stream = new InMemoryRandomAccessStream();
|
|
|
|
|
|
|
|
|
|
|
|
page.RenderToStreamAsync(stream, new PdfPageRenderOptions()
|
|
|
|
|
|
{
|
|
|
|
|
|
DestinationHeight = height,
|
|
|
|
|
|
}).GetAwaiter().GetResult();
|
|
|
|
|
|
|
|
|
|
|
|
imageOfPage = Image.FromStream(stream.AsStream());
|
|
|
|
|
|
|
|
|
|
|
|
return imageOfPage;
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|