Thursday, 09 June 2022 07:27

Java: Convert PDF to Excel

For security reasons, many financial documents such as invoices are usually saved in PDF format. If you want to perform data analysis and calculation on these documents, you may need to convert them to Excel. In this article, we will introduce how to convert PDF to Excel in Java using Spire.PDF for Java.

Install Spire.PDF for Java

First of all, 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 PDF to Excel in Java

The following are the steps to convert a PDF document to Excel:

  • Initialize an instance of PdfDocument class.
  • Load the PDF document using PdfDocument.loadFromFile(String) method.
  • Save the document to Excel using PdfDocument.saveToFile(String, FileFormat) method.
  • Java
import com.spire.pdf.FileFormat;
import com.spire.pdf.PdfDocument;

public class ConvertPdfToExcel {
    public static void main(String[] args) {
        //Initialize an instance of PdfDocument class
        PdfDocument pdf = new PdfDocument();
        //Load the PDF document
        pdf.loadFromFile("Sample.pdf");

        //Save the PDF document to XLSX
        pdf.saveToFile("PdfToExcel.xlsx", FileFormat.XLSX);
    }
}

Java: Convert PDF to Excel

This example converts multiple PDF pages to multiple Excel worksheets. If you want to convert a multi-page PDF to a single Excel sheet, please refer to this article: Java: Convert a Multi-Page PDF to One Excel Worksheet.

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.

Published in Conversion
Wednesday, 23 February 2022 08:44

Java: Convert HTML to PDF

The benefits of converting HTML documents to PDF files are:

  • Read and view offline
  • Easy to edit and comment
  • Easy to share and print
  • Smaller in size

This article demonstrates how to render an HTML webpage (URL) or an HTML string to a PDF document using Spire.PDF for Java with QT web engine.

Install Spire.PDF for Java

First of all, 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>
    

Download Plugin

This scenario also relies on QT web engine which is an open-source library that provides functionality for rendering HTML content. Please download the plugin that fits in with your operating system from the following link.

Unzip the package somewhere on your disk to get the "plugins" folder. In this example, we saved the plugin under the path "F:\Libraries\Plugin\plugins-windows-x64\plugins‪‪".

Java: Convert HTML to PDF

For Linux and Mac environments, please directly copy the plugin package to system and decompress it, and ensure that the decompressed folder has permission to read.

Please note that this conversion method requires GUI (graphical user interface) support. For servers that do not have a graphical user interface installed, follow the following steps to convert.

Convert a URL to PDF

The following are the steps to convert a URL to a PDF document using Spire.PDF for Java with QT plugin.

  • Specify the URL path to convert.
  • Specify the output file path.
  • Specify the plugin path, and pass it as a parameter of HtmlConvert.setPluginPath() method.
  • Call HtmlConverter.convert (String url, String fileName, boolean enableJavaScript, int timeout, com.spire.pdf.htmlconverter.qt.Size pageSize, com.spire.pdf.graphics.PdfMargins margins) method to convert a URL to PDF.
  • Java
import com.spire.pdf.graphics.PdfMargins;
import com.spire.pdf.htmlconverter.qt.HtmlConverter;
import com.spire.pdf.htmlconverter.qt.Size;

public class ConvertUrlToPdf {

    public static void main(String[] args) {

        //Specify the url path
        String url = "https://www.wikipedia.org/";

        //Specify the output file path
        String fileName = "output/UrlToPdf.pdf";

        //Specify the plugin path
        String pluginPath = "F:\\Libraries\\Plugin\\plugins-windows-x64\\plugins";

        //Set the plugin path
        HtmlConverter.setPluginPath(pluginPath);

        //Convert URL to PDF
        HtmlConverter.convert(url, fileName, true, 1000000, new Size(1200f, 1000f), new PdfMargins(0));
    }
}

Java: Convert HTML to PDF

Convert an HTML String to PDF

The following are the steps to convert an HTML string to a PDF document using Spire.PDF for Java with QT plugin.

  • Specify the URL path to convert.
  • Specify the output file path.
  • Specify the plugin path, and pass it as a parameter of HtmlConvert.setPluginPath() method.
  • Call HtmlConverter.convert (String htmlString, String fileName, boolean enableJavaScript, int timeout, com.spire.pdf.htmlconverter.qt.Size pageSize, com.spire.pdf.graphics.PdfMargins margins, com.spire.pdf.htmlconverter.LoadHtmlType htmlType) method to convert an HTML string to PDF.

Note: Only inline CSS style and internal CSS style can be rendered correctly on PDF. If you have an external CSS style sheet, please convert it to inline or internal CSS style.

  • Java
import com.spire.pdf.graphics.PdfMargins;
import com.spire.pdf.htmlconverter.LoadHtmlType;
import com.spire.pdf.htmlconverter.qt.HtmlConverter;
import com.spire.pdf.htmlconverter.qt.Size;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.IOException;

public class ConvertHtmlStringToPdf {

    public static void main(String[] args) throws IOException {

        //Invoke the custom method HtmlToString() to convert HTML file to string
        String htmlString = HtmlToString("C:\\Users\\Administrator\\Desktop\\Sample.html");

        //Specify the output file path
        String outputFile = "output/HtmlToPdf.pdf";

        //Specify the plugin path
        String pluginPath = "F:\\Libraries\\Plugin\\plugins-windows-x64\\plugins";

        //Set the plugin path
        HtmlConverter.setPluginPath(pluginPath);

        //Convert the HTML string to PDF
        HtmlConverter.convert(htmlString, outputFile, true, 100000, new Size(700, 900), new PdfMargins(0), LoadHtmlType.Source_Code);
    }

    //Convert a HTML file to string
    public static String HtmlToString(String filePath) throws IOException {
        String path = filePath;
        File file = new File(path);
        FileReader fileReader = new FileReader(file);
        BufferedReader bufferedReader = new BufferedReader(fileReader);
        StringBuilder stringBuilder = new StringBuilder();
        String temp = "";
        while ((temp = bufferedReader.readLine()) != null) {
            stringBuilder.append(temp + "\n");
        }
        bufferedReader.close();
        String str = stringBuilder.toString();
        return str;
    }
}

Java: Convert HTML to PDF

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.

Published in Conversion

Spire.PDF supports to convert PDF to HTML and save the resulted HTML file to stream by calling the method PdfDocument.saveToStream(). When converting PDF to HTML, it also supports to set the convert options with embedded SVG/Image on the resulted HTML file. This article will demonstrate how to convert the PDF pages to HTML with embedded SVG and embedded image.

import com.spire.pdf.*;
import java.io.*;

public class PDFtoHTML {
    public static void main(String[] args) throws FileNotFoundException {

        String inputFile = "Sample.pdf";
        String outputFile = "output/toHTML_out.html";

        //Load the sample document file
        PdfDocument pdf = new PdfDocument();

        pdf .loadFromFile(inputFile);

        //Set the bool useEmbeddedSvg and useEmbeddedImg as true 
        pdf .getConvertOptions().setPdfToHtmlOptions(true,true);

        //Save to stream
        File outFile = new File(outputFile);
        OutputStream outputStream = new FileOutputStream(outFile);
        pdf.saveToStream(outputStream, FileFormat.HTML);
        pdf.close();

    }
}
Published in Conversion
Friday, 08 April 2022 07:34

Java: Convert Images to PDF

Converting images to PDF is beneficial for many reasons. For one reason, it allows you to convert images into a format that is more readable and easier to share. For another reason, it dramatically reduces the size of the file while preserving the quality of images. In this article, you will learn how to convert images to PDF in Java using Spire.PDF for Java.

There is no straightforward method provided by Spire.PDF to convert images to PDF. You could, however, create a new PDF document and draw images at the specified locations. Depending on whether the page size of the generated PDF matches the image, this topic can be divided into two subtopics.

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>
    

Additionally, the imgscalr library is used in the first code example to resize images. It is not necessary to install it if you do not need to adjust the image’s size.

Add an Image to PDF at a Specified Location

The following are the steps to add an image to PDF at a specified location using Spire.PDF for Java.

  • Create a PdfDocument object.
  • Set the page margins using PdfDocument.getPageSettings().setMargins() method.
  • Add a page using PdfDocument.getPages().add() method
  • Load an image using ImageIO.read() method, and get the image width and height.
  • If the image width is larger than the page (the content area) width, resize the image to make it to fit to the page width using the imgscalr library.
  • Create a PdfImage object based on the scaled image or the original image.
  • Draw the PdfImage object on the first page at (0, 0) using PdfPageBase.getCanvas().drawImage() method.
  • Save the document to a PDF file using PdfDocument.saveToFile() method.
  • Java
import com.spire.pdf.PdfDocument;
import com.spire.pdf.PdfPageBase;
import com.spire.pdf.graphics.PdfImage;
import org.imgscalr.Scalr;

import javax.imageio.ImageIO;
import java.awt.image.BufferedImage;
import java.io.FileInputStream;
import java.io.IOException;

public class AddImageToPdf {

    public static void main(String[] args) throws IOException {

        //Create a PdfDocument object
        PdfDocument doc = new PdfDocument();

        //Set the margins
        doc.getPageSettings().setMargins(20);

        //Add a page
        PdfPageBase page = doc.getPages().add();

        //Load an image
        BufferedImage image = ImageIO.read(new FileInputStream("C:\\Users\\Administrator\\Desktop\\announcement.jpg"));

        //Get the image width and height
        int width = image.getWidth();
        int height = image.getHeight();

        //Declare a PdfImage variable
        PdfImage pdfImage;

        //If the image width is larger than page width
        if (width > page.getCanvas().getClientSize().getWidth())
        {
            //Resize the image to make it to fit to the page width
            int widthFitRate =  width / (int)page.getCanvas().getClientSize().getWidth();
            int targetWidth = width / widthFitRate;
            int targetHeight = height / widthFitRate;
            BufferedImage scaledImage = Scalr.resize(image,Scalr.Method.QUALITY,targetWidth,targetHeight);

            //Load the scaled image to the PdfImage object
            pdfImage = PdfImage.fromImage(scaledImage);

        } else
        {
            //Load the original image to the PdfImage object
            pdfImage = PdfImage.fromImage(image);
        }

        //Draw image at (0, 0)
        page.getCanvas().drawImage(pdfImage, 0, 0, pdfImage.getWidth(), pdfImage.getHeight());

        //Save to file
        doc.saveToFile("output/AddImage.pdf");
    }
}

Java: Convert Images to PDF

Convert an Image to PDF with the Same Width and Height

The following are the steps to convert an image to a PDF with the same page size as the image using Spire.PDF for Java.

  • Create a PdfDocument object.
  • Set the page margins to zero using PdfDocument.getPageSettings().setMargins() method.
  • Load an image using ImageIO.read() method, and get the image width and height.
  • Add a page to PDF based on the size of the image using PdfDocument.getPages().add() method.
  • Create a PdfImage object based on the image.
  • Draw the PdfImage object on the first page from the coordinate (0, 0) using PdfPageBase.getCanvas().drawImage() method.
  • Save the document to a PDF file using PdfDocument.saveToFile() method.
  • Java
import com.spire.pdf.PdfDocument;
import com.spire.pdf.PdfPageBase;
import com.spire.pdf.graphics.PdfImage;

import javax.imageio.ImageIO;
import java.awt.*;
import java.awt.image.BufferedImage;
import java.io.FileInputStream;
import java.io.IOException;

public class ConvertImageToPdfWithSameSize {

    public static void main(String[] args) throws IOException {

        //Create a PdfDocument object
        PdfDocument doc = new PdfDocument();

        //Set the margins to 0
        doc.getPageSettings().setMargins(0);

        //Load an image
        BufferedImage image = ImageIO.read(new FileInputStream("C:\\Users\\Administrator\\Desktop\\announcement.jpg"));

        //Get the image width and height
        int width = image.getWidth();
        int height = image.getHeight();

        //Add a page of the same size as the image
        PdfPageBase page = doc.getPages().add(new Dimension(width, height));

        //Create a PdfImage object based on the image
        PdfImage pdfImage = PdfImage.fromImage(image);

        //Draw image at (0, 0) of the page
        page.getCanvas().drawImage(pdfImage, 0, 0, pdfImage.getWidth(), pdfImage.getHeight());

        //Save to file
        doc.saveToFile("output/ConvertPdfWithSameSize.pdf");
    }
}

Java: Convert Images to PDF

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.

Published in Conversion

In the article of convert PDF to SVG by Spire.PDF, each page on the PDF file has been saved as a single SVG file. For example, if the PDF contains 10 pages, we will get 10 SVG files separately. From version 2.7.6, Spire.PDF for Java supports to convert a multipage PDF to one single SVG file in Java.

import com.spire.pdf.*;


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

        String inputPath = "Sample.pdf";

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

        document.getConvertOptions().setOutputToOneSvg(true);

        document.saveToFile("output.svg", FileFormat.SVG);
        document.close();
    }
}

Effective screenshot of the resulted one SVG file:

Java convert a multipage PDF to one SVG

Published in Conversion
Tuesday, 27 June 2023 08:26

Java: Convert PDF to XPS and XPS to PDF

PDF (Portable Document Format) and XPS (XML Paper Specification) are two commonly used document formats for sharing and printing documents. While PDF is widely known and supported, XPS is a Microsoft-developed format that has gained popularity due to its superior graphics rendering capabilities. In this article, we will demonstrate how to use Spire.PDF for Java to convert PDF to XPS and XPS to PDF in high quality.

Install Spire.PDF for Java

First of all, 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 PDF to XPS in Java

Spire.PDF for Java has a powerful conversion feature, which can convert PDF to XPS in just three steps. The detailed steps are as follows:

  • Create a PdfDocument instance.
  • Load a PDF sample document using PdfDocument.loadFromFile() method.
  • Save the document as XPS using PdfDocument.saveToFile() method.
  • Java
import com.spire.pdf.*;

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

        //Create a PdfDocument instance
        PdfDocument pdf = new PdfDocument();

        //Load the PDF file
       pdf.loadFromFile("C:\\Users\\Administrator\\Desktop\\Input.pdf.pdf");

        //Save to XPS
        pdf.saveToFile("ToXPS.xps", FileFormat.XPS);
        pdf.close();
    }
}

Java: Convert PDF to XPS and XPS to PDF

Convert XPS to PDF in Java

The PdfDocument.saveToFile() method provided by Spire.PDF for Java enables the conversion of a XPS file into a PDF document. The following are steps to convert XPS to PDF.

  • Create a PdfDocument instance.
  • Load a XPS file document using PdfDocument.loadFromFile() method.
  • Save the document as PDF using PdfDocument.saveToFile() method.
  • Java
import com.spire.pdf.*;

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

        //Create a PdfDocument instance
        PdfDocument pdf = new PdfDocument();

        //Load a XPS file
        pdf.loadFromXPS("C:\\Users\\Administrator\\Desktop\\sample.xps");

        //Save to PDF
        pdf.saveToFile("toPDF.pdf", FileFormat.PDF);
        pdf.close();
    }
}

Java: Convert PDF to XPS and XPS to PDF

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.

Published in Conversion
Friday, 10 June 2022 07:10

Java: Convert PDF to SVG

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.

Published in Conversion
Wednesday, 17 May 2023 07:07

Java: Convert PDF to HTML

PDF file format makes the presentation of documents consistent across devices. However, when you need to put PDF documents on web pages, it's better to convert them to HTML files. In this way, all the content of your document can be displayed in the browser directly, with no need for downloading files. And the loading of large PDF documents takes a long time, while HTML files can be rendered in the browser very quickly. In addition, compared to PDF files, it is much easier for search engines to crawl HTML web pages to get information, which will give your website more exposure. This article will show how to convert PDF documents into HTML files in Java using Spire.PDF for Java.

Install Spire.PDF for Java

First of all, 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 a PDF document to an HTML file in Java

The conversion from a PDF document to an HTML file can be directly done by loading a PDF document and saving it as an HTML file using PdfDocument.saveToFile(String filename, FileFormat.HTML) method provided by Spire.PDF for Java. The detailed steps are as follows.

  • Create an object of PdfDocument.
  • Load a PDF file using PdfDocument.loadFromFile() method.
  • Save the PDF file as an HTML file using PdfDocument.saveToFle() method.
  • Java
Java
import com.spire.pdf.*;

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

        //Create an object of PdfDocument
        PdfDocument pdf = new PdfDocument();

        //Load a PDF file
        pdf.loadFromFile("C:/Guide to a Foreign Past.pdf");

        //Save the PDF file as an HTML file
        pdf.saveToFile("PDFToHTML.html",FileFormat.HTML);
        pdf.close();
    }
}

Convert a PDF document to an HTML file with SVG Embedded

Spire.PDF for Java also provides the PdfDocument.getConvertOptions().setPdfToHtmlOptions(true) method to enable embedding SVG while converting. The detailed steps for converting a PDF file to an HTML file with SVG embedded are as follows.

  • Create an object of PdfDocument.
  • Load a PDF file using PdfDocument.loadFromFile() method.
  • Enable embedding SVG using PdfDocument.getConvertOptions().setPdfToHtmlOptions(true) method.
  • Save the PDF file as an HTML file using PdfDocument.saveToFle() method.
  • Java
import com.spire.pdf.*;

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

        //Create an object of PdfDocument
        PdfDocument doc = new PdfDocument();

        //Load a PDF file
        doc.loadFromFile("C:/Guide to a Foreign Past.pdf");

        //Set embedding SVG
        doc.getConvertOptions().setPdfToHtmlOptions(true);

        //Save the PDF file as an HTML file
        doc.saveToFile("PDFToHTMLEmbeddingSVG.html", FileFormat.HTML);
        doc.close();
    }
}

Convert a PDF document to HTML Stream in Java

Spire.PDF for Java also supports converting PDF documents to HTML stream. The detailed steps are as follows.

  • Create an object of PdfDocument.
  • Load a PDF file using PdfDocument.loadFromFile() method.
  • Save the PDF file as HTML stream using PdfDocument.saveToStream() method.
  • Java
import com.spire.pdf.*;

import java.io.*;


public class convertPDFToHTMLStream {
    public static void main(String[] args) throws FileNotFoundException {

        //Create an object of PdfDocument
        PdfDocument pdf = new PdfDocument();

        //Load a PDF file
        pdf.loadFromFile("C:/Guide to a Foreign Past.pdf");

        //Save the PDF file as HTML stream
        File outFile = new File("PDFToHTMLStream.html");
        OutputStream outputStream = new FileOutputStream(outFile);
        pdf.saveToStream(outputStream, FileFormat.HTML);
        pdf.close();
    }
}

Java: Convert PDF to HTML

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.

Published in Conversion
Thursday, 26 May 2022 06:31

Java: Convert PDF to Word

Nowadays, it is not difficult to convert PDF documents into Word files using a software. However, if you want to maintain the layout and even the font formatting while converting, it is not something that every software can accomplish. Spire.PDF for Java does it well and offers you the following two modes when converting PDF to Word in Java.

Fixed Layout mode has fast conversion speed and is conducive to maintaining the original appearance of PDF files to the greatest extent. However, the editability of the resulting document will be limited since each line of text in PDF will be presented in a separate frame in the generated Word document.

Flowable Structure is a full recognition mode. The converted content will not be presented in frames, and the structure of the resulting document is flowable. The generated Word document is easy to re-edit but may look different from the original PDF file.

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 PDF to Doc/Docx with Fixed Layout

The following are the steps to convert PDF to Doc or Docx with fixed layout.

  • Create a PdfDocument object.
  • Load a PDF file using PdfDocument.loadFromFile() method.
  • Convert the PDF document to a Doc or Docx format file using PdfDocument.saveToFile(String fileName, FileFormat fileFormat) method.
  • Java
import com.spire.pdf.FileFormat;
import com.spire.pdf.PdfDocument;

public class ConvertPdfToWordWithFixedLayout {

    public static void main(String[] args) {

        //Create a PdfDocument object
        PdfDocument doc = new PdfDocument();

        //Load a sample PDF document
        doc.loadFromFile("C:\\Users\\Administrator\\Desktop\\sample.pdf");

        //Convert PDF to Doc and save it to a specified path
        doc.saveToFile("output/ToDoc.doc", FileFormat.DOC);

        //Convert PDF to Docx and save it to a specified path
        doc.saveToFile("output/ToDocx.docx", FileFormat.DOCX);
        doc.close();
    }
}

Convert PDF to Doc/Docx with Flowable Structure

The following are the steps to convert PDF to Doc or Docx with flowable structure.

  • Create a PdfDocument object.
  • Load a PDF file using PdfDocument.loadFromFile() method.
  • Set the conversion mode as flow using PdfDocument. getConvertOptions().setConvertToWordUsingFlow() method.
  • Convert the PDF document to a Doc or Docx format file using PdfDocument.saveToFile(String fileName, FileFormat fileFormat) method.
  • Java
import com.spire.pdf.FileFormat;
import com.spire.pdf.PdfDocument;

public class ConvertPdfToWordWithFlowableStructure {

    public static void main(String[] args) {

        //Create a PdfDocument object
        PdfDocument doc = new PdfDocument();

        //Load a sample PDF document
        doc.loadFromFile("C:\\Users\\Administrator\\Desktop\\sample.pdf");

        //Convert PDF to Word with flowable structure
        doc.getConvertOptions().setConvertToWordUsingFlow(true);

        //Convert PDF to Doc
        doc.saveToFile("output/ToDoc.doc", FileFormat.DOC);

        //Convert PDF to Docx
        doc.saveToFile("output/ToDocx.docx", FileFormat.DOCX);
        doc.close();
    }
}

Java: Convert PDF to Word

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.

Published in Conversion
Wednesday, 29 December 2021 06:05

Java: Convert PDF to PDF/A

PDF/A is a kind of PDF format designed for archiving and long-term preservation of electronic documents. Unlike paper documents that are easily damaged or smeared, PDF/A format ensures that documents can be reproduced in exactly the same way even after long-term storage. This article will demonstrate how to convert PDF to PDF/A-1A, 2A, 3A, 1B, 2B and 3B compliant PDF using Spire.PDF for Java.

Install Spire.PDF for Java

First of all, 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 PDF to PDF/A

The detailed steps are as follows:

  • Create a PdfStandardsConverter instance, and pass in a sample PDF file as a parameter.
  • Convert the sample file to PdfA1A conformance level using PdfStandardsConverter.toPdfA1A() method.
  • Convert the sample file to PdfA1B conformance level using PdfStandardsConverter. toPdfA1B() method.
  • Convert the sample file to PdfA2A conformance level using PdfStandardsConverter. toPdfA2A() method.
  • Convert the sample file to PdfA2B conformance level using PdfStandardsConverter. toPdfA2B() method.
  • Convert the sample file to PdfA3A conformance level using PdfStandardsConverter. toPdfA3A() method.
  • Convert the sample file to PdfA3B conformance level using PdfStandardsConverter. toPdfA3B() method.
  • Java
import com.spire.pdf.conversion.PdfStandardsConverter;

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

        //Create a PdfStandardsConverter instance, and pass in a sample file as a parameter
        PdfStandardsConverter converter = new PdfStandardsConverter("sample.pdf");

        //Convert to PdfA1A
        converter.toPdfA1A("output/ToPdfA1A.pdf");

        //Convert to PdfA1B
        converter.toPdfA1B("output/ToPdfA1B.pdf");

        //Convert to PdfA2A
        converter.toPdfA2A( "output/ToPdfA2A.pdf");

        //Convert to PdfA2B
        converter.toPdfA2B("output/ToPdfA2B.pdf");

        //Convert to PdfA3A
        converter.toPdfA3A("output/ToPdfA3A.pdf");

        //Convert to PdfA3B
        converter.toPdfA3B("output/ToPdfA3B.pdf");
    }
}

Java: Convert PDF to PDF/A

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.

Published in Conversion
Page 2 of 3