Home > Java > javaTutorial > body text

How to convert XML to PDF using Java language

王林
Release: 2023-05-14 22:04:04
forward
3525 people have browsed it

Extensible Markup Language (XML) file is a standard text file that uses specific tags to describe the structure and other characteristics of the document. By converting XML to PDF, file transfer and sharing can be facilitated. This article will introduce how to implement this format conversion through Java code.

Import Jar

Introduce the Spire.Doc.jar file in Free Spire.Doc for Java into the program (the file is in the lib folder); if you need to download and import it through Maven, you can Configure pom.xml as follows:

<repositories>
        <repository>
            <id>com.e-iceblue</id>
            <url>https://repo.e-iceblue.cn/repository/maven-public/</url>
        </repository>
    </repositories>
<dependencies>
    <dependency>
        <groupId>e-iceblue</groupId>
        <artifactId>spire.doc.free</artifactId>
        <version>5.2.0</version>
    </dependency>
</dependencies>
Copy after login

Convert XML to PDF

The following are the detailed code steps to implement the conversion:

  • Create the Document class object.

  • Call the Document.loadFromFile(String fileName, FileFormat fileFormat) method to load the XML file.

  • Use the Document.saveToFile(String fileName, FileFormat fileFormat) method to save in PDF format to the specified path.

Java

import com.spire.doc.*;

public class XMLtoPDF {
    public static void main(String[] args) {
        //创建Document类的对象
        Document doc = new Document();
        //加载XML文档
        doc.loadFromFile("sample.xml",FileFormat.Xml);
        //保存为PDF格式到指定路径
        doc.saveToFile("XMLtoPDF.pdf",FileFormat.PDF);
    }
}
Copy after login

Conversion result:

How to convert XML to PDF using Java language

The above is the detailed content of How to convert XML to PDF using Java language. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:yisu.com
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!