I am trying to generate a large amount (>5000) of pdf documents dynamically. The problem is during the pdf generation I get an out of memory exception. I have decided to break up my 5000 page pdf into smaller 500 pdf's but when I try it this way .net still uses the same memory. Is there a way to force my application to free unused memory that a variable is still using? Here is the code causing the problem
For Each patronRow As DataRow In patronsDatatable.Rows
.....some other code
If FARPrintOneLetterPerStudent = False Then
If farApplicationID <> _previousFarApplicationID Or _firstapp = True Then
lettercount = lettercount + 1
'************* test code *****************
If lettercount = 500 Then
If Me._printLabels = False Then
FixEndPdf(finalpdf)
End If
' Export moved from dataprovider
smallpdfcount = smallpdfcount + 1
ExportToPdf(ImportRtf(finalpdf), smallpdfcount)
finalpdf = Nothing
_isfirst = True
lettercount = 0
Else
If lettercount < 500 Then ' we only need to add to this variable when there is less than 500 pages
finalpdf = AddtoletterPdf(letterBody, printmultiple)
'_previewpdf = finalpdf
End If
_previousFarApplicationID = farApplicationID
_firstapp = False
End If
End If
Else
finalpdf = AddtoletterPdf(letterBody, printmultiple)
End If
'create a record in LettersDatatable for POS and FAR letters
AddToLettersDataTable(letterBody, patronID, patronName, farApplicationID, headerImageBuffer)
Else
'Create a record in labels datatable
AddToLabelsDatatable(letterBody, patronName, patronID, farApplicationID)
End If
End If
Next
Public Function ImportRtf(ByVal content As String) As RadDocument
Dim provider As New RtfFormatProvider()
Try
Return provider.Import(content)
Catch ex As Exception
MessageBox.Show("Error in Import to Pdf")
End Try
End Function
Public Sub ExportToPdf(ByVal document As RadDocument, smallpdfcount As Integer)
Dim provider As New PdfFormatProvider
Dim OneSourceFolder As String = GetInstallFolder()
Try
Using output As Stream = File.Create(OneSourceFolder & "\letter" & smallpdfcount & ".pdf")
'Using output As Stream = File.Create(OneSourceFolder & "\letter.pdf")
provider.Export(document, output)
End Using
Catch ex As Exception
MessageBox.Show("Error in ExporttoPdf")
End Try