使用IronPrint與AI程式設計助手
IronPrint是Iron Software針對.NET的打印程式庫。 它可以在Windows、macOS、iOS和Android上從C#程式碼將PDF文件和圖片發送到打印機,無論是靜默打印或是通過系統打印對話框。
打印是許多文件功能的最後一步,也是難以測試的一步,因為其輸出會離開機器。沒有IronPrint的文檔,助手無法知道哪些成員存在。在第一次請求之前請提供文檔。
每位助手需要同樣的四個步驟。 添加套件、從環境中讀取密鑰、給助手提供IronPrint的機器可讀文檔,並檢查每個工作內容後才關閉任務。 相同的步驟涵蓋靜默打印、按名稱選擇打印機、打印設置和對話框打印。
如何使用AI程式設計助手進行IronPrint
- 從NuGet下載C#打印庫
- 依據操作系統特定的框架,例如
net9.0-windows - 在第一個工作之前分配
IronPrint.License.LicenseKey從IronPrint_LICENSE_KEY - 在編寫任何代碼之前,給助手提供IronPrint的llms.txt索引
- 將IronPrint規則保存在您的助手每個會話加載的文件中
- 檢查每個打印機名稱以比對
Printer.GetPrinterNames,並確認每個工作發送了什麼
我的AI助手無法自己處理打印?
靜默打印指南將System.Drawing.Printing路由描述為每種文件類型的頁面事件處理器的PrintDocument,並且沒有內建的PDF渲染。
IronPrint的API圍繞兩個類Printer和PrintSettings,步驟3列出了描述它們的機器可讀文檔。
在助手撰寫第一個打印調用前,需要考慮三點。
- 即席打印代碼成本令牌: 每次嘗試從原始
System.Drawing.Printing調用中重建丟失的設置都會花費令牌,並且該代碼會在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,帶有Production License Required消息。 Print拋出相同的異常,包裹在System.AggregateException中。 使用無效密鑰,消息會改為License Key Not Recognized。 因此未經授權的程序根本不會產生輸出,不論是有水印還是其他。下面的表格發行了一個免費試用密鑰。
立即在您的項目中開始使用 IronPrint,免費試用。
應用程式啟動時在第一次IronPrint調用之前設定一次密鑰。 密鑰來自一個環境變量,所以不包含在提交中。
IronPrint.License.LicenseKey = Environment.GetEnvironmentVariable("IRONPRINT_LICENSE_KEY");
一旦分配了有效的密鑰,IronPrint.License.IsLicensed返回true。 IronPrint.License.IsValidLicense測試任何密鑰字串。 程式庫不會自動讀取IRONPRINT_LICENSE_KEY,並且在本文的探測中,設置變數而不附加令其IsLicensed為假。
步驟3:將助理指向IronPrint的LLM文檔
- llms.txt索引了43個頁面,每行一條。 在每次新會話中包含它。
- llms-full.txt在一個文件中包含33個文檔頁面的摘要。
- llms-full-catalog.txt是索引,其中包括博客文章。
- object reference提供所有成員的簽名。
- 讀取技能文件的工具可以載入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設置為A4、自動方向、灰階和兩頁打印invoice.pdf。
命名IronPrint版本及代碼的目標框架。
較佳 在net9.0-windows上使用IronPrint 2026.10.1,並在第一個工作之前從IronPrint_LICENSE_KEY分配密鑰。
請求整個文件 一個片段可以省略using IronPrint;指令和分配密鑰的行。
較佳 返回Program.cs中的所有代碼,裡面有每個using指令和授權分配,以便編譯無需修改。
步驟 7:驗證您收到的內容
2026.10.1的探測測量了以下每個錯誤后的行為。
- 未檢查的打印機名稱: 一個不匹配已安裝打印機的名稱不會導致錯誤。 在我們的測試中,
PrintAsync不引發異常,工作轉向默認打印機Microsoft Print to PDF,直至有人回應其另存為對話框。 僅接受Printer.GetPrinterNames返回的名稱。 Print在UI執行緒上: 同步調用會阻塞調用的執行緒,導致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頁面在150 DPI下成為1240x1753像素圖像,在我們的探測中則成為300 DPI下的2479x3508像素圖像。 PaperSize.A4生產210x297毫米頁,PaperSize.Letter生產215.9x279.4毫米頁。- 使用
PaperOrientation.Automatic,橫向頁打印為橫向頁。 使用Portrait,相同的頁面縮小以適應縱向紙張,僅填滿其上部。 - 當
Grayscale開啟時,彩色PDF頁以灰階圖像到達。 PNG中的紅色文字保持紅色,只有打印票要求其單色輸出。 NumberOfCopies設置票券的副本數,但工作仍保存一組頁面。
打印設置指南說明打印機可能會限制DPI,某些平台忽略副本數,因此兩者都是发送給驅動程序的請求。
IronPrint的文件格式
在2026.10.1版本,PDF、PNG、JPG、BMP和TIFF文件可以打印。 GIF和HTML文件在創建工作之前會引發System.IO.InvalidDataException,顯示Unsupported file type消息,雖然靜默打印指南列出了這兩種格式。 技能文件列出了可用的五種格式。 首先將HTML渲染為PDF,例如使用IronPDF。
圖像打印為單頁,按頁寬縮放。 一張1200x800像素的PNG在Automatic方向的橫向A4頁打印,並在縱向頁的上部打印與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成為210x297毫米頁面,每個源頁面一個。
- 橫向PDF和PNG成為297x210毫米的橫向頁面。
- 每張票明確A4紙,一份,單色輸出和150x150 DPI。
- GIF被記錄為失敗,由於不支持文件類型錯誤。
- 使用No Such Printer 12345的運行在任何工作之前停下,並留捕獲文件夾不變。
- 對XPS文件的獨立閱讀與運行記錄中的每個數字一致。
- 接受測試構建的新起點產生相同的五個捕獲。
- 關鍵掃描在專案資料夾中未找到授權密鑰的副本。
- 安裝提示保持2026.10.1釘住。
- 文檔提示未改變代碼。
- 它的回答報告顯示靜默打印指南與技能文件對GIF和HTML存在分歧。
- 新的CLAUDE.md接收到設置提示的規則和測量行為。
- 自檢確認所有五項捕獲的工作設置都合乎要求。
- 一些步驟與頁面上運行的不同。 接受提示首先運行,然後在同一會話中運行步驟1、3、5和7的提示。 應用程式不考慮大小寫匹配PRINTER_NAME,並從打印機列表中精確通過名稱。
什麼過關
- 發行構建成功,不需手動編輯代碼。
- 當
IsLicensed為假或PRINTER_NAME不在Printer.GetPrinterNames中時,應用程式在任何工作之前停下。 - 每個可打印文件都有與runrecord.md匹配的XPS捕獲,其中頁面、頁面大小和票據值匹配您自己讀取XPS文件時。
- 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,計畫提示3包括Claude Opus 5 | 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 (High) | 61.9 |
代理特定指南
每個工具指南均通過這些步驟構建一個打印任務。
- 在Claude Code中使用IronPrint來對每種實驗室文件類型路由到指定的打印機並拒絕機器上不存在的名稱
- 在ChatGPT Codex中使用IronPrint用於每晚倉庫工作,打印挑選清單,按每個訂單計算副本數。
- 在GitHub Copilot中使用IronPrint用於打印填寫的PDF交接表,並在打印前展平特征。
- 在Cursor中使用IronPrint用於將裝箱單和標籤打印從
System.Drawing.Printing遷移到IronPrint打印設置。 - 在Google Antigravity中使用IronPrint用於前臺網頁應用程序,從控制器打印註冊卡。
疑難排解
- 失去反應的WPF和WinForms中的Print()方法 用於同步打印期間停止響應的界面
- 在不支持目標框架上顯示打印對話行為 用於通話對話框在普通目標框架上
- 按頁數的方向丟失在打印混合PDF時 當包含縱向和橫向頁的PDF以單一方向打印時
問題?
如果您有任何疑問,請聯繫support@ironsoftware.com
Curtis Chau 擁有卡爾頓大學計算機科學學士學位,專注於前端開發,擅長於 Node.js、TypeScript、JavaScript 和 React。Curtis 熱衷於創建直觀且美觀的用戶界面,喜歡使用現代框架並打造結構良好、視覺吸引人的手冊。