コード例 #1
0
        /**
         * Gets number of indirect. If type of directed indirect is PAGES, it refers PAGE object through KIDS.
         * (Contributed by Kazuya Ujihara)
         * @param indirect
         * 2004-06-13
         */
        private static int GetNumber(PdfIndirectReference indirect)
        {
            PdfDictionary pdfObj = (PdfDictionary)PdfReader.GetPdfObjectRelease(indirect);

            if (pdfObj.Contains(PdfName.TYPE) && pdfObj.Get(PdfName.TYPE).Equals(PdfName.PAGES) && pdfObj.Contains(PdfName.KIDS))
            {
                PdfArray kids = (PdfArray)pdfObj.Get(PdfName.KIDS);
                indirect = (PdfIndirectReference)kids[0];
            }
            return(indirect.Number);
        }
コード例 #2
0
        /**
         * Retrieves the page labels from a PDF as an array of {@link PdfPageLabelFormat} objects.
         * @param reader a PdfReader object that has the page labels you want to retrieve
         * @return  a PdfPageLabelEntry array, containing an entry for each format change
         * or <code>null</code> if no page labels are present
         */
        public static PdfPageLabelFormat[] GetPageLabelFormats(PdfReader reader)
        {
            PdfDictionary dict   = reader.Catalog;
            PdfDictionary labels = (PdfDictionary)PdfReader.GetPdfObjectRelease(dict.Get(PdfName.PAGELABELS));

            if (labels == null)
            {
                return(null);
            }
            Hashtable numberTree = PdfNumberTree.ReadTree(labels);

            int[] numbers = new int[numberTree.Count];
            numberTree.Keys.CopyTo(numbers, 0);
            Array.Sort(numbers);
            PdfPageLabelFormat[] formats = new PdfPageLabelFormat[numberTree.Count];
            String prefix;
            int    numberStyle;
            int    pagecount;

            for (int k = 0; k < numbers.Length; ++k)
            {
                int           key = numbers[k];
                PdfDictionary d   = (PdfDictionary)PdfReader.GetPdfObjectRelease((PdfObject)numberTree[key]);
                if (d.Contains(PdfName.ST))
                {
                    pagecount = ((PdfNumber)d.Get(PdfName.ST)).IntValue;
                }
                else
                {
                    pagecount = 1;
                }
                if (d.Contains(PdfName.P))
                {
                    prefix = ((PdfString)d.Get(PdfName.P)).ToUnicodeString();
                }
                else
                {
                    prefix = "";
                }
                if (d.Contains(PdfName.S))
                {
                    char type = ((PdfName)d.Get(PdfName.S)).ToString()[1];
                    switch (type)
                    {
                    case 'R': numberStyle = UPPERCASE_ROMAN_NUMERALS; break;

                    case 'r': numberStyle = LOWERCASE_ROMAN_NUMERALS; break;

                    case 'A': numberStyle = UPPERCASE_LETTERS; break;

                    case 'a': numberStyle = LOWERCASE_LETTERS; break;

                    default: numberStyle = DECIMAL_ARABIC_NUMERALS; break;
                    }
                }
                else
                {
                    numberStyle = EMPTY;
                }
                formats[k] = new PdfPageLabelFormat(key + 1, numberStyle, prefix, pagecount);
            }
            return(formats);
        }
コード例 #3
0
        /**
         * Retrieves the page labels from a PDF as an array of String objects.
         * @param reader a PdfReader object that has the page labels you want to retrieve
         * @return  a String array or <code>null</code> if no page labels are present
         */
        public static String[] GetPageLabels(PdfReader reader)
        {
            int n = reader.NumberOfPages;

            PdfDictionary dict   = reader.Catalog;
            PdfDictionary labels = (PdfDictionary)PdfReader.GetPdfObjectRelease(dict.Get(PdfName.PAGELABELS));

            if (labels == null)
            {
                return(null);
            }

            String[]  labelstrings = new String[n];
            Hashtable numberTree   = PdfNumberTree.ReadTree(labels);

            int    pagecount = 1;
            String prefix    = "";
            char   type      = 'D';

            for (int i = 0; i < n; i++)
            {
                if (numberTree.ContainsKey(i))
                {
                    PdfDictionary d = (PdfDictionary)PdfReader.GetPdfObjectRelease((PdfObject)numberTree[i]);
                    if (d.Contains(PdfName.ST))
                    {
                        pagecount = ((PdfNumber)d.Get(PdfName.ST)).IntValue;
                    }
                    else
                    {
                        pagecount = 1;
                    }
                    if (d.Contains(PdfName.P))
                    {
                        prefix = ((PdfString)d.Get(PdfName.P)).ToUnicodeString();
                    }
                    if (d.Contains(PdfName.S))
                    {
                        type = ((PdfName)d.Get(PdfName.S)).ToString()[1];
                    }
                }
                switch (type)
                {
                default:
                    labelstrings[i] = prefix + pagecount;
                    break;

                case 'R':
                    labelstrings[i] = prefix + RomanNumberFactory.GetUpperCaseString(pagecount);
                    break;

                case 'r':
                    labelstrings[i] = prefix + RomanNumberFactory.GetLowerCaseString(pagecount);
                    break;

                case 'A':
                    labelstrings[i] = prefix + RomanAlphabetFactory.GetUpperCaseString(pagecount);
                    break;

                case 'a':
                    labelstrings[i] = prefix + RomanAlphabetFactory.GetLowerCaseString(pagecount);
                    break;
                }
                pagecount++;
            }
            return(labelstrings);
        }