Convert Word, Excel, PPT, HTML, txt into a picture __html

Source: Internet
Author: User

This article is written because in the project, the requirements mentioned a function, Android below the implementation of the attachment view, download, online reading functions. There's nothing to say about downloading. Online reading refers to the conversion of various types of accessories into pictures, and then the mobile phone online to open the picture, so as to achieve the effect of reading.

Share my implementation: First use OpenOffice to convert various types of documents to PDF, and then use Imagsio to turn PDFs into pictures.

So let's talk about how to deal with the following:

where to download OpenOffice.

OpenOffice Download the address you can search through Baidu OpenOffice Download, it is directly downloaded to.

How to use it after the installation is complete.

When the installation is complete, locate your installation directory and open the program folder under the directory. The folder contains a Soffice.exe file. When you enter this directory, hold down SHIFT and right-click here to open the command prompt, and then enter the command: soffice-headless-accept= "Socket,host=127.0.0.1,port=8100;urp"- Nofirststartwizard then enter the car and OK. This will start the OpenOffice 8100 port.

How to use OpenOffice in the project.

First, you have to prepare the jar bag.

Commons-io-1.4.jar

Icepdf-core.jar
Jodconverter-2.2.2.jar
Ridl-3.0.1.jar
Slf4j-api-1.5.8.jar
Slf4j-jdk14-1.5.6.jar
Unoil-3.0.1.jar

The sample code is as follows:

   public static void Doc2pdf (String docpath, String pdfpath) throws connectexception {
                           file Inputfile = new File (docpath )//Pre-turn files file
                           outputfile = new Pdfpath;//pdf file
                           openofficeconnection connection = new Socketopenofficeconnection (8100);
                           Connection.connect ()//Establish connection
                           documentconverter converter = new Openofficedocumentconverter (connection);
                           Defaultdocumentformatregistry Formatreg = new Defaultdocumentformatregistry ();   
                           Documentformat txt = formatreg.getformatbyfileextension ("ODT");//Set file format
                           Documentformat PDF = Formatreg.getformatbyfileextension ("PDF");/Set file Format
                           converter.convert (inputfile, TXT, outputfile, pdf);//File conversion
                           connection.disconnect ()//close Connection
                  }


how to convert a PDF to a picture.

The sample code is as follows:

   /** * Convert PDF to Picture * * @param pdfpath * @param imagepath * @return Returns the name of the converted picture * @throws Exce
        Ption */public static list<string> Pdf2imgs (String pdfpath, String imgdirpath) throws Exception {
        Document document = new document ();

        Document.setfile (Pdfpath); Float scale = 5f;//magnification float rotation = 0f;//rotation degree list<string> imgnames = new arraylist<string&
        gt; ();
        int pagenum = Document.getnumberofpages ();
        File Imgdir = new file (Imgdirpath);
        if (!imgdir.exists ()) {imgdir.mkdirs (); for (int i = 0; i < pagenum i++) {bufferedimage image = (bufferedimage) document.getpageimage (i
            , Graphicsrenderinghints.screen, Page.boundary_cropbox, rotation, scale);
            RenderedImage rendimage = image;
                try {String FilePath = Imgdirpath + file.separator + i + ". jpg"; File File =New File (FilePath);
                Imageio.write (rendimage, "JPG", file);
            Imgnames.add (Filenameutils.getname (FilePath));
                catch (IOException e) {e.printstacktrace ();
            return null;
        } image.flush ();
        } document.dispose ();
    return imgnames; }
I'll just say the two scale and rotation in the project.

The meaning of these two parameters:

Scale: Refers to the image to be converted into a picture after the magnification, it affects the font sharpness. But this parameter should not be too large, such as when the file is particularly large will be oom. This parameter depends on your own project, I suggest between 1~2. I tested the oom directly at 5.5 and my Computer configuration is i3 6G RAM.

Rotation: Refers to the degree of rotation. 0 is the default does not rotate, of course, there are 360n.

How to turn any type of file into a picture type.

The code is as follows:

public static void Doc2imags (String docpath, String imgdirpath) {
    	string Pdfpath =string.format ("%s%s.pdf",  Filenameutils.getfullpath (Docpath), Filenameutils.getbasename (Docpath));
    	try {
			doc2pdf (Docpath, pdfpath);
			Pdf2imgs (Pdfpath, imgdirpath);
			File PDF =  new file (Pdfpath);
			if (Pdf.isfile ()) {
				pdf.delete ();
			}

		catch (Connectexception e) {
			e.printstacktrace ();
		} catch (Exception e) {
			e.printstacktrace ();
		}
    }
where the source code is downloaded.

http://download.csdn.net/download/woaixinxin123/6332321

                                                                                                                                                                                            I Web, Android development, QQ Group 255825960

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.