Home  >  Article  >  Java  >  How to convert XML to PDF using Java language

How to convert XML to PDF using Java language

王林
王林forward
2023-05-14 22:04:043482browse

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>

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);
    }
}

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!

Statement:
This article is reproduced at:yisu.com. If there is any infringement, please contact admin@php.cn delete