Beispiel #1
0
        internal static DocumentFont GetFont(PDFObjects pdf, PdfObject pdfObject)
        {
            var dic  = pdf.GetObject <DictionaryObject>(pdfObject);
            var name = pdf.GetObject <NameObject>(dic.Dictionary["BaseFont"]).Value;

            lock (lck) {
                if (m_lstFont.ContainsKey(name))
                {
                    return(m_lstFont[name]);
                }
                if (IsBaseFont(pdf, dic))
                {
                    var font = new DocumentBaseFont(name);
                    m_lstFont.Add(name, font);
                    return(font);
                }
                else if (IsSubsetFont(dic))
                {
                    var font = pdf.GetDocument <DocumentTtfSubsetFont>(pdfObject);
                    m_lstFont.Add(name, font);
                    return(font);
                }
                else if (IsTrueTypeFont(pdf, dic))
                {
                    var font = pdf.GetDocument <DocumentTtfFont>(pdfObject);
                    m_lstFont.Add(name, font);
                    return(font);
                }
            }

            throw new PdfException(PdfExceptionCodes.INVALID_FONT, $"Not supported font type");
        }
Beispiel #2
0
        private DocumentCatalog ReadXRef(Tokenizer tokenizer)
        {
            long xrefPosition = XrefPosition(tokenizer);

            tokenizer.MoveToPosition(xrefPosition);

            Token token = tokenizer.Token();

            if (!TokenValidator.Validate(token, CharacterSetType.Regular, "xref"))
            {
                throw new PdfException(PdfExceptionCodes.INVALID_XREF, "Expected xref");
            }

            token = tokenizer.Token();
            if (!TokenValidator.IsWhiteSpace(token))
            {
                throw new PdfException(PdfExceptionCodes.INVALID_XREF, "after xref must be a whitspace");
            }

            tokenizer.GetInteger();                         // objectNumber
            tokenizer.TokenExcludedComments();
            int numberOfEntries = tokenizer.GetInteger();

            tokenizer.TokenExcludedComments();

            PDFObjects pdfObjects = new PDFObjects();

            for (int i = 0; i < numberOfEntries; i++)
            {
                int pos = tokenizer.GetInteger();           // position
                tokenizer.TokenExcludedComments();          // whitespace
                tokenizer.GetInteger();                     // generation
                tokenizer.TokenExcludedComments();          // whitespace
                string type = tokenizer.Token().ToString(); // f or n
                tokenizer.TokenExcludedComments();          // whitespace

                if (type != "f" && type != "n")
                {
                    throw new PdfException(PdfExceptionCodes.INVALID_XREF, "only xref f or n entries allowed");
                }

                if (type == "f")      // free element
                {
                    continue;
                }

                tokenizer.SavePosition();
                tokenizer.MoveToPosition(pos);

                IndirectObject obj = new IndirectObject(tokenizer);
                pdfObjects.AddObject(obj);
                tokenizer.RestorePosition();
            }

            if (tokenizer.Token().ToString() != "trailer")
            {
                throw new PdfException(PdfExceptionCodes.INVALID_TRAILER, "expected trailer");
            }

            var trailer      = new DictionaryObject(tokenizer);
            var rootIndirect = (IndirectReferenceObject)trailer.Dictionary["Root"];

            return(pdfObjects.GetDocument <DocumentCatalog>(rootIndirect));
        }