C # Build a file format conversion Server

Source: Internet
Author: User

File format conversion is required for viewing office documents online, because the browser does not support opening office documents directly, therefore, in many cases, we need to convert these documents into formats that can be played by flash, but another problem arises. Some devices do not support flash. After testing the three versions, I finally got a better solution. First, most people know that flashpaper is used, but unfortunately this software is very old. Second, it uses office COM, but its disadvantages are very obvious, A lot of code is required to complete simple tasks and many configurations are required. The pain is self-evident. The third method is to use OpenOffice and jodconverter.

 

I. Required Software

Because jodconverter is written in Java, I have converted it into a C # class library, which avoids many events and makes no sense.

1. OpenOffice software (http://www.openoffice.org/download/index.html) can be directly installed by default

2. I encapsulated class library (http://files.cnblogs.com/yaozhenfa/jodconverter.net.rar)

 

2. Start OpenOffice

The author installed OpenOffice in c: \ Program Files (x86) \, so you need to run "cmd" in DOS (START-) and enter the following command:

1 C:\>"C:/Program Files (x86)/OpenOffice 4/program/soffice.exe" -accept=socket,hos2 t=0.0.0.0,port=8080;urp;-headless

 

Host indicates the IP address of the listener and port indicates the port. The setting here is 8080.

 

Iii. Test

Create a new console project in Vs and reference all the encapsulated class libraries.

 

Then write the following code:

 1 using com.artofsolving.jodconverter; 2 using com.artofsolving.jodconverter.openoffice.connection; 3 using com.artofsolving.jodconverter.openoffice.converter; 4 using java.io; 5 using System; 6 using System.Collections.Generic; 7 using System.Linq; 8 using System.Text; 9 10 namespace ConsoleApplication111 {12     class Program13     {14         static void Main(string[] args)15         {16             File inputFile = new File("c://test.doc");17             File outputFile = new File("c://test.pdf");18             OpenOfficeConnection connection = new SocketOpenOfficeConnection(8080);19             connection.connect();20             DocumentConverter converter = new OpenOfficeDocumentConverter(connection);21             converter.convert(inputFile, outputFile);22             connection.disconnect();23             System.Console.ReadKey();24         }25     }26 }

 

 

Here, the document to be tested by the pen is stored in the C drive and named as test.doc. The reader needs to modify the file according to his own situation. Here I will convert the doc to PDF, and of course it can also be converted to HTML.

 

I believe that you will need this solution soon.

 

About Java to C # can download from this website (http://www.ikvm.net /)

 

Iv. References

Thanks for the technologies provided in the following blog:

Http://blog.csdn.net/hnzhangshilong/article/details/7799664 (C # Call the Java class method)

Http://blog.csdn.net/oldjavaman/article/details/1742666 (Build File Format Conversion server)

Http://www.cnblogs.com/shanyou/archive/2007/09/11/890139.html (Turning OpenOffice.org into a file format conversion tool)

 

Hope that the readers who like it can support it so that the author can find a better solution. You can ask any questions in the comments.

 

 

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.