[FileExplorer] Add PDF Thumbnail Provider for Windows Explorer (#13058)

* Add PdfPreviewHandler to build dependency of runner

* PDF Thumbnail Provider

* Remove using brackets

* Pdf Thumbnail - Settings and Unit Tests

* Removed resx

* Add PDF Thumbnail Provider binary

* Install Pdf Thumbnail Provider

* Fix pagee spelling error.

* Update Windows dependency to version 10.0.18362.0 because that is the minimal required version of the PowerToys.

* Add Pdf Preview Handler and Pdf Thumbnail Provider
This commit is contained in:
R. de Veen
2021-09-14 18:01:45 +02:00
committed by GitHub
parent 7e22f26b52
commit 7ac1e00d01
20 changed files with 487 additions and 9 deletions

View File

@@ -65,7 +65,7 @@
<ItemGroup>
<Reference Include="Windows">
<HintPath>$(MSBuildProgramFiles32)\Windows Kits\10\UnionMetadata\10.0.17134.0\Windows.winmd</HintPath>
<HintPath>$(MSBuildProgramFiles32)\Windows Kits\10\UnionMetadata\10.0.18362.0\Windows.winmd</HintPath>
<IsWinMDFile>true</IsWinMDFile>
</Reference>
</ItemGroup>

View File

@@ -0,0 +1,96 @@
// 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>
[Guid("BCC13D15-9720-4CC4-8371-EA74A274741E")]
[ClassInterface(ClassInterfaceType.None)]
[ComVisible(true)]
public class PdfThumbnailProvider : IInitializeWithStream, IThumbnailProvider
{
/// <summary>
/// Gets the stream object to access file.
/// </summary>
public IStream Stream { get; private set; }
/// <summary>
/// The maximum dimension (width or height) thumbnail we will generate.
/// </summary>
private const uint MaxThumbnailSize = 10000;
/// <inheritdoc/>
public void Initialize(IStream pstream, uint grfMode)
{
// Ignore the grfMode always use read mode to access the file.
this.Stream = pstream;
}
/// <inheritdoc/>
public void GetThumbnail(uint cx, out IntPtr phbmp, out WTS_ALPHATYPE pdwAlpha)
{
phbmp = IntPtr.Zero;
pdwAlpha = WTS_ALPHATYPE.WTSAT_UNKNOWN;
if (cx == 0 || cx > MaxThumbnailSize)
{
return;
}
using var dataStream = new ReadonlyStream(this.Stream as IStream);
using var memStream = new MemoryStream();
dataStream.CopyTo(memStream);
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);
phbmp = thumbnail.GetHbitmap();
pdwAlpha = WTS_ALPHATYPE.WTSAT_RGB;
}
}
/// <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;
}
}
}

View File

@@ -0,0 +1,71 @@
<Project Sdk="Microsoft.NET.Sdk.WindowsDesktop">
<PropertyGroup>
<Platforms>x64</Platforms>
<UseWindowsForms>true</UseWindowsForms>
<ProjectGuid>{11491FD8-F921-48BF-880C-7FEA185B80A1}</ProjectGuid>
<RootNamespace>Microsoft.PowerToys.ThumbnailHandler.Pdf</RootNamespace>
<AssemblyName>PdfThumbnailProvider</AssemblyName>
<AssemblyTitle>PdfThumbnailProvider</AssemblyTitle>
<AssemblyDescription>PowerToys PdfPreviewHandler</AssemblyDescription>
<AssemblyCompany>Microsoft Corporation</AssemblyCompany>
<AssemblyCopyright>Copyright (C) 2020 Microsoft Corporation</AssemblyCopyright>
<AssemblyProduct>PowerToys</AssemblyProduct>
<TargetFramework>netcoreapp3.1</TargetFramework>
<EnableComHosting>true</EnableComHosting>
<AutoGenerateBindingRedirects>true</AutoGenerateBindingRedirects>
<Company>Microsoft Corporation</Company>
<Product>PowerToys</Product>
<NeutralLanguage>en-US</NeutralLanguage>
<Description>PowerToys PdfPreviewHandler</Description>
<Copyright>Copyright (C) 2020 Microsoft Corporation</Copyright>
<OutputPath>$(SolutionDir)$(Platform)\$(Configuration)\modules\FileExplorerPreview\</OutputPath>
<TreatWarningsAsErrors>true</TreatWarningsAsErrors>
<AppendTargetFrameworkToOutputPath>false</AppendTargetFrameworkToOutputPath>
<AppendRuntimeIdentifierToOutputPath>false</AppendRuntimeIdentifierToOutputPath>
<CopyLocalLockFileAssemblies>true</CopyLocalLockFileAssemblies>
</PropertyGroup>
<Import Project="..\..\..\Version.props" />
<ItemGroup>
<PackageReference Include="Microsoft.CodeAnalysis.FxCopAnalyzers" Version="3.3.0">
<PrivateAssets>all</PrivateAssets>
</PackageReference>
<PackageReference Include="Microsoft.CodeAnalysis.VersionCheckAnalyzer" Version="3.3.0">
<PrivateAssets>all</PrivateAssets>
</PackageReference>
<PackageReference Include="Microsoft.CodeQuality.Analyzers" Version="3.3.0">
<PrivateAssets>all</PrivateAssets>
</PackageReference>
<PackageReference Include="Microsoft.NetCore.Analyzers" Version="3.3.0">
<PrivateAssets>all</PrivateAssets>
</PackageReference>
<PackageReference Include="Microsoft.NetFramework.Analyzers" Version="3.3.0">
<PrivateAssets>all</PrivateAssets>
</PackageReference>
<PackageReference Include="StyleCop.Analyzers" Version="1.1.118">
<PrivateAssets>all</PrivateAssets>
</PackageReference>
<PackageReference Include="System.Runtime.WindowsRuntime" Version="4.7.0" />
</ItemGroup>
<ItemGroup>
<Compile Include="..\..\..\codeAnalysis\GlobalSuppressions.cs" Link="GlobalSuppressions.cs" />
</ItemGroup>
<ItemGroup>
<ProjectReference Include="..\..\..\common\ManagedTelemetry\Telemetry\ManagedTelemetry.csproj" />
<ProjectReference Include="..\Common\PreviewHandlerCommon.csproj" />
</ItemGroup>
<ItemGroup>
<AdditionalFiles Include="..\..\..\codeAnalysis\StyleCop.json">
<Link>StyleCop.json</Link>
</AdditionalFiles>
</ItemGroup>
<ItemGroup>
<Reference Include="Windows">
<HintPath>$(MSBuildProgramFiles32)\Windows Kits\10\UnionMetadata\10.0.18362.0\Windows.winmd</HintPath>
<IsWinMDFile>true</IsWinMDFile>
</Reference>
</ItemGroup>
</Project>

View File

@@ -12,7 +12,6 @@ using System.Runtime.InteropServices.ComTypes;
using System.Windows.Forms;
using Common.ComInterlop;
using Common.Utilities;
using Microsoft.PowerToys.Telemetry;
using PreviewHandlerCommon;
namespace Microsoft.PowerToys.ThumbnailHandler.Svg

View File

@@ -0,0 +1,93 @@
// 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 System.Text;
using Common.ComInterlop;
using Microsoft.PowerToys.STATestExtension;
using Microsoft.PowerToys.ThumbnailHandler.Pdf;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using Moq;
namespace PdfThumbnailProviderUnitTests
{
[STATestClass]
public class PdfThumbnailProviderTests
{
[TestMethod]
public void GetThumbnailValidStreamPDF()
{
// Act
var file = File.ReadAllBytes("HelperFiles/sample.pdf");
PdfThumbnailProvider provider = new PdfThumbnailProvider();
provider.Initialize(GetMockStream(file), 0);
provider.GetThumbnail(256, out IntPtr bitmap, out WTS_ALPHATYPE alphaType);
Assert.IsTrue(bitmap != IntPtr.Zero);
Assert.IsTrue(alphaType == WTS_ALPHATYPE.WTSAT_RGB);
}
[TestMethod]
public void GetThumbnailInValidSizePDF()
{
// Act
var file = File.ReadAllBytes("HelperFiles/sample.pdf");
PdfThumbnailProvider provider = new PdfThumbnailProvider();
provider.Initialize(GetMockStream(file), 0);
provider.GetThumbnail(0, out IntPtr bitmap, out WTS_ALPHATYPE alphaType);
Assert.IsTrue(bitmap == IntPtr.Zero);
Assert.IsTrue(alphaType == WTS_ALPHATYPE.WTSAT_UNKNOWN);
}
[TestMethod]
public void GetThumbnailToBigPDF()
{
// Act
var file = File.ReadAllBytes("HelperFiles/sample.pdf");
PdfThumbnailProvider provider = new PdfThumbnailProvider();
provider.Initialize(GetMockStream(file), 0);
provider.GetThumbnail(10001, out IntPtr bitmap, out WTS_ALPHATYPE alphaType);
Assert.IsTrue(bitmap == IntPtr.Zero);
Assert.IsTrue(alphaType == WTS_ALPHATYPE.WTSAT_UNKNOWN);
}
private static IStream GetMockStream(byte[] sourceArray)
{
var streamMock = new Mock<IStream>();
var firstCall = true;
streamMock
.Setup(x => x.Read(It.IsAny<byte[]>(), It.IsAny<int>(), It.IsAny<IntPtr>()))
.Callback<byte[], int, IntPtr>((buffer, countToRead, bytesReadPtr) =>
{
if (firstCall)
{
Array.Copy(sourceArray, 0, buffer, 0, sourceArray.Length);
Marshal.WriteInt32(bytesReadPtr, sourceArray.Length);
firstCall = false;
}
else
{
Marshal.WriteInt32(bytesReadPtr, 0);
}
});
return streamMock.Object;
}
}
}

View File

@@ -0,0 +1,78 @@
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<Platforms>x64</Platforms>
<AssemblyTitle>UnitTests-PdfThumbnailProvider</AssemblyTitle>
<AssemblyDescription>PowerToys UnitTests-PdfThumbnailProvider</AssemblyDescription>
<AssemblyCompany>Microsoft Corporation</AssemblyCompany>
<AssemblyCopyright>Copyright (C) 2021 Microsoft Corporation</AssemblyCopyright>
<AssemblyProduct>PowerToys</AssemblyProduct>
<AssemblyTitle>UnitTests-PdfThumbnailProvider</AssemblyTitle>
<Company>Microsoft Corporation</Company>
<Product>PowerToys</Product>
<NeutralLanguage>en-US</NeutralLanguage>
<Description>PowerToys UnitTests-PdfThumbnailProvider</Description>
<Copyright>Copyright (C) 2021 Microsoft Corporation</Copyright>
</PropertyGroup>
<PropertyGroup>
<ProjectGuid>{F40C3397-1834-4530-B2D9-8F8B8456BCDF}</ProjectGuid>
<RootNamespace>PdfThumbnailProviderUnitTests</RootNamespace>
<TargetFramework>netcoreapp3.1</TargetFramework>
<ProjectTypeGuids>{3AC096D0-A1C2-E12C-1390-A8335801FDAB};{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}</ProjectTypeGuids>
<VSToolsPath Condition="'$(VSToolsPath)' == ''">$(MSBuildExtensionsPath32)\Microsoft\VisualStudio\v$(VisualStudioVersion)</VSToolsPath>
<ReferencePath>$(ProgramFiles)\Common Files\microsoft shared\VSTT\$(VisualStudioVersion)\UITestExtensionPackages</ReferencePath>
<IsCodedUITest>False</IsCodedUITest>
<TestProjectType>UnitTest</TestProjectType>
</PropertyGroup>
<Import Project="..\..\..\Version.props" />
<ItemGroup>
<None Remove="HelperFiles\sample.pdf" />
</ItemGroup>
<ItemGroup>
<PackageReference Include="Castle.Core" Version="4.4.1" />
<PackageReference Include="Microsoft.CodeAnalysis.FxCopAnalyzers" Version="3.3.0">
<PrivateAssets>all</PrivateAssets>
</PackageReference>
<PackageReference Include="Microsoft.CodeAnalysis.VersionCheckAnalyzer" Version="3.3.0">
<PrivateAssets>all</PrivateAssets>
</PackageReference>
<PackageReference Include="Microsoft.CodeQuality.Analyzers" Version="3.3.0">
<PrivateAssets>all</PrivateAssets>
</PackageReference>
<PackageReference Include="Microsoft.NetCore.Analyzers" Version="3.3.0">
<PrivateAssets>all</PrivateAssets>
</PackageReference>
<PackageReference Include="Microsoft.NetFramework.Analyzers" Version="3.3.0">
<PrivateAssets>all</PrivateAssets>
</PackageReference>
<PackageReference Include="Moq" Version="4.16.1" />
<PackageReference Include="MSTest.TestAdapter" Version="2.2.5" />
<PackageReference Include="MSTest.TestFramework" Version="2.2.5" />
<PackageReference Include="StyleCop.Analyzers" Version="1.1.118">
<PrivateAssets>all</PrivateAssets>
</PackageReference>
<PackageReference Include="System.Runtime.CompilerServices.Unsafe" Version="5.0.0" />
<PackageReference Include="System.Threading.Tasks.Extensions" Version="4.5.4" />
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="16.10.0" />
</ItemGroup>
<ItemGroup>
<ProjectReference Include="..\Common\PreviewHandlerCommon.csproj" />
<ProjectReference Include="..\PdfThumbnailProvider\PdfThumbnailProvider.csproj" />
</ItemGroup>
<ItemGroup>
<Compile Include="..\..\..\codeAnalysis\GlobalSuppressions.cs" Link="GlobalSuppressions.cs" />
<Compile Include="..\STATestClassAttribute.cs" Link="STATestClassAttribute.cs" />
<Compile Include="..\STATestMethodAttribute.cs" Link="STATestMethodAttribute.cs" />
<AdditionalFiles Include="..\..\..\codeAnalysis\StyleCop.json">
<Link>StyleCop.json</Link>
</AdditionalFiles>
</ItemGroup>
<ItemGroup>
<Content Include="HelperFiles\sample.pdf">
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
</Content>
</ItemGroup>
</Project>

View File

@@ -0,0 +1,15 @@
<?xml version="1.0" encoding="utf-8"?>
<configuration>
<runtime>
<assemblyBinding xmlns="urn:schemas-microsoft-com:asm.v1">
<dependentAssembly>
<assemblyIdentity name="System.Runtime.CompilerServices.Unsafe" publicKeyToken="b03f5f7f11d50a3a" culture="neutral" />
<bindingRedirect oldVersion="0.0.0.0-4.0.6.0" newVersion="4.0.6.0" />
</dependentAssembly>
<dependentAssembly>
<assemblyIdentity name="System.Threading.Tasks.Extensions" publicKeyToken="cc7b13ffcd2ddd51" culture="neutral" />
<bindingRedirect oldVersion="0.0.0.0-4.2.0.1" newVersion="4.2.0.1" />
</dependentAssembly>
</assemblyBinding>
</runtime>
</configuration>

View File

@@ -28,6 +28,9 @@ const CLSID CLSID_SHIMActivateSvgThumbnailProvider = { 0x9C723B8C, 0x4F5C, 0x414
// 36B27788-A8BB-4698-A756-DF9F11F64F84
const CLSID CLSID_SvgThumbnailProvider = { 0x36B27788, 0xA8BB, 0x4698, { 0xA7, 0x56, 0xDF, 0x9F, 0x11, 0xF6, 0x4F, 0x84 } };
// BCC13D15-9720-4CC4-8371-EA74A274741E
const GUID CLSID_PdfThumbnailProvider = { 0xbcc13d15, 0x9720, 0x4cc4, { 0x83, 0x71, 0xea, 0x74, 0xa2, 0x74, 0x74, 0x1e } };
// Pairs of NativeClsid vs ManagedClsid used for preview handlers.
const std::vector<std::pair<CLSID, CLSID>> NativeToManagedClsid({
{ CLSID_SHIMActivateMdPreviewHandler, CLSID_MdPreviewHandler },

View File

@@ -174,4 +174,7 @@
<data name="Prevpane_Pdf_Settings_Displayname" xml:space="preserve">
<value>PDF Previewer</value>
</data>
<data name="Pdf_Thumbnail_Provider_Settings_Description" xml:space="preserve">
<value>Pdf Thumbnail Provider</value>
</data>
</root>

View File

@@ -50,6 +50,16 @@ PowerPreviewModule::PowerPreviewModule() :
std::make_unique<RegistryWrapper>(),
L".svg\\shellex\\{E357FCCD-A995-4576-B01F-234630154E96}"));
// PDF
m_fileExplorerModules.emplace_back(std::make_unique<ThumbnailProviderSettings>(
true,
L"pdf-thumbnail-toggle-setting",
GET_RESOURCE_STRING(IDS_PDF_THUMBNAIL_PROVIDER_SETTINGS_DESCRIPTION),
L"{BCC13D15-9720-4CC4-8371-EA74A274741E}",
L"Pdf Thumbnail Provider",
std::make_unique<RegistryWrapper>(),
L".pdf\\shellex\\{E357FCCD-A995-4576-B01F-234630154E96}"));
// Initialize the toggle states for each module.
init_settings();