Skip to content

Commit 66b9714

Browse files
author
Graham Downs
committed
I #7: Handle exception when encryption not supported by PdfSharp
There is no version of PDF Sharp which will support the encryption algorithm used in a certain file. A human-readable message is at least preferable to a crash.
1 parent 8d7ea69 commit 66b9714

File tree

4 files changed

+50
-3
lines changed

4 files changed

+50
-3
lines changed
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
using System;
2+
3+
namespace SplitPdf.Engine
4+
{
5+
public class EncryptedPdfException : Exception
6+
{
7+
public string FileName { get; set; }
8+
9+
public EncryptedPdfException(string fileName)
10+
: base($"The PDF document, \"{fileName}\", was encrypted using an algorithm not yet " +
11+
"supported by this program.\r\n" +
12+
"\r\n" +
13+
"Open the file and \"PDF Print\" it. Then try this operation again.")
14+
{
15+
FileName = fileName;
16+
}
17+
}
18+
}

SplitPdf.Engine/Runner.cs

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ public void Run(List<string> inputFiles, string mergeOutputFile)
3232

3333
private void DoSplit(string file)
3434
{
35-
var inputDocument = PdfReader.Open(file, PdfDocumentOpenMode.Import);
35+
var inputDocument = OpenPdfFile(file);
3636
var destFolder = Path.GetDirectoryName(inputDocument.FullPath);
3737
var destFileName = Path.GetFileNameWithoutExtension(file);
3838
var destFileExtension = Path.GetExtension(file);
@@ -49,6 +49,22 @@ private void DoSplit(string file)
4949
}
5050
}
5151

52+
private PdfDocument OpenPdfFile(string file)
53+
{
54+
try
55+
{
56+
return PdfReader.Open(file, PdfDocumentOpenMode.Import);
57+
}
58+
catch (PdfReaderException exception)
59+
{
60+
if (exception.Message != "The PDF document is protected with an encryption not " +
61+
"supported by PDFsharp.")
62+
throw;
63+
64+
throw new EncryptedPdfException(file);
65+
}
66+
}
67+
5268
private void DoMerge(List<string> inputFiles, string mergeOutputFile)
5369
{
5470
var outputDocument = new PdfDocument();

SplitPdf.Engine/SplitPdf.Engine.csproj

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,7 @@
4747
<Compile Include="ArgumentsInterpreter.cs" />
4848
<Compile Include="ArgumentsValidator.cs" />
4949
<Compile Include="ArgumentValidationException.cs" />
50+
<Compile Include="EncryptedPdfException.cs" />
5051
<Compile Include="Properties\AssemblyInfo.cs" />
5152
<Compile Include="Runner.cs" />
5253
<Compile Include="RunnerProgressEventArgs.cs" />

SplitPdf/Program.cs

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,8 +25,7 @@ private static void Main(string[] args)
2525
// If it's time for a check, but it wasn't specifically
2626
// requested by the user, we should still run (and validate
2727
// args) first.
28-
runner.Progress += (sender, e) => Console.WriteLine(e.ProgressMessage);
29-
runner.Run(argumentsInterpreter.InputFiles, argumentsInterpreter.MergeOutputFile);
28+
Run(runner, argumentsInterpreter);
3029

3130
if (Settings.Default.DaysBetweenUpgradeCheck == -1)
3231
return;
@@ -45,6 +44,19 @@ private static void Main(string[] args)
4544
}
4645
}
4746

47+
private static void Run(Runner runner, ArgumentsInterpreter argumentsInterpreter)
48+
{
49+
try
50+
{
51+
runner.Progress += (sender, e) => Console.WriteLine(e.ProgressMessage);
52+
runner.Run(argumentsInterpreter.InputFiles, argumentsInterpreter.MergeOutputFile);
53+
}
54+
catch (EncryptedPdfException exception)
55+
{
56+
Console.WriteLine(exception.Message);
57+
}
58+
}
59+
4860
private static void CheckForUpgrades(
4961
UpgradeRequiredChecker upgradeChecker)
5062
{

0 commit comments

Comments
 (0)