本教程指导如何使用 C# 在 Word 中调整图片大小。它提供了有关 IDE 设置的详细信息、编写此应用程序的分步过程以及可运行的示例代码以使用 C# 在 ms Word 中调整图像大小并将其保存在DOCX中的磁盘上,DOC,或任何其他 MS Word 支持的格式。您还将了解用于编写此应用程序的类、方法和属性。
使用 C# 在 Word 中调整图像大小的步骤
- 建立环境在应用程序中添加Aspose.Words以调整图像大小
- 使用 Document 类对象创建一个新的 Word 文件以添加调整大小的照片
- 为新创建的文档实例化 DocumentBuilder 类对象
- 使用 Write() 和 InsertImage() 方法以原始大小插入示例文本和图像
- 获取对特定图像的引用并通过以磅为单位设置宽度和高度来更改其大小
- 使用调整大小的图像将生成的 Word 文件保存在磁盘上
这些步骤描述了通过共享配置信息使用 C#* 在 Word 中*调整照片大小的过程,然后是逐步完成任务的方法。首先,您需要创建一个空的 Word 文档,然后实例化 DocumentBuilder 类对象以插入不同的元素,如文本、图像等。 InsertImage() 方法返回对新插入图像的引用,以设置其宽度和高度等属性点。
使用 C# 在 Word 中更改图片大小的代码
using Aspose.Words; | |
using Aspose.Words.Drawing; | |
namespace AsposeProjects | |
{ | |
class Program | |
{ | |
static void Main(string[] args) // Main function to resize image in Word using C# | |
{ | |
// Initialize license | |
License lic = new License(); | |
lic.SetLicense("Aspose.Total.lic"); | |
// Create a new Word document | |
Document doc = new Document(); | |
// Create New Document. | |
DocumentBuilder docBuilder = new DocumentBuilder(doc); | |
// Insert the image title before resizing | |
docBuilder.Write("Image Before Resize"); | |
// Insert an image into the loaded word file | |
Shape image = docBuilder.InsertImage("ImageToResize.jpg"); | |
// Write the next text in the document for the resized image | |
docBuilder.Write("ReSize image "); | |
// Insert another instance of the image and get its reference | |
image = docBuilder.InsertImage("ImageToResize.jpg"); | |
// Change image size | |
image.Width = ConvertUtil.InchToPoint(0.75); | |
image.Height = ConvertUtil.InchToPoint(0.75); | |
// Save the document with the original and resized image | |
docBuilder.Document.Save("FileWithImages.docx"); | |
System.Console.WriteLine("Image resized successfully"); | |
} | |
} | |
} |
此代码演示如何使用 C# 在 Word 文档中调整图像大小。它使用 DocumentBuilder.InsertImage() 方法插入一个图像,该图像采用图像文件名并返回对 Shape 类对象中图像的引用。 Shape 类对象用于通过提供所需的英寸来设置宽度和高度,这些英寸使用实用程序类 ConvertUtil 方法 InchToPoint() 转换为点。
本文教我们如何使用 C# 在 Word 中调整图片大小。如果您想学习其他过程,例如将 Word 文件拆分为多个文件,请参阅 如何使用C#拆分Word文件 上的文章。