在使用AI编码助手时使用IronPrint
IronPrint是Iron Software为.NET开发的打印库。 它可以将PDF文件和图像从C#代码中发送到打印机,用户可以选择静默打印或通过系统打印对话框进行打印,支持Windows、macOS、iOS和Android。
打印是许多文档功能的最后一步,也是难以测试的一步,因为输出会从机器中输出。没有IronPrint的文档,助手无法知道存在哪些成员。在第一次请求之前请提供文档。
每个助手都需要相同的四个步骤。 添加包,从环境中读取密钥,给助手提供IronPrint的机器可读文档,并在关闭任务之前检查每个作业包含的内容。 相同的步骤涵盖静默打印、按名称选择打印机、打印设置和对话框打印。
如何使用IronPrint和AI编码助手
- 从NuGet下载C#打印库
- 定位一个特定操作系统的框架,例如
net9.0-windows - 在第一次作业之前从
IronPrint_LICENSE_KEY分配IronPrint.License.LicenseKey - 在编写任何代码之前给助手提供IronPrint的llms.txt索引
- 将IronPrint规则保存在助手每个会话加载的文件中
- 检查每个打印机名称是否在
Printer.GetPrinterNames中,并确认每个作业发送的内容
我的AI助手不能独立处理打印吗?
《静默打印指南》将System.Drawing.Printing路线描述为每种文档类型都有页面事件处理程序的PrintDocument,且没有内置PDF渲染。
IronPrint的API集中在两个类Printer和PrintSettings上,步骤3列出了描述它们的机器可读文档。
在助手编写第一个打印调用之前,适用三点。
- 即兴打印代码成本:每次尝试通过原始
System.Drawing.Printing调用重建缺失设置都会消耗token,并且该代码将在macOS上抛出TypeInitializationException。 - 软件包名称重要:NuGet包和命名空间都是
IronPrint。 Slopsquatting是语言模型为不存在的包生成的软件包名称注册,因此提示应准确命名该包。 - 许可没有重复费用:IronPrint许可证是永久的。 许可证页面上的每个级别都有自己设置的开发者、位置和项目限额。
步骤1:安装IronPrint
要求助手添加包并命名其解决的版本。
Add IronPrint version 2026.10.1 from NuGet to this project and restore packages.
Then tell me which IronPrint version the restore resolved.
.NET CLI命令固定相同的发布版。
dotnet add package IronPrint --version 2026.10.1
目标框架决定了包使用哪个打印路径。 一个net9.0-windows构建加载了IronPrint的WPF打印路径,而Windows上的普通net9.0构建加载了基于System.Drawing.Printing的路径。 这两条路径为同一打印机报告了不同的托盘名称。 对于Adobe PDF,WPF路径返回了AutoSelect,而普通构建返回了Automatically Select和OnlyOne。 macOS需要与匹配工作负载的net9.0-macos或net9.0-maccatalyst目标,Linux不是支持的打印平台。
步骤 2:应用您的许可证密钥
没有密钥的情况下,程序仍然可以列出打印机及其托盘,但PrintAsync在创建作业之前会抛出IronSoftware.Exceptions.LicensingException,信息为需要生产许可证。 Print抛出了一个在System.AggregateException中包装的相同异常。 密钥无效情况下,信息变为许可证密钥未被识别。 因此未授权程序不会产生任何输出,无论是否带水印。下面的表单颁发免费试用密钥。
从今天开始在您的项目中使用 IronPrint 免费试用。
程序启动时设定一次密钥,在第一次IronPrint调用之前。 密钥来自环境变量,因此没有提交包含它。
IronPrint.License.LicenseKey = Environment.GetEnvironmentVariable("IRONPRINT_LICENSE_KEY");
一旦分配了有效密钥,IronPrint.License.IsLicensed返回true。 IronPrint.License.IsValidLicense测试任何密钥字符串。 库永远不会自行读取IRONPRINT_LICENSE_KEY,在这篇文章的探测中,设置变量而不分配留下IsLicensed为false。
步骤3:将您的助手指向IronPrint的LLM文档
- llms.txt索引43页,每行一页。 在每个新会话中包含它。
- llms-full.txt在一个文件中包含33个文档页的摘要。
- llms-full-catalog.txt是包含博客文章的索引。
- 对象引用提供了每个成员的签名。
- 可读取技能文件的工具可以加载skill.md。 其章节涵盖安装、授权、打印食谱、平台部署及已知失败的修复。
- AI产品事实表列出了支持的平台和主要用例。
如果任务依赖于具体的指南,请将助手指向该页面。
发送索引并指示其保持在索引内。
Read https://ironsoftware.com/print/csharp/llms.txt first, before writing any IronPrint code.
Work only from what those pages document.
IronPrint's API is small, and it fits together like this:
- Printer is static, with Print, PrintAsync, ShowPrintDialog, GetPrinterNames, and GetPrinterTrays.
- PrintSettings holds the printer name, PaperSize, PaperOrientation, PaperMargins, Dpi, and copies.
- It also has the grayscale switch.
- There's no document object to build. You pass a file path or the file's bytes.
Look up any member those pages don't show.
The API reference is https://ironsoftware.com/print/csharp/object-reference/api/.
Compile once to confirm the call exists.
If it's still doubtful, tell me instead of guessing.
Then check this project's IronPrint calls against the docs and list any they don't cover.
关于不确定的成员的行让助手报告空缺而不是发明一个成员。
步骤4:将IronPrint上下文添加到您的项目中
聊天指令在会话结束时终止,因此IronPrint规则会被放入工具每次读取的项目文件中。Claude Code加载CLAUDE.md,ChatGPT Codex加载AGENTS.md,GitHub Copilot文件为.github/copilot-instructions.md。 Cursor加载.cursor/rules/中的.mdc规则以及AGENTS.md,Google Antigravity加载AGENTS.md或GEMINI.md。 相同文本在每个文件中都适用,并为Cursor规则添加了MDC前材料。 从这个块开始。
All printing in this project goes through IronPrint.
`IronPrint` is the name of both the NuGet package and the namespace.
Follow these rules in all printing code:
- Read the documentation index first at https://ironsoftware.com/print/csharp/llms.txt.
- Tools that accept skill files load https://ironsoftware.com/print/csharp/skill.md.
- Target an OS framework such as `net9.0-windows`, not plain `net9.0`.
- Set `IronPrint.License.LicenseKey` from IRONPRINT_LICENSE_KEY at startup.
- The key never goes to output, logs, or source control.
- Take printer names from `Printer.GetPrinterNames` and match them exactly.
- Refuse any other name. IronPrint sends an unknown name's job to the default printer.
- Use `Printer.PrintAsync` in UI code. `Printer.Print` blocks the thread that calls it.
- Print PDF, PNG, JPG, BMP, or TIFF. GIF and HTML files throw an unsupported file type error.
- Leave `PaperOrientation` on `Automatic` for mixed pages.
- `Portrait` shrinks a landscape page onto a portrait sheet.
- Read tray names in the build that prints. They differ between framework targets.
- Do not add a second printing library. If IronPrint cannot do something, say so.
技能文件的单文件示例仍然固定于IronPrint 2026.8.1,比此处测试的2026.10.1包早两个版本。
步骤5:一次性设置提示
该提示将步骤1到4合并为一条消息,在项目或会话开始时发送。
Get this project ready for printing with IronPrint, under these rules:
- Reference the IronPrint NuGet package at version 2026.10.1.
- Target net9.0-windows or another OS-specific framework, not plain net9.0.
- Assign IronPrint.License.LicenseKey from the IRONPRINT_LICENSE_KEY environment variable.
- Do that at startup, before any other IronPrint call.
- Keep the key out of the console, the logs, and anything committed.
- Read https://ironsoftware.com/print/csharp/llms.txt before coding, and stay within it.
- For members it doesn't cover, search https://ironsoftware.com/print/csharp/object-reference/api/.
- Build before you rely on a member you found there.
- Where the tool takes skill files, load https://ironsoftware.com/print/csharp/skill.md.
- Trust the compiler over the skill file when they disagree.
- Only pass a printer name that Printer.GetPrinterNames returned.
Save these rules in the instructions file that later sessions read.
Then name the file you wrote, and any other change you made.
步骤6:产生可用代码的提示模式
说明打印机名称的来源、打印的文件类型和每个打印设置。
笼统 使用IronPrint打印发票。
更好 从配置中获取打印机名称,确认它出现在
Printer.GetPrinterNames中,并使用PrintSettings将invoice.pdf打印为A4、自动方向、灰度和两份副本。
命名IronPrint发布版本和代码所针对的目标框架。
更好 使用IronPrint 2026.10.1在net9.0-windows上,并在第一个作业前从IronPrint_LICENSE_KEY分配密钥。
要求完整文件。 片段可以省略using IronPrint;指令和分配密钥的行。
更好 返回包含所有using指令和许可证分配在内的Program.cs,以便它无需编辑就可以构建。
步骤7:验证收到的内容
使用2026.10.1进行探测,衡量了以下错误背后的行为。
- 未检查的打印机名称:与未安装的打印机不匹配的名称不会引发错误。 在我们的测试中,
PrintAsync未引发异常,作业发送到默认打印机Microsoft Print to PDF,它保持直到有人回答其另存为对话框。 仅接受Printer.GetPrinterNames返回的名称。 - 在UI线程上的
Print:同步调用会阻止调用的线程,这会冻结WPF和WinForms界面,如挂起故障排除页面所述。 使用PrintAsync。 - 提示的测试打印机:发送到Microsoft Print to PDF的两个作业仅在有人输入文件名后完成。 无监督测试需要一个能够自行完成的打印机,或下一节的捕获方法。
- 在普通目标上的
ShowPrintDialog:故障排除页面表示该调用可能会在普通.NET目标上引发AggregateException。 在Windows上,普通的net9.0构建没有抛出异常。 调用打开一个真实的打印对话框,并在对话框关闭后返回,只用了10.1秒。 保持对话框调用在无监督代码之外。
让代理验证其自己的工作
要求助手运行这些检查并报告证据。
Rebuild in Release and run the app again.
If it has a capture or dry-run mode, use that and send nothing to a real printer.
When it finishes, give me proof:
- List every IronPrint type and member in the code, each with the docs page that covers it.
- Give the value of IronPrint.License.IsLicensed.
- Show where the printer name is checked against Printer.GetPrinterNames.
- For each document, give the printer, the settings sent, and the result.
Say whether the settings each job carried match the settings the code asked for.
使用IronPrint静默打印作业
在Windows上,IronPrint 2026.10.1将每个作业传递给WPF的打印对话框类而不显示它,打印票包含设置。 一个测试补丁截取了该调用,并将每个作业保存为XPS文件,因此本节的数字是在没有纸张的情况下测量的。
- 每个PDF页面在
Dpi设置下呈现为一张图像。 如下运行中,一张A4页面以1240乘1753像素的图像在150 DPI下呈现,而在我们的探测中以2479乘3508像素在300 DPI下呈现。 PaperSize.A4生成210乘297毫米页面,PaperSize.Letter生成215.9乘279.4毫米页面。- 使用
PaperOrientation.Automatic,横向页面作为横向页面打印。 使用Portrait,同一个页面被缩小以适应纵向纸张,只填满了上部部分。 - 当
Grayscale开启时,彩色PDF页面作为灰度图像到达。 PNG中的红色文字保持红色,只有打印票请求将其输出为单色。 NumberOfCopies设定票的副本数量,作业仍保持一页翻页集。
打印设置指南称打印机可以限制DPI,并且一些平台忽略副本数量,因此这两个值都要求发送到驱动程序。
使用IronPrint的文件格式
在2026.10.1中,PDF、PNG、JPG、BMP和TIFF文件进行打印。 GIF和HTML文件在创建作业之前会抛出System.IO.InvalidDataException,信息为不支持的文件类型,尽管静默打印指南列出了这两种格式。 技能文件列出了可以工作的五种格式。 首先将HTML渲染为PDF,例如使用IronPDF。
图像以单页面形式打印,缩放到页面宽度。 一个1200乘800像素的PNG在横向A4页面上用Automatic方向打印,并在纵向页面的上部区域用Portrait方向打印。
一个您可以重复的结果
测试是一个单一提示,所有代码来自助手。 创建一个空文件夹,在其中打开一个新会话,粘贴提示,并根据通过的内容列表进行检查。
IRONPRINT_LICENSE_KEY中的IronPrint试用版或生产密钥,以及PRINTER_NAME中已安装的打印机名称。 提示将每个作业捕获到一个XPS文件,因此打印机不接收任何东西。提示
由于应用程序编写了自己的测试集,测试运行中的六个文件按打印顺序连接在下面。 它的四个PDF未更改,PNG和GIF每个在它们的像素大小的页面上出现。
Build a .NET 9 Windows console app that silently prints a batch of documents with IronPrint.
Start in an empty directory and keep deliverables there.
If it holds a project or source files, tell me and stop.
Work alone and note each choice you make.
Work in this order:
1. Pin IronPrint at 2026.10.1 from NuGet and target net9.0-windows with WPF on, for step 4.
2. Read https://ironsoftware.com/print/csharp/llms.txt, mainly its silent printing guide.
3. Follow these code rules:
- Call only IronPrint members that the linked pages document.
- At startup, assign IRONPRINT_LICENSE_KEY to IronPrint.License.LicenseKey. You never need the key.
- Never show the key, list environment variables, or write the key to any file.
- When IronPrint.License.IsLicensed is false, report it and stop.
- Check PRINTER_NAME against Printer.GetPrinterNames before the first job.
- If the list lacks it, stop with a clear message and print nothing.
- Pass IronPrint only printer names from that list.
- Record its trays from Printer.GetPrinterTrays.
4. Send no job to a real print queue. Capture each one to XPS instead:
- IronPrint hands each Windows job to WPF's PrintDialog.PrintDocument.
- Patch it with Lib.Harmony 2.3.6 so your code runs instead.
- Save the paginator and PrintTicket to capture/<name of the file being printed>.xps.
- Note the queue and ticket settings.
- Before the first job, prove the patch with a null paginator that sends nothing.
- That self-test writes no file. Stop if it fails.
- Never clear capture/ at startup.
5. Generate testset/ with your own code and no added package:
- invoice-01.pdf and invoice-02.pdf, one A4 portrait page each.
- report-03.pdf, three A4 portrait pages, and chart-04.pdf, one A4 landscape page.
- label-05.png, 1200 by 800 pixels, and cover-06.gif, 600 by 400 pixels.
- Each page shows its file name and page number in large text.
6. Print them in that order with one PrintSettings object:
- A4, automatic orientation, one copy, grayscale and 150 DPI.
- Send each job with Printer.PrintAsync and time it.
- Record a failed job and move on.
7. Run a Release build with the given PRINTER_NAME.
8. Rerun with PRINTER_NAME set to No Such Printer 12345, then set it back.
9. That rerun must stop before any job and leave capture/ unchanged.
10. Write runrecord.md from measured values, one row per test file, with these columns:
- File, source pages counted from the file, and elapsed milliseconds.
- Result, either captured or the exception type and message.
- XPS pages read back from the file, each with width and height in millimeters and orientation.
- The ticket's queue, copy count, page size, orientation, color and resolution.
11. Below the table, give these values:
- IsLicensed, the self-test result and the printer's trays.
- Where the code stops if IsLicensed is false, without touching the key.
- What the No Such Printer 12345 run printed, and whether capture/ changed.
12. Tell me the build result, jobs captured out of six, cover-06.gif's outcome and your assumptions.
输出
运行未编辑的运行记录为每个六个文件提供了测量值。
捕获写的每一页在下面的PDF中,以打印顺序。
批量输出为IronPrint生成了作业,捕获到XPS文件中代替打印机,并逐页转换为PDF而未编辑。
- net9.0-windows版本构建报告了0警告和0错误。
- 补丁自测通过,分页器为空,未写文件。
- 捕获了六个作业中的五个,共有7个页面。
- 三个纵向PDF变为210乘297毫米的页面,每个源页面一页。
- 横向PDF和PNG变为297乘210毫米的横向页面。
- 每个票指定A4纸张、一份副本、单色输出和150乘150 DPI。
- GIF被记录为不支持文件类型错误的失败。
- 使用No Such Printer 12345的运行在任何作业开始前停止,捕获文件夹未变。
- 对XPS文件的独立读取与运行记录中的每个数字匹配。
- 接受构件的新启动生成相同的五个捕获。
- 关键扫描未在项目文件夹中发现许可证密钥的副本。
- 安装提示保持2026.10.1固定。
- 文档提示未更改代码。
- 其答案报告了静默打印指南和技能文件对GIF和HTML的意见不一致。
- 一个新的CLAUDE.md接收了设置提示的规则和测量行为。
- 自我检查确认所有五个捕获到的作业都具有请求的设置。
- 有些步骤与页面中的运行不一样。 接受提示先运行,随后在同一会话中运行步骤1、3、5和7的提示。 应用程序匹配PRINTER_NAME,不区分大小写,并从打印机列表中传递了确切名称。
通过的标准
- 发布构建成功,无需对代码进行手动编辑。
- 当
IsLicensedfalse或PRINTER_NAME不在Printer.GetPrinterNames中时,应用程序在任何作业之前停止。 - 每个可打印文件都有一个XPS捕获,其页数、页面大小和票值在您自己读取XPS文件时匹配runrecord.md。
- GIF被记录为失败,批处理继续。
- 未在任何项目文件中发现许可证密钥。
运行记录
- 所有运行都使用一台 Windows 11 x64 台式机,搭载 Core i9-14900KF 和 64 GB 内存。
- 没有指南发送作业到实际打印机。
- 此指南的项目目标是
net9.0-windows并固定IronPrint2026.10.1,使用Lib.Harmony 2.3.6进行捕获模式。 - 指南的持续时间是其页面上提示的日志时间总和,并且此指南的行数仅计算接受提示的。
- 表中的版本和型号来自每个工具自己的会话文件和运行日志,而不是独立的版本检查。
- 最后测试于2026年10月15日,此指南、Claude Code、ChatGPT Codex、GitHub Copilot(仅提示1和2)和Google Antigravity,以及2026年10月16日对于Cursor。
| 指南 | 助手和版本 | 模型 | 持续时间(分钟) |
|---|---|---|---|
| 本指南 | Claude Code 2.1.277,一个会话,关闭权限提示 | Claude Opus 5 | 23.7 |
| Claude Code | Claude Code 2.1.277,opusplan别名,计划模式下的提示3 | Claude Sonnet 5,Claude Opus 5计划提示3 | 16.4 |
| ChatGPT Codex | Codex CLI 0.160.0,一次坐下 | GPT-6 Luna | 17.9 |
| GitHub Copilot | Copilot CLI 1.0.95,在月度配额内在提示2中停止 | GPT-6 Luna,Auto 选择 | 1.5,仅提示1和2 |
| Cursor | Cursor 3.23.23桌面应用,计划模式下的提示3 | Grok 4.7 | 102.8 |
| Google Antigravity | Antigravity CLI 1.3.5,在提示1期间更新为1.3.6,使用/plan发送提示1和5,权限提示跳过 | Gemini 3.8 Flash(高) | 61.9 |
代理特定指南
每个工具指南通过这些步骤构建一个打印任务。
- 为Claude Code使用IronPrint将每种实验室文档类型路由到命名的打印机并拒绝机器没有的名称
- 使用IronPrint与ChatGPT Codex进行夜间仓库作业打印装箱单,按订单数设置副本数
- 使用IronPrint与GitHub Copilot为在打印前平展填充的PDF交接表单的仓库服务打印
- 使用IronPrint与Cursor从
System.Drawing.Printing迁移装箱单和标签打印到IronPrint打印设置 - 使用IronPrint与Google Antigravity为酒店前台web应用从控制器进程注册卡
故障排除
- 在WPF和WinForms中Print()方法挂起,为界面在同步打印期间停止响应
- 不受支持目标框架上的ShowPrintDialog行为,用于在普通目标框架上的对话框调用
- 打印混合PDF时每页方向丢失,当携带纵向和横向页面的PDF以一个方向打印时
有问题吗?
如果您有任何问题,请联系support@ironsoftware.com
Curtis Chau 拥有卡尔顿大学的计算机科学学士学位,专注于前端开发,精通 Node.js、TypeScript、JavaScript 和 React。他热衷于打造直观且美观的用户界面,喜欢使用现代框架并创建结构良好、视觉吸引力强的手册。