Incorrect Line Breaks in a Windows Forms TextBox

OCR text extracted with IronOCR can lose its line breaks when shown in a Windows Forms TextBox, collapsing onto fewer lines than the source image.

The cause is a newline mismatch. IronOCR returns line breaks as a bare line feed (\n), but a Windows Forms TextBox only renders a break when it sees a carriage return followed by a line feed (\r\n). Without the carriage return, the control ignores the break.

The OCR runs against a source image like the one below:

Source image passed to IronOCR for text extraction

Binding the raw OCR result straight to the control collapses the text onto a couple of lines instead of preserving the original layout:

Windows Forms application showing OCR text with its line breaks collapsed onto two lines

Solution

1. Use AdvancedScan in 64-bit Mode

Install the IronOCR.Extensions.AdvancedScan package for more reliable extraction, and run the application in 64-bit mode to avoid compatibility issues.

<PackageReference Include="IronOCR.Extensions.AdvancedScan" />
<PackageReference Include="IronOCR.Extensions.AdvancedScan" />
XML

2. Normalize the Line Endings

Replace every \n in the result with \r\n before assigning the text to the control.

ocr.Language = OcrLanguage.ChineseSimplifiedBest;
using (var ocrInput = new OcrInput())
{
    ocrInput.LoadImage(txtPath.Text);
    ocrInput.Deskew();
    var result = ocr.ReadScreenShot(ocrInput);
    // Replace line feed with carriage return and line feed
    string ocrText = result.Text.Replace("\n", "\r\n");
    // Display the corrected text in a TextBox
    textBox1.Text = ocrText;
}
ocr.Language = OcrLanguage.ChineseSimplifiedBest;
using (var ocrInput = new OcrInput())
{
    ocrInput.LoadImage(txtPath.Text);
    ocrInput.Deskew();
    var result = ocr.ReadScreenShot(ocrInput);
    // Replace line feed with carriage return and line feed
    string ocrText = result.Text.Replace("\n", "\r\n");
    // Display the corrected text in a TextBox
    textBox1.Text = ocrText;
}
Imports System

ocr.Language = OcrLanguage.ChineseSimplifiedBest
Using ocrInput As New OcrInput()
    ocrInput.LoadImage(txtPath.Text)
    ocrInput.Deskew()
    Dim result = ocr.ReadScreenShot(ocrInput)
    ' Replace line feed with carriage return and line feed
    Dim ocrText As String = result.Text.Replace(vbLf, vbCrLf)
    ' Display the corrected text in a TextBox
    textBox1.Text = ocrText
End Using
$vbLabelText   $csharpLabel

The Replace("\n", "\r\n") call rewrites each line feed into the carriage-return/line-feed pair the TextBox expects, so the displayed text keeps the same line structure as the original image.

3. Verify the Output

Run the extraction again and compare the TextBox contents against the source image. The breaks should now line up with the original layout.

Windows Forms application showing the OCR text with its line breaks correctly preserved

Curtis Chau
Technical Writer

Curtis Chau holds a Bachelor’s degree in Computer Science (Carleton University) and specializes in front-end development with expertise in Node.js, TypeScript, JavaScript, and React. Passionate about crafting intuitive and aesthetically pleasing user interfaces, Curtis enjoys working with modern frameworks and creating well-structured, visually appealing manuals.

...

Read More
Ready to Get Started?
Nuget Downloads 6,106,091 | Version: 2026.7 just released
Still Scrolling Icon

Still Scrolling?

Want proof fast? PM > Install-Package IronOcr
run a sample watch your image become searchable text.