コード例 #1
0
        public IndexBuilder(PrintableDocument pd)
        {
            doc           = pd;
            components    = new List <IndexItem>();
            indexElements = new List <IndexComponent>();

            // loop through the whole document, picking up the elements designated as index holders and adding all other elements to the components list;
            foreach (PrintablePage pp in pd.Pages)
            {
                if (indexPage == null)
                {
                    indexesPerIndexPage = 0;
                }

                foreach (PrintableComponent pc in pp.Children)
                {
                    if (pc is CompositeComponent)
                    {
                        AddComponent((CompositeComponent)pc);
                    }

                    if ((indexPage == null || indexPage == pp) && pc is TextComponent && ((TextComponent)pc).BindingOption == TextComponent.SpecialBinding.Index)
                    {
                        if (indexPage == null) // no index page set?
                        {
                            indexPage = pp;
                            indexPageOriginalIndex = doc.Pages.IndexOf(indexPage);
                        }

                        if (indexElementHeight == 0.0)
                        {
                            indexElementHeight = pc.Height;
                        }
                        else
                        {
                            pc.Height = indexElementHeight;
                        }

                        if (indexStyle == null)
                        {
                            indexStyle = ((TextComponent)pc).Style;
                            lineHeight = Util.PointToMillimeter(((TextComponent)pc).Style.AsXFont().GetHeight());
                        }
                        else
                        {
                            ((TextComponent)pc).Style = indexStyle;
                        }

                        indexesPerIndexPage++;
                    }
                }
            }
            if (indexPage != null)
            {
                doc.Pages.Remove(indexPage);
            }
        }
コード例 #2
0
        public IndexComponent(double left, double top, double width, double height, double angle, PrintableStyle ps)
            : base(left, top, width, height, angle)
        {
            PrintableStyle leftStyle = ps.Clone();

            leftStyle.FontAlignment = PdfSharp.Drawing.Layout.XParagraphAlignment.Left;
            PrintableStyle rightStyle = ps.Clone();

            rightStyle.FontAlignment = PdfSharp.Drawing.Layout.XParagraphAlignment.Right;

            leftTextComponent = new TextComponent(left, top, width, height, angle)
            {
                Style = leftStyle
            };

            rightTextComponent = new TextComponent(left, top, width, height, angle)
            {
                Style = rightStyle
            };
        }
コード例 #3
0
 public TextComponent(double left, double top, double width, double height, double angle)
     : base(left, top, width, height, angle)
 {
     Style = new PrintableStyle();
 }
コード例 #4
0
        /// <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");
            }
        }