DEV Community

CodeSharing
CodeSharing

Posted on

[Java] Converting PDF to SVG

SVG (Scalable Vector Graphics) is a vector image format that can be searched, indexed, scripted, compressed, and can be scaled in size without loss of quality. In this article, I will share the following two ways to convert a PDF file to SVG format using Free Spire.PDF for Java.
● Converting each page of the PDF file into a single SVG file.
● Converting multiple pages of the PDF file into one SVG file.

Import jar dependency
Method 1: Download the free library and unzip it. Then add the Spire.Pdf.jar file to your project as dependency.
Method 2: Directly add the jar dependency to maven project by adding the following configurations to the pom.xml.

<repositories>
    <repository>
        <id>com.e-iceblue</id>
        <name>e-iceblue</name>
        <url>http://repo.e-iceblue.com/nexus/content/groups/public/</url>
    </repository>
</repositories>
<dependencies>
    <dependency>
        <groupId>e-iceblue</groupId>
        <artifactId>spire.pdf.free</artifactId>
        <version>4.4.1</version>
    </dependency>
</dependencies>
Enter fullscreen mode Exit fullscreen mode

Sample 1: Converting a 3-page PDF file to 3 SVG files

import com.spire.pdf.*;

public class ToSVG {
    public static void main(String[] args) {

        //Load the PDF file
        PdfDocument pdf = new PdfDocument();
        pdf.loadFromFile("Island.pdf");

        //Save to SVG image
        pdf.saveToFile("ToSVG.svg", FileFormat.SVG);
        pdf.close();
    }
}
Enter fullscreen mode Exit fullscreen mode

ToSVG1

Sample 2: Converting a 3-page PDF file to 1 SVG file

import com.spire.pdf.*;


public class PDFtoSVG {
    public static void main(String[] args) throws Exception {

        String inputPath = "Island.pdf";

        PdfDocument document = new PdfDocument();
        document.loadFromFile(inputPath);

        document.getConvertOptions().setOutputToOneSvg(true);

        document.saveToFile("output.svg", FileFormat.SVG);
        document.close();
    }
}
Enter fullscreen mode Exit fullscreen mode

ToSVG2

Top comments (0)