IronPrint结合Claude Code的使用方法
医院实验室通过一项服务打印三种文档。标本标签打印到采集台的标签打印机,结果报告打印到办公室打印机,快递单打印到调度室。 路由文件为每种文档类型指定了打印机名,文件中的一个错误字符可能会将病人结果发送到另一个房间而不出现错误。 Claude Code,是Anthropic的自主编码工具,在仓库中编辑文件和执行命令。 它可以作为命令行工具、VS Code和JetBrains编辑器的扩展,在Claude桌面应用程序以及claude.ai/code上使用。
IronPrint可以从.NET打印PDF和图像,可以静默打印或通过操作系统对话框打印。 PrintSettings.PrinterName属性选择作业的打印机,Printer.GetPrinterNames列出安装在机器上的打印机。下面测试的四个提示让Claude Code为实验室构建了一个打印机路由器,并记录了每个提示依赖的IronPrint行为。
在项目文件夹中打开终端后启动claude,并在发送第一个提示前用/model opusplan切换模型。 在路径前输入@会将该文件的内容包含在请求中。
Add the IronPrint NuGet package, version 2026.10.1, to LabPrintRouter.csproj.
Then have Program.cs check the printer names in @config/printer-routes.json.
The program does this, in order:
1. Applies the license key and prints License.IsLicensed.
2. Prints each name from Printer.GetPrinterNames on its own line, in quotes.
3. Prints each route's document type, its printer name in quotes, and installed or missing.
A route's printer counts as installed only on an exact match, case and spaces included.
Don't send any print job. Every printer on this machine opens a Save dialog.
My key is in the IRONPRINT_LICENSE_KEY environment variable.
Program.cs reads it at startup and assigns it to License.LicenseKey.
Don't print its value, and don't dump the environment to find it.
If you check the variable, a yes or no on whether it's set is enough.
输出中的引号使得尾部空格可见。
最小工作流程(5 个步骤)
- 从NuGet下载C#打印库
- 从项目文件夹打开Claude Code并使用/model设置模型
- 在CLAUDE.md中记录IronPrint的打印机命名规则,以便每个会话加载它们。
- 请求一个仅在配置的名称与安装的打印机完全匹配时发送作业的路由器。
- 审查路由报告,然后在发布前为每个路由的打印机发送一个测试页。
.NET打印项目中Claude Code的作用是什么?
代理编写了一个路由服务,它读取路由文件,将每个配置的打印机名称与安装的打印机进行比较,并为每个文档记录决定。 Claude Code还可以运行程序及其测试,并修复它们报告的失败。 开发者决定哪个打印机用于每种文档类型。
在第一个提示中说明包名和版本。 在称为slopsquatting的攻击中,攻击者注册了语言模型幻觉下产生的恶意包。 一个自行选择包名的代理可以安装这些包之一。
如何设置Claude Code?
安装和首次登录遵循Anthropic的快速入门。 设置完成后,claude在终端中打开会话并回答提示。
如何设置一个Claude Code可以工作的项目吗?
在第一个提示前将输入添加到仓库中。 本指南的示例项目包含一个用于net9.0-windows的.NET 9控制台应用程序、一个用于四种文档类型的路由文件,和一个以文档类型开头命名的七个PDF的收件箱。 一个路由使用了已安装打印机的准确名称。 另外三个使用未安装的打印机、不同大小写的已安装名称,以及带尾随空格的已安装名称。
Claude Code会在会话开始时读取CLAUDE.md,因此其规则适用于每个后续会话。 Quickstart添加包后,第二个提示编写文件,并定义一个只读子代理来审查打印代码。
Add a Printing section to a new CLAUDE.md at the repository root.
Later sessions should follow these rules from it:
- All printing goes through IronPrint. Add no other printing library.
- The package is IronPrint, at the version LabPrintRouter.csproj references now.
- Look up IronPrint member names in docs/IronPrint.xml.
- config/ and inbox/ are inputs. Leave them unchanged.
- The program sends no print job unless it runs with --send.
- Never run it with --send on this machine.
- Read the key from IRONPRINT_LICENSE_KEY. Never print it, save it, or dump the environment.
Copy IronPrint.xml to docs/ now, from the ironprint 2026.10.1 folder of the local NuGet cache.
Use the one under lib/net7.0-windows7.0.
If it isn't there, tell me, and still add the subagent below.
Then create a subagent at .claude/agents/printer-name-auditor.md.
Limit its tools to Read, Grep and Glob.
It reviews Services/ and returns three lists:
- IronPrint types and members in use, flagging any that docs/IronPrint.xml lacks.
- Types under IronSoftware.*, listed apart, since they ship in other assemblies.
- Every place a PrinterName gets set, and whether that name passed an exact installed check first.
Claude Code监视.claude/agents并在文件夹存在的情况下立即加载新添加的子代理定义,但仅当该文件夹在会话开始时已存在。 因此,示例仓库在会话打开之前持有该文件夹,里面有一个空的.gitkeep文件。 IronPrint的llms.txt索引了其操作指南、代理技能和其他文档。
在第一次打印调用前应用许可证密钥。 如果没有密钥,Printer.Print和Printer.PrintAsync会抛出异常,并且不会有作业排队。 IronPrint许可证密钥页面提供了试用密钥,并展示了代码分配和配置文件设置。
CLAUDE.md 中应包含哪些IronPrint打印机规则?
我们在IronPrint 2026.10.1上测试了每行的行为,一次持证一次无证。 在打印部分下粘贴区块。
These printer rules come from probes on IronPrint 2026.10.1:
- PrintSettings.PrinterName must equal a Printer.GetPrinterNames() entry, character for character.
- Any other name sends the job to the Windows default printer, and nothing throws.
- That covers a missing printer, a different case, a trailing space and an empty string.
- Decide whether to print with StringComparison.Ordinal.
- A trimmed or case-blind match may label a near miss, but it never sends a job.
- Printer.GetPrinterTrays() returns an empty list for a name that isn't installed.
- An empty tray list is also normal for some real printers, so it can't test a name.
- Printer.GetPrinterNames() works without a key, so a printer list proves nothing about licensing.
- IronPrint doesn't read IRONPRINT_LICENSE_KEY by itself. Assign License.LicenseKey at startup.
- Assigning a null key doesn't throw. License.IsLicensed just stays false.
- Without a key, Printer.Print throws AggregateException with a LicensingException inside.
- Without a key, Printer.PrintAsync throws LicensingException directly.
- LicensingException is IronSoftware.Exceptions.LicensingException, internal to IronPrint.dll.
- Print and PrintAsync both wait on the driver, and a Save dialog keeps them waiting.
两个异常形状影响错误处理。 处理程序检查PrintAsync引发的异常,而内层异常来自Print。 两者都不能使用catch (LicensingException),因为类型不是公共的,并且在探测中该子句因CS0122失败。
IronPrint是否拒绝不匹配的打印机名称?
根据指定打印机名称指南,名称比较是区分大小写的,错误的名称要么无声失败要么引发错误。 我们的探测在四个与安装打印机不符的名称下发送了一份两页的报告。 名称是未安装的打印机名,“adobe pdf”对于已安装的“Adobe PDF”,后面带空格的已安装名称,以及一个空字符串。 四次调用都没有抛出异常,并且每个作业都发送到了默认队列,在测试机器上是Microsoft Print to PDF。
因此,路由器必须在调用打印方法之前自行比较每个配置名称与安装名称。
IronPrint是如何构建打印作业的?
IronPrint将每个PDF页面作为PrintSettings.Dpi的分辨率的图像发送给驱动程序。 发给Microsoft Print to PDF的探测作业生成了一个两页的A4 PDF,其中每页是没有文本层的单个1239 x 1754像素的图像,渲染为150 DPI。 指南中的示例输出以默认300 DPI的方式遵循相同的模式,每A4页有2479 x 3508像素。 因此,检查PDF打印机的输出必须比较页面数或图像,因为打印的文本无法搜索。
为了不打印而检查作业,测试设置钩住了IronPrint在Windows上使用的WPF打印调用。 钩子将每个作业以XPS格式写入磁盘,且不排队,并且保存的页面图像测量为1239 x 1754像素,与探测作业相同。 IronPrint将其他设置写入作业的打印票据中。 设置Grayscale为打开时,票据请求了单色输出。 页面图像以RGB形式存储,仅含有灰色像素。 票据的复印份数来自NumberOfCopies,并且作业将每个页面保存一次。
标签路由展示了后备设置对作业的影响。 输入是specimen-label__S-031.pdf,一个100 x 50 mm的来自收件箱的标签,路由文件将其发送到LAB-ZEBRA-01,以300 DPI和PaperSize.PrinterDefault。
输出
标签路由的设置在不检查名称的情况下发送,因此作业回退到Microsoft Print to PDF。 然后,打印机的默认纸张尺寸解析为该打印机的A4纸,并且标签变为A4横页中的1179 x 591像素图像。 作业将该图像缩放到297 x 149 mm,即整页的宽度。
未做名称检查的情况下被IronPrint构建的标本标签S-031。一个300 DPI的A4横页,发送给Microsoft Print to PDF,并以文件代替打印机捕获。
如何为IronPrint生成可编译的代码?
将路由器及其报告作为一个提示发送至计划模式,以便在进行任何文件更改前达成设计一致。 按Shift+Tab直到状态栏显示计划模式,然后发送提示。 在opusplan下,计划来自Opus,而Sonnet在计划获批后进行修改。
Build the router in Services/PrintRouter.cs and the report in Services/RoutingReport.cs.
Call both from Program.cs after the printer check.
Program.cs passes PrintRouter the list from one Printer.GetPrinterNames call.
Each PDF in inbox/ is named <documentType>__<id>.pdf.
For each file, PrintRouter does this:
1. Finds the route for its document type in config/printer-routes.json.
2. Checks the route's printer against that list.
3. Builds PrintSettings from the route's paper size, orientation, DPI, copies and grayscale.
4. Sends the job with Printer.PrintAsync, but only under --send.
Each file gets the first of these decisions that fits:
- send, when the printer name matches an installed name exactly.
- name-mismatch, when it matches one after ignoring case and outer spaces.
- not-installed, when nothing matches.
- no-route, when the config has no route for the type.
Only send files reach the print call.
Put that call behind an interface so tests can use a fake.
Write out/routing-report.csv with a header row and one row per file, sorted by file name.
Its first columns are File, DocumentType, ConfiguredPrinter, Decision and InstalledName.
Then come PaperSize, Orientation, Dpi, Copies and Grayscale from the route.
InstalledName holds the installed printer for send and the near match for name-mismatch.
It stays empty for the other two decisions.
A no-route row leaves the five route columns empty too.
Write Grayscale as true or false.
Print a count of files per decision.
Run the program without --send and show me the CSV.
Then have the printer-name-auditor subagent review Services/.
提示的两个部分保护路由。
- 发送标志:没有
--send程序不发送作业,因此代理可以在打印机打开对话框的机器上运行和测试路由器。 - 审核子代理:一个自定义子代理加载CLAUDE.md,因此打印机名称规则也适用于其审核。 内置代理中,Explore和Plan在没有CLAUDE.md的情况下启动。
IronPrint路由代码能在没有许可证密钥的情况下测试吗?
可以,前提是测试替换了打印调用。 读取路由文件、列出已安装的打印机、构建PrintSettings均可无密钥运行,因此每个路由决定都可以在不持有密钥的构建服务器上进行测试。
Add a LabPrintRouter.Tests xUnit project that targets net9.0-windows.
It tests PrintRouter through a fake print sink.
The tests read the copies of inbox/ and config/ in their output folder.
Give the router a fixed printer list of "Adobe PDF", "Microsoft Print to PDF" and "Fax".
Assert these results:
- The decision for each of the 7 files.
- The fake receives the 2 result reports and nothing else.
- RPT-4471 goes out with A4, Portrait, 150 DPI, 1 copy and grayscale on.
- Printer.GetPrinterNames runs without throwing while no key is applied.
Set the project up this way:
- It lives in its own folder, and the repository root gets no solution file.
- LabPrintRouter.csproj must not compile anything in that folder.
- The test output folder gets copies of config/ and inbox/.
Run dotnet test one time, in a child process that has no IRONPRINT_LICENSE_KEY.
Tell me the pass count from that run.
在信任生成的IronPrint代码前应该验证什么?
用这些检查复审生成的IronPrint代码。
- 构建:IronPrint没有的成员会导致
dotnet build出现编译错误。 发布的IronPrint.xml还记录了PdfiumUtils类的内部方法,并且调用它的代码会以CS0122失败。 - 路由报告:在第一个实际作业前将每个决定与路由文件及已安装打印机列表比较。
- 每个打印机一页测试页:发布前,在每个设备上确认测试页已在路由指示位置打印。
AI编码助手指南提供了一个完整的IronPrint任务,以及固定的通过标准,作为可重复的测试。
在实际运行中这些提示生成了什么?
这四个提示按显示的顺序在单个Claude Code会话中运行。 在第三提示前,规则块被粘贴到CLAUDE.md中,没有人手动编辑代码。
- Quickstart输出命名了所有四个已安装的打印机,并标记了一个已安装的路由,三个缺失。
- 路由报告为每个7个文件保持了一行记录。
- 2个文件标记为发送,2个大小写不匹配,2个未安装,1个无路由。
- 每个决定都与我们对已安装打印机的独立核对相符。
- 近似匹配的是快递清单的Adobe PDF和病房总结的Microsoft Print to PDF。
- 在一个新副本中运行的清理发布生成了相同的报告。
- 没有密钥的情况下,程序报告IsLicensed为False,并且做出了相同的7个决定。
- 在没有IRONPRINT_LICENSE_KEY变量的进程中10/10的测试通过。
- 子代理没有发现IronPrint.xml中缺少IronPrint成员。
- 子代理发现了一个打印机名称分配,放在精确检查之后。
- 运行后的打印队列为空,并且抄本中没有命令使用--send。
- 两个发送作业的捕获中Adobe PDF队列包含3个A4竖版页面,每个是1239 x 1754像素的图像。
- 每个提示都留下了一个无警告和错误的发布构建。
- 对抄本、会话文件和仓库的扫描未发现许可证密钥的副本。
- 有些步骤与页面中的运行不一样。 键入的回复批准了提示3的计划,因为会话是无头运行的。 因为测试机器上的每个打印机打开一个节省对话框没有作业被打印,所以两个发送作业以XPS被捕获。
路由报告中第一个发送行是result-report__RPT-4471.pdf,一个来自收件箱的两页结果报告。
输出
捕获工具将该行的设置从路由报告传递给IronPrint。 下方的PDF在未做更改的情况下包含了两个被捕获的页面。
IronPrint从运行的路由报告中构建的发送作业的结果报告RPT-4471。两页均为Adobe PDF队列的150 DPI A4竖版,捕获到文件中代替打印机。
Cursor也有计划模式,Cursor指南将其提示3发送于此。 Codex从AGENTS.md中读取其项目规则,作为CLAUDE.md的对应部件, 如ChatGPT Codex指南所示。 此运行的Claude Code版本、模型和总持续时间显示在运行记录中,位于测试机器旁边。
故障排除
- 当
Print阻塞UI线程时,Print() Method Hangs in WPF and WinForms - 如果在Web.config中设置的密钥未被应用,web.config许可证设置
- 在没有UI支持的目标框架上的
ShowPrintDialog调用时,ShowPrintDialog在不支持的目标框架上的行为
有问题吗?
如果您有任何问题,请联系support@ironsoftware.com
Curtis Chau 拥有卡尔顿大学的计算机科学学士学位,专注于前端开发,精通 Node.js、TypeScript、JavaScript 和 React。他热衷于打造直观且美观的用户界面,喜欢使用现代框架并创建结构良好、视觉吸引力强的手册。