如何將IronWord與Google Antigravity結合使用
現場工程師以Word文件的形式提交檢查報告,嵌入的照片為證據。 即使沒有錯誤地構建了審查頁面,放置圖片錯誤、表格內圖片遺漏、標題錯誤或圖片大小不準確,審查還是會失敗。 構建日誌不記錄這些失敗。
Google Antigravity可以像審查員一樣測試頁面。 透過/browser指令啟動的沙盒瀏覽器子代理打開頁面,在文件控件中選取每個報告並對結果截圖。 在伺服器上,IronWord打開上傳的DOCX文件,通過ExtractImages返回其照片,無需安裝Microsoft Office。 如下的審查頁面由兩個工具構建,並對六個已知照片數、標題和像素大小的報告進行了測試。
此指南適合那些讓AI代理撰寫讀取Word文件的.NET代碼的開發者。 透過AI模型產生報告本身不在範疇之內。
快速開始當Antigravity打開Blazor專案時,在/plan提示中描述審查頁。
/plan Add a report review page at /photos to this Blazor Web App on .NET 9.
The app uses the Interactive Server render mode.
Here's what the page needs:
- A reviewer picks a site inspection report (.docx) with InputFile.
- The server opens it with IronWord and calls ExtractImages.
- Add the IronWord package from NuGet, pinned to 2026.10.4. Its namespace is IronWord.
- Each photo appears as a thumbnail with its width and height in pixels and its image format.
- The page also shows how many photos the report holds.
- Link the page from NavMenu.
Read IRONWORD_LICENSE_KEY from the environment once at startup into IronWord.License.LicenseKey.
The app reads that variable itself.
Only check that it's set and how long it is.
Never print, log or store its value.
Don't list environment variables either.
Answer these in the Implementation Plan:
- which render mode the page gets
- how the upload stream reaches IronWord
- the size limit passed to OpenReadStream
You're done when dotnet build reports no errors.
成功編譯不會解答計劃必須涵蓋的三個問題中的任何一個。 Microsoft的渲染模式指南說明何時頁面是互動的,而Blazor檔案上傳指南涵蓋了上傳處理及大小限制。
提供變量名稱給代理,但不要分享其值。 IronWord還會在appsettings.json中讀取IronWord.LicenseKey項目,這是一個提交中可能會包含的文件。 授權金鑰指南列出IronWord檢查金鑰的所有位置。
最小工作流程(5 個步驟)
- 從NuGet下載C# Word程式庫
- 從已經在localhost提供頁面的Blazor Web應用開始
- 使審查頁面具有互動性,並提高上傳限制至超過您最大的報告。
- 讓/browser上傳每個測試報告,並對顯示出的照片截圖
- 與已知列表比較數量、標題和像素大小,然後確認API參考中的每個成員
Google Antigravity是什麼,它與聊天助手有何不同?
Google的概述將Antigravity應用程式描述為一款啟動、協調和檢查代理團隊的桌面應用。 一個代理運行命令,讀取和寫入文件,並控制一個沙盒Chrome瀏覽器。 聊天助手以文本回應,而代理則返回工件,例如實施計劃。
slopsquatting攻擊中,有人為從未發布的程式庫註冊了一個AI代理生成的名稱,而請求該名稱的代理會安裝攻擊者的代碼。 在涉及包的每個任務中命名IronWord包。 中心的AI編碼助手指南列出IronWord的代理文檔文件,並包含一個一提示驗收測試。
在計劃、代碼變更和流程演示中,您應該檢查些什麼?
除了快速入門提示要求的三個答案之外,實施計劃應說明無照片報告的消息。 在代碼變更中,找到計算像素大小的行,並檢查每個IronWord成員對照API參考。
每個報告在演練中需要自己的截圖,旁邊是結果表。 Antigravity保存截圖為圖像工件,讀者可以對任何一個進行評論。 評論顯示錯誤照片下方標題的截圖指示代理需要修正的圖像。
如何在真實瀏覽器中測試IronWord審查頁面?
提供/browser一個結果表,其單元格只有透過上傳文件來填寫。
/browser Start the site with dotnet run and open /photos.
Its http profile listens on http://localhost:5036.
Take each .docx file in TestData/reports through the page's file picker, one at a time:
1. Pick the file.
2. Wait for the photo list or an error message.
3. Take a screenshot.
TestData/reports/expected.csv lists each report's site photos in document order.
It gives their number, captions and pixel sizes, with | between entries.
Build a table in the Walkthrough with these columns:
- file
- bytes
- photos expected
- thumbnails shown
- pixel sizes expected
- pixel sizes shown
- pass or fail
For a failed row, quote the server log entry or browser console error behind it.
Do that before you change any code.
If neither recorded one, write that down.
After a fix, restart the app and put all six reports through the page again.
Stop the app when you finish.
六份報告涵蓋照片頁面必須處理的佈局。 一份包含PNG、JPEG和GIF,一份將照片儲存在表格單元格中。 另一份在頁眉中包含一個標誌,一份將同一圖像放置兩次,一份沒有照片,一份是含四張12 MP照片的7.7 MB報告。
照片遺漏或錯誤的原因是什麼?
在我們的測試中,每個失敗的行都是由三個原因之一引起的。 為了識別它們,從IronWord的提取圖像指南中複製提取代碼到新.NET 9 Blazor專案的上傳處理程序中,六個報告均在無頭Chrome中處理。
| 頁面顯示的內容 | 原因 | 修復 |
|---|---|---|
| 任一報告均無輸出 | 靜態渲染 | @rendermode InteractiveServer |
| 無輸出,伺服器日誌保持空白 | 文件構造函數同步讀取上傳流 | LoadAsync於上傳流 |
| 只有全分辨率報告有錯誤 | 512,000字節的預設上傳限制 | 超過最大的報告限制 |
第二行是最難診斷的。 構造函數拋出NotSupportedException,消息為"不支持同步讀取。"例外只有在處理器捕獲並記錄時才可見。
如何設置一個Antigravity可以工作的專案?
給Antigravity一個已經構建並提供頁面的倉庫。 測試專案來自dotnet new blazor -int Server -f net9.0,在任何提示運行前構建時沒有錯誤。 範本將隨機端口寫入Properties/launchSettings.json的http配置檔案中,所以將該URL複製到/browser提示中。
下一個任務審計包和授權金鑰。
Which IronWord version does this project reference? Quote its PackageReference line.
Then point me to the line that applies the license key.
Check the project against these key rules:
- The key reaches the app only through IRONWORD_LICENSE_KEY at startup.
- Neither appsettings.json nor appsettings.Development.json has an IronWord.LicenseKey entry.
- Properties/launchSettings.json doesn't carry the key.
- A missing variable at startup gets one logged warning that names it and shows no value.
Edit Program.cs only if that warning doesn't exist yet.
Leave every other file as it is.
Tell me which rules pass and which fail.
在以後的任務中引用報告的版本。 IronWord 2026.10.4打開每個報告並提取每張照片,無需授權金鑰。 保存使用LicensingException讀出"需要生產許可"。 因此,當應用程式首次寫入文件時,缺少的金鑰才會顯現,而此閱讀頁面不會這樣做。
如何將頁面的規則應用於後續任務?
在所有六行順利通過後,記錄會話的決議。 Antigravity的規則文檔指出,每當代理讀取或編輯文件時,它將加載查找到的每個GEMINI.md或AGENTS.md,位於該文件與工作區根之間的每個資料夾中。 /learn將會話的反饋轉化為永久項目規則。
/learn Create AGENTS.md at the root of the repository and record what this session settled.
Put the notes under the heading "Inspection report photos". They should cover:
- the render mode the /photos page needs
- the upload limit, with the reason for its value
- how the upload reaches IronWord without a synchronous read
- where a photo's pixel size comes from
- that IRONWORD_LICENSE_KEY stays out of every file
- that no page change is done until all six reports in TestData/reports pass the /browser table
一旦文件存在,渲染模式和上傳限制將記錄在任務可讀取的位置中。 代理不會自動更新該文件。 在更改值的同一任務中要求更新。
如何使用IronWord載入已上傳的報告?
指定結果和證據,允許代理構建代碼。
/plan Write the Implementation Plan Artifact before you edit anything.
Move the IronWord code out of the page into Services/ReportPhotoService.cs.
Register it as a singleton.
The service works like this:
- It opens the upload with WordDocument.LoadAsync, straight from OpenReadStream with a 30 MB limit.
- It checks the file's Size first and turns away anything larger before it opens a stream.
- A refused file gets a clear message that the page shows.
- A second public method takes an already loaded WordDocument.
- Tests can call that method without a browser.
For each photo, the service returns:
- the pixel size
- the size the photo is placed at in the report, in inches
- the format
- a JPEG thumbnail no wider than 320 pixels
- the caption
A caption is the first paragraph with text after the photo's own paragraph.
For a photo inside a table cell, it's the text of the cell directly below.
Show each caption under its thumbnail.
Then repeat the /browser table with a captions column checked against expected.csv.
預期一個類似於這個例子的服務。 它使用IronWord 2026.10.4編譯,校準測試頁面正確列出所有六份報告的照片數量、標題和像素大小。
using IronSoftware.Drawing;
using IronWord;
using IronWord.Models;
using Microsoft.AspNetCore.Components.Forms;
public sealed record ReportPhoto(int PixelWidth, int PixelHeight, double WidthPoints, double HeightPoints, string Format, string Caption, string ThumbnailDataUrl);
public sealed class ReportPhotoService
{
public const long MaxUploadBytes = 30 * 1024 * 1024;
public async Task<List<ReportPhoto>> ReadAsync(IBrowserFile file)
{
if (file.Size > MaxUploadBytes)
throw new IOException($"{file.Name} is {file.Size:N0} bytes, over the {MaxUploadBytes:N0} byte limit.");
await using var upload = file.OpenReadStream(MaxUploadBytes);
var doc = await WordDocument.LoadAsync(upload);
return Read(doc);
}
public static List<ReportPhoto> Read(WordDocument doc)
{
var photos = new List<ReportPhoto>();
var body = doc.Paragraphs.Where(p => p.Parent is DocumentSection).ToList();
for (int i = 0; i < body.Count; i++)
{
foreach (var img in body[i].ExtractImages())
{
var caption = body.Skip(i + 1).Select(p => p.ExtractText().Trim()).FirstOrDefault(t => t.Length > 0) ?? "";
photos.Add(Make(img, caption));
}
}
foreach (var table in doc.Tables)
for (int r = 0; r < table.Rows.Count; r++)
for (int c = 0; c < table.Rows[r].Cells.Count; c++)
foreach (var img in ((TableCell)table.Rows[r].Cells[c]).ExtractImages())
{
var caption = r + 1 < table.Rows.Count && c < table.Rows[r + 1].Cells.Count
? ((TableCell)table.Rows[r + 1].Cells[c]).ExtractText().Trim() : "";
photos.Add(Make(img, caption));
}
return photos;
}
static ReportPhoto Make(ImageContent img, string caption)
{
var bmp = (AnyBitmap)img.Image;
int tw = Math.Min(320, bmp.Width);
int th = Math.Max(1, bmp.Height * tw / bmp.Width);
using var thumb = new AnyBitmap(bmp, tw, th);
var jpg = thumb.ExportBytes(AnyBitmap.ImageFormat.Jpeg, 80);
return new ReportPhoto(bmp.Width, bmp.Height, img.Width, img.Height, img.Type.ToString(), caption,
"data:image/jpeg;base64," + Convert.ToBase64String(jpg));
}
}
大小檢查使用瀏覽器報告的值,因此設置過大的文件在打開上傳流之前就會失敗。 Microsoft的上傳指南建議不要僅依賴於此由客戶端提供的值,並在OpenReadStream中設定的限制執行了實際的上限。讀取完整分辨率報告在兩次校準運行中花費1,118毫秒和1,280毫秒。 為避免大型數據傳輸,服務生成縮放的縮略圖,而不是將全尺寸的圖像發送給瀏覽器。
IronWord從文件中提取哪些圖像?
ExtractImages方法返回位於文件正文和表格單元格中的照片。 它略過頁眉中的標誌,因此信頭不算作網站照片。 若照片多次出現,則對每個位置返回一次,每個都有其大小。
每個結果包括其寬度和高度(以點為單位)。 例如,3英寸寬的照片報告為216點,不論分辨率如何。 樣本服務保留這些點的值。 要轉換為英寸,除以72,或如代理所示使用GetWidth(MeasurementUnit.Inch)。 像素大小從解碼的圖像獲得,可以通過將Image屬性轉型為AnyBitmap或者解碼在ImageData中的Base64字串。
段落列表包括表格單元內的段落,按閱讀順序排列,因此選擇下一個有文本的段落會將第一張照片的標題分配給第二張表格照片。 文件的ExtractText也省略了表格文本,因此其輸出不包含表格標題。
何時啟動更多代理?
將檢查分配給兩個代理。 一個在代碼中測試服務。 另一個重播審查員在瀏覽器中的上傳。
Agent A: add an xUnit project named InspectionPhotos.Tests.
Its tests take each report in TestData/reports through these steps:
1. Load the report from disk with WordDocument.
2. Pass it through the extraction code in ReportPhotoService.
3. Assert the photo count, pixel sizes and captions that expected.csv lists, in document order.
Add one more test proving a file above the upload limit is refused before IronWord opens it.
expected.csv quotes any field that holds a comma, so parse it as CSV.
The web project is at the repository root, so keep the test folder out of its compile items.
Run the tests with dotnet test InspectionPhotos.Tests.
Agent B: wait until Agent A's tests are green on the current code.
Then put the six reports through /photos with /browser.
Attach one screenshot per report to the Walkthrough.
Note there that the tests ran first.
從磁碟打開報告的測試不需要瀏覽器或正在運行的應用程式,因此在/browser通過之前會檢測到標題回歸。
如何將頁面部署到Linux容器?
除了其托管的程序集外,IronWord 2026.10.4還包含一個為Windows(x64和x86)、linux-x64和macOS構建的本機佈局庫。
This page will run in a linux-x64 container based on mcr.microsoft.com/dotnet/aspnet:9.0.
Leave the C# untouched.
Docker is missing on this machine, so don't run docker build.
Work through these steps:
1. Write a Dockerfile whose build stage publishes a linux-x64 build.
2. Have the image take IRONWORD_LICENSE_KEY from the container environment when it starts.
3. Run dotnet publish -c Release -r linux-x64 --self-contained false -o out/linux.
Then list every native library (.so) in out/linux with the NuGet package that supplied it.
If there's none, say so.
列出本機文件不需要Docker,因為Windows上的發佈文件夾包含影像接收到的相同文件。 為linux-x64發佈校準應用生成一個本機文件libLayoutEngineInterop.so(8,303,240字節)。 該文件僅鏈接至glibc,並且該包沒有musl或linux-arm64構建,因此此版本沒有用於Alpine或ARM64 Linux映像的本機庫。 在Windows上,載入和提取照片未載入本機庫。 IronWord的Docker指南建議使用Ubuntu、Debian或RHEL映像。
在實際運行中,這些提示產生了什麼結果?
一個Antigravity CLI會話運行所有七個提示無需出現權限提示。 應用僅從IRONWORD_LICENSE_KEY讀取授權金鑰。 我們在無頭Chrome中每個報告上傳了兩次,一次是第一次提示的代碼,一次是最終代碼。
- 在Chrome檢查中,所有六個報告的照片數和像素大小都正確。
- 從沒有標題的第一次提示(該提示要求沒有標題),到最終應用中的12 of 12正確。
- 應用和測試項目的釋出版本報告了0警告和0錯誤。
- 所有七個xUnit測試均通過。
- linux-x64發佈包括一個本機庫。
- 三次瀏覽器通行均通過文件選擇器處理了所有六份報告。
- 一些步驟與頁面上運行的不同。 快速入門提示已設置30 MB限制,並將每次上傳複製到內存中,因此上述三個原因未出現。 每個/plan提示在其計劃結束後停止,直到收到批准。 代理A和代理B在同一對話回合中運行。 縮略圖使用SixLabors.ImageSharp調整大小,這是IronWord的一個間接依賴。 代理提交了第一次瀏覽器通行的截圖和out/linux發布文件夾。
- /learn未撰寫AGENTS.md。 其工件列出所有六個點,並首先要求確認。
ChatGPT Codex指南在並發限制下並行處理整個文件夾的Word文檔,而該頁面每次處理一個上傳。Claude Code指南在文件變更前以計劃模式批准設計,就像這裡的計劃優先提示一樣。
此次運行的Antigravity CLI版本、模型、持續時間和測試機在中心的運行記錄中。
疑難排解
- 提取DOCX中的圖像用於此頁面所用的提取方法
- 從DOCX中提取文本當標題或表格文本遺失時
- IronWord授權金鑰以確認
License.IsLicensed已應用該金鑰 - 在Docker中設置IronWord用於Ubuntu、Debian和RHEL的Dockerfile示例
- IronWord與DocumentFormat.OpenXml當項目同時參考了OpenXML SDK時
問題?
如果您有任何疑問,請聯繫support@ironsoftware.com
Curtis Chau 擁有卡爾頓大學計算機科學學士學位,專注於前端開發,擅長於 Node.js、TypeScript、JavaScript 和 React。Curtis 熱衷於創建直觀且美觀的用戶界面,喜歡使用現代框架並打造結構良好、視覺吸引人的手冊。