I wrote a program which runs through lists of .pdf files and does the following:
1) Opens the original .pdf files
2) Creates a new .pdf file
3) Loops through the pages of the original .pdf file and copies each one to the new .pdf
4) Saves the new .pdf to a different location
As far as I can tell, everything is being Disposed of properly with Using statements. However, if I run this program through a large number of .pdf files, the running program takes up more and more memory until eventually either the program or the computer crashes. The larger the .pdf files being copied, the quicker this happens.
Shouldn't the memory used by the original and new .pdf files be freed back up upon closing them?
The following code runs through 1000 .pdf files (named sample_pdf_1.pdf through sample_pdf_1000.pdf) and basically copies them to another folder, 1 page at a time. I know there are easier ways of doing this, but copying is not my final objective. I will be eventually modifying the individual pages. However, just doing this results in the program using all available free memory and crashing, so moving to the next step is not an option using Spire.PDF at this time.
Sample Code:
- Code: Select all
Imports Spire.Pdf
Imports Spire.Pdf.Graphics
Public Class Sample
Public Sub CopyPDF()
Dim InFolder As String = "c:\in_folder\"
Dim OutFolder As String = "c:\out_folder\"
Dim InFilename As String = String.Empty
For i = 1 To 1000
InFilename = "sample_pdf_" & i.ToString.Trim & ".pdf"
Using OriginalPDF As PdfDocument = New PdfDocument
OriginalPDF.LoadFromFile(InFolder & InFilename)
Using NewPDF As PdfDocument = New PdfDocument
For pg = 0 To OriginalPDF.Pages.Count - 1
NewPDF.InsertPage(OriginalPDF, pg)
Next pg
NewPDF.SaveToFile(OutFolder & InFilename)
End Using
End Using
Next i
End Sub
End Class