mirror of
https://github.com/Stirling-Tools/Stirling-PDF.git
synced 2025-09-24 12:36:13 +00:00
Compare commits
1 Commits
394ecfb08c
...
2c0677cbf4
Author | SHA1 | Date | |
---|---|---|---|
![]() |
2c0677cbf4 |
@ -552,10 +552,10 @@ public class PdfUtils {
|
|||||||
public boolean containsTextInFile(PDDocument pdfDocument, String text, String pagesToCheck)
|
public boolean containsTextInFile(PDDocument pdfDocument, String text, String pagesToCheck)
|
||||||
throws IOException {
|
throws IOException {
|
||||||
PDFTextStripper textStripper = new PDFTextStripper();
|
PDFTextStripper textStripper = new PDFTextStripper();
|
||||||
StringBuilder pdfText = new StringBuilder();
|
String pdfText = "";
|
||||||
|
|
||||||
if (pagesToCheck == null || "all".equals(pagesToCheck)) {
|
if (pagesToCheck == null || "all".equals(pagesToCheck)) {
|
||||||
pdfText = new StringBuilder(textStripper.getText(pdfDocument));
|
pdfText = textStripper.getText(pdfDocument);
|
||||||
} else {
|
} else {
|
||||||
// remove whitespaces
|
// remove whitespaces
|
||||||
pagesToCheck = pagesToCheck.replaceAll("\\s+", "");
|
pagesToCheck = pagesToCheck.replaceAll("\\s+", "");
|
||||||
@ -571,21 +571,21 @@ public class PdfUtils {
|
|||||||
for (int i = startPage; i <= endPage; i++) {
|
for (int i = startPage; i <= endPage; i++) {
|
||||||
textStripper.setStartPage(i);
|
textStripper.setStartPage(i);
|
||||||
textStripper.setEndPage(i);
|
textStripper.setEndPage(i);
|
||||||
pdfText.append(textStripper.getText(pdfDocument));
|
pdfText += textStripper.getText(pdfDocument);
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
// Handle individual page
|
// Handle individual page
|
||||||
int page = Integer.parseInt(splitPoint);
|
int page = Integer.parseInt(splitPoint);
|
||||||
textStripper.setStartPage(page);
|
textStripper.setStartPage(page);
|
||||||
textStripper.setEndPage(page);
|
textStripper.setEndPage(page);
|
||||||
pdfText.append(textStripper.getText(pdfDocument));
|
pdfText += textStripper.getText(pdfDocument);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
pdfDocument.close();
|
pdfDocument.close();
|
||||||
|
|
||||||
return pdfText.toString().contains(text);
|
return pdfText.contains(text);
|
||||||
}
|
}
|
||||||
|
|
||||||
public boolean pageCount(PDDocument pdfDocument, int pageCount, String comparator)
|
public boolean pageCount(PDDocument pdfDocument, int pageCount, String comparator)
|
||||||
|
@ -94,14 +94,14 @@ public class AutoRenameController {
|
|||||||
// Merge lines with same font size
|
// Merge lines with same font size
|
||||||
List<LineInfo> mergedLineInfos = new ArrayList<>();
|
List<LineInfo> mergedLineInfos = new ArrayList<>();
|
||||||
for (int i = 0; i < lineInfos.size(); i++) {
|
for (int i = 0; i < lineInfos.size(); i++) {
|
||||||
StringBuilder mergedText = new StringBuilder(lineInfos.get(i).text);
|
String mergedText = lineInfos.get(i).text;
|
||||||
float fontSize = lineInfos.get(i).fontSize;
|
float fontSize = lineInfos.get(i).fontSize;
|
||||||
while (i + 1 < lineInfos.size()
|
while (i + 1 < lineInfos.size()
|
||||||
&& lineInfos.get(i + 1).fontSize == fontSize) {
|
&& lineInfos.get(i + 1).fontSize == fontSize) {
|
||||||
mergedText.append(" ").append(lineInfos.get(i + 1).text);
|
mergedText += " " + lineInfos.get(i + 1).text;
|
||||||
i++;
|
i++;
|
||||||
}
|
}
|
||||||
mergedLineInfos.add(new LineInfo(mergedText.toString(), fontSize));
|
mergedLineInfos.add(new LineInfo(mergedText, fontSize));
|
||||||
}
|
}
|
||||||
|
|
||||||
// Sort lines by font size in descending order and get the first one
|
// Sort lines by font size in descending order and get the first one
|
||||||
|
@ -38,8 +38,7 @@ public class ShowJavascript {
|
|||||||
description = "desc. Input:PDF Output:JS Type:SISO")
|
description = "desc. Input:PDF Output:JS Type:SISO")
|
||||||
public ResponseEntity<byte[]> extractHeader(@ModelAttribute PDFFile file) throws Exception {
|
public ResponseEntity<byte[]> extractHeader(@ModelAttribute PDFFile file) throws Exception {
|
||||||
MultipartFile inputFile = file.getFileInput();
|
MultipartFile inputFile = file.getFileInput();
|
||||||
StringBuilder script = new StringBuilder();
|
String script = "";
|
||||||
boolean foundScript = false;
|
|
||||||
|
|
||||||
try (PDDocument document = pdfDocumentFactory.load(inputFile)) {
|
try (PDDocument document = pdfDocumentFactory.load(inputFile)) {
|
||||||
|
|
||||||
@ -56,31 +55,28 @@ public class ShowJavascript {
|
|||||||
PDActionJavaScript jsAction = entry.getValue();
|
PDActionJavaScript jsAction = entry.getValue();
|
||||||
String jsCodeStr = jsAction.getAction();
|
String jsCodeStr = jsAction.getAction();
|
||||||
|
|
||||||
if (jsCodeStr != null && !jsCodeStr.trim().isEmpty()) {
|
script +=
|
||||||
script.append("// File: ")
|
"// File: "
|
||||||
.append(
|
+ Filenames.toSimpleFileName(
|
||||||
Filenames.toSimpleFileName(
|
inputFile.getOriginalFilename())
|
||||||
inputFile.getOriginalFilename()))
|
+ ", Script: "
|
||||||
.append(", Script: ")
|
+ name
|
||||||
.append(name)
|
+ "\n"
|
||||||
.append("\n")
|
+ jsCodeStr
|
||||||
.append(jsCodeStr)
|
+ "\n";
|
||||||
.append("\n");
|
|
||||||
foundScript = true;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
if (!foundScript) {
|
if (script.isEmpty()) {
|
||||||
script =
|
script =
|
||||||
new StringBuilder("PDF '")
|
"PDF '"
|
||||||
.append(Filenames.toSimpleFileName(inputFile.getOriginalFilename()))
|
+ Filenames.toSimpleFileName(inputFile.getOriginalFilename())
|
||||||
.append("' does not contain Javascript");
|
+ "' does not contain Javascript";
|
||||||
}
|
}
|
||||||
|
|
||||||
return WebResponseUtils.bytesToWebResponse(
|
return WebResponseUtils.bytesToWebResponse(
|
||||||
script.toString().getBytes(StandardCharsets.UTF_8),
|
script.getBytes(StandardCharsets.UTF_8),
|
||||||
Filenames.toSimpleFileName(inputFile.getOriginalFilename()) + ".js",
|
Filenames.toSimpleFileName(inputFile.getOriginalFilename()) + ".js",
|
||||||
MediaType.TEXT_PLAIN);
|
MediaType.TEXT_PLAIN);
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user