如何在Claude Code中使用IronPrint

Curtis Chau
Curtis Chau
已更新: 2026年10月26日

一個醫院實驗室使用一個服務印出三種類型的文件。樣本標籤被送到採集處的標籤打印機,結果報告被送到辦公室打印機,快遞運單被送到配送室。 一個路由文件為每種文件類型命名打印機,並且文件中一個錯誤的字符就可能將患者結果發送到另一個房間而沒有錯誤提示。 Claude Code是Anthropic的編碼工具,可以編輯文件並在儲存庫中運行命令。 它可以用作命令行工具,作為VS Code和JetBrains編輯器的擴展,在Claude桌面應用程序中,以及在claude.ai/code上使用。

IronPrint可從.NET中打印PDF和圖像,無論是靜默模式還是通過操作系統對話框。 PrintSettings.PrinterName屬性選擇工作的打印機,Printer.GetPrinterNames列出機器上安裝的打印機。下面測試的四個提示讓Claude Code為該實驗室建立一個打印機路由器,提示之間的部分記錄了每個提示所依賴的IronPrint行為。

快速開始
NuGet使用NuGet安裝

PM > Install-Package IronPrint

在 NuGet 包管理器控制台中運行上述命令安裝 IronPrint,或在 NuGet 包管理器中搜索該包。

在專案資料夾中打開的終端中啟動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.
Text

輸出的引號使行尾空白可見。



Claude Code在.NET打印專案中做什麼?

代理編寫一個路由服務,它讀取路由文件,將每個配置的打印機名稱與安裝的打印機進行比較,並為每份文件記錄一個決定。 Claude Code還可以執行程式及其測試並修復它們報告的失敗。 開發者決定每種文件類型的打印機。

在第一個提示中陳述包名和版本。 在稱為slopsquatting的攻擊中,攻擊者註冊語言模型幻覺的名稱下的有害包。 由於代理自己選擇包名,因此可以安裝這些包之一。

您如何設置Claude Code?

安裝和首次登入遵循Anthropic的快速開始。 設置在claude在終端中打開一個會話並回答提示後完成。

如何設立一個Claude Code可以工作的項目?

在第一個提示之前將輸入放入儲存庫中。 本指南的示例專案包含一個用於net9.0-windows的.NET 9控制臺應用程序,四種文件類型的路由文件,以及一個包含七個PDF的收件箱,這些PDF的名稱以它們的文件類型開頭。 一條路由使用已安裝打印機的精確名稱。 其他三個則分別使用未安裝的打印機名稱、不同大小寫的已安裝名稱和後續有空格的已安裝名稱。

當會話開始時Claude Code會讀取CLAUDE.md,因此其規則適用於以後的每次會話。 一旦快速開始添加了包,第二個提示將寫入該文件並定義一個只讀的子代理來審查打印代碼。

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

Claude Code監視.claude/agents並加載新添加的子代理定義而無需重新啟動,但僅限於會話開始時該資料夾已存在。 因此,範例儲存庫在會話開啟之前持有文件夾,裡面有一個空的.gitkeep文件。 IronPrint的llms.txt索引其操作指南、代理技能及其他文檔。

在第一次打印電話前應用授權金鑰。 沒有金鑰,Printer.Print和Printer.PrintAsync將拋出異常,並且沒有工作被排隊。 IronPrint License Keys頁面提供了一個試用金鑰,並顯示了代碼分配和配置文件設置。

哪些IronPrint打印機規則屬於CLAUDE.md?

我們探測了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.
Markdown

兩個異常形狀影響錯誤處理。 處理程序檢查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像素乘以1754像素的單獨圖像,沒有文本層,以150 DPI渲染。 指南上的樣本輸出在默認的300 DPI下遵循相同的模式,每頁A4的像素為2479乘以3508。 因此,檢查PDF打印機的輸出必須比較頁面數或圖像,因為打印的文本無法搜索。

為了檢查工作而不打印它們,測試設置掛鉤了IronPrint在Windows上使用的WPF打印調用。 掛鉤將每個工作都以XPS格式寫入磁盤而不進行假排隊,並且保存的頁面圖像尺寸為1239 x 1754像素,與探測工作相同。 IronPrint將其他設置寫入工作的打印票證中。 開啟Grayscale時,票證請求單色輸出。 該頁面圖像以RGB格式存儲,僅包含灰色像素。 票證的副本數來自NumberOfCopies,工作中每頁都包含一次。

標籤路徑顯示了後備功能如何處理工作。 輸入是來自收件箱的樣本標籤__S-031.pdf,一個100 x 50 mm的標籤,路由文件將其發送到在300 DPI下使用PaperSize.PrinterDefault的LAB-ZEBRA-01。

輸出

標籤路徑的設置未經名稱檢查就發送了,因此工作回退到Microsoft Print to PDF。 然後,打印機的默認紙張尺寸解析為該打印機的A4紙張,並且標籤成為A4橫向頁面上的一個1179 x 591像素的圖像。 該工作將圖像縮放至297 x 149毫米,即紙張的整個寬度。

樣本標籤S-031如同IronPrint在建立工作時沒有進行名稱檢查。於是,建立的工作是A4橫向頁面, 300 DPI,指向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/.
Text

提示的兩個部分保護路由。

  1. 發送標誌:如果沒有--send,程式不會發送工作,因此代理可以在其打印機打開對話框的機器上運行和測試路由器。
  2. 審計子代理:自定義的子代理加載CLAUDE.md,因此打印機名稱規則也適用於其審核。 內建代理中的Explore和Plan在未加載CLAUDE.md的情況下啟動。
提示: 在核准計畫之前,確認給IronPrint的名稱是來自精確檢查的已安裝名稱。 一個忽略大小寫匹配名稱然後傳遞設定名稱的計畫會將快遞單發送至預設印表機。

能否在沒有授權金鑰的情況下測試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.
Text

在信任生成的IronPrint代碼之前應驗證什麼?

用以下檢查來審查生成的IronPrint代碼。

  • 構建:IronPrint沒有的成員將使dotnet build因編譯錯誤而停止。 發佈的IronPrint.xml還記錄了一個內部PdfiumUtils類的方法,調用它的代碼會因CS0122而失敗。
  • 路由報告:在第一個真正的工作之前,將每個決策與路由文件及已安裝的打印機清單進行比較。
  • 每個打印機一個測試頁:在發布前,確認每台裝置是否按照路由指示打印了測試頁。

AI編碼助手指南提供了一個完整的IronPrint任務,具有固定的通過標準,作為可重複的測試。

在實際運行中,這些提示產生了什麼結果?

四個提示在一個Claude Code會話中以顯示的順序運行。 在提示3之前,規則塊被粘貼進CLAUDE.md,並且沒有人手動編輯代碼。

  • Quickstart輸出命名了所有四個已安裝的打印機,並標記一個路由已安裝和三個缺失。
  • 路由報告為每個7份文件持有一行。
  • 2個文件被標記為發送,2個名稱不匹配,2個未安裝,1個無路由。
  • 每個決策都與我們針對已安裝打印機的獨立檢查相匹配。
  • Adobe PDF接近匹配信使清單,Microsoft Print to PDF接近病房摘要。
  • 清理釋放運行在新拷貝中產生了相同的報告。
  • 沒有金鑰,程式報告IsLicensed為False,並做出了相同的7個決策。
  • 在無IRONPRINT_LICENSE_KEY變數的過程中,10個測試中的10個通過。
  • 子代理未發現任何IronPrint成員在IronPrint.xml中缺失。
  • 子代理找到了一個打印機姓名指派,位於精確檢查之後。
  • 迴圈後列隊是空的,並且錄音中的命令沒有使用--send。
  • 兩個發送工作的捕獲存有3頁A4豎向頁面為Adobe PDF排隊中的每個1239 x 1754像素圖像。
  • 每個提示留下了沒有警告或錯誤的釋放構建。
  • 掃描了錄音、會話文件和儲存庫後,沒有找到授權金鑰的副本。
  • 一些步驟與頁面上運行的不同。 由於會話無頭運行,輸入的回覆批准了提示3計劃。 沒有打印工作,因為測試機器上的每台打印機都打開一個保存對話框,所以兩個發送工作都被捕獲到XPS。

路由報告中的第一行是結果報告__RPT-4471.pdf,來自收件箱的兩頁結果報告。

輸出

捕獲工具將此行從路由報告中的設置傳給IronPrint。 下面的PDF保存了未做更改的兩頁捕獲頁。

結果報告RPT-4471如同IronPrint從運行的路由報告中構建的發送任務。兩頁都是A4豎向150 DPI,適用於Adobe PDF排隊,並被捕獲到文件中代替打印機。

Cursor也有一個計劃模式,並且Cursor指南在其中發送了提示3。 Codex從AGENTS.md中讀取其專案規則,與CLAUDE.md的對應物,正如ChatGPT Codex指南所示。 此次運行的Claude Code版本、型號及總持續時間顯示在測試設備旁的運行記錄中。


疑難排解


問題?

如果您有任何疑問,請聯繫support@ironsoftware.com

Curtis Chau 擁有卡爾頓大學計算機科學學士學位,專注於前端開發,擅長於 Node.js、TypeScript、JavaScript 和 React。Curtis 熱衷於創建直觀且美觀的用戶界面,喜歡使用現代框架並打造結構良好、視覺吸引人的手冊。

閱讀更多

準備好開始了嗎?

NuGet 下載次數 52,106版本:2026.11剛剛發布