Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to convert Word into PDF/Html/ Picture / XPS/SVG by Java

2025-03-28 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/03 Report--

This article mainly introduces how Java converts Word into PDF/Html/ picture / XPS/SVG. The article is very detailed and has certain reference value. Interested friends must read it!

Spire.Cloud.SDK for Java API provides ConverApi interface support to convert Word documents to a variety of document formats, including Word to PDF, Html, bitmap Bitmap (JPEG/BMP/ PNG), vector map Vectorgraph (support for WMF/SVG), XPS, PostScript, PCL, dotx, dotm, docm, odt, wordxml, wordml, doc, docx, rtf, epub and so on.

Step 1. Import the jar file

Create a Maven project program and download and import it through the maven repository. Take IDEA as an example, create a new Maven project, configure the maven repository path in the pom.xml file, and specify the dependencies of spire.cloud.sdk, as follows:

Com.e-iceblue cloud http://repo.e-iceblue.cn/repository/maven-public/ cloud spire.cloud.sdk 3.5.0 com.google.code.gson gson 2.8.1 com.squareup.okhttp Logging-interceptor 2.7.5 com.squareup.okhttp okhttp 2.7.5 com.squareup.okio okio 1.6.0 io.gsonfire gson-fire 1.8.0 Io.swagger swagger-annotations 1.5.18 org.threeten threetenbp 1.3.5

When the configuration is complete, click "Import Changes" to import all the required jar files. If you are using Eclipse, refer to the import method here.

Import results:

Step 2: log in to the Ice Blue Cloud account, create a folder, and upload documents

Step 3. Create an application to obtain App ID and App Key

After completing the above steps, you can refer to the following code example for document conversion.

Step 4. Code example

1. Convert Word to PDF

When you convert Word to PDF, you can specify the converted format to PDF format directly through the convert () method, or embed the font file into PDF when converting to PDF through the convertToPdf () method.

1.1 convert directly through the convert () method, where the conversion supports a variety of file formats in addition to PDF format

Import spire.cloud.word.sdk.client.ApiException; import spire.cloud.word.sdk.client.Configuration; import spire.cloud.word.sdk.client.api.ConvertApi; public class WordToPDF {/ / configure App account information static String appId = "App ID"; static String appKey = "App Key"; static String baseUrl = "https://api.e-iceblue.cn"; static Configuration wordConfiguration = new Configuration (appId, appKey, baseUrl); static ConvertApi convertApi = new ConvertApi (wordConfiguration) Public static void main (String [] args) throws ApiException {String name = "Test.docx"; / / Word source document String format = "pdf" for testing; / / converted target document format, which can include multiple document formats, such as postScript, pcl, dot, dotx, dotm, docm, odt, wordxml, wordml, pdf, doc, docx, rtf, epub, xps, html String password = null / / Source document password String folder = "input"; / / folder where the source document is located String storage = null;// 2G free storage space provided by Ice Blue Cloud String destFilePath = "output/convertWordToPDF.pdf"; / / the path of the converted result document (saved here in the output folder) / / call the method to convert the document format convertApi.convert (name, format, destFilePath, password, folder, storage) }}

1.2 when converting, embed the font file

Import spire.cloud.word.sdk.client.*; import spire.cloud.word.sdk.client.api.ConvertApi; import spire.cloud.word.sdk.client.model.PrivateFont; import java.util.ArrayList; import java.util.List; public class WordToPDF {/ / configure App account information static String appId = "App ID"; static String appKey = "APP Key"; static String baseUrl = "https://api.e-iceblue.cn"; Static Configuration wordConfiguration = new Configuration (appId, appKey, baseUrl); static ConvertApi convertApi = new ConvertApi (wordConfiguration); public static void main (String [] args) throws ApiException {String name = "Test.docx"; / / Word source document String password = null;// source document password String folder = "input"; / / folder where the source document is located String storage = null / / 2G free storage space provided by Ice Blue Cloud String destFilePath = "output/convertWordToPdf.pdf"; / / the path of the converted result document (saved here in the output folder) / / when the font used in the Word document is not installed on the system, the font can be added to the pdf document by loading the font file to draw the text List privateFonts = new ArrayList () PrivateFonts.add (new PrivateFont ("Chinese Song", "input/STZHONGS.ttf"); privateFonts.add (new PrivateFont ("consola", "input/consola.ttf")); / / call the method to convert Word to PDF convertApi.convertToPdf (name, destFilePath, password, folder, storage, privateFonts);}}

2. Word to Html

The conversion to Html here can also be done through the conversion method in 1.1, or through the convertToHtml () method.

Import spire.cloud.word.sdk.client.ApiException; import spire.cloud.word.sdk.client.Configuration; import spire.cloud.word.sdk.client.api.ConvertApi; public class WordToHtml {/ / configure App account information static String appId = "App ID"; static String appKey = "App Key"; static String baseUrl = "https://api.e-iceblue.cn"; static Configuration wordConfiguration = new Configuration (appId, appKey, baseUrl); static ConvertApi convertApi = new ConvertApi (wordConfiguration) Public static void main (String [] args) throws ApiException {String name = "Test.docx"; / / Word source document for testing String password = null;// source document password String folder = "input"; / / folder where the source document is located String storage = null;// 2G free storage space provided by ice blue cloud String destFolder = "output/convertWordToHtml.html" / / result document path / / call the method to convert Word to HTML convertApi.convertToHtml (name, destFolder, password, folder, storage);}}

3. Transfer Word to picture

When converting to an image, you can convert Word to bitmap convertToBitmap () and vector map convertToVectorgraph ().

Import spire.cloud.word.sdk.client.ApiException; import spire.cloud.word.sdk.client.Configuration; import spire.cloud.word.sdk.client.api.ConvertApi; public class WordToImage {/ / configure App account information static String appId = "App ID"; static String appKey = "App Key"; static String baseUrl = "https://api.e-iceblue.cn"; static Configuration wordConfiguration = new Configuration (appId, appKey, baseUrl); static ConvertApi convertApi = new ConvertApi (wordConfiguration) Public static void main (String [] args) throws ApiException {String name = "Test.docx"; / / Word source document for testing String imageFormat = "png"; / / specify the converted target document format, which supports jpeg, bmp, png String password = null;// source document password String folder = "input"; / / the folder where the source document resides String storage = null / / 2G free storage space provided by Ice Blue Cloud Integer startPage = null; Integer pageCount = null; String destFolder = "output/ConvertWordToImage.png"; / / converted document path / / call method to bitmap convertApi.convertToBitmap (name, imageFormat, destFolder, password, folder, storage, startPage, pageCount); / * String name = "Test.docx"; / / Word source document String imageType = "svg" / / specify the converted destination document format, which supports wmf, svg String password = null;// source document password String folder = "input"; / / the folder where the source document resides String storage = null; Integer startPage = null; Integer pageCount = null; String destFolder = "output/ConvertWordToVectorgraph.svg" / / the path of the converted result document / / the calling method is converted to vector convertApi.convertToVectorgraph (name, imageType, destFolder, password, folder, storage, startPage, pageCount); * /}} above is all the content of the article "how Java converts Word into PDF/Html/ Picture / XPS/SVG". Thank you for reading! Hope to share the content to help you, more related knowledge, welcome to follow the industry information channel!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report