Converting a PDF file to MHTML can be done in two simple steps using Aspose.Total for Java. Aspose.Total for Java is a powerful suite of APIs that can be used to process documents in various formats.
The first step is to render the PDF file to DOC using Aspose.PDF for Java. Aspose.PDF for Java is a powerful PDF processing API that can be used to create, read, edit, and convert PDF documents. It can be used to render PDF files to DOC format, which is a Microsoft Word document format.
The second step is to convert the DOC file to MHTML using Aspose.Words for Java. Aspose.Words for Java is a powerful document processing API that can be used to create, read, edit, and convert documents in various formats. It can be used to convert DOC files to MHTML, which is a web page format.
By using Aspose.Total for Java, you can easily convert PDF files to MHTML in two simple steps. First, you need to render the PDF file to DOC using Aspose.PDF for Java. Then, you can convert the DOC file to MHTML using Aspose.Words for Java. This will allow you to easily convert PDF files to MHTML in a few simple steps.
Java API to Convert PDF to MHTML
Get Started with Java File Manipulation APIs
You can easily use Aspose.Total for Java directly from a Maven based project and include Aspose.PDF for Java and Aspose.Words for Java in your pom.xml.
Alternatively, you can get a ZIP file from downloads .
// load PDF file with an instance of Document class
Document document = new Document("template.pdf");
// save PDF as a DOC
document.save("DocOutput.doc", SaveFormat.DOC);
// load DOC with an instance of Document
Document outputDocument = new com.aspose.words.Document("DocOutput.doc");
// call save method while passing SaveFormat.MHTML
outputDocument.save("output.mhtml", SaveFormat.MHTML);
Open Password Protected PDF Document via Java
While converting PDF to MHTML, even if your document is password protected, you can still open it using PDF Manipulation API Aspose.PDF for Java . In order to open the encrypted file, you need to create a Document object and open the PDF using the owner’s password.
Document document = new Document("input.pdf", "password");
// save PDF as a DOC
document.save("DocOutput.doc", SaveFormat.DOC);
Save MHTML Document to a Database via Java
While saving your input document to MHTML file format, you can also save your document to database instead of a file system. You may need to implement storing and retrieving Document objects to and from a database. This would be necessary if you were implementing any type of content management system. In order to save your MHTML to database it is often necessary to serialize the document to obtain a byte array. This can be done using Aspose.Words for Java API. After getting your byte array, you can store it in the database using SQL statement.
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.MHTML);
// 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);
}