Exemplo n.º 1
0
        private void                                _readPageResources(PdfDictionary page)
        {
            PdfValueList resources = page.ValueByName <PdfDictionary>("Resources", mandatory: false)?.Children;

            if (resources != null)
            {
                for (int resourceIdx = 0; resourceIdx < resources.Count; ++resourceIdx)
                {
                    if (resources[resourceIdx] is PdfName)
                    {
                        string cls = resources[resourceIdx].Cast <PdfName>().Value;

                        try {
                            if (++resourceIdx >= resources.Count)
                            {
                                throw new PdfException("Resource dictionary corrupt.");
                            }


                            switch (cls)
                            {
#if DEBUG_TEST
                            case "ProcSet":
                                System.Diagnostics.Debug.WriteLine("Resourse " + cls);
                                break;

                            case "Properties": {
                                var resourceItems = resources[resourceIdx].Resolve <PdfDictionary>();

                                if ((resourceItems.Children.Count % 2) != 0)
                                {
                                    throw new PdfException("Resource dictionary corrupt.");
                                }

                                for (int ItemIdx = 0; ItemIdx < resourceItems.Children.Count - 1; ItemIdx += 2)
                                {
                                    System.Diagnostics.Debug.WriteLine("Resourse " + cls + " " + resourceItems.Children[ItemIdx].Cast <PdfName>().Value + " removed.");
                                }

                                ++resourceIdx;
                            }
                            break;
#else
                            case "ProcSet":
                            case "Properties":
                                ++resourceIdx;
                                break;
#endif
                            case "Font":
                            case "XObject":
                            case "ExtGState":
                            case "ColorSpace": {
                                var resourceItems = resources[resourceIdx].Resolve <PdfDictionary>();

                                if ((resourceItems.Children.Count % 2) != 0)
                                {
                                    throw new PdfException("Resource dictionary corrupt.");
                                }

                                for (int ItemIdx = 0; ItemIdx < resourceItems.Children.Count - 1; ItemIdx += 2)
                                {
                                    var value = resourceItems.Children[ItemIdx + 1];

                                    if (cls == "Font")
                                    {
                                        var fontDic = value.Resolve <PdfDictionary>();

                                        if (new PdfName("Type").Equals(fontDic.Children[0]) &&
                                            new PdfName("Font").Equals(fontDic.Children[1]) &&
                                            new PdfName("Subtype").Equals(fontDic.Children[2]) &&
                                            new PdfName("Type1").Equals(fontDic.Children[3]) &&
                                            new PdfName("BaseFont").Equals(fontDic.Children[4]) &&
                                            new PdfName("Encoding").Equals(fontDic.Children[6]) &&
                                            new PdfName("WinAnsiEncoding").Equals(fontDic.Children[7]))
                                        {
                                            var stdFont = PdfStandard.Standard.FindStandardFontByName(fontDic.Children[5].Cast <PdfName>().Value);
                                            if (stdFont != null)
                                            {
                                                value = stdFont;
                                            }
                                        }
                                    }

                                    _resources.Add(cls,
                                                   resourceItems.Children[ItemIdx].Cast <PdfName>().Value,
                                                   value);
                                }
                            }
                            break;
                            }
                        }
                        catch (Exception err) {
                            throw new PdfException("Processing resource '" + cls + "' failed.", err);
                        }
                    }
                }
            }
        }