Example #1
0
        public TrailerPart ParseTrailer(PdfStreamReader streamReader, IPdf pdf)
        {
            var header = streamReader.ReadLine();

            if (header != "trailer")
            {
                throw new ArgumentOutOfRangeException(nameof(streamReader), "First line in Trailer Content is not \"trailer\"");
            }

            var trailerDictionary = this._pdfReader.ReadObject(streamReader, pdf);

            if (!(trailerDictionary is PdfDictionary))
            {
                throw new InvalidDataException("Trailer dictionary content was not a dictionary.");
            }

            var line = streamReader.ReadLine();
            var foundCrossReference = false;

            if (line == "startxref")
            {
                foundCrossReference = true;
            }

            if (!foundCrossReference)
            {
                throw new InvalidDataException("Missing startxref line");
            }

            var crossReferenceLine = streamReader.ReadLine();

            if (!long.TryParse(crossReferenceLine, out var crossReferenceOffset))
            {
                throw new InvalidDataException("Cross Reference Offset value is not a parseable number");
            }

            line = streamReader.ReadLine();

            if (line == null ||
                line == "%%EOF")
            {
                var result = new TrailerPart((PdfDictionary)trailerDictionary, crossReferenceOffset);

                return(result);
            }

            throw new InvalidDataException("Trailer content is invalid, the last 2 lines should be startxref and a number and optionally a 3rd line containing %%EOF");
        }
        public CrossReferencePart ReadPart(PdfStreamReader streamReader, IPdf pdf)
        {
            if (pdf == null)
            {
                throw new ArgumentNullException(nameof(pdf));
            }

            if (pdf.Trailer == null ||
                pdf.Trailer.StartCrossReference == 0)
            {
                return(null);
            }

            var originalPosition = streamReader.Position;

            try
            {
                streamReader.Seek(pdf.Trailer.StartCrossReference, SeekOrigin.Begin);
                var line = streamReader.ReadLine();

                if (line != "xref")
                {
                    throw new ArgumentOutOfRangeException(nameof(streamReader), line, "Invalid Cross Reference Header");
                }

                var subsections = new List <CrossReferencePartSubSection>();
                while (line == "xref")
                {
                    subsections.Add(_subSectionReader.ReadPart(streamReader, pdf));
                    line = streamReader.ReadLine();
                }

                var result = new CrossReferencePart
                {
                    Sections = subsections.ToArray()
                };

                return(result);
            }
            finally
            {
                streamReader.Seek(originalPosition, SeekOrigin.Begin);
            }
        }
        public VersionPart ReadPart(PdfStreamReader streamReader, IPdf pdf)
        {
            var line = streamReader.ReadLine();

            if (line == null)
            {
                throw new ArgumentOutOfRangeException(nameof(streamReader), "Unexpected end of stream");
            }
            else if (line.StartsWith(PdfVersionPrefix))
            {
                var result = this.GetVersionPart(line);
                return(result);
            }
            else
            {
                throw new ArgumentOutOfRangeException(nameof(streamReader), line, "Expected content at current position was not a version");
            }
        }
        public CrossReferencePartSubSectionEntry ReadPart(PdfStreamReader streamReader, IPdf pdf)
        {
            var line = streamReader.ReadLine();

            if (line.Length != 18)
            {
                throw new ArgumentOutOfRangeException(nameof(streamReader), line, "Line is not 18 characters long.");
            }

            var fields = line.Split(' ');

            if (fields.Length != 3)
            {
                throw new ArgumentOutOfRangeException(nameof(streamReader), line, "Line does not have 3 fields.");
            }

            if (!long.TryParse(fields[0], out var offset))
            {
                throw new ArgumentOutOfRangeException(nameof(streamReader), line, "Invalid offset field. Not a valid number.");
            }

            if (!int.TryParse(fields[1], out var generation))
            {
                throw new ArgumentOutOfRangeException(nameof(streamReader), line, "Invalid generation field. Not a valid number.");
            }

            if (fields[2] != "f" &&
                fields[2] != "n")
            {
                throw new ArgumentOutOfRangeException(nameof(streamReader), line, "Invalid in use flag. Not 'n' or 'f'.");
            }

            var inUseFlag = (CrossReferenceEntryFlag)fields[2][0];
            var result    = new CrossReferencePartSubSectionEntry
            {
                Generation = generation,
                InUse      = inUseFlag,
                Offset     = offset
            };

            return(result);
        }
Example #5
0
        public CrossReferencePartSubSection ReadPart(PdfStreamReader streamReader, IPdf pdf)
        {
            var line = streamReader.ReadLine();

            var fields = line.Split(' ');

            if (fields.Length != 2)
            {
                throw new ArgumentOutOfRangeException(nameof(streamReader), line, "Invalid field count for subsection counts.");
            }

            if (!int.TryParse(fields[0], out var startingObject))
            {
                throw new ArgumentOutOfRangeException(nameof(streamReader), line, "Invalid starting object field. Not a valid number.");
            }

            if (!int.TryParse(fields[1], out var objectCount))
            {
                throw new ArgumentOutOfRangeException(nameof(streamReader), line, "Invalid object count. Not a valid number.");
            }

            var entries = new CrossReferencePartSubSectionEntry[objectCount];

            for (var i = 0; i < entries.Length; i++)
            {
                var entry = this._partReader.ReadPart(streamReader, pdf);
                entry.ObjectNumber = startingObject + i;
                entries[i]         = entry;
            }

            var result = new CrossReferencePartSubSection
            {
                Entries        = entries,
                StartingObject = startingObject
            };

            return(result);
        }