如何将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.
成功的构建未能回答计划必须涵盖的三个问题。 微软的渲染模式指南解释了页面何时是交互式的,而Blazor文件上传指南涵盖了上传处理和尺寸限制。
为代理提供变量名,但不要分享其值。 IronWord还读取appsettings.json中的IronWord.LicenseKey条目,该文件可被错误地包含在提交中。 许可证密钥指南列出了IronWord检查密钥的所有位置。
最小工作流程(5 个步骤)
- 从NuGet下载C# Word库
- 从已经在localhost上提供页面的Blazor Web App开始
- 使审核页面交互式并将上传限制提高到最大报告之上
- 让/browser上传每个测试报告并截取它显示的照片
- 与已知清单对比计数、标题和像素大小,然后确认每个成员在API参考中
什么是Google Antigravity,它与聊天助手有何不同?
谷歌的概述描述了Antigravity应用程序作为一个启动、编排和检查代理团队的桌面应用程序。 代理运行命令,读取和写入文件并控制沙盒Chrome浏览器。 聊天助手以文本回复,而代理返回工件,例如实现计划。
在slopsquatting攻击中,有人注册了一个AI代理生成的从未发布库的包名,请求该名称的代理安装攻击者的代码。 在涉及包的每个任务中命名IronWord包。 集线器AI编码助手指南列出IronWord的代理文档文件并包含一个单提示验收测试。
您应该在计划、代码更改和演练中检查什么?
除了Quickstart提示请求的三个答案之外,实现计划还应说明没有照片的报告生成的消息。 在代码更改中,找到计算像素大小的行,并对照API参考检查每个IronWord成员。
每个报告需要在操作指南中拥有自己的截图,位于结果表旁边。 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,一个在表格单元格中存储照片。 另一个在页眉中包含一个徽标,一个重复放置相同的图像,一个不含照片,一个是7.7 MB的报告,内含四张12 MP的照片。
是什么导致照片丢失或错误?
在我们的测试中,每个失败的行都来自三种原因之一。 为了识别它们,将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));
}
}
大小检查使用浏览器报告的值,因此超大小文件在上传流打开之前会失败。 微软的上传指南建议不要仅依靠此客户端提供的值,而在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个中的12个正确。
- 应用程序和测试项目的发行版本报告了0个警告和0个错误。
- 所有七个xUnit测试通过。
- linux-x64发布包括一个本地库。
- 三个浏览器通过中每个都通过文件选择器处理了所有六个报告。
- 有些步骤与页面中的运行不一样。 Quickstart提示已经设置了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示例
- 当项目也引用OpenXML SDK时与DocumentFormat.OpenXml一起使用IronWord
有问题吗?
如果您有任何问题,请联系support@ironsoftware.com
Curtis Chau 拥有卡尔顿大学的计算机科学学士学位,专注于前端开发,精通 Node.js、TypeScript、JavaScript 和 React。他热衷于打造直观且美观的用户界面,喜欢使用现代框架并创建结构良好、视觉吸引力强的手册。