itext removal fixes
This commit is contained in:
parent
a7cd6bfd2e
commit
862086eae5
4 changed files with 91 additions and 61 deletions
|
@ -1,12 +1,15 @@
|
||||||
package stirling.software.SPDF.controller.api;
|
package stirling.software.SPDF.controller.api;
|
||||||
|
|
||||||
import java.awt.geom.AffineTransform;
|
import java.awt.geom.AffineTransform;
|
||||||
|
import java.io.ByteArrayInputStream;
|
||||||
import java.io.ByteArrayOutputStream;
|
import java.io.ByteArrayOutputStream;
|
||||||
import java.io.IOException;
|
import java.io.IOException;
|
||||||
|
import java.io.InputStream;
|
||||||
|
|
||||||
import org.apache.pdfbox.pdmodel.PDDocument;
|
import org.apache.pdfbox.pdmodel.PDDocument;
|
||||||
import org.apache.pdfbox.pdmodel.PDPage;
|
import org.apache.pdfbox.pdmodel.PDPage;
|
||||||
import org.apache.pdfbox.pdmodel.common.PDRectangle;
|
import org.apache.pdfbox.pdmodel.common.PDRectangle;
|
||||||
|
import org.apache.pdfbox.pdmodel.common.PDStream;
|
||||||
import org.apache.pdfbox.pdmodel.graphics.form.PDFormXObject;
|
import org.apache.pdfbox.pdmodel.graphics.form.PDFormXObject;
|
||||||
import org.apache.pdfbox.pdmodel.graphics.image.PDImageXObject;
|
import org.apache.pdfbox.pdmodel.graphics.image.PDImageXObject;
|
||||||
import org.slf4j.Logger;
|
import org.slf4j.Logger;
|
||||||
|
@ -49,37 +52,50 @@ public class ToSinglePageController {
|
||||||
@Parameter(description = "The input multi-page PDF file to be converted into a single page", required = true)
|
@Parameter(description = "The input multi-page PDF file to be converted into a single page", required = true)
|
||||||
MultipartFile file) throws IOException {
|
MultipartFile file) throws IOException {
|
||||||
|
|
||||||
|
// Load the source document
|
||||||
PDDocument sourceDocument = PDDocument.load(file.getInputStream());
|
PDDocument sourceDocument = PDDocument.load(file.getInputStream());
|
||||||
float totalHeight = 0;
|
|
||||||
float width = 0;
|
|
||||||
|
|
||||||
for (PDPage page : sourceDocument.getPages()) {
|
// Calculate total height and max width
|
||||||
PDRectangle pageSize = page.getMediaBox();
|
float totalHeight = 0;
|
||||||
totalHeight += pageSize.getHeight();
|
float maxWidth = 0;
|
||||||
if(width < pageSize.getWidth())
|
for (PDPage page : sourceDocument.getPages()) {
|
||||||
width = pageSize.getWidth();
|
PDRectangle pageSize = page.getMediaBox();
|
||||||
}
|
totalHeight += pageSize.getHeight();
|
||||||
|
maxWidth = Math.max(maxWidth, pageSize.getWidth());
|
||||||
|
}
|
||||||
|
|
||||||
PDDocument newDocument = new PDDocument();
|
// Create new document and page with calculated dimensions
|
||||||
PDPage newPage = new PDPage(new PDRectangle(width, totalHeight));
|
PDDocument newDocument = new PDDocument();
|
||||||
newDocument.addPage(newPage);
|
PDPage newPage = new PDPage(new PDRectangle(maxWidth, totalHeight));
|
||||||
|
newDocument.addPage(newPage);
|
||||||
|
|
||||||
LayerUtility layerUtility = new LayerUtility(newDocument);
|
// Initialize the content stream of the new page
|
||||||
float yOffset = totalHeight;
|
PDPageContentStream contentStream = new PDPageContentStream(newDocument, newPage);
|
||||||
|
contentStream.close();
|
||||||
|
|
||||||
|
LayerUtility layerUtility = new LayerUtility(newDocument);
|
||||||
|
float yOffset = totalHeight;
|
||||||
|
|
||||||
for (PDPage page : sourceDocument.getPages()) {
|
// For each page, copy its content to the new page at the correct offset
|
||||||
PDFormXObject form = layerUtility.importPageAsForm(sourceDocument, sourceDocument.getPages().indexOf(page));
|
for (PDPage page : sourceDocument.getPages()) {
|
||||||
AffineTransform af = AffineTransform.getTranslateInstance(0, yOffset - page.getMediaBox().getHeight());
|
PDFormXObject form = layerUtility.importPageAsForm(sourceDocument, sourceDocument.getPages().indexOf(page));
|
||||||
layerUtility.appendFormAsLayer(newDocument.getPage(0), form, af, page.getResources().getCOSObject().toString());
|
AffineTransform af = AffineTransform.getTranslateInstance(0, yOffset - page.getMediaBox().getHeight());
|
||||||
yOffset -= page.getMediaBox().getHeight();
|
layerUtility.wrapInSaveRestore(newPage);
|
||||||
}
|
String defaultLayerName = "Layer" + sourceDocument.getPages().indexOf(page);
|
||||||
|
layerUtility.appendFormAsLayer(newPage, form, af, defaultLayerName);
|
||||||
|
yOffset -= page.getMediaBox().getHeight();
|
||||||
|
}
|
||||||
|
|
||||||
ByteArrayOutputStream baos = new ByteArrayOutputStream();
|
ByteArrayOutputStream baos = new ByteArrayOutputStream();
|
||||||
newDocument.save(baos);
|
newDocument.save(baos);
|
||||||
newDocument.close();
|
newDocument.close();
|
||||||
sourceDocument.close();
|
sourceDocument.close();
|
||||||
|
|
||||||
byte[] result = baos.toByteArray();
|
byte[] result = baos.toByteArray();
|
||||||
return WebResponseUtils.bytesToWebResponse(result, file.getOriginalFilename().replaceFirst("[.][^.]+$", "") + "_singlePage.pdf");
|
return WebResponseUtils.bytesToWebResponse(result, file.getOriginalFilename().replaceFirst("[.][^.]+$", "") + "_singlePage.pdf");
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
}
|
}
|
||||||
}
|
}
|
|
@ -60,6 +60,11 @@ public class PageNumbersController {
|
||||||
case "large":
|
case "large":
|
||||||
marginFactor = 0.05f;
|
marginFactor = 0.05f;
|
||||||
break;
|
break;
|
||||||
|
case "x-large":
|
||||||
|
marginFactor = 0.075f;
|
||||||
|
break;
|
||||||
|
|
||||||
|
|
||||||
default:
|
default:
|
||||||
marginFactor = 0.035f;
|
marginFactor = 0.035f;
|
||||||
break;
|
break;
|
||||||
|
@ -67,7 +72,12 @@ public class PageNumbersController {
|
||||||
|
|
||||||
float fontSize = 12.0f;
|
float fontSize = 12.0f;
|
||||||
PDType1Font font = PDType1Font.HELVETICA;
|
PDType1Font font = PDType1Font.HELVETICA;
|
||||||
|
if(pagesToNumber == null || pagesToNumber.length() == 0) {
|
||||||
|
pagesToNumber = "all";
|
||||||
|
}
|
||||||
|
if(customText == null || customText.length() == 0) {
|
||||||
|
customText = "{n}";
|
||||||
|
}
|
||||||
List<Integer> pagesToNumberList = GeneralUtils.parsePageList(pagesToNumber.split(","), document.getNumberOfPages());
|
List<Integer> pagesToNumberList = GeneralUtils.parsePageList(pagesToNumber.split(","), document.getNumberOfPages());
|
||||||
|
|
||||||
for (int i : pagesToNumberList) {
|
for (int i : pagesToNumberList) {
|
||||||
|
@ -120,7 +130,7 @@ public class PageNumbersController {
|
||||||
document.save(baos);
|
document.save(baos);
|
||||||
document.close();
|
document.close();
|
||||||
|
|
||||||
return WebResponseUtils.bytesToWebResponse(baos.toByteArray(), URLEncoder.encode(file.getOriginalFilename().replaceFirst("[.][^.]+$", "") + "_numbersAdded.pdf", "UTF-8"), MediaType.APPLICATION_PDF);
|
return WebResponseUtils.bytesToWebResponse(baos.toByteArray(), file.getOriginalFilename().replaceFirst("[.][^.]+$", "") + "_numbersAdded.pdf", MediaType.APPLICATION_PDF);
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -28,20 +28,22 @@ public class ShowJavascript {
|
||||||
String script = "";
|
String script = "";
|
||||||
|
|
||||||
try (PDDocument document = PDDocument.load(inputFile.getInputStream())) {
|
try (PDDocument document = PDDocument.load(inputFile.getInputStream())) {
|
||||||
|
|
||||||
PDNameTreeNode<PDActionJavaScript> jsTree = document.getDocumentCatalog().getNames().getJavaScript();
|
if(document.getDocumentCatalog() != null && document.getDocumentCatalog().getNames() != null) {
|
||||||
|
PDNameTreeNode<PDActionJavaScript> jsTree = document.getDocumentCatalog().getNames().getJavaScript();
|
||||||
if (jsTree != null) {
|
|
||||||
Map<String, PDActionJavaScript> jsEntries = jsTree.getNames();
|
if (jsTree != null) {
|
||||||
|
Map<String, PDActionJavaScript> jsEntries = jsTree.getNames();
|
||||||
for (Map.Entry<String, PDActionJavaScript> entry : jsEntries.entrySet()) {
|
|
||||||
String name = entry.getKey();
|
for (Map.Entry<String, PDActionJavaScript> entry : jsEntries.entrySet()) {
|
||||||
PDActionJavaScript jsAction = entry.getValue();
|
String name = entry.getKey();
|
||||||
String jsCodeStr = jsAction.getAction();
|
PDActionJavaScript jsAction = entry.getValue();
|
||||||
|
String jsCodeStr = jsAction.getAction();
|
||||||
script += "// File: " + inputFile.getOriginalFilename() + ", Script: " + name + "\n" + jsCodeStr + "\n";
|
|
||||||
}
|
script += "// File: " + inputFile.getOriginalFilename() + ", Script: " + name + "\n" + jsCodeStr + "\n";
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
if (script.isEmpty()) {
|
if (script.isEmpty()) {
|
||||||
script = "PDF '" + inputFile.getOriginalFilename() + "' does not contain Javascript";
|
script = "PDF '" + inputFile.getOriginalFilename() + "' does not contain Javascript";
|
||||||
|
|
|
@ -185,24 +185,26 @@ public class GetInfoOnPDF {
|
||||||
|
|
||||||
|
|
||||||
//embeed files TODO size
|
//embeed files TODO size
|
||||||
PDEmbeddedFilesNameTreeNode efTree = catalog.getNames().getEmbeddedFiles();
|
if(catalog.getNames() != null) {
|
||||||
|
PDEmbeddedFilesNameTreeNode efTree = catalog.getNames().getEmbeddedFiles();
|
||||||
ArrayNode embeddedFilesArray = objectMapper.createArrayNode();
|
|
||||||
if (efTree != null) {
|
ArrayNode embeddedFilesArray = objectMapper.createArrayNode();
|
||||||
Map<String, PDComplexFileSpecification> efMap = efTree.getNames();
|
if (efTree != null) {
|
||||||
if (efMap != null) {
|
Map<String, PDComplexFileSpecification> efMap = efTree.getNames();
|
||||||
for (Map.Entry<String, PDComplexFileSpecification> entry : efMap.entrySet()) {
|
if (efMap != null) {
|
||||||
ObjectNode embeddedFileNode = objectMapper.createObjectNode();
|
for (Map.Entry<String, PDComplexFileSpecification> entry : efMap.entrySet()) {
|
||||||
embeddedFileNode.put("Name", entry.getKey());
|
ObjectNode embeddedFileNode = objectMapper.createObjectNode();
|
||||||
PDEmbeddedFile embeddedFile = entry.getValue().getEmbeddedFile();
|
embeddedFileNode.put("Name", entry.getKey());
|
||||||
if (embeddedFile != null) {
|
PDEmbeddedFile embeddedFile = entry.getValue().getEmbeddedFile();
|
||||||
embeddedFileNode.put("FileSize", embeddedFile.getLength()); // size in bytes
|
if (embeddedFile != null) {
|
||||||
}
|
embeddedFileNode.put("FileSize", embeddedFile.getLength()); // size in bytes
|
||||||
embeddedFilesArray.add(embeddedFileNode);
|
}
|
||||||
}
|
embeddedFilesArray.add(embeddedFileNode);
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
other.set("EmbeddedFiles", embeddedFilesArray);
|
||||||
}
|
}
|
||||||
other.set("EmbeddedFiles", embeddedFilesArray);
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
@ -374,7 +376,7 @@ public class GetInfoOnPDF {
|
||||||
|
|
||||||
|
|
||||||
ObjectNode pageInfoParent = objectMapper.createObjectNode();
|
ObjectNode pageInfoParent = objectMapper.createObjectNode();
|
||||||
for (int pageNum = 1; pageNum <= pdfBoxDoc.getNumberOfPages(); pageNum++) {
|
for (int pageNum = 0; pageNum < pdfBoxDoc.getNumberOfPages(); pageNum++) {
|
||||||
ObjectNode pageInfo = objectMapper.createObjectNode();
|
ObjectNode pageInfo = objectMapper.createObjectNode();
|
||||||
|
|
||||||
// Retrieve the page
|
// Retrieve the page
|
||||||
|
@ -411,8 +413,8 @@ public class GetInfoOnPDF {
|
||||||
|
|
||||||
// Content Extraction
|
// Content Extraction
|
||||||
PDFTextStripper textStripper = new PDFTextStripper();
|
PDFTextStripper textStripper = new PDFTextStripper();
|
||||||
textStripper.setStartPage(pageNum -1);
|
textStripper.setStartPage(pageNum + 1);
|
||||||
textStripper.setEndPage(pageNum - 1);
|
textStripper.setEndPage(pageNum +1);
|
||||||
String pageText = textStripper.getText(pdfBoxDoc);
|
String pageText = textStripper.getText(pdfBoxDoc);
|
||||||
|
|
||||||
pageInfo.put("Text Characters Count", pageText.length()); //
|
pageInfo.put("Text Characters Count", pageText.length()); //
|
||||||
|
|
Loading…
Reference in a new issue