使用IronOCR與AI編碼助手
IronOCR是用於.NET的商業OCR程式庫,下載超過六百萬次。 其教程、操作指南和疑難解答說明已公開多年,因此編碼助手通常會在其訓練數據中看到它。
但這種熟悉性並不延伸到您專案所固定的版本。 閱讀IronOCR文件的助手更可能編寫構建並返回準確文本的OCR代碼。 在首次請求之前分享文件。
這些工具分為兩類,它們在您審核結果之前的處理量有所不同。
- 助手在您的工作流程中操作一部分。 它完成一行,解釋一種方法,或進行微小編輯,如GitHub Copilot的內聯建議。
- 代理處理多步驟的任務,跨檔案編輯,運行構建和測試,並提交結果以供審查,如Claude Code、ChatGPT Codex、Cursor和Google Antigravity。
兩者的設置相同。 安裝封裝,從環境變量應用許可證,提供IronOCR的機器可讀文件,並檢查每一提取值對應的您已知文本的頁面。 這適用於圖像和掃描的PDF,以及使掃描可搜尋、閱讀多種語言、從同一讀取中提取條碼或改進低質量掃描。
如何用AI編碼助手或代理使用IronOCR
- 從NuGet下載C# OCR程式庫
- 在首次閱讀之前從環境變量加載許可證
- 在助手寫任何代碼之前給它IronOCR的
llms.txt索引 - 在助手保留專案指示的地方記錄IronOCR規則
- 使用下面的單次設置提示開始一個新的會話
- 編譯,然後將IronOCR的輸出與每個示例頁面上您期望的值進行比較
我的AI助手無法自己處理OCR嗎?
沒有文件的話,助手依賴於訓練中吸收的任何OCR代碼,這通常混合了原始Tesseract包裝器或更早的IronOCR版本的設置。
IronOCR發布供工具閱讀的文件,包括教程、操作指南和五十多篇疑難解答文章。 一旦助手加載了它,文件會顯示IronTesseract、OcrInput和OcrResult如何結合。
引擎選擇仍然很重要。 免費的Tesseract包裝器處理乾淨、豎直的英語頁面效果良好。
- 可讀的輸出不保證正確的輸出:OCR結果可以流利地讀取,但仍將589.50變為580.50。
OcrResult為每個單詞報告一個置信度得分,以顯示查看位置。 - 掃描需要準備:彎曲、噪雜或低分辨率的頁面在任何引擎上都讀不好。
OcrInput包含的過濾器把頁面矯直,去除噪音,提高分辨率等等。 - 手工調整引擎成本高昂:助手每次對原始Tesseract設置的嘗試都會消耗封裝已經完成的工作資源,並且每種新文件類型都會重複此成本。
- 封裝依賴於平台:
IronOcr涵蓋Windows,IronOcr.MacOs涵蓋Intel Macs,IronOcr.MacOs.ARM涵蓋蘋果芯片。 助手還可能編造一個看似合理的包名稱,攻擊者會註冊這種名稱進行惡意代碼,這種做法稱為slopsquatting。 在提示中命名包。 - 許可是可預測的:雲OCR服務通常按頁或按調用計費,助手不會自己提到這些條款。 IronOCR在本地運行,持續的商業許可基於開發者、地點和專案分層定價,無每頁計量。
步驟1:安裝IronOCR
一旦包引用存在,助手可以看到IronOCR的實際類型並生成代碼。
Install the IronOcr NuGet package into this project and confirm it restores.
高級NuGet安裝指南列出了其他安裝選項。
步驟 2:套用您的許可證金鑰
IronOCR沒有帶水印的未授權模式。 在沒有密鑰的情況下構建IronTesseract是成功的,但首次閱讀時會拋出IronSoftware.Exceptions.LicensingException,信息為"需要生產許可"。 一個存在但輸入錯誤的密鑰同樣會從同一調用中得到"許可金鑰未識別"。 您可以在下面獲取免費試用密鑰。
立即在您的項目中開始使用 IronOCR,免費試用。
在應用程序啟動時設置IronOcr.License.LicenseKey,在任何其他IronOCR調用之前,並從IRONOCR_LICENSE_KEY環境變量中讀取它,這樣它永遠不會進入源控制。 同一類別也有一個方法來測試密鑰字符串而不應用它。
Web.config和appsettings.json。步驟3:將您的助手指向IronOCR的LLM文件
llms.txt是索引,每頁一行。- 145頁的摘要可從
llms-full.txt一次提取中獲得。 llms-full-catalog.txt將部落格加入索引。- 每種類型和成員的API參考文件。
- 設置、許可、配方和修復在
skill.md中。 - AI產品數據單總結了範圍、平台和限制。
當任務依賴於某個指南的細節時,讓助手打開該指南的頁面。
提供直接指示與索引一起。
Before writing any IronOCR code, read https://ironsoftware.com/ocr/csharp/llms.txt
and use only APIs documented there. A read is assembled on an OcrInput, whose
Load overloads differ by source type, and run through IronTesseract.Read.
Confirm the overload you pick against
https://ironsoftware.com/ocr/csharp/object-reference/api/, and if you cannot find
it, say so instead of guessing.
步驟4:將IronOCR的上下文添加到您的專案中
將IronOCR規則放在您的團隊已經保留的任何文件中。 Claude Code閱讀CLAUDE.md,GitHub Copilot閱讀.github/copilot-instructions.md,ChatGPT Codex和Google Antigravity閱讀AGENTS.md。 Cursor閱讀.cursor/rules/*.mdc或AGENTS.md,只有它的規則文件需要MDC前置內容。 使用以下作為起點。
## OCR with IronOCR
This project uses IronOCR (NuGet package `IronOcr`, or `IronOcr.Linux` for Linux and Docker, namespace `IronOcr`) for all OCR work.
- Reference: https://ironsoftware.com/ocr/csharp/llms.txt
- Skill file: https://ironsoftware.com/ocr/csharp/skill.md
- Read with `IronTesseract` and an `OcrInput` inside a `using` block, and use the `OcrResult` it returns.
- Read the license key from the IRONOCR_LICENSE_KEY environment variable at startup. Never inline it.
- Every OCR change must be tested against sample pages whose text is known, reporting the values read and their confidence.
- Do not add another OCR engine or a cloud OCR service. If IronOCR cannot do something, say so.
技能文件的單文件示例仍然固定IronOcr 2026.7.2,儘管NuGet現已提供2026.10.2。它還說保持每個Iron包在相同版本上,但語言包不能這樣做,因為IronOcr.Languages.German最後發佈為2020.11.2。其單行ConvertToSearchablePdf配方在我們的測試中對於只有圖像的掃描PDF報錯,而加載PDF,讀取它,並對結果調用SaveAsSearchablePdf有效。
步驟 5:單次設置提示
步驟1至4的所有內容可以在一個單一消息中發送。
Use IronOCR for all OCR work in this project.
1. Install the IronOcr NuGet package (IronOcr.Linux when the app runs on Linux or Docker).
2. Apply the license key at application startup, before any other IronOCR call,
reading it from the IRONOCR_LICENSE_KEY environment variable. Never write
the key into source or into a committed file.
3. Read https://ironsoftware.com/ocr/csharp/llms.txt before writing OCR code
and work only from APIs documented there, checking the API reference when a
member is unfamiliar.
4. Load https://ironsoftware.com/ocr/csharp/skill.md if your tooling supports skill files.
5. Keep these instructions for future sessions in this project.
步驟 6:產生可運行程式碼的提示模式
結果不佳通常來自給助手留下猜測空間的提示。
命名輸入、字段和語言。
模糊 使用IronOCR讀取發票。
更好 用IronTesseract和每個掃描PDF的OcrInput,讀取發票號碼和起始為總支付的行,並返回該行的金額及置信度。
也要說明發布和框架。
更好 目標.NET 9和IronOcr 2026.9.4。
請求整個文件。
更好 交還完整的Program.cs,包括指令和許可設置,準備好編譯無需編輯。
步驟 7:驗證您收到的內容
注意這些常見錯誤。
- 已經有文字的PDF上的OCR:提取實際的文字層比在頁面上運行OCR更快、更準確。 首先檢查文字層,僅在掃描頁上運行OCR。
- 所有篩選器同時應用:在乾淨頁面上依次堆砌修正會降低準確度並常常增加時間。在我們的測試中,密集發票集中,
DeNoise將旋轉後發票號的最後一個"1"變成了"4",而添加Dilate治愈了該頁,但讓其他集合少了一到三行。 僅應用修復所見缺陷的篩選器。 IronOCR的技能文件提供了相同的建議,並且圖像質量修正指南説明每個篩選器在何時適用。
讓代理驗證自己的工作
Build and run this project in Release. Confirm that every IronOCR member you used
appears in the documentation, that IronOcr.License.IsLicensed returns true, and
that every sample page reads back with the values it is known to contain.
Report the build result and a table of page, expected value, value read, confidence, and match.
您如何閱讀英語以外的語言?
英語是IronOcr封裝中的唯一語言。 IronOCR的語言列表涵蓋125種。向助手提供這些事實。
- 另一種語言需要它的包,例如
IronOcr.Languages.German,然後Language = OcrLanguage.German。 - 沒有NuGet訪問的情況下,使用
.ocrdata文件和Installation.LanguagePackDirectory。 - 快速和最佳變種如
OcrLanguage.GermanFast在同一包中交付。 - 多語頁面調用
AddSecondaryLanguage,最常用的首先。 - 財務表採用
OcrLanguage.Financial從IronOcr.Languages.Financial。 - 模型在其他地方訓練通過
UseCustomTesseractLanguageFile加載,與.traineddata文件。
缺少的包在第一次閱讀時會引發LanguagePackException。 其信息指出確切的NuGet包和.ocrdata替代方案,因此讓助手安裝信息中給出的內容。
IronOCR 的快速配置指南報告指出,將 EnglishFast 與關閉條碼讀取結合使用可提升 17% 的性能。 在我們的十頁中,該變種獨自在與英語的運行噪音範圍內,因此在您的頁面上計算變種時間,比較準確性與速度之前。
ClickOnce發布忽略次要語言的.ocrdata文件,如ClickOnce說明所解釋。 在Windows上,將EnglishFast作為次要語言加入可能在2025.12.3至2026.7.2版本的大PDF上卡住,如此問題排查條目所記錄。
IronOCR設置默認打開哪些?
生成的代碼經常設置已經是默認值的值,或錯過一個默認關閉的設置。 這些是IronOcr 2026.9.4的起始值,從一個新的IronTesseract閱讀。
| 設定 | Default | 將其換為 |
|---|---|---|
Language | 英語 | 其他語言 |
Configuration.ReadBarCodes | 關閉 | 條形碼 |
Configuration.ReadDataTables | 關閉 | 表格結構 |
Configuration.RenderSearchablePdf | 開啟 | 跳過可搜尋輸出 |
Configuration.RenderHocr | 關閉 | hOCR 輸出 |
Configuration.PageSegmentationMode | AutoOsd | 單行或單詞剪裁 |
Configuration.EngineMode | LstmOnly | 很少 |
Configuration.WhiteListCharacters和BlackListCharacters | 空 | 窄字符集 |
Configuration.TesseractVariables | 三個預設條目 | 原生Tesseract變數 |
MultiThreaded | 開啟 | 很少 |
MaxDegreeOfParallelism | Environment.ProcessorCount | 大批次記憶體不足 |
OcrInput.TargetDPI | 0 | 低分辨率掃描 |
除了標準閱讀之外,IronTesseract提供了為其他輸入調整的閱讀器。 ReadPhoto處理文件的照片,ReadScreenShot處理屏幕捕捉。 專門文件教程仍然列出了IronOcr.Extensions.AdvancedScan給他們用,但那個包最後一次發布是在2025年,兩個閱讀器都不需要它。 相同的教程還涵蓋了護照和車牌閱讀者。
可重現的結果
提示
Build a .NET 9 console app that reads totals from scanned invoices. Start in an empty folder; if it already holds code or project files, stop and report. Work start to finish alone: write, build, run, and fix, making sensible assumptions where anything is unspecified. Save every deliverable under the project root, in the folders named below.
1. Install the IronOcr NuGet package pinned to version 2026.9.4.
2. Read https://ironsoftware.com/ocr/csharp/llms.txt and the pages it links to, and use only IronOCR APIs documented there for the OCR work. Any .NET imaging library may draw the test images.
3. Read the license key from the IRONOCR_LICENSE_KEY environment variable at startup and never write it into source or any output file. If IronOcr.License.IsLicensed is false, stop and report that.
4. With random seed 2026, generate testset/invoice-01.png to invoice-10.png. Each is a US Letter page of exactly 1700 x 2200 pixels, saved as PNG with 200 DPI in the file metadata, with all text in Arial inside 0.75 inch margins, headings at 14 point bold and everything else at 11 point. Each invoice has an invoice number made of INV- and six random digits, a customer name, three line items with random prices in dollars and cents, and a total equal to their sum, with no two totals alike.
5. Rotate invoice-02 and invoice-07 by 3 degrees about the page center, keeping the 1700 x 2200 canvas. Add speckle noise to invoice-04 and invoice-09: dark square specks 1 or 2 pixels wide, placed anywhere including over the text, until they have darkened 1% of the page's pixels, counting a pixel as dark below 128 of 255. Leave the other six clean. Write manifest.csv with each file, invoice number, the three prices, total, and distortion (none, rotated, or speckled).
6. Before the first OCR call, check every saved image: 1700 x 2200 pixels, 200 DPI when rounded, and, measured on each page before distortion, the smallest distance from any ink to an edge, at least 0.75 inch. On each speckled page, check that speckling raised the share of dark pixels by 0.9 to 1.1 percentage points. Also draw a capital H with the 11 point font on a 200 DPI bitmap and check that its ink is 21 to 23 pixels tall. If a check fails, fix the generator. After the first OCR call, never regenerate or change the saved image files; filters applied in memory are expected.
7. Read all 10 images in one OcrInput with one set of filters for every page. Each run of the program uses one filter set; it must not try several or choose between them, and choosing filters is your job between runs. No manifest value, file name, or expected value may steer the OCR or the parsing of its text; read the manifest only to write roundtrip.md. The reader must hold no invoice number, price, or total as a literal and must run the same code for every page. Extract each page's invoice number and total, save each page's OCR text as ocr/invoice-01.txt to invoice-10.txt, and save the result as invoices-searchable.pdf.
8. Build in Release and run.
9. Write roundtrip.md: the step 6 checks as measured numbers for every image, not pass or fail words, the filter set used, then a table of file, distortion, expected invoice number, invoice number read, expected total, total read, the Confidence (0 to 100) of the OCR line holding the total or n/a, and match, then the page count of invoices-searchable.pdf loaded back with OcrInput. A row matches only when the invoice number read is identical to the expected one and the total read equals the expected total to the cent.
10. All 10 rows must match and the PDF must have 10 pages. If not, read the saved OCR text of the failing pages, change the filter set or the reader, rebuild, and run again. If rows still fail after eight such runs, stop and report which rows failed, the filter sets tried, and why.
11. Finish with a short report: build result, match count, filters used, font sizes used, and assumptions made.
輸出
這是發票-09,運行中兩頁帶斑點的頁面之一,裁剪到印刷區域。 普通閱讀匹配了10張發票中的8張,漏掉了兩頁帶斑點的頁面,並返回此張發票號碼為ANV-938066,單靠Deskew效果沒有更好。 加上DeNoise後,IronOCR返回INV-939066和1731.53,總行的置信度為41.45,是運行中最低的。 另一頁帶斑點的總行讀取為63.86,和清潔的頁面大約為95。Claude Code首個草稿選擇了這兩個篩選器,並在程序的所有五次運行中保留它們。
- 獨立檢查再次測量了每個要求,並且所有10張發票匹配。
- ChatGPT Codex與GPT-6 Luna在相同提示下也匹配10之10。
roundtrip.md未經編輯發佈,
縮小尺寸。
什麼過關
- 代碼不需要您的任何編輯。
- 打開任何圖像顯示小字,邊緣沒有截斷的,文本上有斑點,還有邊緣。
- 在
roundtrip.md中的每個發票號碼和總數也出現在該頁保存的OCR文本中。
運行記錄
- 機器在Core i9-14900KF上運行Windows 11 x64,擁有64 GB記憶體。
- 每個專案都面向 .NET 9。
- 本指南、Copilot和Cursor運行
IronOcr2026.9.4,而Claude Code、Codex和Antigravity運行2026.10.2。Copilot額外添加IronOcr.Linux2026.9.4,並且Codex、Cursor和Antigravity額外添加IronOcr.Languages.German2020.11.2。 - 最後測試於2026年10月8日,用Cursor運行。 每次其他運行來自10月6日。
| 指南 | 助手和版本 | 模型 | 時長(分鐘) |
|---|---|---|---|
| 此指南 | Claude Code 2.1.277,權限提示關閉 | Claude Opus 5 | 20.8 |
| Claude Code | Claude Code 2.1.277 | Claude Sonnet 5 | 9.4 |
| ChatGPT Codex | Codex CLI 0.160.0 | GPT-6 Luna | 27.7 |
| GitHub Copilot | 在VS Code 1.140中的Copilot代理模式 | GPT-6 Luna, routed by Auto | 22.0 |
| Cursor | Cursor 3.23.23 桌面應用 | Grok 4.7 | 83.5 |
| Google Antigravity | Antigravity CLI 1.3.0 | Gemini 3.8 Flash (High) | 32.7 |
代理特定指南
適合一個工具的步驟,打開其指南。
- 與Claude Code一同使用IronOCR
- 與ChatGPT Codex一同使用IronOCR
- 與GitHub Copilot一同使用IronOCR
- 與Cursor一同使用IronOCR
- 與Google Antigravity一同使用IronOCR
疑難排解
- 在IronOCR中應用許可金鑰
- 提高OCR準確性 如果識別的文本與頁面不符
- 一般OCR故障排除 如果部署後出現錯誤
問題?
如果您有任何疑問,請聯繫support@ironsoftware.com
Curtis Chau 擁有卡爾頓大學計算機科學學士學位,專注於前端開發,擅長於 Node.js、TypeScript、JavaScript 和 React。Curtis 熱衷於創建直觀且美觀的用戶界面,喜歡使用現代框架並打造結構良好、視覺吸引人的手冊。