Umbraco Export to Word using Aspose.Words

Umbraco ‘Export to Word’ macro allow users to export online content into Word document using Aspose.Words. This macro makes it super simple to have an offline copy of your favorite online content for editing, sharing and printing etc. in most popular Word (doc/docx) format. It adds a simple ‘Export to Word’ button at any desired location on the page and clicking it dynamically exports the content of the page to a word document and then automatically downloads the file to the disk location selected by the user in just couple of seconds.

Export to word Umbraco Export to Word using Aspose.Words

How to install and use Export to Word macro?

Please check our blog post for more details http://www.aspose.com/blogs/aspose-products/aspose-words-product-family/archive/2014/01/15/umbraco-export-to-word-using-aspose.words.html

Sample Code

Variables declared

 private string OutputFileName
        {
            get
            {
                string name = HttpContext.Current.Request.RawUrl.Substring(HttpContext.Current.Request.RawUrl.LastIndexOf("/"));
                name = name.Replace("/", string.Empty).Replace(".aspx", ".doc");
                return name;
            }
        }

        private string CurrentPageURL
        {
            get
            {
                string url = Request.Url.Authority + HttpContext.Current.Request.RawUrl.ToString();

                if (Request.ServerVariables["HTTPS"] == "on")
                {
                    url = "https://" + url;
                }
                else
                {
                    url = "http://" + url;
                }

                return url;
            }
        }

        private string BaseURL
        {
            get
            {
                string url = Request.Url.Authority;

                if (Request.ServerVariables["HTTPS"] == "on")
                {
                    url = "https://" + url;
                }
                else
                {
                    url = "http://" + url;
                }

                return url;
            }
        }

Below is the code behind Export to Word in web user control:

string html = new WebClient().DownloadString(CurrentPageURL);

            // To make the relative image paths work, base URL must be included in head section
            html = html.Replace("", string.Format("", BaseURL));

            // Check for license and apply if exists
            string licenseFile = Server.MapPath("~/App_Data/Aspose.Total.lic");
            if (File.Exists(licenseFile))
            {
                License license = new License();
                license.SetLicense(licenseFile);
            }

            MemoryStream stream = new MemoryStream(Encoding.UTF8.GetBytes(html));
            Document doc = new Document(stream);
            doc.Save(Response, OutputFileName, ContentDisposition.Inline, null);
            Response.End();

Video

Please check the video below to see this macro in action.
http://www.youtube.com/watch?v=3EHf3Giq6l0

Download

Last edited Feb 13, 2014 at 6:54 AM by asposemarketplace, version 7