Esempio n. 1
0
        public ActionResult UploadDoc(HttpPostedFileBase File)
        {
            if (File == null)
            {
                throw new Exception("Please, browse to UploadTemplate.docx in the Template folder");
            }
            if (File.InputStream.Length == 0)
            {
                throw new Exception("Invalid file");
            }

            BinaryReader b = new BinaryReader(File.InputStream);

            byte[] binData = b.ReadBytes(File.ContentLength);

            // Write the byte array in a memory stream
            using (MemoryStream streamFile = new MemoryStream())
            {
                streamFile.Write(binData, 0, binData.Length);
                // Read the stream package in a word processing document
                using (WordprocessingDocument wordDoc = WordprocessingDocument.Open(streamFile, true))
                {
                    ViewBag.WordML = wordDoc.MainDocumentPart.Document.Body.InnerXml;
                    TableCell myTableCell = wordDoc.MainDocumentPart.Document.Body.Elements <Table>().FirstOrDefault().Elements <TableRow>().ElementAt(1).Elements <TableCell>().FirstOrDefault();
                    ViewBag.HtmlContent = HTMLConverterHelper.ConvertToHtml(wordDoc, myTableCell);
                    ViewBag.Hyperlinks  = HTMLConverterHelper.GetHyperlinks(wordDoc, String.Empty);
                    wordDoc.Close();
                }
            }
            return(View());
        }
Esempio n. 2
0
        /// <summary>
        /// Reads UploadTemplate.docx to get cell's wordML and hyperlinks info
        /// </summary>
        /// <returns>cell's wordML and hyperlinks info</returns>
        private string GetWordMLAndHyperlinks()
        {
            string wordMlOriginal = String.Empty;
            string hyperlinks     = String.Empty;
            //Read template
            string templatePath = GetRootPath() + @"\Template\UploadTemplate.docx";

            using (var templateFile = System.IO.File.Open(templatePath, FileMode.Open, FileAccess.Read)) //read our template
            {
                using (var stream = new MemoryStream())
                {
                    templateFile.CopyTo(stream);
                    using (var wordDoc = WordprocessingDocument.Open(stream, true))
                    {
                        TableCell myTableCell = wordDoc.MainDocumentPart.Document.Body.Elements <Table>().FirstOrDefault().Elements <TableRow>().ElementAt(1).Elements <TableCell>().FirstOrDefault();
                        //Get TableCell wordML
                        wordMlOriginal = myTableCell.InnerXml;
                        //Get Document Hyperlinks
                        wordMlOriginal = HTMLConverterHelper.UpdateHyperlinkIds(wordDoc, wordMlOriginal, "MyId");
                        hyperlinks     = HTMLConverterHelper.GetHyperlinks(wordDoc, "MyId");
                    }
                }
            }
            return(wordMlOriginal + hyperlinks);
        }
Esempio n. 3
0
        /// <summary>
        /// Reads DownloadTemplate.docx to create a new docx. It copies the wordML and hyperlinks information passed
        /// </summary>
        /// <param name="wordMlAndLinks">wordML and hyperlinks info</param>
        /// <returns>New docx</returns>
        private byte[] GetDownload(string wordMlAndLinks)
        {
            //Create/read another word doc and save wordML and links
            string templatePath = GetRootPath() + @"\Template\DownloadTemplate.docx";
            //split WordML and Hyperlinks
            string hyperlinks      = wordMlAndLinks.Substring(wordMlAndLinks.IndexOf("<hyperlinks>"));
            string tableCellWordML = wordMlAndLinks.Substring(0, wordMlAndLinks.IndexOf("<hyperlinks>"));

            //Update Hyperlinks
            using (var templateFile = System.IO.File.Open(templatePath, FileMode.Open, FileAccess.Read))
            {
                using (var stream = ConvertStreamToMemoryStream(templateFile))
                {
                    using (var wordDoc = WordprocessingDocument.Open(stream, true))
                    {
                        TableCell DownloadTemplateTableCell = wordDoc.MainDocumentPart.Document.Body.Elements <Table>().FirstOrDefault().Elements <TableRow>().ElementAt(1).Elements <TableCell>().FirstOrDefault();
                        DownloadTemplateTableCell.InnerXml = tableCellWordML;
                        //Update Hyperlinks
                        HTMLConverterHelper.AddHyperlinks(wordDoc, hyperlinks);
                        wordDoc.MainDocumentPart.Document.Save();
                        wordDoc.Close();

                        return(stream.ToArray());
                    }
                }
            }
        }