/// <summary> /// Parses an XElement that contains a component /// </summary> /// <param name="componentElement">XElement to be parsed</param> /// <param name="cc">Parent Component</param> /// <returns>Parsed PrintableComponent</returns> private PrintableComponent parseComponent(XElement componentElement, CompositeComponent cc = null) { if (componentElement.Attribute("Type") != null) { PrintableComponent pc; double left = 0, top = 0, width = 0, height = 0, angle = 0; if (componentElement.Attribute("X") == null || componentElement.Attribute("Y") == null || componentElement.Attribute("Width") == null || componentElement.Attribute("Height") == null) { throw new Exception("One of the translation parameters is missing"); } else { left = double.Parse(componentElement.Attribute("X").Value, CultureInfo.InvariantCulture); top = double.Parse(componentElement.Attribute("Y").Value, CultureInfo.InvariantCulture); width = double.Parse(componentElement.Attribute("Width").Value, CultureInfo.InvariantCulture); height = double.Parse(componentElement.Attribute("Height").Value, CultureInfo.InvariantCulture); angle = (componentElement.Attribute("Angle") != null) ? double.Parse(componentElement.Attribute("Angle").Value, CultureInfo.InvariantCulture) : 0.0; } switch (componentElement.Attribute("Type").Value) { case "Image": pc = new ImageComponent(left, top, width, height, angle); break; case "Composite": pc = new CompositeComponent(0, 0, 100, 100, 0); if (componentElement.Element("Binding") != null) { parseBinding(componentElement.Element("Binding"), (CompositeComponent)pc); } if (componentElement.Element("Children") != null) { foreach (XElement childelem in componentElement.Element("Children").Elements()) { parseComponent(childelem, (CompositeComponent)pc); } } break; case "Barcode": pc = new BarcodeComponent(left, top, width, height, angle); break; case "Textbox": pc = new TextComponent(left, top, width, height, angle); if (componentElement.Element("TextStyle") != null) { XElement styleElement = componentElement.Element("TextStyle"); PrintableStyle style = new PrintableStyle(); if (styleElement.Attribute("Bold") != null) { style.Bold = bool.Parse(styleElement.Attribute("Bold").Value); } if (styleElement.Attribute("Font") != null) { style.Font = FontFamily.Families.FirstOrDefault(c => c.Name == styleElement.Attribute("Font").Value) ?? FontFamily.GenericSansSerif; } if (styleElement.Attribute("Alignment") != null) { string align = styleElement.Attribute("Alignment").Value; style.FontAlignment = (align == "Left") ? XParagraphAlignment.Left : ((align == "Right") ? XParagraphAlignment.Right : XParagraphAlignment.Center); } if (styleElement.Attribute("PrimaryColor") != null) { style.PrimaryColor = System.Drawing.ColorTranslator.FromHtml(styleElement.Attribute("PrimaryColor").Value); } if (styleElement.Attribute("SecondaryColor") != null) { style.SecondaryColor = System.Drawing.ColorTranslator.FromHtml(styleElement.Attribute("SecondaryColor").Value); } if (styleElement.Attribute("Size") != null) { style.FontSize = double.Parse(styleElement.Attribute("Size").Value); } if (styleElement.Attribute("Italic") != null) { style.Italic = bool.Parse(styleElement.Attribute("Italic").Value); } if (styleElement.Attribute("Underline") != null) { style.Underlined = bool.Parse(styleElement.Attribute("Underline").Value); } ((TextComponent)pc).Style = style; } break; case "Rectangle": pc = new RectangleComponent(left, top, width, height, angle); if (componentElement.Element("TextStyle") != null) { XElement styleElement = componentElement.Element("TextStyle"); if (styleElement.Attribute("PrimaryColor") != null) { var brush = new SolidBrush(System.Drawing.ColorTranslator.FromHtml(styleElement.Attribute("PrimaryColor").Value)); ((RectangleComponent)pc).FillColor = (PdfSharp.Drawing.XBrush)brush; ((RectangleComponent)pc).Fill = brush.Color.A > 0; } if (styleElement.Attribute("SecondaryColor") != null) { ((RectangleComponent)pc).BorderColor = System.Drawing.ColorTranslator.FromHtml(styleElement.Attribute("SecondaryColor").Value); } if (styleElement.Attribute("CornerRadius") != null) { ((RectangleComponent)pc).BorderRadius = double.Parse(styleElement.Attribute("CornerRadius").Value, CultureInfo.InvariantCulture); } if (styleElement.Attribute("BorderWidth") != null) { ((RectangleComponent)pc).BorderWidth = double.Parse(styleElement.Attribute("BorderWidth").Value, CultureInfo.InvariantCulture); } } break; case "LayoutBox": return(null); case "None": return(null); case "ImageBag": { pc = new ImageBagComponent(left, top, width, height, angle); if (componentElement.Element("TextStyle") != null) { XElement styleElement = componentElement.Element("TextStyle"); PrintableStyle style = new PrintableStyle(); if (styleElement.Attribute("ImageWidth") != null) { ((ImageBagComponent)pc).ImageWidth = double.Parse(styleElement.Attribute("ImageWidth").Value, CultureInfo.InvariantCulture); } if (styleElement.Attribute("ImageHeight") != null) { ((ImageBagComponent)pc).ImageHeight = double.Parse(styleElement.Attribute("ImageHeight").Value, CultureInfo.InvariantCulture); } if (styleElement.Attribute("ImagePaddingX") != null) { ((ImageBagComponent)pc).ImagePaddingX = double.Parse(styleElement.Attribute("ImagePaddingX").Value, CultureInfo.InvariantCulture); } if (styleElement.Attribute("ImagePaddingY") != null) { ((ImageBagComponent)pc).ImagePaddingY = double.Parse(styleElement.Attribute("ImagePaddingY").Value, CultureInfo.InvariantCulture); } if (styleElement.Attribute("RightToLeft") != null) { ((ImageBagComponent)pc).RightToLeft = bool.Parse(styleElement.Attribute("RightToLeft").Value); } } } break; default: throw new Exception("Unknown component type encountered!"); } if (componentElement.Attribute("DataSource") != null) { pc.DataBindingSource = componentElement.Attribute("DataSource").Value; } if (componentElement.Element("Binding") != null) { XElement binding = componentElement.Element("Binding"); switch (int.Parse(binding.Attribute("ID").Value)) { case -1: // page number { if (pc is TextComponent) { if (pc.DataBindingSource == "Index") { ((TextComponent)pc).BindingOption = TextComponent.SpecialBinding.Index; } if (pc.DataBindingSource == "Page Number") { ((TextComponent)pc).BindingOption = TextComponent.SpecialBinding.PageNumber; } } }; break; case -2: // static image foreach (XElement input in binding.Element("Inputs").Elements()) { if (input.Attribute("Name").Value == "ImagePath") { string url = input.Attribute("Value").Value; if (ConfigurationManager.AppSettings.AllKeys.Contains("DesignerImageDirectory") && !string.IsNullOrEmpty(ConfigurationManager.AppSettings["DesignerImageDirectory"]) && ConfigurationManager.AppSettings.AllKeys.Contains("DesignerImageURL") && !string.IsNullOrEmpty(ConfigurationManager.AppSettings["DesignerImageURL"])) { string directory = ConfigurationManager.AppSettings["DesignerImageDirectory"].ToString(); string designerurl = ConfigurationManager.AppSettings["DesignerImageURL"]; if (url.Contains(designerurl)) { url = url.Replace(designerurl, directory).Replace(@"\\", @"\").Replace("/", @"\"); } } pc.SetData(url); } } ; break; case -3: // static text foreach (XElement input in binding.Element("Inputs").Elements()) { if (input.Attribute("Name").Value == "TextInput") { pc.SetData(input.Attribute("Value").Value); } } break; } } cc.AddChild(pc); return(pc); } else { throw new Exception("Component has no type defined"); } }