diff --git a/src/UglyToad.PdfPig.Tests/Graphics/Operations/SpecialGraphicsState/PopTests.cs b/src/UglyToad.PdfPig.Tests/Graphics/Operations/SpecialGraphicsState/PopTests.cs index 97ac12958..bf9ce18cf 100644 --- a/src/UglyToad.PdfPig.Tests/Graphics/Operations/SpecialGraphicsState/PopTests.cs +++ b/src/UglyToad.PdfPig.Tests/Graphics/Operations/SpecialGraphicsState/PopTests.cs @@ -14,7 +14,7 @@ public void PopSymbolCorrect() Assert.Equal("Q", Pop.Value.Operator); } - [Fact] + [Fact(Skip = "The stack size check has been moved out of the Pop Operation, and is now in BaseStreamProcessor.PopState().")] public void CannotPopWithSingleFrame() { Action action = () => Pop.Value.Run(context); diff --git a/src/UglyToad.PdfPig.Tests/Integration/GithubIssuesTests.cs b/src/UglyToad.PdfPig.Tests/Integration/GithubIssuesTests.cs index 89f3790c4..eb011cf2a 100644 --- a/src/UglyToad.PdfPig.Tests/Integration/GithubIssuesTests.cs +++ b/src/UglyToad.PdfPig.Tests/Integration/GithubIssuesTests.cs @@ -8,10 +8,34 @@ public class GithubIssuesTests { [Fact] - public void Issue959() + public void Issue973() { + var path = IntegrationHelpers.GetSpecificTestDocumentPath("JD5008.pdf"); + // Lenient parsing ON + using (var document = PdfDocument.Open(path, new ParsingOptions() { UseLenientParsing = true })) + { + var page = document.GetPage(2); + Assert.NotNull(page); + Assert.Equal(2, page.Number); + Assert.NotEmpty(page.Letters); + } + + // Lenient parsing OFF + using (var document = PdfDocument.Open(path, new ParsingOptions() { UseLenientParsing = false })) + { + var exception = Assert.Throws(() => document.GetPage(2)); + Assert.Equal("Cannot execute a pop of the graphics state stack, it would leave the stack empty.", exception.Message); + } + } + + + [Fact] + public void Issue959() + { var path = IntegrationHelpers.GetSpecificTestDocumentPath("algo.pdf"); + + // Lenient parsing ON using (var document = PdfDocument.Open(path, new ParsingOptions() { UseLenientParsing = true })) { for (int i = 1; i <= document.NumberOfPages; ++i) diff --git a/src/UglyToad.PdfPig.Tests/Integration/SpecificTestDocuments/JD5008.pdf b/src/UglyToad.PdfPig.Tests/Integration/SpecificTestDocuments/JD5008.pdf new file mode 100644 index 000000000..9f7d3e1c9 Binary files /dev/null and b/src/UglyToad.PdfPig.Tests/Integration/SpecificTestDocuments/JD5008.pdf differ diff --git a/src/UglyToad.PdfPig/Graphics/BaseStreamProcessor.cs b/src/UglyToad.PdfPig/Graphics/BaseStreamProcessor.cs index a3467f689..8bbf93932 100644 --- a/src/UglyToad.PdfPig/Graphics/BaseStreamProcessor.cs +++ b/src/UglyToad.PdfPig/Graphics/BaseStreamProcessor.cs @@ -195,7 +195,21 @@ public CurrentGraphicsState GetCurrentState() /// public virtual void PopState() { - GraphicsStack.Pop(); + if (StackSize > 1) + { + GraphicsStack.Pop(); + } + else + { + const string error = "Cannot execute a pop of the graphics state stack, it would leave the stack empty."; + ParsingOptions.Logger.Error(error); + + if (!ParsingOptions.UseLenientParsing) + { + throw new InvalidOperationException(error); + } + } + ActiveExtendedGraphicsStateFont = null; } diff --git a/src/UglyToad.PdfPig/Graphics/Operations/SpecialGraphicsState/Pop.cs b/src/UglyToad.PdfPig/Graphics/Operations/SpecialGraphicsState/Pop.cs index ad343f075..43812a0a9 100644 --- a/src/UglyToad.PdfPig/Graphics/Operations/SpecialGraphicsState/Pop.cs +++ b/src/UglyToad.PdfPig/Graphics/Operations/SpecialGraphicsState/Pop.cs @@ -1,6 +1,5 @@ namespace UglyToad.PdfPig.Graphics.Operations.SpecialGraphicsState { - using System; using System.IO; /// @@ -29,15 +28,7 @@ private Pop() /// public void Run(IOperationContext operationContext) { - var currentStackSize = operationContext.StackSize; - if (currentStackSize > 1) - { - operationContext.PopState(); - } - else - { - throw new InvalidOperationException("Cannot execute a pop of the graphics state stack, it would leave the stack empty."); - } + operationContext.PopState(); } ///