示例#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");
        }
示例#2
0
        public static DocumentImage GetImage(PDFObjects pdf, PdfObject pdfObject)
        {
            var dic = pdf.GetObject <DictionaryObject>(pdfObject);

            if (dic.Dictionary.ContainsKey("Filter") && pdf.GetObject <NameObject>(dic.Dictionary["Filter"]).Value == "DCTDecode")
            {
                return(GetImageInternal(dic.Stream));
            }

            int    width             = pdf.GetObject <IntegerObject>(dic.Dictionary["Width"]).IntValue;
            int    height            = pdf.GetObject <IntegerObject>(dic.Dictionary["Height"]).IntValue;
            int    bitsPerComponents = pdf.GetObject <IntegerObject>(dic.Dictionary["BitsPerComponent"]).IntValue;
            string colorSpace        = pdf.GetObject <NameObject>(dic.Dictionary["ColorSpace"]).Value;

            return(GetRawImage(dic.Stream, width, height, bitsPerComponents, Enum.Parse <DeviceColorSpace>(colorSpace)));
        }
示例#3
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));
        }
示例#4
0
 private static bool IsBaseFont(PDFObjects pdf, DictionaryObject dic)
 => dic.Dictionary.ContainsKey("BaseFont") &&
 dic.Dictionary.ContainsKey("Subtype") &&
 pdf.GetObject <NameObject>(dic.Dictionary["Subtype"]).Value == "Type1";