ASP.NET Page - Save To PDF

Currently we are using your product to export a ASP.NET page to Excel with the following code in the LoadComplete event of the page:


'Get HTML of the page
Dim oStringWriter As New StringWriter()
Dim oHtmlTextWriter As New HtmlTextWriter(oStringWriter)

Render(oHtmlTextWriter)

Dim reader = New StringReader(oStringWriter.ToString())

Dim strContent As String = reader.ReadToEnd.ToString()

'Let strip off all the header info
strContent = Mid(strContent, InStr(strContent, “–>”) + 4)

strContent = StripTags(strContent)

'Get rid of tags
strContent = Replace(strContent, “&nbsp”, “”)
strContent = Replace(strContent, “”, “”)
strContent = Replace(strContent, “”, “”)
strContent = Replace(strContent, “”, “”)

strContent = Trim(strContent)

'Set the values for splitting the data into an array
Dim arg() As String = {vbCrLf, vbLf}

Dim objArrayList As New ArrayList(strContent.Split(arg, StringSplitOptions.RemoveEmptyEntries))

'Loop through removing items that are blank
For iLoop As Integer = objArrayList.Count - 1 To 0 Step -1
If Trim(objArrayList(iLoop).ToString()) = “” Then
objArrayList.RemoveAt(iLoop)
End If
Next

'Excel
Dim workbook As Workbook = New Workbook()
Dim sheet As Worksheet = workbook.Worksheets(0)

'Put the data into the spreadsheet
sheet.Cells.ImportArrayList(objArrayList, 1, 0, True)

'Get Worksheet Cells Collection
Dim cell As Cells = sheet.Cells

'Set Style
For i As Integer = 0 To cell.Count

'Increase the width of First Column Width
cell.SetColumnWidth(0, 150)

'Increase the height of first row
cell.SetRowHeight(0, 36)

'Trim the extra spaces
cell(i, 0).Value = Trim(cell(i, 0).Value)

'Get Style
Dim style As Style = cell(i, 0).GetStyle()

'Make Cell’s Text wrap
style.IsTextWrapped = True

'Set Style
cell(i, 0).SetStyle(style)
Next

'Save
workbook.Save(HttpContext.Current.Response, “TDRDATA_Export.xls”, Aspose.Cells.ContentDisposition.Attachment, New XlsSaveOptions(Aspose.Cells.SaveFormat.Excel97To2003))

HttpContext.Current.Response.End()



We’d like to do the the same logic but with exporting to PDF. Any ideas?

Hi Cean,


Thanks for your inquiry. Aspose.Pdf for .NET supports creation of PDF from a text string containing HTML tags. It renders that text according to its HTML tags. Please check following documentation link for the details/code snippet. Hopefully it will help you achieve your desired results.


Please feel free to contact us for any further assistance.

Best Regards,

I’m able to get the Export to work but the Save method causes an exception - "Thread was being aborted"

Here is the code that is in the LoadComplete event of the form:

'Get HTML of the page
Dim oStringWriter As New StringWriter()
Dim oHtmlTextWriter As New HtmlTextWriter(oStringWriter)

Render(oHtmlTextWriter)

Dim reader = New StringReader(oStringWriter.ToString())
Dim stream = New MemoryStream(System.Text.Encoding.UTF8.GetBytes(reader.ReadToEnd()))

’ Instantiate an object PDF class
Dim pdf As Aspose.Pdf.Generator.Pdf = New Aspose.Pdf.Generator.Pdf()

’ add the section to PDF document sections collection
Dim section As Aspose.Pdf.Generator.Section = pdf.Sections.Add()

’ Create text paragraphs containing HTML text
Dim text2 As Aspose.Pdf.Generator.Text = New Aspose.Pdf.Generator.Text(section, oHtmlTextWriter.InnerWriter.ToString())

’ enable the property to display HTML contents within their own formatting
text2.IsHtmlTagSupported = True

’ Add the text object containing HTML contents to PD Sections
section.Paragraphs.Add(text2)

’ Save the pdf document
pdf.Save(“Export.pdf”, Generator.SaveType.OpenInBrowser, Response)

reader.Close()
stream.Close()


So, everything works but an exception occurs. Any ideas on why? Am I not calling it correctly? Am I calling it in the wrong event?

Hi Cean,

Thanks for your inquiry. The problem probably occurs because of operation timeout. Are you getting same issue with other pages, while convert to Pdf? Please increase executionTimeout property of IIS server. Hopefully your issue will be resolved. If issue persist then please share a sample project here to replicate the issue? So we will look into it and will provide you more information.

Sorry for the inconvenience faced.

Best Regards,