Skip to content

BAEL-9267: Determine If a File is a PDF File in Java #18493

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
May 1, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 7 additions & 1 deletion text-processing-libraries-modules/pdf-2/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,11 @@
<artifactId>poi-ooxml</artifactId>
<version>${poi-ooxml.version}</version>
</dependency>
<dependency>
<groupId>org.apache.tika</groupId>
<artifactId>tika-core</artifactId>
<version>${tika.version}</version>
</dependency>
<dependency>
<groupId>org.apache.logging.log4j</groupId>
<artifactId>log4j-api</artifactId>
Expand Down Expand Up @@ -70,8 +75,9 @@
<itextpdf.version>5.5.13.3</itextpdf.version>
<itextpdf.core.version>7.2.3</itextpdf.core.version>
<itextpdf.cleanup.version>3.0.1</itextpdf.cleanup.version>
<pdfbox.version>3.0.0</pdfbox.version>
<pdfbox.version>3.0.4</pdfbox.version>
<poi-ooxml.version>5.2.5</poi-ooxml.version>
<tika.version>3.1.0</tika.version>
<log4j-api.version>2.20.0</log4j-api.version>
<log4j-core.version>2.20.0</log4j-core.version>
</properties>
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
package com.baeldung.detect;

import org.apache.pdfbox.Loader;
import org.apache.pdfbox.pdmodel.PDDocument;
import org.apache.tika.Tika;
import org.junit.jupiter.api.Test;

import static org.junit.jupiter.api.Assertions.assertTrue;

import java.io.*;
import java.util.Objects;

import com.itextpdf.commons.exceptions.ITextException;
import com.itextpdf.kernel.pdf.PdfDocument;
import com.itextpdf.kernel.pdf.PdfReader;


public class PdfDetectUnitTest {

private static final File PDF_FILE = new File("src/test/resources/input.pdf");

@Test
void whenDetectPdfByPdfBox_thenCorrect() {
boolean isPdf;
try (PDDocument document = Loader.loadPDF(PDF_FILE)) {
isPdf = true;
} catch (IOException ioe) {
isPdf = false;
}
assertTrue(isPdf);
}

@Test
void whenDetectPdfByItext_thenCorrect() {
boolean isPdf;
try (PdfDocument pdfDoc = new PdfDocument(new PdfReader(PDF_FILE))) {
isPdf = true;
} catch (ITextException | IOException e) {
isPdf = false;
}
assertTrue(isPdf);
}

@Test
void whenDetectPdfByFileSignature_thenCorrect() throws IOException {
boolean isPdf = false;
try (InputStream fis = new BufferedInputStream(new FileInputStream(PDF_FILE))) {
byte[] bytes = new byte[5];
if (fis.read(bytes) == 5) {
String header = new String(bytes);
isPdf = Objects.equals(header, "%PDF-");
}
}
assertTrue(isPdf);
}

@Test
void whenDetectPdfByTika_thenCorrect() throws IOException {
Tika tika = new Tika();
boolean isPdf = Objects.equals(tika.detect(PDF_FILE), "application/pdf");
assertTrue(isPdf);
}

}