diff --git a/src/UglyToad.PdfPig.Tests/Integration/GithubIssuesTests.cs b/src/UglyToad.PdfPig.Tests/Integration/GithubIssuesTests.cs index 073203d9..14d705e0 100644 --- a/src/UglyToad.PdfPig.Tests/Integration/GithubIssuesTests.cs +++ b/src/UglyToad.PdfPig.Tests/Integration/GithubIssuesTests.cs @@ -7,6 +7,33 @@ public class GithubIssuesTests { + [Fact] + public void Issue953() + { + // NB: We actually do not fix issue 953 here, but another bug found with the same document. + var path = IntegrationHelpers.GetSpecificTestDocumentPath("FailedToParseContentForPage32.pdf"); + + // Lenient parsing ON + Skip missing fonts + using (var document = PdfDocument.Open(path, new ParsingOptions() { UseLenientParsing = true, SkipMissingFonts = true})) + { + var page = document.GetPage(33); + Assert.Equal(33, page.Number); + Assert.Equal(792, page.Height); + Assert.Equal(612, page.Width); + } + + // Lenient parsing ON + Do not Skip missing fonts + using (var document = PdfDocument.Open(path, new ParsingOptions() { UseLenientParsing = true, SkipMissingFonts = false })) + { + var pageException = Assert.Throws(() => document.GetPage(33)); + Assert.Equal("Could not find the font with name /TT4 in the resource store. It has not been loaded yet.", pageException.Message); + } + + var docException = Assert.Throws(() => + PdfDocument.Open(path, new ParsingOptions() { UseLenientParsing = false, SkipMissingFonts = false })); + Assert.Equal("Could not find dictionary associated with reference in pages kids array: 102 0.", docException.Message); + } + [Fact] public void Issue987() { diff --git a/src/UglyToad.PdfPig.Tests/Integration/SpecificTestDocuments/FailedToParseContentForPage32.pdf b/src/UglyToad.PdfPig.Tests/Integration/SpecificTestDocuments/FailedToParseContentForPage32.pdf new file mode 100644 index 00000000..76830e0d Binary files /dev/null and b/src/UglyToad.PdfPig.Tests/Integration/SpecificTestDocuments/FailedToParseContentForPage32.pdf differ diff --git a/src/UglyToad.PdfPig/Content/IResourceStore.cs b/src/UglyToad.PdfPig/Content/IResourceStore.cs index eb499a4f..cfb34883 100644 --- a/src/UglyToad.PdfPig/Content/IResourceStore.cs +++ b/src/UglyToad.PdfPig/Content/IResourceStore.cs @@ -35,7 +35,7 @@ /// /// Get the extended graphics state dictionary corresponding to the name. /// - DictionaryToken GetExtendedGraphicsStateDictionary(NameToken name); + DictionaryToken? GetExtendedGraphicsStateDictionary(NameToken name); /// /// Get the font from the . diff --git a/src/UglyToad.PdfPig/Content/ResourceStore.cs b/src/UglyToad.PdfPig/Content/ResourceStore.cs index 4982d173..58f3e490 100644 --- a/src/UglyToad.PdfPig/Content/ResourceStore.cs +++ b/src/UglyToad.PdfPig/Content/ResourceStore.cs @@ -336,8 +336,14 @@ return DirectObjectFinder.TryGet(new IndirectReferenceToken(indirectReference), scanner, out stream); } - public DictionaryToken GetExtendedGraphicsStateDictionary(NameToken name) + public DictionaryToken? GetExtendedGraphicsStateDictionary(NameToken name) { + if (parsingOptions.UseLenientParsing && !extendedGraphicsStates.ContainsKey(name)) + { + parsingOptions.Logger.Error($"The graphic state dictionary does not contain the key '{name}'."); + return null; + } + return extendedGraphicsStates[name]; } diff --git a/src/UglyToad.PdfPig/Util/StackDictionary.cs b/src/UglyToad.PdfPig/Util/StackDictionary.cs index 4b521439..f88bf625 100644 --- a/src/UglyToad.PdfPig/Util/StackDictionary.cs +++ b/src/UglyToad.PdfPig/Util/StackDictionary.cs @@ -5,7 +5,7 @@ namespace UglyToad.PdfPig.Util using System.Collections.Generic; using System.Diagnostics.CodeAnalysis; - internal class StackDictionary where K : notnull + internal sealed class StackDictionary where K : notnull { private readonly List> values = new List>(); @@ -13,6 +13,11 @@ namespace UglyToad.PdfPig.Util { get { + if (values.Count == 0) + { + throw new InvalidOperationException($"Cannot get item from empty stack, call {nameof(Push)} before use."); + } + if (TryGetValue(key, out var result)) { return result; @@ -35,7 +40,8 @@ namespace UglyToad.PdfPig.Util { if (values.Count == 0) { - throw new InvalidOperationException($"Cannot get item from empty stack, call {nameof(Push)} before use."); + result = default!; + return false; } for (var i = values.Count - 1; i >= 0; i--)