Beispiel #1
0
        // Write labels to the PDF
        internal void WriteLabels()
        {
            if (!edited)
            {
                return;
            }

            // Grab the labels element, creating it if necessary
            PdfDictionary labels_dict;

            if (!pdf_elements.ContainsKey(name_labels))
            {
                labels_dict = new PdfDictionary(pdf_document);
                pdf_elements.Add(name_labels, labels_dict);
            }
            else
            {
                labels_dict = pdf_elements.GetDictionary(name_labels);
            }
            labels_dict.Elements.Clear();

            // Create the number tree
            PdfArray number_tree = new PdfArray(pdf_document);

            // Add the range-start, attrib-dict pairs
            foreach (int range_start in page_labels.Keys)
            {
                number_tree.Elements.Add(new PdfInteger(range_start));
                PageLabelFormat label_format = page_labels[range_start];
                PdfDictionary   r_attribs    = new PdfDictionary(pdf_document);

                if (label_format.number_style.Length > 0)
                {
                    r_attribs.Elements.Add(name_fmt, new PdfName(label_format.number_style));
                }
                if (label_format.first_number > 1)
                {
                    r_attribs.Elements.Add(name_start_at, new PdfInteger(label_format.first_number));
                }
                if (label_format.prefix.Length > 0)
                {
                    r_attribs.Elements.Add(name_prefix, new PdfString(label_format.prefix));
                }
                number_tree.Elements.Add(r_attribs);
            }
            labels_dict.Elements.Add(name_numtree, number_tree);
        }
Beispiel #2
0
        internal PageLabels(PdfDocument document)
        {
            page_labels  = new SortedDictionary <int, PageLabelFormat> ();
            pdf_elements = document.Internals.Catalog.Elements;
            pdf_document = document;
            edited       = false;

            // Ignore documents that don't have labelling stuff defined
            if (!pdf_elements.ContainsKey(name_labels))
            {
                return;
            }

            // Ignore documents that don't have a properly-defined PageLabelFmt section
            PdfDictionary my_labels = pdf_elements.GetDictionary(name_labels);

            if (!my_labels.Elements.ContainsKey(name_numtree))
            {
                return;
            }

            /* The number tree (not my term) is a PdfArray arranged as follows: [##, dict, ##, dict, ##, dict ...]
             * ## represents the starting index of the page (0-based) and the following dict is a PdfDictionary
             * containing formatting information regarding the range
             */

            PdfArray number_tree = my_labels.Elements.GetArray(name_numtree);

            for (int i = 0; i < number_tree.Elements.Count / 2; ++i)
            {
                Console.WriteLine("Range # {0}", i);
                PageLabelFormat temp_label = new PageLabelFormat();

                int           range_start = number_tree.Elements.GetInteger(i * 2);
                PdfDictionary label_data  = number_tree.Elements.GetDictionary(i * 2 + 1);

                // Set the prefix, default to ""
                if (label_data.Elements.ContainsKey(name_prefix))
                {
                    temp_label.prefix = label_data.Elements.GetString(name_prefix);
                }
                else
                {
                    temp_label.prefix = "";
                }

                // Set the start number, default to 1
                if (label_data.Elements.ContainsKey(name_start_at))
                {
                    temp_label.first_number = label_data.Elements.GetInteger(name_start_at);
                }
                else
                {
                    temp_label.first_number = 1;
                }

                // Set the format type, default to no numbering (only show the prefix)
                if (label_data.Elements.ContainsKey(name_fmt))
                {
                    temp_label.number_style = label_data.Elements.GetString(name_fmt);
                }
                else
                {
                    temp_label.number_style = "";
                }

                page_labels.Add(range_start, temp_label);
            }
        }