Zum Fußzeileninhalt springen
PRODUKTVERGLEICHE

Wie man PDF in Bild mit Itextsharp konvertiert

In today's increasingly digitized world, the Portable Document Format (PDF) has become a ubiquitous file format for sharing and preserving digital documents. However, there are instances where converting PDFs to images becomes necessary, unlocking a myriad of possibilities for users. Converting PDF to image format provides a versatile solution, enabling seamless integration of documents into presentations, web pages, or social media platforms. In this era of visual communication, the ability to transform PDFs into images offers enhanced accessibility and opens up new avenues for creativity and convenience. This article explores the significance of converting PDFs to images using Java and tools available to accomplish this task efficiently.

For this purpose, we will use and compare two Java PDF libraries named as follows:

  1. iTextSharp (iText7)
  2. IronPDF

How to Convert PDF file to Image Using iTextSharp

  1. To convert PDF files to images using iTextSharp (iText7), first set up the environment.
  2. Load the existing PDF files using the renderPdf object.
  3. Set the rendering properties using the PdfRenderImageType method on the PDF file.
  4. Instantiate the conversion of the PDF document using the PdfToImageRenderer.
  5. Save the images to the specified path using the OUTPUT_DIRECTORY.

1. IronPDF for Java

IronPDF for Java opens the door to powerful PDF manipulation and generation capabilities within the Java programming ecosystem. As businesses and developers seek efficient solutions to handle PDF-related tasks programmatically, IronPDF emerges as a reliable and feature-rich library. Whether it's converting HTML content to PDF, merging, splitting, or editing existing PDF documents, IronPDF equips Java developers with a robust set of tools to streamline their workflow. With its easy integration and extensive documentation, this library empowers Java applications to seamlessly interact with PDFs, offering a comprehensive solution for all PDF-related requirements. In this article, we will explore the key features and benefits of IronPDF for Java and illustrate how it simplifies the PDF handling process in Java applications.

2. iTextSharp for Java (iText7)

iTextSharp for Java (iText7), a powerful and versatile PDF library, equips developers with the ability to create, modify, and manipulate PDF documents programmatically. Originally developed for .NET, iTextSharp (iText7) has been adapted for Java, providing a seamless and efficient solution for all PDF-related tasks within the Java ecosystem. With its extensive functionality and easy-to-use API, iText7 enables Java developers to generate dynamic PDFs, add content, insert images, and extract data from existing PDFs effortlessly. Whether it's creating invoices, generating reports, or integrating PDF processing into enterprise applications, iText7 is a valuable tool that empowers developers to take full control of their PDF handling requirements. In this article, we will explore the essential features and benefits of iTextSharp for Java (iText7) and demonstrate its capabilities through practical examples.

3. Install IronPDF Java Library

To integrate IronPDF and its required logger dependency, SLF4J, into your Maven project, follow these steps:

  1. Open your project's pom.xml file.
  2. Navigate to the dependencies section. If it is not already present, create one.
  3. Include the following dependency entries for IronPDF and SLF4J:

    <dependency>
        <groupId>com.ironsoftware</groupId>
        <artifactId>com.ironsoftware</artifactId>
        <version>2023.7.2</version>
    </dependency>
    <dependency>
        <groupId>org.slf4j</groupId>
        <artifactId>slf4j-simple</artifactId>
        <version>2.0.3</version>
    </dependency>
    <dependency>
        <groupId>com.ironsoftware</groupId>
        <artifactId>com.ironsoftware</artifactId>
        <version>2023.7.2</version>
    </dependency>
    <dependency>
        <groupId>org.slf4j</groupId>
        <artifactId>slf4j-simple</artifactId>
        <version>2.0.3</version>
    </dependency>
    XML
  4. Save the pom.xml file.

    How to Convert PDF to Image Using iTextSharp: Figure 1

That's all! Just press the button shown above to include these dependencies in your project.

4. Install iText7 Java Library

To install iText7, follow the steps below to add the dependency:

  1. Open the pom.xml file.
  2. Find the dependencies tags. If they do not exist, create them and place the following code between these tags:

    <!-- https://mvnrepository.com/artifact/com.itextpdf/itext7-core -->
    <dependency>
        <groupId>com.itextpdf</groupId>
        <artifactId>itext7-core</artifactId>
        <version>8.0.0</version>
        <type>pom</type>
    </dependency>
    <!-- https://mvnrepository.com/artifact/com.itextpdf/itext7-core -->
    <dependency>
        <groupId>com.itextpdf</groupId>
        <artifactId>itext7-core</artifactId>
        <version>8.0.0</version>
        <type>pom</type>
    </dependency>
    XML
  3. Click the small button that appears on the top right of the screen.

Just like that, your dependencies are installed.

5. Convert PDF documents to images using IronPDF

Extracting images from PDF pages using IronPDF is easier than you thought with just a few lines of code. IronPDF offers compatibility with many image file types, such as JPEG and PNG.

In this section, we will go through the sample code to convert a PDF file to images using IronPDF for Java.

import com.ironsoftware.ironpdf.PdfDocument;
import javax.imageio.ImageIO;
import java.awt.image.BufferedImage;
import java.nio.file.Paths;
import java.util.List;
import java.io.File;

public class Main {
    public static void main(String[] args) throws Exception {
        // Load the PDF document
        PdfDocument pdf = PdfDocument.fromFile(Paths.get("composite.pdf"));

        // Extract all images from the PDF document into a list
        List<BufferedImage> images = pdf.extractAllImages();

        int i = 1; // Image counter
        // Traverse the extracted images list and save each image
        for (BufferedImage extractedImage : images) {
            String fileName = "assets/extracted_" + i++ + ".png";
            ImageIO.write(extractedImage, "PNG", new File(fileName)); // Save image as PNG
        }
    }
}
import com.ironsoftware.ironpdf.PdfDocument;
import javax.imageio.ImageIO;
import java.awt.image.BufferedImage;
import java.nio.file.Paths;
import java.util.List;
import java.io.File;

public class Main {
    public static void main(String[] args) throws Exception {
        // Load the PDF document
        PdfDocument pdf = PdfDocument.fromFile(Paths.get("composite.pdf"));

        // Extract all images from the PDF document into a list
        List<BufferedImage> images = pdf.extractAllImages();

        int i = 1; // Image counter
        // Traverse the extracted images list and save each image
        for (BufferedImage extractedImage : images) {
            String fileName = "assets/extracted_" + i++ + ".png";
            ImageIO.write(extractedImage, "PNG", new File(fileName)); // Save image as PNG
        }
    }
}
JAVA

The above code first opens the PDF file using the PdfDocument.fromFile() method, which takes the file path as a parameter. Then it uses the extractAllImages() method to extract all images from the PDF document and save them in a list named images. Then, it loops through the images and saves each image using the ImageIO.write() method, which takes the image, file type ("PNG"), and the path + name as parameters.

Output Directory Screenshot

How to Convert PDF to Image Using iTextSharp: Figure 2

6. Convert PDF Files to Images using iText7

In this section, we will see how you can extract images from PDF using the iText7 Java PDF Library. Here is an example code for the iText7 PDF to image extraction.

import com.itextpdf.pdfrender.PdfRenderImageType;
import com.itextpdf.pdfrender.PdfToImageRenderer;
import com.itextpdf.pdfrender.RenderingProperties;

import java.io.File;
import java.io.IOException;

public class PdfRender_Demo {

    private static final String ORIG = "/uploads/input.pdf";
    private static final String OUTPUT_DIRECTORY = "/myfiles/";

    public static void main(String[] args) throws IOException {
        // Set rendering properties for the PDF to image conversion
        final RenderingProperties properties = new RenderingProperties();
        properties.setImageType(PdfRenderImageType.JPEG); // Set image type to JPEG
        properties.setScaling(1.0f); // Maintain original size

        // Perform rendering from PDF to Image format
        PdfToImageRenderer.renderPdf(
            new File(ORIG),
            new File(OUTPUT_DIRECTORY),
            "/customfilename-%d",
            properties
        );
    }
}
import com.itextpdf.pdfrender.PdfRenderImageType;
import com.itextpdf.pdfrender.PdfToImageRenderer;
import com.itextpdf.pdfrender.RenderingProperties;

import java.io.File;
import java.io.IOException;

public class PdfRender_Demo {

    private static final String ORIG = "/uploads/input.pdf";
    private static final String OUTPUT_DIRECTORY = "/myfiles/";

    public static void main(String[] args) throws IOException {
        // Set rendering properties for the PDF to image conversion
        final RenderingProperties properties = new RenderingProperties();
        properties.setImageType(PdfRenderImageType.JPEG); // Set image type to JPEG
        properties.setScaling(1.0f); // Maintain original size

        // Perform rendering from PDF to Image format
        PdfToImageRenderer.renderPdf(
            new File(ORIG),
            new File(OUTPUT_DIRECTORY),
            "/customfilename-%d",
            properties
        );
    }
}
JAVA

When working with iText7, it was observed that iText7 is slow in speed and cannot easily process large files.

Output

How to Convert PDF to Image Using iTextSharp: Figure 3

7. Conclusion

In today's digitized world, the ability to convert PDFs to images offers diverse possibilities for seamless integration of documents into presentations, web pages, or social media platforms, enhancing accessibility and creativity. Both iTextSharp for Java (iText7) and IronPDF for Java present valuable solutions for this task.

iTextSharp empowers developers with a powerful and versatile PDF library, enabling the creation, modification, and manipulation of PDF documents programmatically. However, it may face challenges with large files and slower processing speed.

In contrast, the IronPDF for Java page offers a feature-rich and efficient library, providing developers with tools for handling PDF-related tasks programmatically, including extracting images, merging, splitting, and editing PDF documents. IronPDF for PDF to Image Conversion clearly stands victorious in this comparison.

For a complete tutorial on extracting images from PDF using Java, visit the following comprehensive guide on extracting images using IronPDF for Java. The complete comparison is available at this full comparison of PDF to image libraries.

iText7 Pricing Information starts at $0.15 per PDF. As for IronPDF, it offers a lifetime license starting from $liteLicense for a single-time purchase and also provides a free trial license for IronPDF. For more information, visit the IronPDF Licensing Information.

Hinweis:iTextSharp (iText7) is a registered trademark of its respective owner. This site is not affiliated with, endorsed by, or sponsored by iTextSharp (iText7). All product names, logos, and brands are property of their respective owners. Comparisons are for informational purposes only and reflect publicly available information at the time of writing.

Häufig gestellte Fragen

Warum ist das Umwandeln von PDFs in Bilder wichtig?

Das Umwandeln von PDFs in Bilder ist wichtig, weil es die vielseitige Integration von Dokumenten in Präsentationen, Webseiten und soziale Medienplattformen ermöglicht, was die Zugänglichkeit und Kreativität in der visuellen Kommunikation verbessert.

Was sind die Vorteile der Nutzung einer spezifischen Java-Bibliothek zur PDF-Bearbeitung?

IronPDF für Java bietet leistungsstarke PDF-Bearbeitungsfähigkeiten, einschließlich der Umwandlung von HTML in PDF, Zusammenführung, Aufteilung und Bearbeitung von Dokumenten. Es ist funktionsreich, leicht zu integrieren und bietet umfassende Dokumentation.

Wie unterscheidet sich eine Java-PDF-Bibliothek von einer anderen?

iTextSharp (iText7) bietet eine robuste Bibliothek zur programmatischen Erstellung und Bearbeitung von PDFs, kann jedoch Probleme mit großen Dateien und langsamer Verarbeitung haben. IronPDF hingegen ist effizient und bietet umfassende Werkzeuge für PDF-bezogene Aufgaben.

Welche Schritte sind notwendig, um ein PDF mit einer spezifischen PDF-Bibliothek in ein Bild umzuwandeln?

Um ein PDF mit IronPDF in ein Bild umzuwandeln, laden Sie das PDF-Dokument hoch, spezifizieren Sie das gewünschte Bildformat (z.B. JPEG oder PNG) und verwenden Sie die von IronPDF bereitgestellten Bildumwandlungsmethoden, um die Ausgabebilder zu speichern.

Wie kann ich eine Java-Bibliothek zur PDF-Bearbeitung installieren?

Um IronPDF zu installieren, fügen Sie die Abhängigkeiten von IronPDF und SLF4J zu Ihrer Maven-Projektdatei pom.xml hinzu und speichern Sie die Datei, um die Bibliothek zu integrieren.

Wie ist der Prozess zum Extrahieren von Bildern mit einer spezifischen PDF-Bibliothek?

Mit IronPDF laden Sie das PDF-Dokument, verwenden Sie die Methode extractAllImages, um Bilder in eine Liste zu bekommen, und speichern Sie jedes Bild mit ImageIO.write.

Was sind die Vorteile der Nutzung einer spezifischen Java-Bibliothek zur PDF-Bearbeitung?

iTextSharp (iText7) ist vielseitig, ermöglicht Entwicklern das Erstellen, Modifizieren und Bearbeiten von PDF-Dokumenten programmatisch. Es eignet sich zum Generieren dynamischer PDFs, Hinzufügen von Inhalten und Extrahieren von Daten.

Gibt es Preisinformationen zu Java-PDF-Bibliotheken?

iText7-Preise beginnen bei $0.15 pro PDF. IronPDF bietet eine lebenslange Lizenz ab $liteLicense und eine kostenlose Testlizenz.

Welche Schritte sind notwendig, um PDF-Dateien mit einer spezifischen PDF-Bibliothek in Bilder umzuwandeln?

Um PDFs mit IronPDF in Bilder umzuwandeln, initialisieren Sie das Dokument, wählen Sie das Ausgabeformat für das Bild und verarbeiten Sie die PDF-Seiten, um Bilder zu generieren, die an Ihrem gewünschten Ort gespeichert werden können.

Wo finde ich eine umfassende Anleitung zum Extrahieren von Bildern mit einer spezifischen Java-Bibliothek?

Eine umfassende Anleitung zum Extrahieren von Bildern mit IronPDF für Java ist auf der IronPDF-Website verfügbar und bietet detaillierte Anweisungen und Beispielcode.

Wie gehe ich mit großen PDF-Dateien um, wenn ich sie in Bilder umwandle?

IronPDF verarbeitet große PDF-Dateien effizient durch optimiertes Speicher-Management und Umwandlungsalgorithmen und sorgt so für schnelle und zuverlässige PDF-zu-Bild-Transformationen.

In welche Formate kann ich PDF-Seiten mit einer PDF-Bibliothek umwandeln?

IronPDF unterstützt die Umwandlung von PDF-Seiten in verschiedene Bildformate wie JPEG und PNG, was Flexibilität auf Basis Ihrer spezifischen Projektanforderungen bietet.

Curtis Chau
Technischer Autor

Curtis Chau hat einen Bachelor-Abschluss in Informatik von der Carleton University und ist spezialisiert auf Frontend-Entwicklung mit Expertise in Node.js, TypeScript, JavaScript und React. Leidenschaftlich widmet er sich der Erstellung intuitiver und ästhetisch ansprechender Benutzerschnittstellen und arbeitet gerne mit modernen Frameworks sowie der Erstellung gut strukturierter, optisch ansprechender ...

Weiterlesen