如何在Cursor中使用IronPrint
当一个AI代理将C#打印服务从System.Drawing.Printing移植到IronPrint时,未更改的设置仍然可以编译,但其含义会发生变化。 旧代码中的50边距表示半英寸,而在IronPrint中同样的50表示50毫米。 打印机名称也保持不变。 在我们的测试中,IronPrint将一个未安装的打印机名称的工作发送到了默认打印机,没有引发错误。
Cursor基于VS Code代码库,增加了一个代理到编辑器。 迁移是否保留这些细节取决于Cursor的两个功能,即给代理的文档链接和在.cursor/rules中的项目规则。 这些示例适用于打印包装单、货架标签和退货单的发货办公室服务。 服务从一个以百分之一英寸为单位的配置文件中构建每个作业,并通过提示将其移动到IronPrint。
有关涉及的设置的详细信息,请参见纸张边距指南和打印机名称指南。
快速入门打开发货仓库,开始一个新的代理聊天,并发送此提示。
@src/DespatchDesk/ @config/despatch.json
Start by fetching https://ironsoftware.com/print/csharp/llms.txt.
Also read the pages it links on printer names and print settings.
Then add a printers command to Program.cs:
`dotnet run --project src/DespatchDesk -- printers`
Have it show what IronPrint reports on this machine:
- Every name from Printer.GetPrinterNames, in square brackets so spaces show.
- The trays GetPrinterTrays returns for each printer, or "none".
- Whether each printer named in config/despatch.json is in that list.
Have the program read IRONPRINT_LICENSE_KEY from the environment.
Set License.LicenseKey from it before anything else runs.
Don't echo the key or dump the environment.
IronPrint stays on version 2026.10.1.
Send no print jobs, and leave DespatchJobs.cs on System.Drawing.Printing for now.
Run the printers command and show me what it prints.
不要将许可证密钥粘贴到聊天中。 Cursor的代理可以编辑文件和运行shell命令,因此可以将粘贴的密钥复制到提交消息、源文件或构建日志中。 这个提示让程序将IRONPRINT_LICENSE_KEY分配给License.LicenseKey,因为单独设置变量在测试中导致IronPrint 2026.10.1未获授权。 在没有密钥的探测中,IronPrint列出了打印机和托盘,并且Printer.Print在发送任何作业之前抛出了一个围绕LicensingException的AggregateException。 许可证密钥指南列出了IronPrint读取密钥的位置,例如appsettings.json。
最小工作流程(5 个步骤)
- 从NuGet下载C#打印库
- 在.cursor/rules下添加一个包含IronPrint文档链接的项目规则
- 在规则中记录边距单位为毫米,打印机名称必须完全匹配
- 在每次请求中引用打印代码和签署的设置报告
- 将每个作业的设置与该报告进行比较,然后确认API参考中的每个成员名称
您应该使用哪种Cursor模型进行IronPrint工作?
Composer是Cursor自己的代理模型。 模型选择器还提供Cursor共同开发的SpaceXAI的Grok模型,以及来自OpenAI,Anthropic,Google和其他实验室的模型。 下面的测试运行使用了Grok 4.7。
如何将IronPrint文档提供给Cursor?
在每次请求中添加IronPrint的文档链接,或者在一个项目规则中为整个团队添加一次。 IronPrint的llms.txt列出了每个指导、示例和故障排除页面并附带摘要,而llms-full.txt则包含33个文档页面的摘要。 截至2026年10月,Cursor在其@菜单中列出了文件夹,文件,过去的聊天,浏览器,终端和Git差异,因此代理获得IronPrint的文档作为链接。
文档说明空打印机名称选择默认打印机,IronPrint关于指定打印机名称的指南说不匹配的名称会静默失败或引发错误。 测试中未发生任何一种结果,因此项目规则记录了测量的行为。
@src/DespatchDesk/Printing/ReprintService.cs
Answer five questions about IronPrint without editing any files:
1. What unit does the Margins class use?
2. Which sides does each value of new Margins(10, 20) set?
3. What does Printer.Print do when PrintSettings.PrinterName is null?
4. What do the docs say about a name GetPrinterNames doesn't return, and does the rule agree?
5. Does NumberOfCopies always produce that many copies?
Base each answer on https://ironsoftware.com/print/csharp/llms.txt or a page linked from it.
Check them against the printers output from the last turn where that helps.
Cite the page behind each answer.
If no page covers a question, say so plainly rather than guess.
如何编写项目规则以编码您的IronPrint约定?
项目规则是存储在.cursor/rules中的.mdc文件,并提交到存储库,因此每次克隆都会加载相同的规则。 Cursor的规则系统忽略那里一个普通的.md文件,因为它不带前置内容。
将以下规则保存为.cursor/rules/ironprint-despatch.mdc。 当请求上下文中的C#文件位于src/DespatchDesk或tests下时,Cursor在globs设置和alwaysApply为false时附加它,其他请求时则不附加。
---
description: IronPrint conventions for the despatch printing code
globs: src/DespatchDesk/**/*.cs, tests/**/*.cs
alwaysApply: false
---
Despatch printing code uses IronPrint and follows these rules:
- Print with IronPrint only.
- Reference IronPrint 2026.10.1 from NuGet and use its IronPrint namespace.
- Do not add another printing package.
- Move existing code to IronPrint only when a request asks for it.
- The IronPrint docs index is https://ironsoftware.com/print/csharp/llms.txt.
- Read the license key from the IRONPRINT_LICENSE_KEY variable.
- Keep the license key out of output and files.
- IronPrint Margins uses whole millimeters, as left, top, right, bottom.
- System.Drawing.Printing.Margins uses hundredths of an inch, as left, right, top, bottom.
- new Margins(a, b) sets left and right to a, and top and bottom to b.
- Printer.Print sends the job to the default printer if PrinterName is not an exact installed name.
- Check the name against Printer.GetPrinterNames before every job.
- Run the print and reprint commands only when I ask for them.
- Check settings with the jobs and reprint-jobs reports instead.
- If an IronPrint member is not in the docs, say that and cite the page you searched.
- Never make up an IronPrint member.
根据Cursor的规则页面,代理使用规则,但Tab不使用,并且用户规则未包含在内联编辑中。 手动检查来自Tab和内联编辑的建议是否符合规则。
如何将System.Drawing.Printing设置映射到IronPrint?
使用IronPrint的请求并未说明每个PrintDocument设置被哪个IronPrint设置替换。 下表给出了这种映射。
| System.Drawing.Printing | IronPrint |
|---|---|
PrinterSettings.PrinterName | PrintSettings.PrinterName |
PageSettings.PaperSize,以百分之一英寸为单位 | PaperSize枚举,例如PaperSize.A4 |
PageSettings.Landscape | PaperOrientation.Landscape或PaperOrientation.Portrait |
PageSettings.Margins,百分之一英寸,左,右,上,下 | PaperMargins 带有 Margins 值,毫米,左,上,右,下 |
PrinterSettings.Copies | NumberOfCopies |
PageSettings.Color设为false | Grayscale设为true |
PrintDocument.Print有一个PrintPage处理程序 | Printer.Print有一个文件路径和设置 |
配置的50边距是12.7毫米,舍入为13,因为构造函数接受整数。 IronPrint的纸张大小指南列出四值顺序为上、右、下、左,而随附的XML文档和库的探测都给出左、上、右、下。
如何规划和执行向IronPrint的迁移?
Shift+Tab循环切换聊天的模式。 在计划模式停止,发送以下请求。 Cursor将计划保存到一个markdown文件中,可以在审批前编辑。
@src/DespatchDesk/Printing/ @TestData/expected-jobs.tsv
Plan the move of DespatchJobs from System.Drawing.Printing to IronPrint.
Write no code until I approve.
ReprintService already uses IronPrint, so keep it out of this plan.
The plan needs to cover these points:
- Each PrintDocument setting DespatchJobs uses, with its PrintSettings replacement or none.
- How margins in hundredths of an inch become IronPrint margins, including the order of the sides.
- How the jobs command keeps its columns and its values.
Keep config/despatch.json, JobSummary and the public Describe and Print signatures unchanged.
Where something is unclear, write down the assumption you made.
在错误的地方编辑计划。 要批准它,请点击Build,或返回到代理模式并发送此请求。
@ironprint-despatch
Go ahead with the approved plan.
TestData/expected-jobs.tsv holds the jobs report the despatch office signed off.
A row matches when all eight columns agree with it.
Then follow these four steps:
1. Run `dotnet run --project src/DespatchDesk -- jobs` and count the matching rows.
2. Move DespatchJobs to IronPrint.
3. Remove System.Drawing.Common from the project file.
4. Build the solution and run the jobs command a second time.
Tell me the match count before and after the move, then show me the diff.
@ironprint-despatch提及通过名称将规则添加到请求。 工作报告打印每个文档的设置而不发送作业,因此比较在没有连接打印机的机器上工作。
Cursor功能适合哪个变化?
根据变更的大小选择功能。 Tab提供了已经进行中的编辑的其余部分。 内联编辑(Ctrl+K)从一个单句指令更改所选代码。 代理更适合跨多个文件的更改或需要构建的更改。 当撤销错误更改比编写计划需更长时间时,计划模式值得多一步,并且云代理处理无人值守的工作。
选择DespatchJobs的Print方法,并使用Ctrl+Shift+L或在macOS上使用Cmd+Shift+L将其添加到聊天中作为上下文。 然后从代理模式发送此请求。
Make DespatchJobs.Print refuse a printer that isn't installed:
- Check the name against Printer.GetPrinterNames before anything is sent.
- Use an ordinal, case-sensitive comparison so spaces and case must match too.
- If it's missing, throw an error whose message includes every installed printer name.
- Have the print command show that message and exit with code 2.
Then run these two commands and show me what each one prints:
`dotnet run --project src/DespatchDesk -- print packing-slip`
`dotnet run --project src/DespatchDesk -- print packing-slip --printer "adobe pdf"`
第二个命令与已安装的打印机名称仅在大小写上不同。
当IronPrint找不到打印机名称时会发生什么?
旧服务因错误而停止。 System.Drawing.Printing抛出一个InvalidPrinterException,消息为“访问打印机'发货-A4-01'的设置无效。” IronPrint 2026.10.1将该名称的捕获作业以及另外三个名称的作业发送到了默认打印机。 这些是IronPrint边距指南中的示例打印机名称,已安装的打印机以小写键入,同一个打印机名称带有尾随空格。
Microsoft Print to PDF是测试机器的默认打印机,要求文件名,因此无人值守的服务将停在“另存为”对话框。 打印机名称指南指出每个返回的名称与Windows打印机设置中的名称完全匹配,这使得与Printer.GetPrinterNames的序数比较成为每个作业之前要运行的检查。
如何调试两个不一致的打印路径?
重复打印亭代码已经使用IronPrint,并且其设置报告与签署的报告在每一行中都不一致。
@src/DespatchDesk/Printing/ReprintService.cs @TestData/expected-jobs.tsv
Find out why the reprint report disagrees with expected-jobs.tsv on every row.
The jobs report matches it, and both read config/despatch.json.
Work through these steps before changing any code:
1. Run the reprint-jobs command and the jobs command, and compare them.
2. List each column where reprint-jobs differs, per document type.
3. Explain what ReprintService does to cause each difference.
Then fix ReprintService and keep it on IronPrint.
Give its Print method the same printer check.
Keep the check out of BuildSettings and Describe, since reprint-jobs reads those.
Run reprint-jobs again and tell me how many rows match, before and after the fix.
不一致有两个原因。 亭代码直接将百分之值传递给两值构造函数,因此包装单报告了50毫米的边距,退货单在底部重复其上部值,而配置设置为80百分之。 它还为每个文档设置横向,因为它是为货架标签而写的。
输出
在修复之前的运行捕获中,横向方向发送的纵向包装单被布局在297乘210毫米的纸张上。 其页面被缩放到148.4乘209.9毫米,纸张的其他148.5毫米保持空白。
具有未修复ReprintService设置的包装单,IronPrint构建的作业,捕获到文件代替打印机。
IronPrint发送到打印机的是什么?
测试机器上的四台打印机中没有自动完成作业,因此没有在纸上打印任何内容。 对于这些测量,边距指南的样本和已迁移的服务保持不变,只有进入Windows打印的最终调用被替换。 在该调用中,补丁将每个作业的页面和打印票据保存为XPS文件,打印队列没有接收任何内容。 以下测量来自这些文件,并描述了打印机将接收到的内容。
- 边距: 边距为0、10、15、20、30和50毫米产生相同的页面,页面图像填满了纸张。 System.Drawing.Printing版的包装单在12.7毫米的边距内绘制了其图像。
- 灰度: 对于PNG页面,设置仅更改了打印票据,图像仍然是彩色的。 对于PDF页面,图像本身是灰色的。
- 分辨率: PDF 以
Dpi设置渲染,A4在300时为2479乘3508像素。 PNG按其自身的像素尺寸发送。
在IronPrint将PaperMargins应用于Windows上的页面之前,需要在边缘留白的布局必须在文档本身中包含白边。
输入
服务从TestData/jobs打印三个PNG文件,slip-1001.png,labels-aisle-07.png 和 return-2207.png。 每个都是150 DPI的图像,边缘有红色边框。
输出
以下PDF显示了Cursor写的代码中的每个作业。 每个页面图像未更改,并保持捕获页面的大小。
具有IronPrint构建的作业的包装单,捕获到文件代替打印机。 该作业持有一张A4纵向页面,且其打印票据请求两个单色副本。
具有IronPrint构建的作业的货架标签,捕获到文件代替打印机。 这张A4横向页面的尺寸为297乘210毫米,打印票据指定彩色输出。
具有IronPrint构建的作业的退货单,捕获到文件代替打印机。 它的单张A5纵向页面尺寸为148乘210毫米,并且在打印票据中设置了单色输出。
如何为打印代码编写回归测试?
Cursor的云代理运行在Ubuntu机器上,示例目标是net9.0-windows,因此在本地代理聊天中发送此请求。
@tests/DespatchDesk.Tests/ @TestData/
Write xUnit tests for DespatchJobs and ReprintService. None of them may send a print job.
Cover these cases with tests:
- Describe returns every row of TestData/expected-jobs.tsv, for both classes.
- The returns note reports a 20 mm bottom margin.
- Print throws when the printer is missing from GetPrinterNames.
- That error message names each printer GetPrinterNames returns when the test runs.
- An installed printer name with its case changed is refused as well.
- Hundredths of an inch convert to the millimeters in expected-jobs.tsv.
Link config and TestData from the repo root into the test output, keeping their relative paths.
Leave expected-jobs.tsv as it is, then run dotnet test.
Tell me how many tests passed and how many failed.
拒绝测试使用未安装的打印机名称,因此在调用Printer.Print之前,它们在打印机检查处停止。
AI编码助理指南将一个IronPrint任务从一个空文件夹引导到与书面通过标准进行检查的结果。
在实际运行中这些提示生成了什么?
Cursor按顺序接收了针对发货示例存储库的七个提示。 运行后,每次改变代码的提交的新版本构建重复了下面的每个报告和命令。
- 在迁移到IronPrint之前和之后,工作报告匹配所有3行签署报告。
- 在修复之前再打印报告匹配0/3行,修复后匹配3/3。
- 打印机命令列出了四台安装的打印机及其托盘,并标记了两台配置的打印机为未安装。
- Cursor的五个文档回答中每个都引用了一页,没有文件发生变化。
- 对于一个GetPrinterNames不返回的名称,Cursor重复了指南的陈述并指出项目规则描述了不同的结果。
- 计划没有找到文档名称的PrintSettings替代,并记录为假设。
- 迁移后,应用仅引用IronPrint软件包,未保留System.Drawing.Printing代码。
- DespatchJobs保留了其公共Describe和Print签名。
- 打印命令拒绝了所有三个文档类型的配置打印机,并以退出代码2和四台安装的打印机的列表退出。
- 打印机检查还拒绝了以小写键入或带尾随空格的已安装Adobe PDF打印机。
- 在其修复之前,重新打印命令将作业发送到默认打印机用于缺少的配置打印机和"adobe pdf"。
- 修复后,重新打印命令拒绝了两个名称。 正如提示06所请求的,未设置退出代码时,它停止时出现未处理的异常。
- 所有9个xUnit测试通过。 它们来自五个测试方法,并且没有发作。
- 这些声明测试数据和项目规则未发生更改。
- 每个Release 构建以0警告和0错误结束,没有文件被手动编辑。
- 有些步骤与页面中的运行不一样。 每个提示都由一个本地桌面聊天手动发送。 提示04首先被粘贴而未发送,并在运行的日志脚本报告缺少时被发送。 该计划通过在代理中发送提示04获批,从未点击Build。 Cursor的迁移已经根据其计划检查了打印机名称,因此提示05更改了现有检查。
GitHub Copilot指南也将路径特定的指令文件保留在存储库中,并且Google Antigravity指南在真实的浏览器中测试打印页面。 运行记录列出了每个指南的工具版本,型号,持续时间和测试机器。
故障排除
- 如何获取打印机名称用于发送到默认打印机而不是指定的一个工作
- 将边距从百分之一英寸转换时如何设置纸张边距
- C#打印设置每个设置的默认值
- 使用许可证密钥当生成的打印代码因授权异常失败时
有问题吗?
如果您有任何问题,请联系support@ironsoftware.com
Curtis Chau 拥有卡尔顿大学的计算机科学学士学位,专注于前端开发,精通 Node.js、TypeScript、JavaScript 和 React。他热衷于打造直观且美观的用户界面,喜欢使用现代框架并创建结构良好、视觉吸引力强的手册。