Aspose Word改变图片大小为绝对大小
时间: 2024-04-16 20:25:35 浏览: 45
要在 Aspose.Words 中将图片大小更改为绝对大小,可以使用 `Shape` 对象的 `Width` 和 `Height` 属性。以下是一个示例代码,演示如何将图片大小更改为绝对大小:
```csharp
using Aspose.Words;
using Aspose.Words.Drawing;
// 加载文档
Document doc = new Document("YourDocumentPath.docx");
// 获取第一个段落中的第一个图片
Shape shape = (Shape)doc.GetChild(NodeType.Shape, 0, true);
// 设置图片的绝对高度和宽度(单位:磅)
double targetHeight = 200; // 单位:磅
double targetWidth = 300; // 单位:磅
// 将图片的高度和宽度设置为绝对值
shape.Height = ConvertUtil.PixelToPoint(targetHeight);
shape.Width = ConvertUtil.PixelToPoint(targetWidth);
// 保存文档
doc.Save("OutputDocumentPath.docx");
```
请将 "YourDocumentPath.docx" 替换为您要操作的 Word 文档路径。在示例代码中,我们假设文档中第一个段落中有一个图片,并获取该图片的 `Shape` 对象。然后,我们将目标高度和宽度转换为磅(使用 `ConvertUtil.PixelToPoint` 方法),并将其分配给图片的 `Height` 和 `Width` 属性。
请注意,这里使用的是绝对单位(磅),而不是相对单位(百分比)。确保选择适当的高度和宽度值,以确保图片以所需的绝对大小显示。
相关推荐
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)