Implementation of printing law enforcement documents (III) (Implementation of word → png) and document png
Implementation of printing law enforcement documents (implementation of word ^ png)
I am very worried about writing this blog. Due to the knowledge structure and other reasons, it takes a lot of time and effort to start the openoffice environment on redhat. Some commands and methods that have passed the verification are changed slightly, and the progress is stuck. If you have to choose, I should not use openoffice. After all, my interests and pre-occupations are jee and front-end. I am a little biased in doing this kind of work, but I can't mention it.
This part of the code is copied on the Internet, and most of the Code is the same. It is a fixed format and there is not much room for anyone to use. Openoffice implements word to pdf and icepdf implements pdf to png. Icepdf is a paid software. It is risky to use it. The converted images are amazing, and you can adjust the precision freely.
Openoffice installation and Service Startup
Download installation package: http://www.openoffice.org/download/index.html
Decompress the Installation File in redhat: tar-zxvf compressed file name .tar.gz. The extracted file can only be placed in the current directory.
Install dependency: cd zh-CN/RPMS/rpm-ivh *. rpm
Install desktop: cd desktop-integration/rpm-ivh openoffice4.1.1-redhat-menus-4.1.1-9775.noarch.rpm
Go to the installation directory: cd/opt/openoffice4/program/
Start the service: soffice-headless-accept = "socket, host = 127.0.0.1, port = 8100; urp;"-nofirststartwizard & (Note: There is a pitfall here, it is best to add the "&" character at the end of the command. soffice is killed last week. the bin process cannot be started until it is started. The reason is that "&" is not added. I don't know why. I hope you can solve this problem)
Check whether the listener is successfully created: netstat-apn | grep 8100
Uninstall: rpm-e 'rpm-qa | grep openoffice 'rpm-qa | grep ooobasis'
Specific word → png: Java code
/***** @ Param sourceName doc file name * @ param zoom image definition * @ return * @ throws Exception */public String [] opt2png (String sourceName, float zoom) throws Exception {// absolute path of the image Disk: the return value of the method String imgFilePaths []; // a random pdf is generated each time, every morning, an automatic task is started to clear the pdfFile directory File inputFile = new File (wordFilePath + File. separator + sourceName); if (! InputFile. exists () {// return-1; // if the source file cannot be found,-1 System is returned. out. println ("word File not found" + wordFilePath + File. separator + sourceName); return null;} // generate a random PDF file String using name = UUID. randomUUID (). toString (); pdfFilePath = pdfFilePath + File. separator + separator name + ". pdf "; File outputFile = new File (pdfFilePath); if (! OutputFile. getParentFile (). exists () {outputFile. getParentFile (). mkdirs ();} // start the service: it is not very useful. You need to manually start String command = officeHome + "program" + File. separator + "soffice-headless-accept = \" socket, host = 127.0.0.1, port = 8100; urp; \ ""; Process pro = null; try {pro = runtime.getruntime(cmd.exe c (command);} catch (IOException e) {e. printStackTrace ();} // establish the connection OpenOfficeConnection connection = new SocketOpenOfficeConnection (8100); try {connection. connect ();} catch (Exception e) {// catch Block e automatically generated by TODO. printStackTrace ();} DocumentConverter converter = new OpenOfficeDocumentConverter (connection); try {converter. convert (inputFile, outputFile); // the execution of this method cannot be debugged} catch (Exception e) {// catch Block e automatically generated by TODO. printStackTrace ();} finally {// close the connection. disconnect (); // closes the process pro of the OpenOffice service. destroy (); // pdf generated successfully} // icepdf work Document document = null; float rotation = 0f; document = new Document (); document. setFile (pdfFilePath); // get the document's page number int pageNum = document. getNumberOfPages (); imgFilePaths = new String [pageNum]; String imgFileName; for (int I = 0; I <pageNum; I ++) {// the following line of code execution cannot debug BufferedImage img = (BufferedImage) document. getPageImage (I, GraphicsRenderingHints. SCREEN, Page. BOUNDARY_CROPBOX, rotation, zoom); Iterator iter = ImageIO. getImageWritersBySuffix ("png"); ImageWriter writer = (ImageWriter) iter. next (); imgFileName = UUID. randomUUID (). toString (); File outFile = new File (pngFilePath + File. separator + imgFileName + ". png "); FileOutputStream out = new FileOutputStream (outFile); ImageOutputStream outImage = ImageIO. createImageOutputStream (out); writer. setOutput (outImage); writer. write (new IIOImage (img, null, null); imgFilePaths [I] = pngFilePath + File. separator + imgFileName + ". png ";}return imgFilePaths ;}