Java: Convert PDF to SVG

2022-06-10 07:10:00 Written by  support iceblue
Rate this item
(0 votes)

SVG, short for scalable vector graphics, is a vector image format based on XML for two-dimensional graphics. Vector image files, like SVG and PDF files, are very similar. They can display text, images, and other elements in the same appearance and keep the definition no matter how you zoom them. And because of their similarity, PDF files can be converted to SVG files almost losslessly. This article shows an easy method to convert PDF files to SVG files using Spire.PDF for Java.

Install Spire.PDF for Java

First, you're required to add the Spire.Pdf.jar file as a dependency in your Java program. The JAR file can be downloaded from this link. If you use Maven, you can easily import the JAR file in your application by adding the following code to your project's pom.xml file.

<repositories>
    <repository>
        <id>com.e-iceblue</id>
        <name>e-iceblue</name>
        <url>https://repo.e-iceblue.com/nexus/content/groups/public/</url>
    </repository>
</repositories>
<dependencies>
    <dependency>
        <groupId>e-iceblue</groupId>
        <artifactId>spire.pdf</artifactId>
        <version>10.10.7</version>
    </dependency>
</dependencies>
    

Convert Each Page of a PDF File to an SVG File

The detailed steps are as follows:

  • Create an object of PdfDocument class.
  • Load a PDF document from disk using PdfDocument.loadFromFile() method.
  • Convert the document to SVG file and save it using PdfDocument.saveToFile() method.
  • Java
import com.spire.pdf.*;

public class PDFToSVG {
    public static void main(String[] args) {
        //Create an object of Document class
        PdfDocument pdf = new PdfDocument();

        //Load a PDF document from disk
        pdf.loadFromFile("D:/Samples/Sample.pdf");

        //Convert the document to SVG and Save it
        pdf.saveToFile("D:/javaOutput/PDFToSVG.svg", FileFormat.SVG);
    }
}

Java: Convert PDF to SVG

Convert All the Pages of a PDF File to a Single SVG File

The detailed steps are as follows:

  • Create an object of PdfDocument class.
  • Load a PDF document from disk using PdfDocument.loadFromFile() method.
  • Change the conversion settings to convert the PDF file to a single SVG file using PdfDocument.getConvertOptions().setOutputToOneSvg() method.
  • Convert the document to SVG file and save it using PdfDocument.saveToFile() method.
  • Java
import com.spire.pdf.*;

public class PDFToSVG {
    public static void main(String[] args) {
        //Create an object of Document class
        PdfDocument pdf = new PdfDocument();

        //Load a PDF document from disk
        pdf.loadFromFile("D:/Samples/Sample.pdf");

        //Change the conversion settings to convert the PDF file to a single SVG file
        pdf.getConvertOptions().setOutputToOneSvg(true);

        //Convert the document to SVG and Save it
        pdf.saveToFile("D:/javaOutput/PDFToSVG.svg", FileFormat.SVG);
    }
}

Java: Convert PDF to SVG

Apply for a Temporary License

If you'd like to remove the evaluation message from the generated documents, or to get rid of the function limitations, please request a 30-day trial license for yourself.

Additional Info

  • tutorial_title:
Last modified on Thursday, 27 July 2023 06:26