IronWord 操作指南 添加图像 How to Add Image to DOCX Ahmad Sohail 已更新:十一月 12, 2025 Download IronWord NuGet 下载 Start Free Trial Copy for LLMs Copy for LLMs Copy page as Markdown for LLMs Open in ChatGPT Ask ChatGPT about this page Open in Gemini Ask Gemini about this page Open in Grok Ask Grok about this page Open in Perplexity Ask Perplexity about this page Share Share on Facebook Share on X (Twitter) Share on LinkedIn Copy URL Email article This article was translated from English: Does it need improvement? Translated View the article in English Images can be used to add visual context to an existing word document. To describe a sentence, a paragraph, or just a plain block of text. Adding images to a Word document (.docx) are made simple and straightforward with IronWord (A C# library for working with DOCX files). This tutorial will cover the essentials of adding images to a .docx file within a .NET environment. Try IronWord 今天在您的项目中使用 IronWord,免费试用。 第一步: 免费开始 How to Add an Image to DOCX Download the latest stable version of IronWord Initialize a new Word document Define an image object (.bmp, .jpg, .png, or other supported formats) Add the image to the document Save and export the document file Adding an Image An image is referred programatically using its defined path in the local system. Firstly, instantiate the ImageContent class by providing the file path in string. Then, the image variable can be used all throughout the file in various locations or just to modify its properties, e.g. width, height. Finally, add the image to the .docx file using the AddImage() function. Once added, the document can be exported and saved locally. In the example below, we are adding the image inside the document without any parent node. The supported file formats are: .jpg, .png, .bmp, .tiff, .gif. 提示An image can be inserted as a child element within a paragraph for better document hierarchy. The paragraph can also define text wrapping and other text formatting properties. :path=/static-assets/word/content-code-examples/how-to/add-image-insert-image.cs using IronWord; using IronWord.Models; using IronWord.Models.Enums; // initializing docx file WordDocument doc = new IronWord.WordDocument(); // instantiating image file IronWord.Models.ImageContent image = new IronWord.Models.ImageContent("sample-image.jpg"); // modifying image properties image.Width = 200; image.Height = 200; // AddImage function saving the image doc.AddImage(image); // Save and export the file doc.SaveAs("inserted-image.docx"); IRON VB CONVERTER ERROR developers@ironsoftware.com $vbLabelText $csharpLabel Adding Image via Stream Images which are served locally or via a static URL are quite easy to directly add into a .docx file using the previous method. To add images which are behind a secure API, requiring authentication, the Stream method would be appropriate. In the example below, an HTTP client sends authorization tokens to the server, retrieves the authenticated image stream upon verification, and integrates it directly before exporting the final .docx file. :path=/static-assets/word/content-code-examples/how-to/add-image-insert-image-via-http-stream.cs using IronWord; using IronWord.Models; using IronWord.Models.Enums; // initializing docx file WordDocument doc = new IronWord.WordDocument(); using (HttpClient client = new HttpClient()) { // Add authentication headers client.DefaultRequestHeaders.Add("Authorization", "Bearer YOUR_API_KEY_HERE"); client.DefaultRequestHeaders.Add("User-Agent", "MyApp/1.0"); // Get image from authenticated endpoint Stream authenticatedStream = client.GetStreamAsync("https://api.example.com/secure/image.png"); doc.AddImage(authenticatedStream); } // Export docx doc.SaveAs("added-image-via-http-stream.docx"); IRON VB CONVERTER ERROR developers@ironsoftware.com $vbLabelText $csharpLabel Modifying image properties IronWord provides default customization methods for an image. Settings Description Example Width Horizontal dimension of the image in pixels image.Width = 500; Height Vertical dimension of the image in pixels image.Height = 300; WrapText Text wrapping behavior around the image image.WrapText = WrapText.Square; DistanceFromLeft Spacing measurement from left edge in pixels image.DistanceFromLeft = 10; DistanceFromRight Spacing measurement from right edge in pixels image.DistanceFromRight = 10; DistanceFromTop Spacing measurement from top edge in pixels image.DistanceFromTop = 15; DistanceFromBottom Spacing measurement from bottom edge in pixels image.DistanceFromBottom = 15; Position Spatial placement information (X and Y coordinates) image.Position = new ElementPosition(50, 100); Scale Proportional sizing factors for X and Y axes image.Scale = new PointF(1.5f, 1.5f); Translate Displacement coordinates for repositioning image.Translate = new PointF(20, 30); Customizing Width & Height Custom width and height can be implemented altering predefined aspect ratio. An example is shown below. :path=/static-assets/word/content-code-examples/how-to/add-image-custom-size.cs using IronWord; using IronWord.Models; using IronWord.Models.Enums; // initializing docx file WordDocument doc = new IronWord.WordDocument(); // instantiating image file IronWord.Models.ImageContent image = new IronWord.Models.ImageContent("sample-image.tiff"); // modifying the aspect ratio by introducing custom width image.Width = 800; image.Height = 200; // AddImage function saving the image doc.AddImage(image); // Save and export the file doc.SaveAs("custom-size-image.docx"); IRON VB CONVERTER ERROR developers@ironsoftware.com $vbLabelText $csharpLabel 常见问题解答 如何使用 C# 将图像添加到 DOCX 文件? 您可以通过使用 IronWord 以编程方式在 C# 中将图像添加到 DOCX 文件中。它允许您轻松操作 Word 文档,包括插入图像。 使用 IronWord 可以添加哪些类型的图像到 DOCX? IronWord 支持各种图像格式,包括 JPEG、PNG、BMP 和 GIF,可以添加到 DOCX 文件中。 是否可以使用 IronWord 在 Word 文档中定位图像? 是的,IronWord 允许对 Word 文档中的图像定位进行精确控制,使您能够指定位置和对齐方式。 我可以使用 IronWord 向单个 Word 文档添加多个图像吗? 当然可以。IronWord 支持向单个 Word 文档添加多个图像,允许您设计复杂的文档布局。 IronWord 是否支持向页眉和页脚添加图像? 是的,IronWord 可以用于向 Word 文档的页眉和页脚添加图像,提升文档设计。 IronWord 如何处理图像缩放和调整大小? IronWord 提供功能可以在将图像添加到 Word 文档时缩放和调整其大小,确保它们在布局中无缝契合。 我可以使用 IronWord 在 DOCX 文件中为图像添加标题吗? 是的,IronWord 允许您为图像添加标题,增强 Word 文档中的清晰度和上下文。 是否可以使用 IronWord 替换 Word 文档中的现有图像? IronWord 使您能够替换 Word 文档中的现有图像,提供了文档更新和编辑的灵活性。 使用 IronWord 向 DOCX 文件添加图像的系统要求是什么? IronWord 需要 .NET 框架支持,并且与 Windows、macOS 和 Linux 环境兼容以添加图像到 DOCX 文件。 我可以使用 IronWord 自动化向 DOCX 文件添加图像的过程吗? 是的,IronWord 便利了 DOCX 文件中图像添加过程的自动化,简化文档创建和编辑任务。 Ahmad Sohail 立即与工程团队聊天 全栈开发者 Ahmad 是一名全栈开发人员,拥有扎实的 C#、Python 和 Web 技术基础。他对构建可扩展的软件解决方案深感兴趣,并喜欢探索设计和功能在实际应用中如何结合。在加入 Iron Software 团队之前,Ahmad 致力于自动化项目和 API 集成,专注于提高性能和开发人员体验。在业余时间,他喜欢尝试 UI/UX 想法,贡献开源工具,并偶尔从事技术写作和文档工作,以便让复杂主题更易于理解。 准备开始了吗? Nuget 下载 25,807 | 版本: 2025.11 刚刚发布 免费 NuGet 下载 总下载量:25,807 查看许可证