Exemplo n.º 1
0
        protected void butDownloadHTML_Click(object sender, EventArgs e)
        {
            if (fileupload.HasFile)
            {
                try
                {
                    string myDate                     = DateTime.Now.ToString("MM-dd-yyyy--H-mm-ss--");
                    string filename                   = Path.GetFileName(fileupload.FileName);
                    string fullpath                   = Server.MapPath("/Uploaded/") + myDate + filename;
                    string fullpathExportDocX         = fullpath.Replace(".docx", "-GroupDocsExport.docx");
                    string fullpathExportEmbeddedHTML = fullpath.Replace(".docx", "-GroupDocsExportEmbedded.html");
                    string fullpathExportHTML         = fullpath.Replace(".docx", "-GroupDocsExport.html");
                    string fullpathExportPDF          = fullpath.Replace(".docx", "-GroupDocsExport.pdf");
                    fullpathExportDocX         = fullpathExportDocX.Replace("Uploaded", "Exported");
                    fullpathExportHTML         = fullpathExportHTML.Replace("Uploaded", "Exported");
                    fullpathExportPDF          = fullpathExportPDF.Replace("Uploaded", "Exported");
                    fullpathExportEmbeddedHTML = fullpathExportEmbeddedHTML.Replace("Uploaded", "Exported");
                    fileupload.SaveAs(fullpath);
                    using (Editor editor = new Editor(fullpath))
                    {
                        WordProcessingEditOptions editOptions = new WordProcessingEditOptions();
                        editOptions.EnablePagination = false;
                        EditableDocument readyToSave = editor.Edit(editOptions);

                        if (!string.IsNullOrEmpty(fullpathExportDocX))
                        {
                            editor.Save(readyToSave, fullpathExportDocX, new WordProcessingSaveOptions(WordProcessingFormats.Docx));
                        }
                        if (!string.IsNullOrEmpty(fullpathExportPDF))
                        {
                            editor.Save(readyToSave, fullpathExportPDF, new PdfSaveOptions());
                        }
                        if (!string.IsNullOrEmpty(fullpathExportHTML))
                        {
                            readyToSave.Save(fullpathExportHTML);
                        }
                        if (!string.IsNullOrEmpty(fullpathExportEmbeddedHTML))
                        {
                            var html = readyToSave.GetEmbeddedHtml();
                            File.WriteAllText(fullpathExportEmbeddedHTML, html);
                        }

                        readyToSave.Dispose();
                        editor.Dispose();
                    }
                    lblStatus.Text = "Upload status: File uploaded and converted!";
                }
                catch (Exception ex)
                {
                    lblStatus.Text = "Upload status: The file could not be uploaded. The following error occured: " + ex.Message;
                }
            }
        }
        private static IEditOptions GetEditOptions(string guid)
        {
            string       extension = Path.GetExtension(guid).Replace(".", "").ToLowerInvariant();
            IEditOptions options   = null;

            if (extension.ToLowerInvariant().Equals("txt"))
            {
                options = new TextEditOptions();
            }
            else
            {
                foreach (var item in typeof(WordProcessingFormats).GetFields())
                {
                    if (item.Name.ToLowerInvariant().Equals("auto"))
                    {
                        continue;
                    }

                    if (item.Name.ToLowerInvariant().Equals(extension))
                    {
                        options = new WordProcessingEditOptions();
                        break;
                    }
                }

                foreach (var item in typeof(PresentationFormats).GetFields())
                {
                    if (item.Name.ToLowerInvariant().Equals("auto"))
                    {
                        continue;
                    }

                    if (item.Name.ToLowerInvariant().Equals(extension))
                    {
                        options = new PresentationEditOptions();
                        break;
                    }
                }

                if (options == null)
                {
                    options = new SpreadsheetEditOptions();
                }
            }

            return(options);
        }