Skip to content

Commit

Permalink
Support password-protected PDFs (#603)
Browse files Browse the repository at this point in the history
Co-authored-by: Arnaud TAMAILLON <arnaud.tamaillon@younited-credit.fr>
  • Loading branch information
Greybird and Arnaud TAMAILLON authored Jun 28, 2024
1 parent 729f5c2 commit d5aee24
Show file tree
Hide file tree
Showing 11 changed files with 73 additions and 3 deletions.
20 changes: 18 additions & 2 deletions readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ VerifyImageMagick.RegisterPdfToPngConverter();
public Task VerifyPdf() =>
VerifyFile("sample.pdf");
```
<sup><a href='/src/Tests/Samples.cs#L24-L30' title='Snippet source file'>snippet source</a> | <a href='#snippet-VerifyPdf' title='Start of snippet'>anchor</a></sup>
<sup><a href='/src/Tests/Samples.cs#L32-L38' title='Snippet source file'>snippet source</a> | <a href='#snippet-VerifyPdf' title='Start of snippet'>anchor</a></sup>
<!-- endSnippet -->


Expand All @@ -70,7 +70,7 @@ public Task VerifyPdfStream()
return Verify(stream, "pdf");
}
```
<sup><a href='/src/Tests/Samples.cs#L32-L41' title='Snippet source file'>snippet source</a> | <a href='#snippet-VerifyPdfStream' title='Start of snippet'>anchor</a></sup>
<sup><a href='/src/Tests/Samples.cs#L40-L49' title='Snippet source file'>snippet source</a> | <a href='#snippet-VerifyPdfStream' title='Start of snippet'>anchor</a></sup>
<!-- endSnippet -->


Expand Down Expand Up @@ -121,6 +121,22 @@ public Task BackgroundColor() =>
<!-- endSnippet -->



### Open password-protected PDFs

For images with a transparent background, that background can be overridden:

<!-- snippet: PdfPassword -->
<a id='snippet-PdfPassword'></a>
```cs
[Test]
public Task PdfPassword() =>
VerifyFile("password.pdf")
.ImageMagickPdfPassword("password");
```
<sup><a href='/src/Tests/Samples.cs#L23-L30' title='Snippet source file'>snippet source</a> | <a href='#snippet-PdfPassword' title='Start of snippet'>anchor</a></sup>
<!-- endSnippet -->

## Icon

[Swirl](https://thenounproject.com/term/wizard/2744075/) designed by [Philipp Petzka](https://thenounproject.com/masteroficon) from [The Noun Project](https://thenounproject.com/).
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.
12 changes: 12 additions & 0 deletions src/Tests/PasswordSamples.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
#if DEBUG

[TestFixture]
public class PasswordSamples
{
[Test]
public Task PasswordSample() =>
VerifyFile("password.pdf")
.ImageMagickPdfPassword("password");
}

#endif
Binary file added src/Tests/Samples.PdfPassword#00.verified.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added src/Tests/Samples.PdfPassword#01.verified.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
10 changes: 9 additions & 1 deletion src/Tests/Samples.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
#if DEBUG
#if DEBUG

[TestFixture]
public class Samples
Expand All @@ -20,6 +20,14 @@ public Task BackgroundColor() =>

#endregion

#region PdfPassword

[Test]
public Task PdfPassword() =>
VerifyFile("password.pdf")
.ImageMagickPdfPassword("password");

#endregion

#region VerifyPdf

Expand Down
3 changes: 3 additions & 0 deletions src/Tests/Tests.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -19,5 +19,8 @@
<None Update="transparent.*">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</None>
<None Update="password.*">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</None>
</ItemGroup>
</Project>
Binary file added src/Tests/password.pdf
Binary file not shown.
21 changes: 21 additions & 0 deletions src/Verify.ImageMagick/ImageMagickSettings.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ namespace VerifyTestsImageMagick;
public static class ImageMagickSettings
{
static MagickColor? background;
static string? pdfPassword;

public static void ImageMagickBackground(MagickColor color) =>
background = color;
Expand Down Expand Up @@ -68,4 +69,24 @@ public static SettingsTask ImageMagickBackground(this SettingsTask settings, Mag

return background;
}


public static void ImageMagickPdfPassword(this VerifySettings settings, string password) =>
settings.Context["ImageMagick.PdfPassword"] = password;

public static SettingsTask ImageMagickPdfPassword(this SettingsTask settings, string password)
{
settings.CurrentSettings.ImageMagickPdfPassword(password);
return settings;
}

internal static string? PdfPassword(this IReadOnlyDictionary<string, object> context)
{
if (context.TryGetValue("ImageMagick.PdfPassword", out var value))
{
return (string?) value;
}

return pdfPassword;
}
}
10 changes: 10 additions & 0 deletions src/Verify.ImageMagick/VerifyImageMagick_Pdf.cs
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
using ImageMagick.Formats;

namespace VerifyTests;

public static partial class VerifyImageMagick
Expand All @@ -15,6 +17,14 @@ static ConversionResult Convert(Stream stream, IReadOnlyDictionary<string, objec
var streams = new List<Stream>();
var magickSettings = context.MagickReadSettings();
magickSettings.Format = magickFormat;
var password = context.PdfPassword();
if (password != null)
{
magickSettings.SetDefines(new PdfReadDefines
{
Password = password
});
}
using var images = new MagickImageCollection();
images.Read(stream, magickSettings);
var count = images.Count;
Expand Down

0 comments on commit d5aee24

Please sign in to comment.