Skip to content
Merged
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
16 changes: 9 additions & 7 deletions src/UglyToad.PdfPig/Parser/FileStructure/FileTrailerParser.cs
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,8 @@ public static long GetFirstCrossReferenceOffset(IInputBytes bytes, ISeekableToke

private static long GetStartXrefPosition(IInputBytes bytes, int offsetFromEnd)
{
var startXrefs = new List<int>();
int startXref = 0;
int startXrefsCount = 0;

var index = 0;

Expand Down Expand Up @@ -106,23 +107,24 @@ private static long GetStartXrefPosition(IInputBytes bytes, int offsetFromEnd)

if (index == StartXRefBytes.Length)
{
// Add this "startxref" (position from the start of the document to the first 's').
startXrefs.Add((int)bytes.CurrentOffset - StartXRefBytes.Length);
// Set this "startxref" (position from the start of the document to the first 's').
startXref = (int)bytes.CurrentOffset - StartXRefBytes.Length;
startXrefsCount++;

// Continue scanning in case there are further "startxref"s. Not sure if this ever happens.
index = 0;
}
}

actualStartOffset = Math.Max(0, fileLength - (offsetFromEnd * multiple));
} while (startXrefs.Count == 0 && actualStartOffset > 0);
if (startXrefs.Count == 0)
} while (startXrefsCount == 0 && actualStartOffset > 0);

if (startXrefsCount == 0)
{
throw new PdfDocumentFormatException($"Could not find the startxref within the last {offsetFromEnd} characters.");
}

return startXrefs[startXrefs.Count - 1];
return startXref;
}
}
}
Loading