-
Notifications
You must be signed in to change notification settings - Fork 6.3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[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
- Loading branch information
Showing
20 changed files
with
487 additions
and
9 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
96 changes: 96 additions & 0 deletions
96
src/modules/previewpane/PdfThumbnailProvider/PdfThumbnailProvider.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,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; | ||
} | ||
} | ||
} |
71 changes: 71 additions & 0 deletions
71
src/modules/previewpane/PdfThumbnailProvider/PdfThumbnailProvider.csproj
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,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> |
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
Binary file added
BIN
+43.7 KB
src/modules/previewpane/UnitTests-PdfThumbnailProvider/HelperFiles/sample.pdf
Binary file not shown.
Oops, something went wrong.