Word documents - import to SQL Server - retrieve to display in web application

Hi

We have between 2 and 3 hundred documets, formatted and currently in DOCX format.

We are moving to an Azure solution and wish to import these documents in SQL Server (Azure SQL), then using C# in ASP.NET we wish to retrieve a subset of these documents, update a mergefield/marker in each document, merge the documents together and then display them to the user in the Web App.

The user depending on their role, can have read-only OR a read-only with a print option.

Ideally in the next phase of our system, we need the ability for the user to be able to edit the document within the viewer.

Currently I have used a number of methods to import the documents, I have converted them to XML, Opendocument, XPS and stored the in a field in the database. I have used XML, nchar, vchar and binary, which i fine as they will store them.

Its when I retrieve the document (XML string or databasefield) from the database that when I try to display the document in its original format.

One methd I used to store the document; I had to replac the UTF-8 as SQL server wasn't allowing it! This is the OPENXML method

string sXML;
            WordprocessingDocument wordDoc =   WordprocessingDocument.Open(@"D:\Dev\FullHealth\CodeBase\Paragraphs\CVS-1.docx", true);
            using (StreamReader sr = new StreamReader(wordDoc.MainDocumentPart.GetStream()))
            {
                 sXML = sr.ReadToEnd();
            }
        sXML = sXML.Replace(<span style="color: rgb(163, 21, 21);">@"UTF-8"</span>, <span style="color: rgb(163, 21, 21);">@"UTF-16"</span>);

        FHAzureBO.<span style="color: rgb(43, 145, 175);">ParasOutputs</span> prsOutput = <span style="color: blue;">new</span> <span style="color: rgb(43, 145, 175);">ParasOutputs</span>();
        prsOutput.Text = sXML;
        prsOutput.Paragraph = <span style="color: rgb(163, 21, 21);">"CVS-1"</span>;
        prsOutput.IsNew = <span style="color: blue;">false</span>;
        prsOutput.UPDATE(<span style="color: rgb(43, 145, 175);">ConfigurationManager</span>.ConnectionStrings[<span style="color: rgb(163, 21, 21);">"ParasConnectionString"</span>].ConnectionString);</pre><p>Then to retrieve it; One of many methods I have tried. The output docuemtn is just plan XML and doesn't reflect the original format;</p><p> </p><pre style="background: white; color: black; font-family: Consolas;">      <span style="color: rgb(43, 145, 175);">WordprocessingDocument</span> doc = <span style="color: rgb(43, 145, 175);">WordprocessingDocument</span>.Create
           (<span style="color: rgb(163, 21, 21);">@"D:\Dev\FullHealth\CodeBase\Paragraphs\CVS-TEST.xml"</span>, <span style="color: rgb(43, 145, 175);">WordprocessingDocumentType</span>.Document);

        <span style="color: green;">//GET THE FILE</span>
        <span style="color: green;">//FHAzureBO.ParasOutputs thisParasOutputs;</span>
        FHAzureBO.<span style="color: rgb(43, 145, 175);">ParasOutputs</span> prsOutput = <span style="color: blue;">new</span> <span style="color: rgb(43, 145, 175);">ParasOutputs</span>();
        <span style="color: rgb(43, 145, 175);">DataSet</span> ds = prsOutput.ParasOutputsDSGet_ByCode(<span style="color: rgb(163, 21, 21);">"CVS-1"</span>, <span style="color: rgb(43, 145, 175);">ConfigurationManager</span>.ConnectionStrings[<span style="color: rgb(163, 21, 21);">"ParasConnectionString"</span>].ConnectionString);

        <span style="color: blue;">string</span> sXML = ds.Tables[0].Rows[0][<span style="color: rgb(163, 21, 21);">"Text"</span>].ToString();
                   
        <span style="color: rgb(43, 145, 175);">MainDocumentPart</span> mainPart = doc.AddMainDocumentPart();
        mainPart.Document = <span style="color: blue;">new</span> <span style="color: rgb(43, 145, 175);">Document</span>();


        <span style="color: rgb(43, 145, 175);">Text</span> textFirstLine = <span style="color: blue;">new</span> <span style="color: rgb(43, 145, 175);">Text</span>(sXML);
        <span style="color: rgb(43, 145, 175);">Run</span> run = <span style="color: blue;">new</span> <span style="color: rgb(43, 145, 175);">Run</span>(textFirstLine);
        <span style="color: rgb(43, 145, 175);">Paragraph</span> para = <span style="color: blue;">new</span> <span style="color: rgb(43, 145, 175);">Paragraph</span>(run);
        <span style="color: rgb(43, 145, 175);">Body</span> body = <span style="color: blue;">new</span> <span style="color: rgb(43, 145, 175);">Body</span>(para);

        mainPart.Document.Append(body);
        mainPart.Document.Save();
        doc.Close();</pre><pre style="background: white; color: black; font-family: Consolas;"> </pre><p>Many thanks</p>

Hi,

Thanks for your query. Please read following documentation link to load and save a document to database.

http://www.aspose.com/documentation/.net-components/aspose.words-for-.net/howto-load-and-save-a-document-to-database.html