The solution is simple: read the text.txt file line by line, create a Paragraph
for each line and add each Paragraph
object to the document as soon as possible.
Why is this the solution?
Your code is flawed by design: it consumes an enormous amount of memory: first you load 2 MByte in the richtextbox1
object. Then you load the same 2 MByte into the Paragraph
object. The original 2 MByte is still in memory, but Paragraph
starts allocating memory to process the text. Then you add the Paragraph
to the document. Memory is released on a page per page basis (iText flushes content as soon as a page is full), but the processing still requires plenty of memory. When your computer "hangs", he's probably swapping memory.
I see that your nickname is Smart Man, but I guess you're a Young Man. If you were as old as I am, you'd have known the days when memory was expensive and one couldn't afford wasting memory by design ;-)