Still Exception of type 'System.OutOfMemoryException' was thrown

The following code generated error as "System.OutOfMemoryException'" for version 6.0.0. The error was solved by 6.1.0 and 6.2.0. However, if run the code the second time (e.g. on the same page, click button that run the code again), the error generated.

The file the code run against is attached.

Tip for creating error:

1. make copy of the attached. 2. run the code (click a button that run the code). 3. delete generated pdf. 4. re_name the copied. 4. click the button again at in the same session.

Private Sub ResetPageNumber_Pack()

'Move old page number

Dim contentEditor As New Aspose.Pdf.Facades.PdfContentEditor()

Try

'bind input PDF file

contentEditor.BindPdf(outPutFile)

'make sure the regular expression strategy is being used

contentEditor.ReplaceTextStrategy.IsRegularExpressionUsed = True

'specify that you want to replace all the matching string

'by default only the first string will be replaced

contentEditor.ReplaceTextStrategy.ReplaceScope = Aspose.Pdf.Facades.ReplaceTextStrategy.Scope.REPLACE_ALL

'replace any string like 5467-3986-7 with new string

contentEditor.ReplaceText("[0-9.]*[A-Z.]*[a-z.]*[ ]*Page [0-9]+ of [0-9]+", String.Empty)

'save output PDF file

contentEditor.Save(outPutFile)

contentEditor.Close()

contentEditor = Nothing

'Write new page number

'open document

Dim pdfDocument As New Aspose.Pdf.Document(outPutFile)

'create page number stamp

Dim pageNumberStamp As New PageNumberStamp()

'whether the stamp is background

pageNumberStamp.Background = False

pageNumberStamp.Format = "Page # of " & pdfDocument.Pages.Count

pageNumberStamp.BottomMargin = 10

pageNumberStamp.HorizontalAlignment = HorizontalAlignment.Center

pageNumberStamp.StartingNumber = 1

pageNumberStamp.TextState.FontSize = 9.0F

For i As Integer = 1 To pdfDocument.Pages.Count Step 1

'pageNumberStamp.Format = "Page " & i & " of " & pdfDocument.Pages.Count

pdfDocument.Pages(i).AddStamp(pageNumberStamp)

Next

'save output document

pdfDocument.Save(outPutFile)

pdfDocument = Nothing

Catch

Throw

End Try

End Sub

Hi Samuel,

I have tried replicating the scenario while following the specified below steps.

  1. I create a simple VB project with .NET Framework 2.0 as my target platform and have placed the code snippet that you have shared.
  2. Placed the attached “LAR_PDF.pdf” file over my system and have provided its reference to PdfContentEditor object.
  3. Once the text has been replaced, I have saved the file with name LAR_PDF_updated.pdf.
  4. Document class object is created and I have passed LAR_PDF_updated.pdf as an argument to its constructor.
  5. TextStamp has been added and I have saved the final file with name LAR_PDF_Stamped.pdf.
  6. Executed the code and it generated 2 files (LAR_PDF_updated.pdf and LAR_PDF_Stamped.pdf).
  7. While keeping the application running, I have deleted both (LAR_PDF_updated.pdf and LAR_PDF_Stamped.pdf and have again pressed the button over dialog.
  8. Both the files are again generated successfully and I am unable to notice any issue.

I have used Aspose.Pdf for .NET 6.2.0 in VisualStudio 2010 Ultimate project over Windows7 X64 Professional. Can you please help me in replicating this issue. Do you think I have missed something ?

We are sorry for your inconvenience.

My Framwork is 3.5.

Your 1 and 2 step make sense. But I am confused after.

I am sorry that I cofused you also.

Simply speaking, if you associate my shared code with a button click in a website app, you click button two times. For the first time, after the process finished, you click button again, then error.... Before you click the button again (without stopping the app session), you may replace the updated LAR_PDF.pdf with original LAR_PDF.pdf

sxia

Looks like the code can not release the memory completely.

Once the error as "OutOfMemory" is created, ocasionally I can run the app any more even I tried to start a new session. I have to reboot my mechine to run.

And I believe that the following code caused te issue

'Move old page number

Dim contentEditor As New Aspose.Pdf.Facades.PdfContentEditor()

Try

'bind input PDF file

contentEditor.BindPdf(outPutFile)

'make sure the regular expression strategy is being used

contentEditor.ReplaceTextStrategy.IsRegularExpressionUsed = True

'specify that you want to replace all the matching string

'by default only the first string will be replaced

contentEditor.ReplaceTextStrategy.ReplaceScope = Aspose.Pdf.Facades.ReplaceTextStrategy.Scope.REPLACE_ALL

'replace any string like 5467-3986-7 with new string

contentEditor.ReplaceText("[0-9.]*[A-Z.]*[a-z.]*[ ]*Page [0-9]+ of [0-9]+", String.Empty)

'save output PDF file

contentEditor.Save(outPutFile)

Samuel