将文档另存为多页 TIFF
处理文档时,您经常需要将文档转换为光栅图像文件。如果您必须以可读和可打印但不可编辑的格式呈现文档,这一点尤其重要。例如,您可以使用文档第一页的光栅图像作为预览。本文以 TIFF 格式(一种更流行的图像格式)为例介绍如何将文档转换为光栅图像。
将 DOC 转换为多页 TIFF
在 Aspose.Words 中,只需将"保存到"路径和相关文件扩展名传递给 Save 方法,即可使用一行代码执行从 DOC 到 TIFF 的转换。 Save 方法自动从路径中指定的文件扩展名派生 SaveFormat
。以下示例演示如何将文档转换为 TIFF 格式:
// For complete examples and data files, please go to https://github.com/aspose-words/Aspose.Words-for-.NET | |
// Save the document as multipage TIFF. | |
doc.Save(dataDir + "TestFile Multipage TIFF_out.tiff"); |
渲染 TIFF 时指定附加选项
您经常需要指定其他选项,这会影响渲染结果。为此,请使用 ImageSaveOptions 类,该类包含确定文档如何在图像上显示的属性。您可以指定以下内容:
- 保存格式以确定可用选项列表(SaveFormat)
- 分辨率(HorizontalResolution、VerticalResolution、Resolution)
- 页数(PageIndex、PageCount)
- 颜色和灯光设置(PaperColor、ImageColorMode、ImageBrightness、ImageContrast)
- 图像质量(JpegQuality、Scale、TiffCompression、GraphicsQualityOptions)
- 用于二值化图像的方法(TiffBinarizationMethod、ThresholdForFloydSteinbergDithering)
- 生成图像的像素格式 (PixelFormat)
- 通过 Aspose.Words(MetafileRenderingOptions、UseGdiEmfRenderer)处理 Windows 图元文件
- 您可以在 ImageSaveOptions 类中看到的其他选项
以下示例显示如何使用配置的选项将 DOC 转换为 TIFF:
// For complete examples and data files, please go to https://github.com/aspose-words/Aspose.Words-for-.NET | |
// Create an ImageSaveOptions object to pass to the Save method | |
ImageSaveOptions options = new ImageSaveOptions(SaveFormat.Tiff); | |
options.PageIndex = 0; | |
options.PageCount = 2; | |
options.TiffCompression = TiffCompression.Ccitt4; | |
options.Resolution = 160; | |
dataDir = dataDir + "TestFileWithOptions_out.tiff"; | |
doc.Save(dataDir, options); |
TIFF 二值化阈值
通过将 PixelFormat 属性设置为 Format1bppIndexed 像素格式类型,并将 TiffCompression 属性设置为 Ccitt3 或 Ccitt4,可以将 TIFF 图像保存为 1bpp 黑白格式。
对于图像分割,Aspose.Words 使用最简单的方法–阈值化。该方法使用阈值将灰度 TIFF 图像转换为二值图像。因此,当文档需要转换为TIFF文件格式时,可以通过ThresholdForFloydSteinbergDithering属性获取或设置TIFF二值化的阈值。该属性的默认值设置为 128,该值越高,图像越暗。
以下示例显示如何使用指定阈值执行 TIFF 二值化:
// For complete examples and data files, please go to https://github.com/aspose-words/Aspose.Words-for-.NET | |
Document doc = new Document(dataDir + "TestFile.Colors.docx"); | |
ImageSaveOptions options = new ImageSaveOptions(Aspose.Words.SaveFormat.Tiff); | |
options.TiffCompression = TiffCompression.Ccitt3; | |
options.ImageColorMode = ImageColorMode.Grayscale; | |
options.TiffBinarizationMethod = ImageBinarizationMethod.FloydSteinbergDithering; | |
options.ThresholdForFloydSteinbergDithering = 254; | |
dataDir = dataDir + "ThresholdForFloydSteinbergDithering_out.tiff"; | |
doc.Save(dataDir, options); |
您可以在下面比较在不同阈值下执行 TIFF 二值化的图像:
