We are currently attempting to generate a word document from one of our HTML generated reports in .NET 3.5 using Aspose.Words 9.4.0.0. I have attached a .txt file containing the HTML that is generated through the RenderControl.
Case 1:
When we attempt to convert to Docx through our own response stream, we can generate the docx file but we receive the error that the file is corrupt upon opening the file in Word.
The exact error is:
*The file “filename” cannot be opened because there are problems with the contents.
Details - The file is corrupt and cannot be opened.
Word found unreadable content in “filename”. Do you want to recover the contents of this document? If you trust the source of this docuement, click Yes.*
The code we use for this case is:
Dim strWriter As New System.IO.StringWriter
Dim htmWriter As New System.Web.UI.HtmlTextWriter(strWriter)
Dim fileName As String = "Report" & lblContractNumber.Text
Me.RenderControl(htmWriter)
ExportToDocX(fileName, strWriter.ToString(), Response, 1)
'in another file/class
Public Sub ExportToDocX(ByRef fileName As String, ByRef htmlString As String, ByRef response As System.Web.HttpResponse, ByVal type As Integer)
Select Case type
Case 1
ExportDocXTest(fileName, response, htmlString)
Case 2
Dim outStream As New System.IO.MemoryStream
ExportToDocX(htmlString, outStream)
OutputDocument(response, outStream, fileName, "docx")
End Select
End Sub
Private Sub ExportDocXTest(ByRef fileName As String, ByRef response As System.Web.HttpResponse, ByRef htmlString As String)
Dim docx As Document = New Document()
Dim options As New Aspose.Words.Saving.OoxmlSaveOptions(Aspose.Words.SaveFormat.Docx)
options.PrettyFormat = True
Dim builder As DocumentBuilder = New DocumentBuilder(docx)
builder.ParagraphFormat.Borders(Aspose.Words.BorderType.Top).LineStyle = Aspose.Words.LineStyle.Single
builder.ParagraphFormat.Alignment = ParagraphAlignment.Center
builder.InsertHtml(htmlString)
docx.Save(response, fileName & ".docx", Words.ContentDisposition.Attachment, options)
End Sub
Case 2
When we convert to Docx through passing the response into the docx.Save method from Aspose we get the server error:
Server Error in ‘/’ Application.
A page can have only one server-side Form tag.
The code we use for this case is:
Dim strWriter As New System.IO.StringWriter
Dim htmWriter As New System.Web.UI.HtmlTextWriter(strWriter)
Dim fileName As String = "Report" & lblContractNumber.Text
Me.RenderControl(htmWriter)
ExportToDocX(fileName, strWriter.ToString(), Response, 2)
'in another file/class
Public Sub ExportToDocX(ByRef fileName As String, ByRef htmlString As String, ByRef response As System.Web.HttpResponse, ByVal type As Integer)
Select Case type
Case 1
ExportDocXTest(fileName, response, htmlString)
Case 2
Dim outStream As New System.IO.MemoryStream
ExportToDocX(htmlString, outStream)
OutputDocument(response, outStream, fileName, "docx")
End Select
End Sub
Private Sub ExportToDocX(ByRef htmlString As String, ByRef outStream As System.IO.MemoryStream)
Dim docx As Document = New Document()
Dim builder As DocumentBuilder = New DocumentBuilder(docx)
builder.ParagraphFormat.Borders(Aspose.Words.BorderType.Top).LineStyle = Aspose.Words.LineStyle.Single
builder.ParagraphFormat.Alignment = ParagraphAlignment.Center
builder.InsertHtml(htmlString)
docx.UpdateTableLayout()
docx.Save(outStream, Words.SaveFormat.Docx)
End Sub
Private Function OutputDocument(ByRef response As System.Web.HttpResponse, ByRef outStream As System.IO.MemoryStream, ByRef fileName As String, ByVal OutputType As String)
Dim outBuf() As Byte = outStream.GetBuffer()
response.Expires = 0
response.Buffer = True
response.Cache.SetCacheability(HttpCacheability.NoCache)
response.ClearHeaders()
response.AddHeader("content-disposition", "attachment; filename=" & fileName & "." & OutputType)
response.ClearContent()
Select Case OutputType
Case "doc"
response.ContentType = "application/msword" 'Word 97-2003
Case "docx"
response.ContentType = "application/vnd.openxmlformats-officedocument.wordprocessingml.document"
Case "pdf"
response.ContentType = "application/x-pdf" 'Adobe PDF (download)
End Select
'Download Output
response.BinaryWrite(outBuf)
response.End()
outStream.Close()
End Function
What is causing these errors and/or file corruptions? And is there a way we can tell the converted html to fit inside the resulting document?