您可以使用两个简单的步骤将 MD 转换为 GIF。首先,您需要使用 Aspose.PDF for Java 将 MD 文件渲染为 DOC。之后,通过使用强大的文档处理 API Aspose.Words for Java ,您可以将 DOC 转换为 GIF。这两个 API 都属于 Aspose.Total for Java 包。
Java API 将 MD 转换为 GIF
Document document = new Document("template.md"); | |
document.save("DocOutput.doc", SaveFormat.DOC); | |
Document outputDocument = new com.aspose.words.Document("DocOutput.doc"); | |
// call save method while passing SaveFormat.GIF | |
outputDocument.save("output.gif", SaveFormat.GIF); |
通过 Java 打开受密码保护的 MD 文档
在将 MD 转换为 GIF 时,即使您的文档受密码保护,您仍然可以使用 PDF 操作 API Aspose.PDF for Java 打开它。为了打开加密文件,您需要创建一个 Document 对象并使用所有者的密码打开 MD。
// open encrypted document
Document document = new Document("input.md", "password");
// save MD as a DOC
document.save("DocOutput.doc", SaveFormat.DOC);
通过 Java 将 GIF 文档保存到数据库
在将输入文档保存为 GIF 文件格式的同时,您还可以将文档保存到数据库而不是文件系统。您可能需要实现在数据库中存储和检索 Document 对象。如果您正在实施任何类型的内容管理系统,这将是必要的。为了将您的 GIF 保存到数据库,通常需要序列化文档以获取字节数组。这可以使用 Aspose.Words for Java API 来完成。获取字节数组后,您可以使用 SQL 语句将其存储在数据库中。
public static void StoreToDatabase(Document doc, Connection mConnection) throws Exception {
// create an output stream which uses byte array to save data
ByteArrayOutputStream aout = new ByteArrayOutputStream();
// save the document to byte array
doc.save(aout, SaveFormat.GIF);
// get the byte array from output steam
// the byte array now contains the document
byte[] buffer = aout.toByteArray();
// get the filename from the document.
String fileName = doc.getOriginalFileName();
String filePath = fileName.replace("\\", "\\\\");
// create the SQL command.
String commandString = "INSERT INTO Documents (FileName, FileContent) VALUES('" + filePath + "', '" + buffer + "')";
Statement statement = mConnection.createStatement();
statement.executeUpdate(commandString);
}