#BAEL-5880: add main source and test for iText
This commit is contained in:
parent
2ab80881c2
commit
d8a230c995
31
pdf-2/src/main/java/com/baeldung/pdfinfo/PdfInfoIText.java
Normal file
31
pdf-2/src/main/java/com/baeldung/pdfinfo/PdfInfoIText.java
Normal file
@ -0,0 +1,31 @@
|
||||
package com.baeldung.pdfinfo;
|
||||
|
||||
|
||||
import com.itextpdf.text.pdf.PdfReader;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.util.HashMap;
|
||||
|
||||
public class PdfInfoIText {
|
||||
|
||||
public static int getNumberOfPage(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 HashMap<String, String> getInfo(final String pdfFile) throws IOException {
|
||||
PdfReader reader = new PdfReader(pdfFile);
|
||||
HashMap<String, String> info = reader.getInfo();
|
||||
reader.close();
|
||||
return info;
|
||||
}
|
||||
}
|
@ -0,0 +1,50 @@
|
||||
package com.baeldung.pdfinfo;
|
||||
|
||||
import org.junit.Assert;
|
||||
import org.junit.Test;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.util.HashMap;
|
||||
|
||||
public class PdfInfoITextUnitTest {
|
||||
|
||||
private static final String PDF_FILE = "src/test/resources/input.pdf";
|
||||
|
||||
@Test
|
||||
public void givenPdf_whenGetNumberOfPage_thenOK() throws IOException {
|
||||
// given
|
||||
int expectedNumberOfPage = 4;
|
||||
|
||||
// when
|
||||
int actualNumberOfPage = PdfInfoIText.getNumberOfPage(PDF_FILE);
|
||||
|
||||
// then
|
||||
Assert.assertEquals(expectedNumberOfPage, actualNumberOfPage);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenPdf_whenIsPasswordRequired_thenOK() throws IOException {
|
||||
// given
|
||||
boolean expectedPasswordRequired = false;
|
||||
|
||||
// when
|
||||
boolean actualPasswordRequired = PdfInfoIText.isPasswordRequired(PDF_FILE);
|
||||
|
||||
// then
|
||||
Assert.assertEquals(expectedPasswordRequired, actualPasswordRequired);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenPdf_whenGetInfo_thenOK() throws IOException {
|
||||
// given
|
||||
String expectedProducer = "LibreOffice 4.2";
|
||||
String expectedCreator = "Writer";
|
||||
|
||||
// when
|
||||
HashMap<String, String> info = PdfInfoIText.getInfo(PDF_FILE);
|
||||
|
||||
// then
|
||||
Assert.assertEquals(expectedProducer, info.get("Producer"));
|
||||
Assert.assertEquals(expectedCreator, info.get("Creator"));
|
||||
}
|
||||
}
|
Loading…
x
Reference in New Issue
Block a user