本主题说明如何在没有 Interop 的情况下使用 Java** 将 DOCX 转换为 TIFF。 Tiff 文件广泛用于应用程序,本主题将使您能够使用 Java 将 DOCX 导出到 TIFF,而无需依赖任何操作系统(如 macOS、Ubuntu 和 Windows)中的 Microsoft Word 或 Interop,只需几行代码.
使用 Java 将 DOCX 转换为 TIFF 的步骤
- 使用 Maven 存储库包含 Aspose.Words JAR 文件
- 实例化 Document Class 对象以加载源 DOCX
- 实例化 ImageSaveOptions 类对象以设置图像保存选项
- 使用 Java 通过 Save 方法从 DOCX 创建 TIFF
在以下简单步骤中,我们将使用 Document Class 实例从磁盘加载源 DOCX 文件。然后使用 ImageSaveOptions Class 对象,我们将设置所需 TIFF 文件的不同属性,包括压缩和颜色模式。最后,使用 save 方法,我们将从 Java 中的 DOCX 生成 TIFF。
使用 Java 将 DOCX 导出为 TIFF 的代码
import com.aspose.words.Document; | |
import com.aspose.words.ImageColorMode; | |
import com.aspose.words.ImageSaveOptions; | |
import com.aspose.words.License; | |
import com.aspose.words.SaveFormat; | |
import com.aspose.words.TiffCompression; | |
public class WordsKB { | |
public static void main(String[] wordsArgs) throws Exception { | |
// Setting Aspose.Words for license to remove evaluation | |
// version limitations while converting DOCX to TIFF | |
License licConvertingDOCXtoTIFF = new License(); | |
licConvertingDOCXtoTIFF.setLicense("Aspose.Words.Java.lic"); | |
// Open the source Document for exporting to Tiff | |
Document doc = new Document("SampleWord.docx"); | |
// Set Tiff options like color mode, compression for the output TIFF image | |
ImageSaveOptions tiffOptions = new ImageSaveOptions(SaveFormat.TIFF); | |
tiffOptions.setImageColorMode(ImageColorMode.GRAYSCALE); | |
tiffOptions.setTiffCompression(TiffCompression.LZW); | |
// Save the DOCX document as TIFF image file | |
doc.save("ExportedTiff.tiff" , tiffOptions); | |
} | |
} |
在上面的示例代码中,源 DOCX 文件被加载并指定 SaveFormat enumerator 来创建 TIFF 文件。您可以根据需要设置不同的图像渲染属性,包括颜色模式、对比度、图像亮度和分辨率。
在上面的教程中,我们了解到在 Java 中从 DOCX 生成 TIFF 是多么简单,而不依赖于 Microsoft Words。但是,在上一个主题中,我们探讨了您需要更新现有 Word 文件,请参阅 如何在没有互操作的情况下使用 Java 生成 Word 文档 上的文章。