diff --git a/src/main/java/Ellithium/Utilities/helpers/PDFHelper.java b/src/main/java/Ellithium/Utilities/helpers/PDFHelper.java index 9332d5f..ef6d1b0 100644 --- a/src/main/java/Ellithium/Utilities/helpers/PDFHelper.java +++ b/src/main/java/Ellithium/Utilities/helpers/PDFHelper.java @@ -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; @@ -35,7 +36,6 @@ public static void writePdf(String filePath, List 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 @@ -48,7 +48,6 @@ public static void writePdf(String filePath, List content) { contentStream.endText(); } - document.save(file); Reporter.log("Successfully wrote content to PDF file: ", LogLevel.INFO_GREEN,filePath); } catch (IOException e) { @@ -60,11 +59,9 @@ public static void writePdf(String filePath, List content) { // Method to append text to an existing PDF file public static void appendToPdf(String filePath, List 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); @@ -82,4 +79,49 @@ public static void appendToPdf(String filePath, List content) { Reporter.log("Root Cause: ",LogLevel.ERROR,e.getCause().toString()); } } + // Method to merge multiple PDF files + public static void mergePdfs(List 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; + } + } + } \ No newline at end of file diff --git a/src/main/java/Ellithium/Utilities/helpers/TextHelper.java b/src/main/java/Ellithium/Utilities/helpers/TextHelper.java index 743e085..bb195bf 100644 --- a/src/main/java/Ellithium/Utilities/helpers/TextHelper.java +++ b/src/main/java/Ellithium/Utilities/helpers/TextHelper.java @@ -45,7 +45,6 @@ public static void writeTextFile(String filePath, List 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); @@ -103,9 +102,7 @@ public static String findLineWithKeyword(String filePath, String keyword) { public static void deleteLine(String filePath, String lineToDelete) { File textFile = new File(filePath + ".txt"); List 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) { @@ -113,7 +110,6 @@ public static void deleteLine(String filePath, String lineToDelete) { updatedLines.add(line); } } - writeTextFile(filePath, updatedLines); Reporter.log("Successfully deleted line from text file: ", LogLevel.INFO_GREEN, filePath);