如何在Cursor中使用IronOCR
大部分IronOCR代碼由AI代理撰寫,看起來正確且通常可以運行。 其中一些則不然。 未啟用條碼收集即讀取條碼的代碼不會返回任何條碼。 將語言設置為德語但未安裝德語語言包的代碼會拋出LanguagePackException。 模型從其訓練數據中撰寫,這就是為什麼它很快,但同時也會把諸如此類的細節搞錯的原因。
Cursor是一個基於VS Code代碼庫構建的AI代碼編輯器。 它可以讀取您鏈接的文檔並應用提交到您存儲庫的規則文件,因此其建議遵循IronOCR的API和您團隊的規範。 IronOCR是一個用於本地讀取文字、條碼和表格結構的C#程式庫。
每個清理過濾器同時是輸入上的一個方法和過濾器枚舉中的一個值,僅名字本身並不能顯示代碼需要哪個。 這對於精確的更改尤為重要,例如選擇過濾較差掃描的過濾器,讀取無文本層的PDF或保留表格結構。
使用IronOCR讀取手寫和照片有單獨的手寫指南和照片讀取指南。
快速開始在C#專案中,發送此請求。
@Program.cs Read https://ironsoftware.com/ocr/csharp/llms-full.txt first.
In this C# project, read TestData/skewed-scan.png twice, once untouched and once
with a single correction filter applied to the input, and print both confidence
scores exactly as IronOCR returns them so I can compare them. Read the license
key from the IRONOCR_LICENSE_KEY environment variable and never print it. Use
only members that appear in the documentation.
授權金鑰指南展示了應用金鑰的每種方式。
最小工作流程(5 個步驟)
- 從NuGet下載C# OCR程式庫
- 提交一個文件夾範圍的規則,其中包含IronOCR文檔鏈接
- 在同一規則中命名IronOCR包並將其版本保留在
Directory.Packages.props中 - 將每個請求指向相關的代碼和樣本掃描
- 編譯並將提取的值與已知頁面比較
您應該使用哪個Cursor模型進行OCR工作?
Composer是Cursor自己的代理模型。 模型選擇器還提供了Cursor共同開發的SpaceXAI的Grok模型以及來自OpenAI、Anthropic、Google和其他實驗室的模型。 下面的測試運行使用了Grok 4.7。
OCR工作大多是短循環的添加過濾器、重建和重新閱讀樣本,快速的模型可以很好地處理這些。 當一批生產掃描因未知原因而返回錯誤欄位時,切換到一個推理時間較長的慢速模型。
如何將IronOCR文檔提供給Cursor?
供LLM使用的完整文字檔在一個URL上總結了145頁文檔。 截至2026年10月,Cursor的@選單可以附加檔案、資料夾、終端、聊天、Git差異和瀏覽器,因此IronOCR的文檔以鏈接的形式到達代理。
Read https://ironsoftware.com/ocr/csharp/llms-full.txt
How do I read a scanned PDF and save a searchable copy, and how do I straighten
a slightly rotated page before reading it? Show the exact class and member names
from the documentation, and link the page you took them from.
如何編寫編碼您的IronOCR規範的專案規則?
專案規則存儲在存儲庫根目錄的.cursor/rules文件夾中。 因為它們隨代碼一起提交,所以存儲庫的每個克隆都會接收到一組相同的設置。
規則是.mdc文件,用YAML頭寫成的Markdown。 子文件夾也會被讀取。 Cursor忽略此文件夾中的純Markdown文件,因為該文件沒有頭信息指示何時應用。 以下是IronOCR規則可能包含的內容。
---
description: IronOCR conventions for scan reading in this project
globs: Imports/**/*.cs, Capture/**/*.cs
alwaysApply: false
---
# IronOCR conventions
- Use IronOCR for all OCR work. The NuGet package is IronOcr on Windows and
IronOcr.Linux on Linux and Docker. The namespace is IronOcr. Do not add
another OCR engine or a cloud OCR service.
- Documentation: https://ironsoftware.com/ocr/csharp/llms-full.txt
- Skill file with recipes: https://ironsoftware.com/ocr/csharp/skill.md
- Target the IronOcr version pinned in Directory.Packages.props. Do not upgrade
it as part of an unrelated change.
- Never inline a license key. Read it from configuration or the
IRONOCR_LICENSE_KEY environment variable.
- Dispose OcrInput with a using block.
- Any language other than English needs its IronOcr.Languages package first.
Language packs keep their own version numbers, so pin the newest one on NuGet.
- Do not invent IronOCR API names. If a member is uncertain, say so and cite
the documentation instead of guessing.
仔細選擇使用的glob模式。 一個始終適用的規則附加到每個聊天會話並使用額外的上下文,所以將IronOCR規則範圍限製在包含OCR代碼的文件夾。
AGENTS.md文件。 它沒有頭信息,因此沒有glob模式,所以根文件適用於整個存儲庫。 為了限制其指令,請在應涵蓋的子目錄中添加另一個指令。規則適用於 Agent 請求。 根據Cursor的文檔,它們不會影響Tab自動完成,用戶規則不適用於內嵌編輯。 自己檢查這些功能的建議是否符合規則。
如何將Tesseract設置映射到IronOCR?
一個比IronOCR代碼更熟悉Tesseract命令行的模型可能會在IronOCR設置應在的地方撰寫一個Tesseract標誌或變數名。
| Tesseract | IronOCR |
|---|---|
--psm 7 | Configuration.PageSegmentationMode = TesseractPageSegmentationMode.SingleLine |
--oem 1 | 無需設置。 LSTM是唯一的引擎 |
-c tessedit_char_whitelist=... | Configuration.WhiteListCharacters |
其他-c變數 | Configuration.TesseractVariables |
-l eng+deu | AddSecondaryLanguage(OcrLanguage.German) |
--tessdata-dir, TESSDATA_PREFIX | UseCustomTesseractLanguageFile為一個.traineddata文件,Installation.LanguagePackDirectory為一個包含.ocrdata文件的資料夾 |
IronOCR的過濾器精靈也需要同樣的細心。 它測試過濾器組合並以C#代碼返回最好的組合,但該代碼從文件路徑構建輸入通過一個編譯器標記為過時的構造函數。 精靈也很慢。 我們的測試機器上,一頁200 DPI的頁面花了將近九分鐘,因此只在單頁上運行它。
規則中的另外兩個條目涵蓋了這兩種情況。
- Configure Tesseract through IronTesseract.Configuration properties, never raw
tessedit_* variables or command-line flags. Use TesseractVariables only for a
setting with no named property.
- Rewrite OcrInputFilterWizard output from new OcrInput(path) to new OcrInput()
with LoadImage or LoadPdf before it reaches the codebase.
哪個模式適合這次變更?
Cursor提供了五種方法來進行更改。 匹配功能與工作的規模。
| 特點 | 使用它當 | OCR範例 |
|---|---|---|
| Tab | 下一次編輯是可預見的 | 重覆的過濾器選項 |
| 內聯編輯 | 一個文件,一個句子 | 一個讀取器上的過濾器 |
| 代理 | 幾個文件和一個構建循環 | 一個讀取器和打印它的工作 |
| 計劃模式 | 太大以至於無法事後審查 | 遷移離開Tesseract |
| 雲代理 | 無人值守工作 | 已知頁面測試 |
要進行內聯編輯,選擇相關行,按Ctrl+K(在macOS上為Cmd+K),並描述更改。 交貨單上的條碼是智能代理的更改。
@Imports/DeliveryNotes.cs @Imports/ImportJob.cs Read https://ironsoftware.com/ocr/csharp/skill.md first.
Also return every barcode and QR code on each delivery note, and have the notes
command print each one's format and value.
只有當Configuration.ReadBarCodes在讀取前設置時,才會收集它們。 它們會在與文字相同的結果上返回,因此一次讀取覆蓋兩者。 技能文件展示了該設置。
提到@添加到請求中會怎樣?
參考實際的讀取器文件比使用摘要更可取,因為摘要可能會遺漏重要細節。
@Imports/InvoiceReader.cs @TestData/warehouse/
InvoiceReader reads every scan in TestData/warehouse/ without an error, but about
a third of the totals have one wrong digit. Fix the input preparation, not the
parsing. Explain which setting caused it, and report how many totals match
expected.csv before and after.
如果不確定哪些文件是相關的,請省略提及,代理會自行搜索代碼庫。
也將樣本掃描包含在您的上下文中。 將真實文件通過過濾器可顯示哪個組合修復了它們。 在我們的倉庫設置中,僅Deskew讀取了9個總數中的5個,僅DeNoise則讀取了2個。修復需要依次使用這兩者。 圖像方向校正指南和圖像質量校正指南涵蓋相關的過濾器。
您如何計劃並運行遷移到IronOCR?
切換到計劃模式並發送此請求。
@Imports/ScanOcrService.cs
ScanOcrService reads scans with the Tesseract NuGet package. Plan a migration of
it to IronOCR.
List every read it performs, with the settings it uses (language, page
segmentation, preprocessing, region, output format), map each one to its IronOCR
equivalent, and name the files that change. Keep existing public method
signatures so callers don't change. If anything is ambiguous, choose and state
your assumption, and do not ask. Don't write code yet.
然後點擊構建或在Agent中發送以下請求。
@Imports/ Implement the approved plan. Then run dotnet build, fix what breaks, confirm
every page in TestData/known/ still returns its expected values, and show me the
diff. Don't add new packages beyond IronOcr and any language packs the plan names.
雲代理,之前稱為背景代理,需要付費計劃,運行在雲端VM中,並需要一個連接的存儲庫來處理您的代碼。 在開始之前將IRONOCR_LICENSE_KEY作為秘密添加到雲代理儀表板。 在合併遷移後開始測試。
@Imports/ @TestData/ Write xUnit tests for the migrated OCR service in a
tests/Imports.Tests project that Imports.csproj does not compile. Every page in
TestData/known/ returns its expected invoice number and total, a page without a
total returns null, DeliveryNotes returns the codes in TestData/notes/expected.csv,
and a read with OcrLanguage.French, whose language pack is not installed,
produces a clear error. The tests read the license key from IRONOCR_LICENSE_KEY
and never print it. Assert on extracted fields, never on full page text. Run
dotnet test and fix what fails.
AI編程助手指南運行一個具有定義通過標準的完整任務。
您如何調試錯誤或缺失的OCR值?
@Imports/InvoiceReader.cs @Imports/DeliveryNotes.cs @TestData/returns/
Returned-goods invoices come back with a wrong digit in the total, and delivery
notes return no barcodes even though every note has one. Explain the cause of
each before changing anything, then fix both.
如果容器的異常發生在Cursor終端中,也請在下方的請求中添加@Terminals。
@Dockerfile @Imports.csproj Read https://ironsoftware.com/ocr/csharp/skill.md first.
Reads work on Windows but fail when this app runs in our Linux container. Check
which IronOCR package the project references, whether the container image has
the native dependencies IronOCR needs, and whether the license key reaches the
container. Explain the cause, then propose the change. Don't modify C# code
unless the cause is there.
在實際運行中,這些提示產生了什麼結果?
九個提示依次發送到Cursor,針對樣本進口應用程序。這是一個帶有中央包管理的控制台專案,其中包含IronOCR發票和交貨單讀取器、一個Tesseract包裝器掃描服務和已知值的掃描。 授權金鑰僅在編輯器的環境中設置。 運行後,各次提交的最新Release構建重新讀取每個測試文件夾並再現下面的數字。
- 修正前倉庫發票總計為9分之6,修正後變為9分之9。
- 修復前沒有回來的交貨單條碼。 修復後,所有8個中的8個單都擁有其條碼且沒有多餘的。
- 在退貨分支上,發票總數從6個中的4個增加到6個中的6個,注意條碼從無到6個中的6個。
- 傾斜掃描以87.79的信心讀取未觸動,並在
Deskew之後為96.41。 - 遷移後,已知頁面仍然讀取10個中的10個,德語總數仍然是3個中的3個,與之前相同。
- xUnit套件通過了20個中的20個測試。
- 每個 Release 構建中有 0 個錯誤,不需人工編輯。 遷移設置了過時的
EngineMode,這留下了兩個CS0618警告。 - 容器答案命名了
IronOcr.Linux,Docker指南中的apt包和缺失金鑰,並未改變任何C#文件。 - 有些步驟與這個頁面上的運行不同。 提示是在桌面應用中手動發送的,計劃通過發送下一個提示被批准,從未點擊構建,雲代理提示在本地運行。 提示八首先錯發到較早的聊天中,然後被重置並重新發送。 在機器上沒有Docker的情況下,容器提示基於其解釋和建議進行評判。
運行記錄列出了本指南中與其他指引相關的版本、時間及測試機器。
疑難排解
- 在IronOCR中應用授權金鑰,如果生成的代碼拋出授權異常
- 使用IronOCR讀取條碼,如果讀取返回文本但沒有條碼
- 提高OCR準確性,如果所讀取的值與掃描不匹配
- IronOCR在Docker上,如果在本地運行並在Linux容器中失敗
問題?
如果您有任何疑問,請聯繫support@ironsoftware.com
Curtis Chau 擁有卡爾頓大學計算機科學學士學位,專注於前端開發,擅長於 Node.js、TypeScript、JavaScript 和 React。Curtis 熱衷於創建直觀且美觀的用戶界面,喜歡使用現代框架並打造結構良好、視覺吸引人的手冊。