Esempio n. 1
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. 2
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());
        }