Skip to content

Commit

Permalink
update
Browse files Browse the repository at this point in the history
  • Loading branch information
Abdelrhman-Ellithy committed Dec 2, 2024
1 parent 1045e6f commit c871507
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 8 deletions.
50 changes: 46 additions & 4 deletions src/main/java/Ellithium/Utilities/helpers/PDFHelper.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import Ellithium.core.logging.LogLevel;
import Ellithium.core.reporting.Reporter;
import org.apache.pdfbox.multipdf.PDFMergerUtility;
import org.apache.pdfbox.pdmodel.PDDocument;
import org.apache.pdfbox.pdmodel.PDPage;
import org.apache.pdfbox.pdmodel.PDPageContentStream;
Expand Down Expand Up @@ -35,7 +36,6 @@ public static void writePdf(String filePath, List<String> content) {
try (PDDocument document = new PDDocument()) {
PDPage page = new PDPage();
document.addPage(page);

try (PDPageContentStream contentStream = new PDPageContentStream(document, page)) {
contentStream.beginText();
contentStream.setFont(PDType1Font.HELVETICA, 12); // Set font
Expand All @@ -48,7 +48,6 @@ public static void writePdf(String filePath, List<String> content) {

contentStream.endText();
}

document.save(file);
Reporter.log("Successfully wrote content to PDF file: ", LogLevel.INFO_GREEN,filePath);
} catch (IOException e) {
Expand All @@ -60,11 +59,9 @@ public static void writePdf(String filePath, List<String> content) {
// Method to append text to an existing PDF file
public static void appendToPdf(String filePath, List<String> content) {
File file = new File(filePath + ".pdf");

try (PDDocument document = PDDocument.load(file)) {
PDPage page = document.getPage(document.getNumberOfPages() - 1); // Get last page
PDPageContentStream contentStream = new PDPageContentStream(document, page, PDPageContentStream.AppendMode.APPEND, true);

contentStream.beginText();
contentStream.setFont(PDType1Font.HELVETICA, 12); // Set font
contentStream.newLineAtOffset(100, 700);
Expand All @@ -82,4 +79,49 @@ public static void appendToPdf(String filePath, List<String> content) {
Reporter.log("Root Cause: ",LogLevel.ERROR,e.getCause().toString());
}
}
// Method to merge multiple PDF files
public static void mergePdfs(List<String> inputFilePaths, String outputFilePath) {
PDFMergerUtility merger = new PDFMergerUtility();
merger.setDestinationFileName(outputFilePath + ".pdf");
try {
for (String path : inputFilePaths) {
merger.addSource(new File(path + ".pdf"));
}
merger.mergeDocuments(null);
Reporter.log("Successfully merged PDF files into: ", LogLevel.INFO_GREEN, outputFilePath);
} catch (IOException e) {
Reporter.log("Failed to merge PDF files: ", LogLevel.ERROR, outputFilePath);
Reporter.log("Root Cause: ", LogLevel.ERROR, e.getMessage());
}
}
// Method to extract a specific page from a PDF
public static void extractPdfPage(String inputFilePath, String outputFilePath, int pageIndex) {
File file = new File(inputFilePath + ".pdf");

try (PDDocument document = PDDocument.load(file);
PDDocument outputDocument = new PDDocument()) {
PDPage page = document.getPage(pageIndex);
outputDocument.addPage(page);
outputDocument.save(outputFilePath + ".pdf");
Reporter.log("Successfully extracted page " + pageIndex + " to: ", LogLevel.INFO_GREEN, outputFilePath);
} catch (IOException e) {
Reporter.log("Failed to extract page from PDF file: ", LogLevel.ERROR, inputFilePath);
Reporter.log("Root Cause: ", LogLevel.ERROR, e.getMessage());
}
}
public static boolean comparePdfFiles(String filePath1, String filePath2) {
try (PDDocument doc1 = PDDocument.load(new File(filePath1 + ".pdf"));
PDDocument doc2 = PDDocument.load(new File(filePath2 + ".pdf"))) {
String text1 = new PDFTextStripper().getText(doc1);
String text2 = new PDFTextStripper().getText(doc2);
boolean isEqual = text1.equals(text2);
Reporter.log("Comparison result for PDF files: ", LogLevel.INFO_GREEN, String.valueOf(isEqual));
return isEqual;
} catch (IOException e) {
Reporter.log("Failed to compare PDF files: ", LogLevel.ERROR, filePath1 + " & " + filePath2);
Reporter.log("Root Cause: ", LogLevel.ERROR, e.getMessage());
return false;
}
}

}
4 changes: 0 additions & 4 deletions src/main/java/Ellithium/Utilities/helpers/TextHelper.java
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,6 @@ public static void writeTextFile(String filePath, List<String> lines) {
Reporter.log("Root Cause: ", LogLevel.ERROR, e.getCause().toString());
}
}

try (BufferedWriter writer = new BufferedWriter(new FileWriter(textFile))) {
for (String line : lines) {
writer.write(line);
Expand Down Expand Up @@ -103,17 +102,14 @@ public static String findLineWithKeyword(String filePath, String keyword) {
public static void deleteLine(String filePath, String lineToDelete) {
File textFile = new File(filePath + ".txt");
List<String> updatedLines = new ArrayList<>();

Reporter.log("Attempting to delete line from text file: ", LogLevel.INFO_BLUE, filePath);

try (BufferedReader reader = new BufferedReader(new FileReader(textFile))) {
String line;
while ((line = reader.readLine()) != null) {
if (!line.equals(lineToDelete)) {
updatedLines.add(line);
}
}

writeTextFile(filePath, updatedLines);
Reporter.log("Successfully deleted line from text file: ", LogLevel.INFO_GREEN, filePath);

Expand Down

0 comments on commit c871507

Please sign in to comment.