将文档转换为图像
Contents
[
Hide
]
有时需要获取图像而不是其他格式的文档,例如 DOCX 或 PDF。例如,您需要将任何文档页面的预览添加到您的网站或应用程序,或者创建文档的"扫描"以发送发票。此时您可能需要将任何 支持的加载格式 格式的文档转换为任何 支持的保存格式 格式的图像。
转换为图像格式
与已经描述的所有转换示例一样,您需要创建一个新文档或以任何支持的格式加载现有文档,进行必要的更改,并将其保存为任何可用的图像格式,例如 JPEG、PNG 或 BMP。
以下代码示例展示了如何将 PDF 转换为 JPEG:
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# For complete examples and data files, please go to https://github.com/aspose-words/Aspose.Words-for-Python-via-.NET | |
doc = aw.Document(docs_base.my_dir + "Pdf Document.pdf") | |
doc.save(docs_base.artifacts_dir + "BaseConversions.pdf_to_jpeg.jpeg") |
转换为图像 {#specify-save-options-when-converting-to-an-image} 时指定保存选项
Aspose.Words 为您提供了 ImageSaveOptions 类,它可以更好地控制如何以各种图像格式保存文档。此类的某些属性继承或重载了基类(例如 FixedPageSaveOptions 或 SaveOptions)的属性,但也有一些特定于保存图像的选项。
可以使用 page_set 属性指定要转换为图像格式的页面。例如,如果您只需要预览第一页或特定页面,则可以应用它。
还可以使用以下属性控制输出图像质量和像素格式 - horizontal_resolution、vertical_resolution、scale、pixel_format,以及使用以下属性设置图像颜色设置 - image_brightness、image_color_mode、image_contrast、paper_color。
还有一些属性适用于某种格式,例如 jpeg_quality 或 tiff_compression。
以下代码示例演示如何通过应用一些附加设置来创建第一个文档页面的预览:
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# For complete examples and data files, please go to https://github.com/aspose-words/Aspose.Words-for-Python-via-.NET | |
doc = aw.Document(docs_base.my_dir + "Rendering.docx") | |
options = aw.saving.ImageSaveOptions(aw.SaveFormat.JPEG) | |
# Set the "PageSet" to "0" to convert only the first page of a document. | |
options.page_set = aw.saving.PageSet(0) | |
# Change the image's brightness and contrast. | |
# Both are on a 0-1 scale and are at 0.5 by default. | |
options.image_brightness = 0.3 | |
options.image_contrast = 0.7 | |
# Change the horizontal resolution. | |
# The default value for these properties is 96.0, for a resolution of 96dpi. | |
options.horizontal_resolution = 72 | |
doc.save(docs_base.artifacts_dir + "WorkingWithImageSaveOptions.get_jpeg_page_range.jpeg", options) |