Java program ref file to Doc file

Source: Internet
Author: User

Originally wanted to convert the ref file directly into an HTML file, but due to garbled problems can not be solved, so chose a compromise method, first convert RTF to Doc document, and then use the Hwpfdocument class to convert the doc file to an HTML file

1. Download OpenOffice installed on the computer that needs to be converted to the instrument: Http://rj.baidu.com/soft/detail/15989.html?ald

2, add jar package in the corresponding program, corresponding download jar package Baidu Cloud address: Get the code:

or directly on the Internet search jodconverter-core-3.0-beta-4, download down, there is a corresponding package

3, Write Java class, run the project for debugging transformation

Package com.thunisoft.test;

Import Java.io.File;
Import Java.io.FileInputStream;
Import Java.io.FileWriter;
Import java.io.IOException;
Import Java.io.InputStream;
Import Java.io.OutputStream;
Import Java.io.PrintWriter;
Import Java.text.SimpleDateFormat;
Import Java.util.Date;

Import Org.artofsolving.jodconverter.OfficeDocumentConverter;
Import org.artofsolving.jodconverter.office.DefaultOfficeManagerConfiguration;
Import Org.artofsolving.jodconverter.office.OfficeManager;

public class Testrtf2word {
private static String Office_home = "C:\\Program Files (x86) \\OpenOffice 4"; "/opt/libreoffice5.0";
private static int port[] = {8100};
private static int port = 8100;
private static Officemanager Officemanager;

Open Server
public static void StartService () {
Defaultofficemanagerconfiguration configuration = new Defaultofficemanagerconfiguration ();
try {
System.out.println ("Ready to start OpenOffice service ....");
Configuration.setofficehome (office_home);//Set OpenOffice.org installation directory
Configuration.setportnumbers (port);
Configuration.settaskexecutiontimeout (5L);//Set task execution timeout to 5 minutes
Configuration.settaskqueuetimeout ($ * 24L);//Set Task queue timeout to 24 hours
Officemanager = Configuration.buildofficemanager ();
Officemanager.start (); Start the service
SYSTEM.OUT.PRINTLN ("Office conversion service started successfully!");
} catch (Exception CE) {
SYSTEM.OUT.PRINTLN ("Office conversion service failed to start!" Details: "+ CE");
System.exit (0);
}
}

Shutting down the server
public static void StopService () {
System.out.println ("Turn off Office conversion service ....");
if (Officemanager! = null) {
Officemanager.stop ();
}
System.out.println ("Turn off office conversion succeeded!");
}

public static void Main (string[] args) {
StartService ();
Officedocumentconverter converter = new Officedocumentconverter (
Officemanager);
try {
File OutFile = new file ("E:\\rtf\\1\\qwe123.doc");
if (!outfile.getparentfile (). exists ()) {
Outfile.getparentfile (). Mkdirs ();
}
System.out.println (Outfile.exists ());
File InFile = new file ("E:\\rtf\\1\\3606c3de7612-ffa5-44cd-bbc1-f5312de0aa3d.rtf");
System.out.println (Infile.exists ());

Converter.convert (InFile, outFile);
} catch (Exception e) {
E.printstacktrace ();
} finally {
StopService ();
}
}

}

4. After the conversion is complete, it can be stored in a directory, converted to an HTML file by the following way

private static Bytearrayoutputstream Dochandler (String fileName) {
InputStream is = new FileInputStream (fileName);
Hwpfdocument worddocument = null;
Worddocument = new Hwpfdocument (IS);
Wordtohtmlconverter wordtohtmlconverter = new Wordtohtmlconverter (documentbuilderfactory
. newinstance (). Newdocumentbuilder (). NewDocument ());
Wordtohtmlconverter.setpicturesmanager (New Picturesmanager () {
Public String savepicture (byte[] content, PictureType PictureType,
String Suggestedname, float widthinches, float heightinches) {
Return "pic/" + suggestedname;
// }
// });
Wordtohtmlconverter.processdocument (worddocument);
Save pictures
List<picture> pics = worddocument.getpicturestable (). Getallpictures ();
if (pics! = null) {
for (int i = 0; i < pics.size (); i++) {
Picture pic = (picture) pics.get (i);
try {
Pic.writeimagecontent (New FileOutputStream (Urldecoder.decode (Getpicpath (), "UTF-8")
+ Pic.suggestfullfilename ()));
} catch (FileNotFoundException e) {
E.printstacktrace ();
}
}
}

Document HTMLDocument = Wordtohtmlconverter.getdocument ();
Bytearrayoutputstream out = new Bytearrayoutputstream ();
Domsource Domsource = new Domsource (htmldocument);
Streamresult Streamresult = new Streamresult (out);

Transformerfactory tf = Transformerfactory.newinstance ();
Transformer serializer = Tf.newtransformer ();
Serializer.setoutputproperty (outputkeys.encoding, "GB2312");
Serializer.setoutputproperty (outputkeys.encoding, "UTF-8");
Serializer.setoutputproperty (outputkeys.indent, "yes");
Serializer.setoutputproperty (Outputkeys.method, "html");
Serializer.transform (Domsource, Streamresult);
Out.close ();
return out;
}

Java program ref file to Doc file

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.