Test in a live environment
Test in production without watermarks.
Works wherever you need it to.
If you're working on a Java project and need to generate PDF documents from HTML or extract text from a PDF, you should search for a reliable and efficient Java PDF Library. You may stumble upon IronPDF, which simplifies PDF generation and manipulation of PDF documents, making your life as a developer much more manageable. There are multiple PDF libraries in the market, like PDF Clown or iText library, but this article will dive into the world of IronPDF and explore how it can help you in your Java projects.
IronPDF is a Java Library that allows you to create, read, and edit PDF documents in Java applications. This versatile library supports generating PDFs from HTML, existing PDF files, and URLs. You can extract text from PDFs, making it a must-have tool for any Java developer working with PDF documents.
Maven is a popular build automation and dependency management tool for Java projects. To install IronPDF in your Maven project, you'll need to add the necessary dependencies to your pom.xml
file.
Here's how you can add IronPDF and its required logger dependency, SLF4J, to your Maven project:
pom.xml
file.dependencies
section. If it doesn't exist, create one.Add the following dependency entries for IronPDF and SLF4J:
<dependency>
<groupId>com.ironsoftware</groupId>
<artifactId>com.ironsoftware</artifactId>
<version>2024.9.1</version>
</dependency>
pom.xml
file.mvn install
command in your project's root directory. Maven will automatically download and add the IronPDF and SLF4J dependencies to your project.Now, you have successfully added IronPDF to your Maven project, and you can start using this powerful Java PDF Library to create, edit, and manipulate PDF documents in your Java applications. Don't forget to check the IronPDF documentation for more examples and detailed information about the library's features and capabilities. Let's explore a few functionalities here.
One of the most common tasks when working with PDFs is converting HTML to PDF. IronPDF makes this a breeze with just a few lines of code. Here's an example that demonstrates how to convert a simple HTML string into a PDF document:
import com.ironsoftware.ironpdf.*;
import java.io.IOException;
import java.nio.file.Paths;
// Apply your license key
License.setLicenseKey("YOUR-LICENSE-KEY");
// Set a log path
Settings.setLogPath(Paths.get("C:/tmp/IronPdfEngine.log"));
// Render the HTML as a PDF. Stored in myPdf as type PdfDocument;
PdfDocument myPdf = PdfDocument.renderHtmlAsPdf(" ~Hello World~ Made with IronPDF!");
// Save the PdfDocument to a file
myPdf.saveAs(Paths.get("html_saved.pdf"));
This code snippet imports the necessary IronPDF classes and applies a license key. Next, it sets a log path for debugging purposes. The renderHtmlAsPdf
method takes an HTML string as input and converts it to a PDF document, which you can then save to a file.
The output PDF file
IronPDF also supports converting HTML files to PDF documents. The process is quite similar to the previous example, with a minor change in the method used:
import com.ironsoftware.ironpdf.*;
import java.io.IOException;
import java.nio.file.Paths;
// Apply your license key
License.setLicenseKey("YOUR-LICENSE-KEY");
// Set a log path
Settings.setLogPath(Paths.get("C:/tmp/IronPdfEngine.log"));
// Render the HTML as a PDF. Stored in myPdf as type PdfDocument;
PdfDocument myPdf = PdfDocument.renderHtmlFileAsPdf("example.html");
// Save the PdfDocument to a file
myPdf.saveAs(Paths.get("html_file_saved.pdf"));
In this case, the renderHtmlFileAsPdf
method is used instead of renderHtmlAsPdf
, taking the path to the HTML file as input and creating PDF documents.
IronPDF is capable of converting web pages into PDF documents. This is particularly helpful when you want to generate a PDF report from a dynamic web page. The following code snippet demonstrates how to do this:
import com.ironsoftware.ironpdf.*;
import java.io.IOException;
import java.nio.file.Paths;
// Apply your license key
License.setLicenseKey("YOUR-LICENSE-KEY");
// Set a log path
Settings.setLogPath(Paths.get("C:/tmp/IronPdfEngine.log"));
// Render the HTML as a PDF. Stored in myPdf as type PdfDocument;
PdfDocument myPdf = PdfDocument.renderUrlAsPdf("https://ironpdf.com");
// Save the PdfDocument to a file
myPdf.saveAs(Paths.get("url.pdf"));
The renderUrlAsPdf
method takes a URL as input and creates PDF files from the web page's content. This method can handle complex pages, including those with JavaScript and CSS.
The output PDF file from a URL
In addition to creating PDFs, IronPDF also provides functionality for extracting text from existing PDF documents. This feature can be useful when you need to search, index, or analyze the content of a PDF file. The following code snippet shows how to extract text from a PDF document:
PdfDocument pdf = PdfDocument.renderUrlAsPdf("https://unsplash.com/");
String text = pdf.extractAllText();
System.out.println("Text extracted from the website: " + text);
In this example, a PDF is rendered from a URL and then uses the [extractAllText
](/java/object-reference/api/com/ironsoftware/ironpdf/PdfDocument.html#extractAllText()) method to obtain the text content of the PDF document. The extracted text can then be printed to the console or manipulated as needed.
IronPDF can also convert images to a PDF document. This can come in handy when you need to create a PDF portfolio or combine multiple images into a single file. The following code snippet demonstrates how to convert a set of images in a directory into a single PDF document:
import com.ironsoftware.ironpdf.*;
import java.io.IOException;
import java.nio.file.*;
import java.util.ArrayList;
import java.util.List;
Path imageDirectory = Paths.get("assets/images");
List imageFiles = new ArrayList();
try (DirectoryStream stream = Files.newDirectoryStream(imageDirectory, "*.{png,jpg}")) {
for (Object entry : stream) {
imageFiles.add(entry);
}
PdfDocument.fromImage(imageFiles).saveAs(Paths.get("assets/composite.pdf"));
} catch (IOException exception) {
throw new RuntimeException(String.format("Error converting images to PDF from directory: %s: %s",
imageDirectory,
exception.getMessage()),
exception);
}
With just this one code snippet, IronPDF makes it easy to convert multiple images into a single PDF document, further demonstrating the library's versatility and usefulness in various PDF-related tasks.
IronPDF is a powerful and versatile Java PDF Library that simplifies PDF generation and manipulation in Java applications. With its easy-to-use API, you can effortlessly convert HTML, HTML files, and URLs into PDF documents, as well as extract text from existing PDF files. By integrating IronPDF into your Java projects, you can streamline your workflow and create high-quality PDFs with ease.
Additionally, IronPDF is also capable of rendering charts in PDFs, watermarking, working with PDF forms and digital signatures programmatically.
IronPDF offers a free trial, giving you ample time to evaluate its features and determine if it's the right fit for your project. If you decide to continue using IronPDF after the trial period, licensing starts at $749.
9 .NET API products for your office documents