Merge pull request #12965 from hmdrzsharifi/BAEL-5880

Bael-5880: Get Information about PDF in Java
This commit is contained in:
davidmartinezbarua 2022-11-14 16:10:16 -03:00 committed by GitHub
commit f250a5c104
6 changed files with 135 additions and 0 deletions

View File

@ -25,6 +25,16 @@
<artifactId>cleanup</artifactId>
<version>${itextpdf.cleanup.version}</version>
</dependency>
<dependency>
<groupId>com.itextpdf</groupId>
<artifactId>itextpdf</artifactId>
<version>5.5.13.3</version>
</dependency>
<dependency>
<groupId>org.apache.pdfbox</groupId>
<artifactId>pdfbox</artifactId>
<version>3.0.0-RC1</version>
</dependency>
</dependencies>
<build>

View File

@ -0,0 +1,31 @@
package com.baeldung.pdfinfo;
import com.itextpdf.text.pdf.PdfReader;
import java.io.IOException;
import java.util.Map;
public class PdfInfoIText {
public static int getNumberOfPages(final String pdfFile) throws IOException {
PdfReader reader = new PdfReader(pdfFile);
int pages = reader.getNumberOfPages();
reader.close();
return pages;
}
public static boolean isPasswordRequired(final String pdfFile) throws IOException {
PdfReader reader = new PdfReader(pdfFile);
boolean isEncrypted = reader.isEncrypted();
reader.close();
return isEncrypted;
}
public static Map<String, String> getInfo(final String pdfFile) throws IOException {
PdfReader reader = new PdfReader(pdfFile);
Map<String, String> info = reader.getInfo();
reader.close();
return info;
}
}

View File

@ -0,0 +1,36 @@
package com.baeldung.pdfinfo;
import org.apache.pdfbox.Loader;
import org.apache.pdfbox.pdmodel.PDDocument;
import org.apache.pdfbox.pdmodel.PDDocumentInformation;
import java.io.File;
import java.io.IOException;
public class PdfInfoPdfBox {
public static int getNumberOfPages(final String pdfFile) throws IOException {
File file = new File(pdfFile);
PDDocument document = Loader.loadPDF(file);
int pages = document.getNumberOfPages();
document.close();
return pages;
}
public static boolean isPasswordRequired(final String pdfFile) throws IOException {
File file = new File(pdfFile);
PDDocument document = Loader.loadPDF(file);
boolean isEncrypted = document.isEncrypted();
document.close();
return isEncrypted;
}
public static PDDocumentInformation getInfo(final String pdfFile) throws IOException {
File file = new File(pdfFile);
PDDocument document = Loader.loadPDF(file);
PDDocumentInformation info = document.getDocumentInformation();
document.close();
return info;
}
}

View File

@ -0,0 +1,29 @@
package com.baeldung.pdfinfo;
import org.junit.Assert;
import org.junit.Test;
import java.io.IOException;
import java.util.Map;
public class PdfInfoITextUnitTest {
private static final String PDF_FILE = "src/test/resources/input.pdf";
@Test
public void givenPdf_whenGetNumberOfPages_thenOK() throws IOException {
Assert.assertEquals(4, PdfInfoIText.getNumberOfPages(PDF_FILE));
}
@Test
public void givenPdf_whenIsPasswordRequired_thenOK() throws IOException {
Assert.assertFalse(PdfInfoIText.isPasswordRequired(PDF_FILE));
}
@Test
public void givenPdf_whenGetInfo_thenOK() throws IOException {
Map<String, String> info = PdfInfoIText.getInfo(PDF_FILE);
Assert.assertEquals("LibreOffice 4.2", info.get("Producer"));
Assert.assertEquals("Writer", info.get("Creator"));
}
}

View File

@ -0,0 +1,29 @@
package com.baeldung.pdfinfo;
import org.apache.pdfbox.pdmodel.PDDocumentInformation;
import org.junit.Assert;
import org.junit.Test;
import java.io.IOException;
public class PdfInfoPdfBoxUnitTest {
private static final String PDF_FILE = "src/test/resources/input.pdf";
@Test
public void givenPdf_whenGetNumberOfPages_thenOK() throws IOException {
Assert.assertEquals(4, PdfInfoPdfBox.getNumberOfPages(PDF_FILE));
}
@Test
public void givenPdf_whenIsPasswordRequired_thenOK() throws IOException {
Assert.assertFalse(PdfInfoPdfBox.isPasswordRequired(PDF_FILE));
}
@Test
public void givenPdf_whenGetInfo_thenOK() throws IOException {
PDDocumentInformation info = PdfInfoPdfBox.getInfo(PDF_FILE);
Assert.assertEquals("LibreOffice 4.2", info.getProducer());
Assert.assertEquals("Writer", info.getCreator());
}
}

Binary file not shown.