C#如何使用Spire.Doc插入图片到word
时间: 2024-03-27 17:35:52 浏览: 188
使用 Spire.Doc 插入图片到 Word 文档可以按照以下步骤进行:
1. 创建一个 Spire.Doc.Document 对象,用于操作 Word 文档。
```csharp
Document document = new Document();
```
2. 创建一个 Spire.Doc.Section 对象,用于操作 Word 文档的节。
```csharp
Section section = document.AddSection();
```
3. 创建一个 Spire.Doc.Paragraph 对象,用于操作 Word 文档的段落。
```csharp
Paragraph paragraph = section.AddParagraph();
```
4. 创建一个 Spire.Doc.DocPicture 对象,用于存储要插入的图片。
```csharp
DocPicture picture = new DocPicture(document);
```
5. 加载要插入的图片,并将其赋值给 DocPicture 对象。
```csharp
picture.LoadImage(Image.FromFile("image.png"));
```
6. 将 DocPicture 对象插入到 Paragraph 对象中。
```csharp
paragraph.AppendPicture(picture);
```
7. 最后保存 Word 文档。
```csharp
document.SaveToFile("output.docx", FileFormat.Docx);
```
完整的代码如下:
```csharp
Document document = new Document();
Section section = document.AddSection();
Paragraph paragraph = section.AddParagraph();
DocPicture picture = new DocPicture(document);
picture.LoadImage(Image.FromFile("image.png"));
paragraph.AppendPicture(picture);
document.SaveToFile("output.docx", FileFormat.Docx);
```
注意,Spire.Doc 插入图片支持多种格式的图片,如 BMP、JPG、PNG、GIF 等。
阅读全文
相关推荐
















