You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
PdfPig throws an exception when iterating to the last page of my PDFs using foreach (var page in pdfDocument.GetPages()). The same issue occurs when directly attempting to retrieve the last page with var page = pdfDocument.GetPage(lastPageNumber).
I am not an expert in PDF internals, but this might be due to invalid operators within these PDFs. Despite this, the PDFs open without any issues in web browsers (Microsoft Edge or Chrome) and can be parsed successfully using other libraries like Aspose.PDF, IronPDF, or iTextSharp.LGPLv2.Core.
Code:
using var pdfDocument = PdfDocument.Open(pdfPath);
foreach (var page in pdfDocument.GetPages()) // here is the exception on last iteration
{
textBuilder.AppendLine(page.Text);
}
Exception:
Unhandled exception. System.InvalidOperationException: Cannot execute a pop of the graphics state stack, it would leave the stack empty.
at UglyToad.PdfPig.Graphics.Operations.SpecialGraphicsState.Pop.Run(IOperationContext operationContext)
at UglyToad.PdfPig.Graphics.BaseStreamProcessor`1.ProcessOperations(IReadOnlyList`1 operations)
at UglyToad.PdfPig.Graphics.ContentStreamProcessor.Process(Int32 pageNumberCurrent, IReadOnlyList`1 operations)
at UglyToad.PdfPig.Parser.PageFactory.ProcessPage(Int32 pageNumber, DictionaryToken dictionary, NamedDestinations namedDestinations, MediaBox mediaBox, CropBox cropBox, UserSpaceUnit userSpaceUnit, PageRotationDegrees rotation, TransformationMatrix initialMatrix, IReadOnlyList`1 operations)
at UglyToad.PdfPig.Content.BasePageFactory`1.ProcessPageInternal(Int32 pageNumber, DictionaryToken dictionary, NamedDestinations namedDestinations, Medi
aBox mediaBox, CropBox cropBox, UserSpaceUnit userSpaceUnit, PageRotationDegrees rotation, TransformationMatrix& initialMatrix, ReadOnlyMemory`1 contentBytes)
at UglyToad.PdfPig.Content.BasePageFactory`1.Create(Int32 number, DictionaryToken dictionary, PageTreeMembers pageTreeMembers, NamedDestinations namedDestinations)
at UglyToad.PdfPig.Content.Pages.GetPage[TPage](IPageFactory`1 pageFactory, Int32 pageNumber, NamedDestinations namedDestinations, ParsingOptions parsingOptions)
at UglyToad.PdfPig.Content.Pages.GetPage(Int32 pageNumber, NamedDestinations namedDestinations, ParsingOptions parsingOptions)
at UglyToad.PdfPig.PdfDocument.GetPage(Int32 pageNumber)
at UglyToad.PdfPig.PdfDocument.GetPages()+MoveNext()
at Program.<Main>$(String[] args) in E:\Projects\Temporary\ConsoleApp10\ConsoleApp10\Program.cs:line 15
GhostScript message for the pdf:
Page 2
The following warnings were encountered at least once while processing this file:
encountered more Q than q
encountered more q than Q
invalid operator used in text block
**** This file had errors that were repaired or ignored.
The text was updated successfully, but these errors were encountered:
PdfPig throws an exception when iterating to the last page of my PDFs using
foreach (var page in pdfDocument.GetPages())
. The same issue occurs when directly attempting to retrieve the last page withvar page = pdfDocument.GetPage(lastPageNumber)
.I am not an expert in PDF internals, but this might be due to invalid operators within these PDFs. Despite this, the PDFs open without any issues in web browsers (Microsoft Edge or Chrome) and can be parsed successfully using other libraries like Aspose.PDF, IronPDF, or iTextSharp.LGPLv2.Core.
Code:
Exception:
GhostScript message for the pdf:
The text was updated successfully, but these errors were encountered: