Saving DocX doc Client side from server-side ASP.NET Application

Jan 9, 2014 at 7:50 PM
Thank you for viewing my question. I was introduced to DocX yesterday, and everything has been going well so far, except this one issue.

I have built an asp.net application that builds a document automatically from user input in a web browser. The problem is that program is server side. Figuring out how to open the document client said has been a big pain for me.

Thus far, I have been able to save the document to a MemoryStream, but that is as far as I have gotten. How can I 'grab' the data from memory stream and allow the user on the client side download the doc?

I will post segments of code below. (This is obviously not all the code, but only a portion that pertains to this question.

ANY help will be greatly appreciated!

Protected Sub Button1_Click(ByVal sender As Object, ByVal e As EventArgs) Handles Button1.Click
    __Dim filename As String = "Minutes.docx"
    Dim contents As Byte()
    Using ms As New MemoryStream()
        Using document As DocX = DocX.Create(filename)

            document.AddHeaders()
            Dim header As Header = document.Headers.odd
            Dim header2 As Header = document.Headers.even
            Dim h As Paragraph = header.InsertParagraph()
            h.Append("XXXXXXXXX").Font(New FontFamily("Arial"))
            h.Alignment = Alignment.center
            Dim h2 As Paragraph = header2.InsertParagraph()
            h2.Append("XXXXXXXXX").Font(New FontFamily("Arial"))
            h2.Alignment = Alignment.center

            Dim p As Paragraph = document.InsertParagraph()
            p.Append(commnamedd.Text + " Committee").Font(New FontFamily("Arial"))
            p.Alignment = Alignment.center
            p.LineSpacingAfter = 1

        document.SaveAs(ms)

        End Using
        contents = ms.ToArray()
    End Using__

End Sub
__
Thanks,

Josh
Jan 9, 2014 at 9:42 PM
Edited Jan 9, 2014 at 9:56 PM
I was able to get it to work with the following code. Maybe this will help others...
            Using ms As MemoryStream = New MemoryStream()
            Using document As DocX = DocX.Create(ms)

            document.AddHeaders()
            Dim header As Header = document.Headers.odd
            Dim header2 As Header = document.Headers.even
            Dim h As Paragraph = header.InsertParagraph()
            h.Append("XXXXX").Font(New FontFamily("Arial"))
            h.Alignment = Alignment.center
            Dim h2 As Paragraph = header2.InsertParagraph()
            h2.Append("XXXXX").Font(New FontFamily("Arial"))
            h2.Alignment = Alignment.center 

            document.SaveAs(ms)
            Response.ContentType = "application/msword"
            Response.AddHeader("content-disposition", "inline; filename=minutes.docx")
            Response.AddHeader("content-length", ms.Length.ToString())
            Response.BinaryWrite(ms.ToArray())
            Response.End()

            End Using

            End Using

Thanks,

Josh
Marked as answer by jwellslc on 1/9/2014 at 2:51 PM