Aspose.Words for .NET 使您可以更轻松地将 DOC/DOCX 转换为所需的图像格式。您可以使用SaveFormat枚举将输出图像格式设置为 PNG、JPEG、BMP、GIF 或 TIFF 。以下是在 C# 中将 DOC/DOCX 转换为 PNG 图像的步骤。
基础使用
使用Document 类加载 DOC/DOCX 文档。
使用ImageSaveOptions类指定输出图像格式。
循环浏览文档中的页面。
使用Document.Save(string, ImageSaveOptions)方法将 DOC 中的每个页面转换为 PNG 。
Document doc = new Document("calibre.doc");
// set output image format using SaveFormat
var options = new ImageSaveOptions(SaveFormat.Png);
// loop through pages and convert them to PNG images
for (int pageNumber = 0; pageNumber < doc.PageCount; pageNumber++)
{
// Save page as PNG
options.PageSet = new PageSet(pageNumber);
doc.Save(pageNumber + "_page.png", options);
}
高级应用
您还可以使用不同的选项控制 DOC 到 PNG 的转换。例如,您可以设置水平分辨率、垂直分辨率、整体分辨率、比例、 像素 格式、亮度、颜色模式、对比度和纸张颜色。以下是在 C# 中自定义 DOC 到 PNG 转换的步骤。
使用Document类加载 DOC 文件。
使用ImageSaveOptions类指定输出图像格式。
设置所需的选项,例如ImageBrightness、ImageContrast等。
循环浏览文档中的页面。
使用Document.Save(string, ImageSaveOptions)方法将每个页面转换为 PNG 。
// load document
Document doc = new Document("calibre.doc");
// set output image format using SaveFormat
var options = new ImageSaveOptions(SaveFormat.Png);
// change the image's brightness and contrast
// both are on a 0-1 scale and are at 0.5 by default
options.ImageBrightness = 0.30f;
options.ImageContrast = 0.7f;
options.Resolution= 200;// 数值越大,图片清晰度越高,文件越大
// change the horizontal resolution
// the default value for these properties is 96.0, for a resolution of 96dpi
options.HorizontalResolution = 72f;
// loop through pages and convert them to PNG images
for (int pageNumber = 0; pageNumber < doc.PageCount; pageNumber++)
{
// Save page as PNG
options.PageSet = new PageSet(pageNumber);
doc.Save(pageNumber + "_page.png", options);
}
