Skip to content

Instantly share code, notes, and snippets.

@naelabdeljawad
Last active January 29, 2021 15:50
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save naelabdeljawad/ae402d94b3b5be571bdb99a1790f6358 to your computer and use it in GitHub Desktop.
Save naelabdeljawad/ae402d94b3b5be571bdb99a1790f6358 to your computer and use it in GitHub Desktop.
PDF Files Utilities
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.List;
import com.itextpdf.text.Document;
import com.itextpdf.text.DocumentException;
import com.itextpdf.text.Element;
import com.itextpdf.text.Font;
import com.itextpdf.text.Paragraph;
import com.itextpdf.text.pdf.PdfReader;
import com.itextpdf.text.pdf.PdfWriter;
import com.itextpdf.text.pdf.parser.PdfTextExtractor;
public class PDFUtils {
//Please add itextpdf dependency
// <dependency>
// <groupId>com.itextpdf</groupId>
// <artifactId>itextpdf</artifactId>
// <version>version</version>
// </dependency>
private static void writeUsingIText(String PDFpath) {
Document document = new Document();
try {
PdfWriter.getInstance(document, new FileOutputStream(new File(PDFpath)));
// open
document.open();
Paragraph p = new Paragraph();
p.add("This is my paragraph 1");
p.setAlignment(Element.ALIGN_CENTER);
document.add(p);
Paragraph p2 = new Paragraph();
p2.add("This is my paragraph 2"); // no alignment
document.add(p2);
Font f = new Font();
f.setStyle(Font.BOLD);
f.setSize(8);
document.add(new Paragraph("This is my paragraph 3", f));
// close
document.close();
System.out.println("Done");
} catch (FileNotFoundException | DocumentException e) {
e.printStackTrace();
} catch (@SuppressWarnings("hiding") IOException e) {
e.printStackTrace();
}
}
private static PdfReader loadPDF(String PDFpath) {
PdfReader reader = null;
try {
reader = new PdfReader(PDFpath);
} catch (IOException e) {
e.printStackTrace();
}
return reader;
}
public static boolean isTextDisplayedInFile(String PDFpath, String singleText) {
int pages = 0;
String textFromPage = null;
PdfReader reader = loadPDF(PDFpath);
pages = reader.getNumberOfPages();
try {
for (int i = 1; i < pages; i++) {
textFromPage = PdfTextExtractor.getTextFromPage(reader, 1);
if (textFromPage.contains(singleText)) {
System.out.println(textFromPage);
return true;
}
}
reader.close();
return true;
} catch (IOException e) {
e.printStackTrace();
}
return false;
}
public static boolean isTextDisplayedInFile(String PDFpath, List<String> texts) {
int pages = 0;
String textFromPage = null;
PdfReader reader = loadPDF(PDFpath);
pages = reader.getNumberOfPages();
try {
for (int firstLoop = 0; firstLoop < texts.size(); firstLoop++) {
for (int secondLoop = 1; secondLoop < pages; secondLoop++) {
textFromPage = PdfTextExtractor.getTextFromPage(reader, 1);
String[] strArray = texts.get(firstLoop).split("[\n\\s\\@&.,/:?$+-]+");
for (int i = 0; i < strArray.length; i++) {
if (!textFromPage.contains(strArray[i])) {
System.out.println(textFromPage);
return false;
}
System.out.println(strArray[i]);
}
}
}
reader.close();
return true;
} catch (IOException e) {
System.err.println(e.getMessage() + "Not all texts displayed in the file!");
}
return true;
}
public static void main(String[] args) {
isTextDisplayedInFile(
"path//fileName.pdf",
"");
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment