Untitled
unknown
plain_text
10 months ago
1.7 kB
4
Indexable
public String extractContent(String keyStart, String keyend){ String extractedText = ""; String filePath = "input/s3-userguide.pdf"; try { PDDocument document = PDDocument.load(new File(filePath)); // Kiểm tra mã hóa if (!document.isEncrypted()) { PDFTextStripper pdfStripper = new PDFTextStripper(); String text = pdfStripper.getText(document); // Tìm vị trí int start = text.indexOf(keyStart); int end = text.lastIndexOf(keyend); // Cắt đoạn văn bản if (start != -1 && end != -1 && start < end) { // Tìm end int nextLineStart = text.indexOf("\n", end); if (nextLineStart != -1) { // Lấy vị trí bắt đầu của dòng tiếp theo int nextLineIndex = nextLineStart + 1; // Cắt text extractedText = text.substring(start, nextLineIndex); // System.out.println(extractedText); } else { System.out.println("Cannot find row after last."); } } else { System.out.println("Cannot find Start line or end line index"); } } else { System.out.println("Cannot read document."); } document.close(); } catch (IOException e) { e.printStackTrace(); } if (!extractedText.isEmpty()){ return extractedText; } return null; }
Editor is loading...
Leave a Comment