private static string GetName(PdfDictionary.DictionaryElements dict, string key) { var obj = (object)dict[key]; if (obj == null) { return(string.Empty); } if (obj is PdfReference pdfReference) { obj = pdfReference.Value; } var pdfName = obj as PdfName; // do not change! hacky casting to string - there's a bug in PdfSharp string nullString = null; if (pdfName != nullString) { return(pdfName.Value); } if (obj is PdfNameObject pdfNameObject) { return(pdfNameObject.Value); } return(null); }
public static string GetName2(this PdfDictionary.DictionaryElements dict, string key) { object obj = dict[key]; if (obj == null) { return(string.Empty); } if (obj is PdfReference pdfReference) { obj = pdfReference.Value; } if (obj is PdfName pdfName) { return(pdfName.Value); } if (obj is PdfNameObject pdfNameObject) { return(pdfNameObject.Value); } throw new InvalidCastException("GetName: Object is not a name."); }
/// <summary> /// Sets the color to the specified key. /// </summary> /// <param name="elements">The elements.</param> /// <param name="key">The key.</param> /// <param name="color">The color.</param> public static void SetColor(this PdfDictionary.DictionaryElements elements, string key, XColor color) { if (elements == null) { throw new ArgumentNullException("elements"); } if (!string.IsNullOrEmpty(key)) { var arr = new PdfArray(); arr.Elements.Add(new PdfReal(color.R / (double)byte.MaxValue)); arr.Elements.Add(new PdfReal(color.G / (double)byte.MaxValue)); arr.Elements.Add(new PdfReal(color.B / (double)byte.MaxValue)); elements[key] = arr; } }
public static string GetName3(this PdfDictionary.DictionaryElements dict, string key) { var value = dict[key]; switch (value) { case PdfReference pdfReference: switch (pdfReference.Value) { case PdfNameObject pdfNameObject: return(pdfNameObject.Value); default: return(string.Empty); } case PdfName pdfName: return(pdfName.Value); default: return(string.Empty); } }
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); } }