Windows Forms TextBox中的不正確換行符
This article was translated from English: Does it need improvement?
Translated
View the article in English
從IronOCR提取的OCR文字在Windows Forms TextBox中顯示時,可能會失去其換行符,導致比源圖像顯示的行數更少。
原因是換行符不匹配。 IronOCR返回的換行符是一個單獨的換行(\n),但Windows Forms \r\n),才會渲染換行。 沒有回車,控件會忽略該換行。
OCR針對以下圖像進行運行:

將原始OCR結果直接綁定到控件會導致文字收縮到幾行,而不是保留原始的佈局:

解決方案
1. 在64位模式下使用AdvancedScan
安裝IronOcr.Extensions.AdvancedScan套件以獲得更可靠的提取,並在64位模式下運行應用程式以避免相容性問題。
<PackageReference Include="IronOcr.Extensions.AdvancedScan" />
<PackageReference Include="IronOcr.Extensions.AdvancedScan" />
XML
2. 標準化行結尾
在將結果指派給控件前,將每一個\r\n。
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
TextBox預期的回車/換行對,以便顯示的文字保持與原始圖像相同的行結構。
3. 驗證輸出
再次運行提取,並將TextBox的內容與源圖像進行比較。 現在換行應該與原始佈局對齊。

準備開始了嗎?
Nuget 下載 6,136,090 | 版本: 2026.7 剛剛發布

