diff --git a/src/UglyToad.PdfPig.Tests/Integration/GithubIssuesTests.cs b/src/UglyToad.PdfPig.Tests/Integration/GithubIssuesTests.cs index 71c3cff7..e16bc557 100644 --- a/src/UglyToad.PdfPig.Tests/Integration/GithubIssuesTests.cs +++ b/src/UglyToad.PdfPig.Tests/Integration/GithubIssuesTests.cs @@ -7,6 +7,29 @@ public class GithubIssuesTests { + [Fact] + public void Issue1013() + { + // NB: We actually do not fix issue 953 here, but another bug found with the same document. + var path = IntegrationHelpers.GetSpecificTestDocumentPath("document_with_failed_fonts.pdf"); + + // Lenient parsing ON + Skip missing fonts + using (var document = PdfDocument.Open(path, new ParsingOptions() { UseLenientParsing = true, SkipMissingFonts = true })) + { + var page2 = document.GetPage(2); + Assert.NotEmpty(page2.Letters); + + var words2 = NearestNeighbourWordExtractor.Instance.GetWords(page2.Letters).ToArray(); + Assert.Equal("Doplňující", words2[0].Text); + + var page3 = document.GetPage(3); + Assert.NotEmpty(page3.Letters); + + var words3 = NearestNeighbourWordExtractor.Instance.GetWords(page3.Letters).ToArray(); + Assert.Equal("Vinohradská", words3[8].Text); + } + } + [Fact] public void Issue1016() { diff --git a/src/UglyToad.PdfPig.Tests/Integration/SpecificTestDocuments/document_with_failed_fonts.pdf b/src/UglyToad.PdfPig.Tests/Integration/SpecificTestDocuments/document_with_failed_fonts.pdf new file mode 100644 index 00000000..00db2d2f Binary files /dev/null and b/src/UglyToad.PdfPig.Tests/Integration/SpecificTestDocuments/document_with_failed_fonts.pdf differ diff --git a/src/UglyToad.PdfPig/Tokenization/Scanner/PdfTokenScanner.cs b/src/UglyToad.PdfPig/Tokenization/Scanner/PdfTokenScanner.cs index 71d35530..1c021867 100644 --- a/src/UglyToad.PdfPig/Tokenization/Scanner/PdfTokenScanner.cs +++ b/src/UglyToad.PdfPig/Tokenization/Scanner/PdfTokenScanner.cs @@ -835,11 +835,13 @@ } if (!stream.StreamDictionary.TryGet(NameToken.First, out var firstToken) - || !(firstToken is NumericToken)) + || !(firstToken is NumericToken firstTokenNum)) { throw new PdfDocumentFormatException($"Object stream dictionary did not provide first object offset {stream.StreamDictionary}."); } + long firstTokenOffset = firstTokenNum.Long; + // Read the N integers var bytes = new MemoryInputBytes(stream.Decode(filterProvider, this)); @@ -854,7 +856,7 @@ scanner.MoveNext(); var byteOffset = (NumericToken)scanner.CurrentToken; - objects.Add((objectNumber.Long, byteOffset.Long)); + objects.Add((objectNumber.Long, firstTokenOffset + byteOffset.Long)); } var results = new List(); @@ -863,6 +865,16 @@ { var obj = objects[i]; + // Check item offset is in [currentPosition - 1; currentPosition + 1] + bool isBetween = ((obj.Item2 - (scanner.CurrentPosition - 1)) | ((scanner.CurrentPosition + 1) - obj.Item2)) >= 0; + if (!isBetween) + { + // TODO - Not sure if it belongs here but fixes issue 1013. + // It is not clear what happens with this specific document 'document_with_failed_fonts.pdf' + // I could not find where the same logic is applied in pdfbox. + scanner.Seek(obj.Item2); + } + scanner.MoveNext(); var token = scanner.CurrentToken;