Skip to content

Commit

Permalink
Fix the interactions between Bounds and Width/Height (#77)
Browse files Browse the repository at this point in the history
* Fix the interactions between Bounds and Width/Height (ignoring DPI)

* Set version to 4.0.1
  • Loading branch information
sungaila authored Mar 14, 2024
1 parent 87335f3 commit 634cabb
Show file tree
Hide file tree
Showing 113 changed files with 460 additions and 24 deletions.
2 changes: 1 addition & 1 deletion .github/ISSUE_TEMPLATE/bug_report.yml
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ body:
attributes:
label: PDFtoImage version
description: Which version of PDFtoImage is affected?
value: 4.0.0
value: 4.0.1
validations:
required: true
- type: dropdown
Expand Down
2 changes: 1 addition & 1 deletion src/Console/Console.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
<AssemblyName>PDFtoImage.Console</AssemblyName>
<RootNamespace>PDFtoImage.Console</RootNamespace>
<StartupObject>PDFtoImage.Console.Program</StartupObject>
<Version>4.0.0</Version>
<Version>4.0.1</Version>
<Configurations>Debug;Release;ReleaseSigned</Configurations>
</PropertyGroup>

Expand Down
4 changes: 2 additions & 2 deletions src/FrameworkTests/MauiApp/MauiApp.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -50,8 +50,8 @@

<ItemGroup>
<PackageReference Include="Microsoft.Extensions.Logging.Debug" Version="8.0.0" />
<PackageReference Include="Microsoft.Maui.Controls" Version="8.0.7" />
<PackageReference Include="Microsoft.Maui.Controls.Compatibility" Version="8.0.7" />
<PackageReference Include="Microsoft.Maui.Controls" Version="8.0.10" />
<PackageReference Include="Microsoft.Maui.Controls.Compatibility" Version="8.0.10" />
</ItemGroup>

<ItemGroup>
Expand Down
24 changes: 17 additions & 7 deletions src/PDFtoImage/Internals/PdfDocument.cs
Original file line number Diff line number Diff line change
Expand Up @@ -73,9 +73,13 @@ public SKBitmap Render(int page, float width, float height, float dpiX, float dp
if (_disposed)
throw new ObjectDisposedException(GetType().Name);

var originalWidth = PageSizes[page].Width;
var originalHeight = PageSizes[page].Height;

if (rotate == PdfRotation.Rotate90 || rotate == PdfRotation.Rotate270)
{
(width, height) = (height, width);
(originalWidth, originalHeight) = (originalHeight, originalWidth);
(dpiX, dpiY) = (dpiY, dpiX);
}

Expand All @@ -84,6 +88,9 @@ public SKBitmap Render(int page, float width, float height, float dpiX, float dp
width *= dpiX / 72f;
height *= dpiY / 72f;

originalWidth *= dpiX / 72f;
originalHeight *= dpiY / 72f;

if (bounds != null)
{
bounds = new RectangleF(
Expand All @@ -97,10 +104,13 @@ public SKBitmap Render(int page, float width, float height, float dpiX, float dp

if (bounds != null)
{
var factorX = width / originalWidth;
var factorY = height / originalHeight;

if (rotate == PdfRotation.Rotate90)
{
bounds = new RectangleF(
width - bounds.Value.Height - bounds.Value.Y,
((originalWidth - bounds.Value.Height) * factorX) - bounds.Value.Y,
bounds.Value.X,
bounds.Value.Height,
bounds.Value.Width
Expand All @@ -110,16 +120,16 @@ public SKBitmap Render(int page, float width, float height, float dpiX, float dp
{
bounds = new RectangleF(
bounds.Value.Y,
height - bounds.Value.Width - bounds.Value.X,
((originalHeight - bounds.Value.Width) * factorY) - bounds.Value.X,
bounds.Value.Height,
bounds.Value.Width
);
}
else if (rotate == PdfRotation.Rotate180)
{
bounds = new RectangleF(
width - bounds.Value.Width - bounds.Value.X,
height - bounds.Value.Height - bounds.Value.Y,
((originalWidth - bounds.Value.Width) * factorX) - bounds.Value.X,
((originalHeight - bounds.Value.Height) * factorY) - bounds.Value.Y,
bounds.Value.Width,
bounds.Value.Height
);
Expand All @@ -133,7 +143,7 @@ public SKBitmap Render(int page, float width, float height, float dpiX, float dp

if (!useTiling || (horizontalTileCount == 1 && verticalTileCount == 1))
{
bitmap = RenderSubset(_file!, page, width, height, rotate, flags, renderFormFill, backgroundColor, bounds, width, height, cancellationToken);
bitmap = RenderSubset(_file!, page, width, height, rotate, flags, renderFormFill, backgroundColor, bounds, originalWidth, originalHeight, cancellationToken);
}
else
{
Expand All @@ -143,8 +153,8 @@ public SKBitmap Render(int page, float width, float height, float dpiX, float dp

float currentTileWidth = width / horizontalTileCount;
float currentTileHeight = height / verticalTileCount;
float boundsWidthFactor = bounds != null ? bounds.Value.Width / width : 0f;
float boundsHeightFactor = bounds != null ? bounds.Value.Height / height : 0f;
float boundsWidthFactor = bounds != null ? bounds.Value.Width / originalWidth : 0f;
float boundsHeightFactor = bounds != null ? bounds.Value.Height / originalHeight : 0f;

using var canvas = new SKCanvas(bitmap);
canvas.Clear(backgroundColor);
Expand Down
16 changes: 7 additions & 9 deletions src/PDFtoImage/PDFtoImage.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@

<!-- NuGet -->
<PropertyGroup>
<VersionPrefix>4.0.0</VersionPrefix>
<VersionPrefix>4.0.1</VersionPrefix>
<VersionSuffix></VersionSuffix>
<Authors>David Sungaila</Authors>
<PackageRequireLicenseAcceptance>false</PackageRequireLicenseAcceptance>
Expand All @@ -22,9 +22,7 @@
<PackageProjectUrl>https://github.com/sungaila/PDFtoImage</PackageProjectUrl>
<PackageIconUrl>https://raw.githubusercontent.com/sungaila/PDFtoImage/master/etc/Icon_128.png</PackageIconUrl>
<Description>A .NET library to render PDF files into images.</Description>
<PackageReleaseNotes>- Added optional parameter Bounds.
- Added optional parameter UseTiling.
- RenderOptions introduced for most API calls. This is a breaking change, see README for more information.</PackageReleaseNotes>
<PackageReleaseNotes>- Fixed interactions between parameters Bounds and Width/Height.</PackageReleaseNotes>
<PackageTags>PDF Bitmap Image Convert Conversion C# PDFium SkiaSharp Skia PNG JPG JPEG WEBP Xamarin Android MonoAndroid MAUI wasm WebAssembly</PackageTags>
<RepositoryUrl>https://github.com/sungaila/PDFtoImage.git</RepositoryUrl>
<RepositoryType>git</RepositoryType>
Expand Down Expand Up @@ -75,9 +73,9 @@

<!-- Shared packages -->
<ItemGroup Condition="'$(TargetFramework)'!='net7.0-android' and '$(TargetFramework)'!='net8.0-android' and '$(TargetFramework)'!='monoandroid10.0'">
<PackageReference Include="bblanchon.PDFium.Linux" Version="124.0.6322" PrivateAssets="analyzers" />
<PackageReference Include="bblanchon.PDFium.macOS" Version="124.0.6322" PrivateAssets="analyzers" />
<PackageReference Include="bblanchon.PDFium.Win32" Version="124.0.6322" PrivateAssets="analyzers" />
<PackageReference Include="bblanchon.PDFium.Linux" Version="124.0.6350" PrivateAssets="analyzers" />
<PackageReference Include="bblanchon.PDFium.macOS" Version="124.0.6350" PrivateAssets="analyzers" />
<PackageReference Include="bblanchon.PDFium.Win32" Version="124.0.6350" PrivateAssets="analyzers" />
<PackageReference Include="SkiaSharp.NativeAssets.Linux.NoDependencies" Version="2.88.7" PrivateAssets="analyzers" />
<PackageReference Include="SkiaSharp.NativeAssets.macOS" Version="2.88.7" PrivateAssets="analyzers" />
<PackageReference Include="SkiaSharp.NativeAssets.Win32" Version="2.88.7" PrivateAssets="analyzers" />
Expand All @@ -96,14 +94,14 @@

<!-- Android packages -->
<ItemGroup Condition="'$(TargetFramework)'=='net7.0-android' or '$(TargetFramework)'=='net8.0-android' or '$(TargetFramework)'=='monoandroid10.0'">
<PackageReference Include="bblanchon.PDFium.Android" Version="124.0.6322" PrivateAssets="analyzers" />
<PackageReference Include="bblanchon.PDFium.Android" Version="124.0.6350" PrivateAssets="analyzers" />
<PackageReference Include="SkiaSharp.NativeAssets.Android" Version="2.88.7" PrivateAssets="analyzers" />
</ItemGroup>

<!-- Blazor WebAssembly stuff -->
<ItemGroup Condition="'$(TargetFramework)'=='net7.0' or '$(TargetFramework)'=='net8.0'">
<PackageReference Include="SkiaSharp.NativeAssets.WebAssembly" Version="2.88.7" PrivateAssets="analyzers" />
<PackageReference Include="Sungaila.PDFium.BlazorWebAssembly" Version="124.0.6322" PrivateAssets="analyzers" />
<PackageReference Include="Sungaila.PDFium.BlazorWebAssembly" Version="124.0.6350" PrivateAssets="analyzers" />
</ItemGroup>

<Import Project="PDFtoImage.PropertiesSigning.targets" />
Expand Down
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
68 changes: 68 additions & 0 deletions src/Tests/BoundsTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -188,6 +188,74 @@ public void WithWidthAndHeight(float x, float y, float width, float height, int?
CompareStreams(expectedPath, outputStream);
}

[TestMethod]
[DataRow(0f, 0f, 419.528f, 595.276f, default, default, default)]
[DataRow(0f, 0f, 209.764f, 297.638f, default, default, default)]
[DataRow(209.764f, 0f, 209.764f, 297.638f, default, default, default)]
[DataRow(0f, 297.638f, 209.764f, 297.638f, default, default, default)]
[DataRow(209.764f, 297.638f, 209.764f, 297.638f, default, default, default)]
[DataRow(0f, 0f, 419.528f, 595.276f, 500, 500, default)]
[DataRow(0f, 0f, 209.764f, 297.638f, 500, 500, default)]
[DataRow(209.764f, 0f, 209.764f, 297.638f, 500, 500, default)]
[DataRow(0f, 297.638f, 209.764f, 297.638f, 500, 500, default)]
[DataRow(209.764f, 297.638f, 209.764f, 297.638f, 500, 500, default)]

[DataRow(0f, 0f, 419.528f, 595.276f, default, default, PdfRotation.Rotate0)]
[DataRow(0f, 0f, 209.764f, 297.638f, default, default, PdfRotation.Rotate0)]
[DataRow(209.764f, 0f, 209.764f, 297.638f, default, default, PdfRotation.Rotate0)]
[DataRow(0f, 297.638f, 209.764f, 297.638f, default, default, PdfRotation.Rotate0)]
[DataRow(209.764f, 297.638f, 209.764f, 297.638f, default, default, PdfRotation.Rotate0)]
[DataRow(0f, 0f, 419.528f, 595.276f, 500, 500, PdfRotation.Rotate0)]
[DataRow(0f, 0f, 209.764f, 297.638f, 500, 500, PdfRotation.Rotate0)]
[DataRow(209.764f, 0f, 209.764f, 297.638f, 500, 500, PdfRotation.Rotate0)]
[DataRow(0f, 297.638f, 209.764f, 297.638f, 500, 500, PdfRotation.Rotate0)]
[DataRow(209.764f, 297.638f, 209.764f, 297.638f, 500, 500, PdfRotation.Rotate0)]

[DataRow(0f, 0f, 419.528f, 595.276f, default, default, PdfRotation.Rotate90)]
[DataRow(0f, 0f, 209.764f, 297.638f, default, default, PdfRotation.Rotate90)]
[DataRow(209.764f, 0f, 209.764f, 297.638f, default, default, PdfRotation.Rotate90)]
[DataRow(0f, 297.638f, 209.764f, 297.638f, default, default, PdfRotation.Rotate90)]
[DataRow(209.764f, 297.638f, 209.764f, 297.638f, default, default, PdfRotation.Rotate90)]
[DataRow(0f, 0f, 419.528f, 595.276f, 500, 500, PdfRotation.Rotate90)]
[DataRow(0f, 0f, 209.764f, 297.638f, 500, 500, PdfRotation.Rotate90)]
[DataRow(209.764f, 0f, 209.764f, 297.638f, 500, 500, PdfRotation.Rotate90)]
[DataRow(0f, 297.638f, 209.764f, 297.638f, 500, 500, PdfRotation.Rotate90)]
[DataRow(209.764f, 297.638f, 209.764f, 297.638f, 500, 500, PdfRotation.Rotate90)]

[DataRow(0f, 0f, 419.528f, 595.276f, default, default, PdfRotation.Rotate180)]
[DataRow(0f, 0f, 209.764f, 297.638f, default, default, PdfRotation.Rotate180)]
[DataRow(209.764f, 0f, 209.764f, 297.638f, default, default, PdfRotation.Rotate180)]
[DataRow(0f, 297.638f, 209.764f, 297.638f, default, default, PdfRotation.Rotate180)]
[DataRow(209.764f, 297.638f, 209.764f, 297.638f, default, default, PdfRotation.Rotate180)]
[DataRow(0f, 0f, 419.528f, 595.276f, 500, 500, PdfRotation.Rotate180)]
[DataRow(0f, 0f, 209.764f, 297.638f, 500, 500, PdfRotation.Rotate180)]
[DataRow(209.764f, 0f, 209.764f, 297.638f, 500, 500, PdfRotation.Rotate180)]
[DataRow(0f, 297.638f, 209.764f, 297.638f, 500, 500, PdfRotation.Rotate180)]
[DataRow(209.764f, 297.638f, 209.764f, 297.638f, 500, 500, PdfRotation.Rotate180)]

[DataRow(0f, 0f, 419.528f, 595.276f, default, default, PdfRotation.Rotate270)]
[DataRow(0f, 0f, 209.764f, 297.638f, default, default, PdfRotation.Rotate270)]
[DataRow(209.764f, 0f, 209.764f, 297.638f, default, default, PdfRotation.Rotate270)]
[DataRow(0f, 297.638f, 209.764f, 297.638f, default, default, PdfRotation.Rotate270)]
[DataRow(209.764f, 297.638f, 209.764f, 297.638f, default, default, PdfRotation.Rotate270)]
[DataRow(0f, 0f, 419.528f, 595.276f, 500, 500, PdfRotation.Rotate270)]
[DataRow(0f, 0f, 209.764f, 297.638f, 500, 500, PdfRotation.Rotate270)]
[DataRow(209.764f, 0f, 209.764f, 297.638f, 500, 500, PdfRotation.Rotate270)]
[DataRow(0f, 297.638f, 209.764f, 297.638f, 500, 500, PdfRotation.Rotate270)]
[DataRow(209.764f, 297.638f, 209.764f, 297.638f, 500, 500, PdfRotation.Rotate270)]
public void WithWidthAndHeightAndRotation(float x, float y, float width, float height, int? outputWidth = null, int? outputHeight = null, PdfRotation? rotation = null)
{
var bounds = new RectangleF(x, y, width, height);
var expectedPath = Path.Combine("Assets", "Expected", GetPlatformAsString(), "Bounds", $"Wikimedia_Commons_web_{GetFileName(bounds, rotation, default, default)}_{outputWidth?.ToString() ?? "null"}x{outputHeight?.ToString() ?? "null"}.png");

using var inputStream = GetInputStream(Path.Combine("Assets", "Wikimedia_Commons_web.pdf"));
using var outputStream = CreateOutputStream(expectedPath);

SavePng(outputStream, inputStream, options: new(Width: outputWidth, Height: outputHeight, Bounds: bounds, Rotation: rotation ?? default));

CompareStreams(expectedPath, outputStream);
}

private static string GetFileName(RectangleF? input, PdfRotation? rotation, bool? withAnnotations, bool? withFormFill)
{
if (input == null)
Expand Down
Loading

0 comments on commit 634cabb

Please sign in to comment.