Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 27 additions & 0 deletions src/UglyToad.PdfPig.Tests/Integration/GithubIssuesTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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<InvalidOperationException>(() => 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<PdfDocumentFormatException>(() =>
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()
{
Expand Down
Binary file not shown.
2 changes: 1 addition & 1 deletion src/UglyToad.PdfPig/Content/IResourceStore.cs
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ public interface IResourceStore
/// <summary>
/// Get the extended graphics state dictionary corresponding to the name.
/// </summary>
DictionaryToken GetExtendedGraphicsStateDictionary(NameToken name);
DictionaryToken? GetExtendedGraphicsStateDictionary(NameToken name);

/// <summary>
/// Get the font from the <see cref="IndirectReferenceToken"/>.
Expand Down
8 changes: 7 additions & 1 deletion src/UglyToad.PdfPig/Content/ResourceStore.cs
Original file line number Diff line number Diff line change
Expand Up @@ -336,8 +336,14 @@ public bool TryGetXObject(NameToken name, [NotNullWhen(true)] out StreamToken? s
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];
}

Expand Down
10 changes: 8 additions & 2 deletions src/UglyToad.PdfPig/Util/StackDictionary.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,19 @@ namespace UglyToad.PdfPig.Util
using System.Collections.Generic;
using System.Diagnostics.CodeAnalysis;

internal class StackDictionary<K, V> where K : notnull
internal sealed class StackDictionary<K, V> where K : notnull
{
private readonly List<Dictionary<K, V>> values = new List<Dictionary<K, V>>();

public V this[K key]
{
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;
Expand All @@ -35,7 +40,8 @@ public bool TryGetValue(K key, [NotNullWhen(true)] out V result)
{
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--)
Expand Down
Loading