/// <summary>
        /// Load class defined into the string parameters.
        /// Use 'classname' tag to define class and try to pass string parameters to configure it. Class must be IVarFormatter.
        /// </summary>
        /// <returns></returns>
        protected virtual void LoadCustomClass()
        {
            IDictionary <string, string> parameters = GetFormatterParametersData();

            if (parameters == null)
            {
                throw new ArgumentNullException("Class Name");
            }

            if (!parameters.ContainsKey(ClassNameTag))
            {
                throw new ArgumentNullException("Class Name");
            }

            string className = parameters[ClassNameTag];
            Type   tLoad     = AssembliesTypesLoader.GetType(className);

            if (tLoad == null)
            {
                throw new Exception("Class '" + className + "' not found!");
            }

            object objLoad = null;

            try{
                objLoad = Activator.CreateInstance(tLoad, new object[] { FormatterParameters });                 //string constructor first
            }catch (Exception) {
                //if crashes use anonymous constructor..
                try{
                    objLoad = Activator.CreateInstance(tLoad, new object[] {});
                }catch (Exception) {      }
            }

            if (objLoad == null)
            {
                throw new Exception("Object invocation failed!");
            }

            if (!(objLoad is IVarFormatter))
            {
                throw new InvalidCastException("Object isn't IVarFormatter");
            }

            this._customFormatter = (IVarFormatter)objLoad;
        }
        private void Init()
        {
            //20130604 :: mellorasinxelas
            //iTextSharp control.
            Version iTextSharpVersion = AssembliesTypesLoader.GetAssemblyVersion(typeof(Image));

            if (iTextSharpVersion == new Version(0, 0, 0, 0))
            {
                throw new DllNotFoundException("iTextSharp DLL not loaded!");
            }
            if (iTextSharpVersion < new Version(5, 0))
            {
                throw new NotSupportedException(GetType().Name + " requires iTextSharp 5.0 or higher!");
            }
            //----

            //20130607 :: mellorasinxelas :: add background image
            //            if (fontpath != string.Empty)
            //            {
            //                PdfDrawer = new Moon.PDFDrawItextSharp.PDFDrawItextSharp(
            //                    pageSize,
            //                    PageDefinition.Margin_left,
            //                    PageDefinition.Margin_right,
            //                    PageDefinition.Margin_top,
            //                    PageDefinition.Margin_bottom,
            //                    stream,
            //                    fontpath);
            //            }
            //            else
            //            {
            //                PdfDrawer = new Moon.PDFDrawItextSharp.PDFDrawItextSharp(
            //                    pageSize,
            //                    PageDefinition.Margin_left,
            //                    PageDefinition.Margin_right,
            //                    PageDefinition.Margin_top,
            //                    PageDefinition.Margin_bottom,
            //                    stream);
            //            }

            //20150914: customizable builder.
            PdfDrawer = BuildPDFDrawer(pageSize,
                                       PageDefinition.Margin_left,
                                       PageDefinition.Margin_right,
                                       PageDefinition.Margin_top,
                                       PageDefinition.Margin_bottom,
                                       stream,
                                       fontpath);
            if (PdfDrawer == null)
            {
                throw new NullReferenceException("PDF Drawer");
            }
            //---

            //20130607
            if (PageDefinition.BackgroundImage != null)
            {
                //adds background image.
                PdfDrawer.SetBackgroundImage(PageDefinition.BackgroundImage);
            }
            //----

            //20130606 :: Absolute Footer
            if (PageDefinition.Footer != null && PageDefinition.Footer.RowGroup is FooterGroup && ((FooterGroup)PageDefinition.Footer.RowGroup).Absolute)
            {
                //reserve space to absolute footer...
                ((PDFDrawItextSharp.PDFDrawItextSharp)PdfDrawer).ReserveSpaceToFooter(PageDefinition.Footer.RowGroup.Y);
            }
            //----
        }
Example #3
0
        //20130406 :: mellorasinxelas
        /// <summary>
        /// Build programmer custom element.
        /// </summary>
        /// <param name="node"></param>
        /// <param name="fontAttrs"></param>
        /// <returns></returns>
        public DrawElement BuildCustomElement(XmlNode node, XmlAttributeCollection fontAttrs)
        {
            //load object name
            string classname = XmlHelper.GetAttributeValue(DefaultCustomElement.ClassNameAttributeCustomElement, node.Attributes, null);

            if (classname == null)
            {
                throw new NullReferenceException("Custom elements needs Class Name!");
            }

            //load VARS

            List <Variable> vars = new List <Variable>();

            XmlNodeList varNodes = node.SelectNodes(VarElementConstant);

            foreach (XmlNode varNode in varNodes)
            {
                string name = XmlHelper.GetAttributeValue(Variable.NameAttributeConstant, varNode.Attributes, "");

                if (name != string.Empty)
                {
                    //load formatter
                    DefaultFormat?formatter       = null;
                    string        formatterParams = null;
                    formatter = BasicVarFormatter.Parse(XmlHelper.GetAttributeValue(FormatableVariable.FormatterAttributeConstant, varNode.Attributes, null));
                    if (formatter != null)
                    {
                        formatterParams = XmlHelper.GetAttributeValue(FormatableVariable.FormatterParametersAttributeConstant, varNode.Attributes, null);
                    }

                    if (UseOptionalTags)
                    {
                        bool optional = XmlHelper.GetAttributeBoolean(Variable.OptionalAttributeConstant, varNode.Attributes, false);
                        if (formatter == null)
                        {
                            vars.Add(new Variable(name, optional));
                        }
                        else
                        {
                            vars.Add(new FormatableVariable(name, optional, formatter.Value, formatterParams));
                        }
                    }
                    else
                    {
                        if (formatter == null)
                        {
                            vars.Add(new Variable(name));
                        }
                        else
                        {
                            vars.Add(new FormatableVariable(name, formatter.Value, formatterParams));
                        }
                    }
                }
            }

            //load object

            Type tLoad = null;

            try
            {
                tLoad = AssembliesTypesLoader.GetType(classname);
            }
            catch (Exception ex1)
            {
                Console.WriteLine(ex1.ToString());
            }

            if (tLoad == null)
            {
                throw new Exception("Custom element '" + classname + "' type don't found!");
            }

            object objLoad = null;

            try
            {
                objLoad = Activator.CreateInstance(tLoad, new object[] { fontAttrs, node.Attributes, vars });
            }
            catch (Exception ex2)
            {
                try
                {
                    objLoad = Activator.CreateInstance(tLoad, new object[] { fontAttrs, node.Attributes });
                }
                catch (Exception ex3)
                {
                    Console.WriteLine("Custom element must have (fontAttr, textAttr) builder or (fontAttr, textAttr, List<Variables>)");
                    Console.WriteLine(ex2.ToString());
                    Console.WriteLine(ex3.ToString());
                }
            }
            if (objLoad == null)
            {
                throw new NullReferenceException("Object '" + classname + "' don't loaded!");
            }
            if (!(objLoad is DrawElement))
            {
                throw new Exception("Object '" + classname + "' type doesn't match!. DrawElement is required!");
            }

            return((DrawElement)objLoad);
        }