Beispiel #1
0
        private void readPerms(PDFDictionary dict)
        {
            PDFString perms = dict["Perms"] as PDFString;

            if (perms == null || perms.GetBytes().Length != 16)
            {
                throw new PDFException();
            }
            _perms = perms.GetBytes();
        }
Beispiel #2
0
        private void readUE(PDFDictionary dict)
        {
            PDFString ue = dict["UE"] as PDFString;

            if (ue == null || ue.GetBytes().Length != 32)
            {
                throw new PDFException();
            }
            _ue = ue.GetBytes();
        }
Beispiel #3
0
        private void readOE(PDFDictionary dict)
        {
            PDFString oe = dict["OE"] as PDFString;

            if (oe == null || oe.GetBytes().Length != 32)
            {
                throw new PDFException();
            }
            _oe = oe.GetBytes();
        }
Beispiel #4
0
        internal override string ConvertFromFontEncoding(PDFString str)
        {
            if (str == null)
            {
                return("");
            }

            if (_encoding == null)
            {
                _encoding = new FontEncoding(GetDictionary()["Encoding"]);
            }

            if (str == null)
            {
                return("");
            }

            StringBuilder result = new StringBuilder();

            byte[] data = str.GetBytes();
            for (int i = 0; i < data.Length; ++i)
            {
                result.Append(_encoding.GetChar(data[i]));
            }

            return(result.ToString());
        }
Beispiel #5
0
        internal override float GetTextWidth(PDFString str, float fontSize)
        {
            PDFArray  widths    = GetDictionary()["Widths"] as PDFArray;
            PDFNumber fc        = GetDictionary()["FirstChar"] as PDFNumber;
            int       firstChar = 0;

            if (fc != null)
            {
                firstChar = (int)fc.GetValue();
            }

            byte[] data   = str.GetBytes();
            float  result = 0;

            for (int i = 0; i < data.Length; ++i)
            {
                PDFNumber w = widths[data[i] - firstChar] as PDFNumber;
                if (w != null)
                {
                    return((float)(w.GetValue() * fontSize / 1000.0f));
                }
            }

            return(result);
        }
Beispiel #6
0
        private byte[] getPasswordHash(string key, PDFDictionary dictionary)
        {
            PDFString hash = dictionary[key] as PDFString;

            if (hash == null)
            {
                throw new PDFException();
            }

            byte[] result = hash.GetBytes();
            if (_revision < 5)
            {
                if (result.Length != 32)
                {
                    throw new PDFException();
                }
            }
            else
            {
                if (result.Length != 48)
                {
                    throw new PDFException();
                }
            }
            return(result);
        }
Beispiel #7
0
        internal override string ConvertFromFontEncoding(PDFString str)
        {
            byte[] data   = str.GetBytes();
            char[] result = new char[data.Length];

            for (int i = 0; i < data.Length; ++i)
            {
                result[i] = (char)data[i];
            }

            return(new string(result));
        }
Beispiel #8
0
        internal override float GetTextWidth(PDFString str, float fontSize)
        {
            byte[] data   = str.GetBytes();
            float  result = 0;

            for (int i = 0; i < data.Length - 1; i += 2)
            {
                ushort glyf = (ushort)(data[i] * 256 + data[i + 1]);
                result += getGlyfWidth(glyf);
            }

            return(result * fontSize / 1000.0f);
        }
Beispiel #9
0
        internal override float GetTextWidth(PDFString str, float fontSize)
        {
            PDFArray widths = GetDictionary()["Widths"] as PDFArray;
            float    result = 0.0f;

            byte[] buf = str.GetBytes();
            for (int i = 0; i < buf.Length; ++i)
            {
                PDFNumber w = widths[buf[i] - 32] as PDFNumber;
                if (w != null)
                {
                    result += (float)w.GetValue();
                }
            }

            result = result * fontSize / 1000.0f;
            return(result);
        }
Beispiel #10
0
        public string ConvertFrom(PDFString str)
        {
            byte[] data   = str.GetBytes();
            char[] result = new char[data.Length];
            for (int i = 0; i < data.Length; i += 2)
            {
                ushort glyf = (ushort)(data[i] * 256 + data[i + 1]);
                for (int j = 0; j < _map.Length; ++j)
                {
                    if (_map[j] == glyf)
                    {
                        result[i] = (char)j;
                        break;
                    }
                }
            }

            return(new string(result));
        }
Beispiel #11
0
        internal override float GetTextWidth(PDFString str, float fontSize)
        {
            PDFArray widths = GetDictionary()["Widths"] as PDFArray;

            byte[] data   = str.GetBytes();
            float  result = 0;

            if (widths != null)
            {
                PDFNumber fc        = GetDictionary()["FirstChar"] as PDFNumber;
                int       firstChar = 0;
                if (fc != null)
                {
                    firstChar = (int)fc.GetValue();
                }

                for (int i = 0; i < data.Length; ++i)
                {
                    PDFNumber w = widths[data[i] - firstChar] as PDFNumber;
                    if (w != null)
                    {
                        result += (float)w.GetValue();
                    }
                }

                return(result * fontSize / 1000.0f);
            }

            StandardFonts font;

            if (isStandardFont(out font))
            {
                IGlyfMetrics metrics = getStandardFontMetrics(font);
                string       tmp     = ConvertFromFontEncoding(str);
                for (int i = 0; i < tmp.Length; ++i)
                {
                    result += metrics.GetCharWidth(tmp[i]);
                }
            }

            return(result * fontSize / 1000.0f);
        }
Beispiel #12
0
        internal override string ConvertFromFontEncoding(PDFString str)
        {
            if (str == null)
            {
                return("");
            }

            loadCmapper();
            byte[]        data   = str.GetBytes();
            StringBuilder result = new StringBuilder(data.Length / 2 + 1);
            ushort        glyf;
            char          tmp;

            for (int i = 0; i < data.Length - 1; i += 2)
            {
                glyf = (ushort)(data[i] * 256 + data[i + 1]);
                tmp  = _toUnicode.GetChar(glyf);
                if (tmp == 0)
                {
                    string s = _toUnicode.GetLigature(glyf);
                    if (s == null)
                    {
                        tmp = _cidToGIDMap.GetChar(glyf);
                        if (tmp != 0)
                        {
                            result.Append(tmp);
                        }
                    }
                    else
                    {
                        result.Append(s);
                    }
                }
                else
                {
                    result.Append(tmp);
                }
            }

            return(result.ToString());
        }
Beispiel #13
0
        internal override string ConvertFromFontEncoding(PDFString str)
        {
            if (str == null)
            {
                return("");
            }

            loadCmappers();
            byte[]        data   = str.GetBytes();
            StringBuilder result = new StringBuilder(data.Length);

            char tmp;

            for (int i = 0; i < data.Length; ++i)
            {
                tmp = m_toUnicode.GetChar(data[i]);
                if (tmp == 0)
                {
                    string s = m_toUnicode.GetLigature(data[i]);
                    if (s == null)
                    {
                        tmp = m_encoding.GetChar(data[i]);
                        if (tmp != 0)
                        {
                            result.Append(tmp);
                        }
                    }
                    else
                    {
                        result.Append(s);
                    }
                }
                else
                {
                    result.Append(tmp);
                }
            }

            return(result.ToString());
        }
Beispiel #14
0
        private void parseFontAndColor()
        {
            if (Owner != null)
            {
                byte[]    data = null;
                PDFString da   = Dictionary["DA"] as PDFString;
                if (da != null)
                {
                    data = da.GetBytes();
                }
                else
                {
                    PDFString ada = Owner.GetAcroFormDefaultAttribute();
                    if (ada != null)
                    {
                        data = ada.GetBytes();
                    }
                }

                if (data == null)
                {
                    _fontColor             = new ColorRGB(0, 0, 0);
                    _font                  = new Font(StandardFonts.Helvetica, 12);
                    _font.ChangedFontSize += changedFontSize;
                    return;
                }

                PageOperationParser      parser     = new PageOperationParser(new MemoryStream(data));
                List <IPDFPageOperation> operations = new List <IPDFPageOperation>();
                IPDFPageOperation        operation;
                while ((operation = parser.Next()) != null)
                {
                    operations.Add(operation);
                }

                parseFont(operations);
                parseFontColor(operations);
            }
        }
Beispiel #15
0
        private void readID(PDFArray arr)
        {
            if (arr == null)
            {
                _docID        = new byte[16];
                _isDocIDEmpty = true;
                return;
            }

            PDFString stringItem = arr[0] as PDFString;

            if (stringItem == null)
            {
                _docID        = new byte[16];
                _isDocIDEmpty = true;
                return;
            }

            byte[] id = stringItem.GetBytes();
            _docID        = id;
            _isDocIDEmpty = false;
        }