使用IronOCR与AI编码助理

Curtis Chau
Curtis Chau
更新: 2026年10月9日

IronOCR是一个用于.NET的商业OCR库,NuGet下载量超过六百万。 其教程、操作指南和故障排除说明已公开多年,因此编码助手通常在其训练数据中见过它。

这种熟悉程度不包括您项目固定的发行版。 一个阅读IronOCR文档的助手更有可能编写构建并返回准确文本的OCR代码。 在第一次请求前分享文档。

这些工具分为两类,它们在您审查结果之前的工作量有所不同。

  • 一个助手在您的工作流中执行一部分工作。 它可以完成行、解释方法或进行小编辑,如GitHub Copilot的内联建议所做的那样。
  • 一个代理处理多步任务,跨文件编辑,运行构建和测试,并提交结果供审核,如Claude Code、ChatGPT Codex、Cursor和Google Antigravity所做的那样。

两者的设置是相同的。 安装软件包,从环境变量应用许可证密钥,提供IronOCR的机器可读文档,并检查每个提取值与您已知文本的页面是否一致。 这同样适用于图像和扫描PDF,以及制作扫描件可搜索、读取多种语言、从同一读取中提取条形码或改善低质量扫描件。

Claude Code AI编码代理徽标ChatGPT Codex AI编码代理徽标GitHub Copilot AI编码助手徽标Cursor AI代码编辑器徽标Google Antigravity AI编码代理徽标

AI助手不能单独处理OCR?

没有文档,助手依赖于它在训练中吸收的OCR代码,这些代码通常混入了来自原始Tesseract封装或早期IronOCR版本的设置。

IronOCR发布了构建工具的文档,包括教程、操作指南和五十多篇故障排除文章。 一旦助手加载了它,文档会显示IronTesseract、OcrInput和OcrResult如何组合在一起。

引擎选择仍然很重要。 免费的Tesseract包装能够很好地处理干净、直立的英文页面。

  • 可读输出不保证正确输出:一个OCR结果可能流畅读取,但仍将589.50变成580.50。OcrResult报告每个单词的置信度得分,以显示需关注的地方。
  • 扫描需要准备:任何引擎中的倾斜、嘈杂或低分辨率页面都读得不好。OcrInput带有过滤器,可使页面拉直、去除噪音、提高分辨率等。
  • 手动调节引擎成本高昂:助手对原始Tesseract设置的每次尝试都在花费库已经完成的工作,并且每种新文档类型费用都会重复。
  • 软件包依赖于平台:IronOcr涵盖Windows,IronOcr.MacOs涵盖英特尔Mac,IronOcr.MacOs.ARM涵盖苹果硅。 助手也可以捏造一个合理的软件包名称,攻击者使用这种名称注册恶意代码,这种行为被称为slopsquatting。 在提示中命名软件包。
  • 许可是可预见的:云OCR服务经常按页面或调用收费,而助手不会自行提到这些条款。 IronOCR在本地运行,持有永久商业许可证,按开发者、位置和项目分层定价,没有页面计费。

步骤1:安装IronOCR

一旦存在软件包引用,助手可以看到IronOCR的实际类型,因为它会生成代码。

Install the IronOcr NuGet package into this project and confirm it restores.
Text

高级NuGet安装指南列出了其他安装选项。


步骤 2:应用您的许可证密钥

IronOCR没有水印的未授权模式。 构建一个没有键的IronTesseract会成功,但第一次读取时会引发IronSoftware.Exceptions.LicensingException,并带有“需要生产许可证”的消息。 存在但拼写错误的密钥会在同一次调用中产生“未能识别许可证密钥”。 您可以在下面获得免费试用密钥。

从今天开始在您的项目中使用 IronOCR 免费试用。

第一步:
arrow pointer

在应用程序启动时设置IronOcr.License.LicenseKey,在任何其他IronOCR调用前,并从IRONOCR_LICENSE_KEY环境变量中读取,这样它就不会进入源代码管理。 同一个类也有一个测试密钥字符串的方法而不需应用它。

请注意: IronOCR许可证密钥涵盖存储密钥的其他地方,包括Web.config和appsettings.json。

步骤3:将您的助手指向IronOCR的LLM文档

当任务依赖于一个指南的细节时,让助手打开该指南的页面。

提供直接指令和索引。

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.
Text

步骤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.
Markdown

技能文件的单文件示例仍然固定在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.
Text

步骤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.
Text

你如何阅读英语以外的语言?

英文是IronOcr包中的唯一语言。 IronOCR的语言列表涵盖了125种。 给助手这些事实。

  • 另一个语言需要其包,例如IronOcr.Languages.German,然后Language = OcrLanguage.German。
  • 没有NuGet访问权限时,使用.ocrdata文件和Installation.LanguagePackDirectory。
  • 诸如OcrLanguage.GermanFast之类的快速和最佳变体在同一包中。
  • 对于多种语言的页面,按最常用的调用AddSecondaryLanguage。
  • 财务表格从IronOcr.Languages.Financial采用OcrLanguage.Financial。
  • 其他地方训练的模型通过UseCustomTesseractLanguageFile加载.traineddata文件。

缺少包会在第一次读取时引发LanguagePackException。 其消息命名了确切的NuGet包和.ocrdata替代方案,因此让助手安装消息中命名的内容。

IronOCR的快速配置指南报告EnglishFast结合关闭Barcode Reading获得了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.PageSegmentationModeAutoOsd单行或单词裁剪
Configuration.EngineModeLstmOnly很少
Configuration.WhiteListCharacters和BlackListCharacters空的窄字符集
Configuration.TesseractVariables三个预设条目原始Tesseract变量
MultiThreaded开启很少
MaxDegreeOfParallelismEnvironment.ProcessorCount大批处理时内存不足
OcrInput.TargetDPI0低分辨率扫描

超出标准读取,IronTesseract提供针对其他输入调整的读取器。 ReadPhoto处理文档的照片,ReadScreenShot处理屏幕截图。 专业文件教程仍为它们列出IronOcr.Extensions.AdvancedScan,但该包最后于2025年发布,两者不再需要它。 该教程还涵盖了护照和车牌识别器。


一个您可以重复的结果

请注意: 运行提示需要一个新的会话,.NET SDK、网络连接、助理与终端访问和IronOCR 试用或生产密钥。

提示

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.
Text

输出

Speckled test invoice that IronOCR read correctly in the acceptance test run

这是发票-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未经编辑公开,并且invoice.png具有缩小尺寸。

通过的标准

  • 您不需要编辑代码。
  • 打开任何图像显示小文本,边缘无裁剪,文本以及边缘上有斑点。
  • roundtrip.md中的每个发票号码和总数也出现在该页保存的OCR文本中。
请注意: IronOCR 变更日志列出了每次发布更改的内容。

运行记录

  • 机器运行在Windows 11 x64上,使用Core i9-14900KF,具有64 GB内存。
  • 每个项目都以.NET 9为目标。
  • 本指南、Copilot和Cursor运行了IronOcr 2026.9.4,而Claude Code、Codex和Antigravity运行了2026.10.2。Copilot添加了IronOcr.Linux 2026.9.4,而Codex、Cursor和Antigravity添加了IronOcr.Languages.German 2020.11.2。
  • 最后测试 2026年10月8日,与Cursor一起运行。 其他所有运行都发生在10月6日。
指南助手和版本模型持续时间(分钟)
本指南Claude Code 2.1.277,关闭权限提示Claude Opus 520.8
Claude CodeClaude Code 2.1.277Claude Sonnet 59.4
ChatGPT CodexCodex CLI 0.160.0GPT-6 Luna27.7
GitHub CopilotCopilot代理模式在VS Code 1.140中GPT-6 Luna,由Auto引导22.0
CursorCursor 3.23.23桌面应用Grok 4.783.5
Google AntigravityAntigravity CLI 1.3.0Gemini 3.8 Flash(高)32.7

代理特定指南

对于适合某个工具的步骤,请打开其指南。


故障排除


有问题吗?

如果您有任何问题,请联系support@ironsoftware.com

Curtis Chau 拥有卡尔顿大学的计算机科学学士学位,专注于前端开发,精通 Node.js、TypeScript、JavaScript 和 React。他热衷于打造直观且美观的用户界面,喜欢使用现代框架并创建结构良好、视觉吸引力强的手册。

阅读更多

准备开始了吗?

NuGet 下载量 6,394,824版本:2026.11刚刚发布