public static Form LoadForm(this IDocument document, XElement formRoot, CustomFormProvider customForm)
        {
            Form ret = DocumentHelper.FormObjectCreator.CreateForm(formRoot, document);

            if (customForm != null)
            {
                ret.PageWidth   = Math.Max(customForm.PageSize.Width, 0);
                ret.PageHeight  = Math.Max(customForm.PageSize.Height, 0);
                ret.PageMargins = customForm.PageMargins;
            }

            return(ret);
        }
        public static XElement GetFormXmlRoot(this IDocument document, out CustomFormProvider customForm, string documentTemplatesFolder = null, bool ignoreExisting = false)
        {
            if (documentTemplatesFolder == null)
            {
                documentTemplatesFolder = BusinessDomain.AppConfiguration.DocumentTemplatesFolder;
            }

            string    templatesDir = Path.GetFileName(documentTemplatesFolder);
            XDocument templatesXml = null;

            if (Directory.Exists(documentTemplatesFolder) && (string.IsNullOrWhiteSpace(templatesDir) || !templatesDir.StartsWith("Custom_") || !ignoreExisting))
            {
                string templateFile = Path.Combine(documentTemplatesFolder, Path.ChangeExtension(document.FormName, "xml"));
                if (File.Exists(templateFile))
                {
                    templatesXml = XDocument.Load(templateFile);
                }
            }

            if (templatesXml == null)
            {
                string dir   = Path.GetFileName(documentTemplatesFolder);
                int    index = dir.IndexOf("_");
                if (index >= 0)
                {
                    string name = dir.Substring(index + 1);
                    foreach (string directory in Directory.GetDirectories(StoragePaths.DocumentTemplatesFolder))
                    {
                        if (Path.GetFileName(directory) == name)
                        {
                            string templateFile = Path.Combine(directory, Path.ChangeExtension(document.FormName, "xml"));
                            if (File.Exists(templateFile))
                            {
                                templatesXml = XDocument.Load(templateFile);
                            }
                            break;
                        }
                    }
                }
            }

            if (templatesXml == null)
            {
                templatesXml = LoadXML(Assembly.GetExecutingAssembly(), "Warehouse.Component.Printing." + Path.ChangeExtension(document.FormName, "xml"));
            }

            // Get the coresponding report form template
            XElement formRoot = null;

            if (templatesXml != null)
            {
                formRoot = templatesXml.XPathSelectElement(string.Format(@"//Form [@formName='{0}']", document.FormName));
            }

            customForm = null;
            if (formRoot == null && AddonForms.TryGetValue(document.FormName, out customForm))
            {
                formRoot = customForm.GetFormRoot();
            }

            if (formRoot == null)
            {
                throw new ArgumentException(string.Format("The document template form \"{0}\" could not be found", document.FormName));
            }

            return(formRoot);
        }