mirror of
https://github.com/Frooodle/Stirling-PDF.git
synced 2025-09-03 17:52:30 +02:00
Fix split mode null crash by defaulting to ALL
This commit is contained in:
parent
ad0bdc6d7e
commit
483d8b812b
@ -4,10 +4,7 @@ import java.io.ByteArrayOutputStream;
|
|||||||
import java.io.IOException;
|
import java.io.IOException;
|
||||||
import java.nio.file.Files;
|
import java.nio.file.Files;
|
||||||
import java.nio.file.Path;
|
import java.nio.file.Path;
|
||||||
import java.util.ArrayList;
|
import java.util.*;
|
||||||
import java.util.HashSet;
|
|
||||||
import java.util.List;
|
|
||||||
import java.util.Set;
|
|
||||||
import java.util.zip.ZipEntry;
|
import java.util.zip.ZipEntry;
|
||||||
import java.util.zip.ZipOutputStream;
|
import java.util.zip.ZipOutputStream;
|
||||||
|
|
||||||
@ -61,10 +58,14 @@ public class SplitPdfBySectionsController {
|
|||||||
|
|
||||||
MultipartFile file = request.getFileInput();
|
MultipartFile file = request.getFileInput();
|
||||||
String pageNumbers = request.getPageNumbers();
|
String pageNumbers = request.getPageNumbers();
|
||||||
String splitMode = request.getSplitMode();
|
SplitTypes splitMode = Optional.ofNullable(request.getSplitMode())
|
||||||
|
.map(SplitTypes::valueOf)
|
||||||
|
.orElse(SplitTypes.SPLIT_ALL);
|
||||||
|
|
||||||
PDDocument sourceDocument = pdfDocumentFactory.load(file);
|
PDDocument sourceDocument = pdfDocumentFactory.load(file);
|
||||||
|
|
||||||
Set<Integer> pagesToSplit = getPagesToSplit(pageNumbers, splitMode, sourceDocument.getNumberOfPages());
|
Set<Integer> pagesToSplit =
|
||||||
|
getPagesToSplit(pageNumbers, splitMode, sourceDocument.getNumberOfPages());
|
||||||
|
|
||||||
// Process the PDF based on split parameters
|
// Process the PDF based on split parameters
|
||||||
int horiz = request.getHorizontalDivisions() + 1;
|
int horiz = request.getHorizontalDivisions() + 1;
|
||||||
@ -119,16 +120,17 @@ public class SplitPdfBySectionsController {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// Based on the mode, get the pages that need to be split and return the pages set
|
// Based on the mode, get the pages that need to be split and return the pages set
|
||||||
private Set<Integer> getPagesToSplit(String pageNumbers, String splitMode, int totalPages) {
|
private Set<Integer> getPagesToSplit(String pageNumbers, SplitTypes splitMode, int totalPages) {
|
||||||
Set<Integer> pagesToSplit = new HashSet<>();
|
Set<Integer> pagesToSplit = new HashSet<>();
|
||||||
|
|
||||||
switch (SplitTypes.valueOf(splitMode)) {
|
switch (splitMode) {
|
||||||
case CUSTOM:
|
case CUSTOM:
|
||||||
if (pageNumbers == null || pageNumbers.isBlank()) {
|
if (pageNumbers == null || pageNumbers.isBlank()) {
|
||||||
throw new IllegalArgumentException("Custom mode requires page numbers input.");
|
throw new IllegalArgumentException("Custom mode requires page numbers input.");
|
||||||
}
|
}
|
||||||
String[] pageOrderArr = pageNumbers.split(",");
|
String[] pageOrderArr = pageNumbers.split(",");
|
||||||
List<Integer> pageListToSplit = GeneralUtils.parsePageList(pageOrderArr, totalPages, false);
|
List<Integer> pageListToSplit =
|
||||||
|
GeneralUtils.parsePageList(pageOrderArr, totalPages, false);
|
||||||
pagesToSplit.addAll(pageListToSplit);
|
pagesToSplit.addAll(pageListToSplit);
|
||||||
break;
|
break;
|
||||||
|
|
||||||
@ -163,9 +165,11 @@ public class SplitPdfBySectionsController {
|
|||||||
return pagesToSplit;
|
return pagesToSplit;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
public List<PDDocument> splitPdfPages(
|
public List<PDDocument> splitPdfPages(
|
||||||
PDDocument document, int horizontalDivisions, int verticalDivisions, Set<Integer> pagesToSplit)
|
PDDocument document,
|
||||||
|
int horizontalDivisions,
|
||||||
|
int verticalDivisions,
|
||||||
|
Set<Integer> pagesToSplit)
|
||||||
throws IOException {
|
throws IOException {
|
||||||
List<PDDocument> splitDocuments = new ArrayList<>();
|
List<PDDocument> splitDocuments = new ArrayList<>();
|
||||||
|
|
||||||
@ -193,12 +197,12 @@ public class SplitPdfBySectionsController {
|
|||||||
subDoc.addPage(subPage);
|
subDoc.addPage(subPage);
|
||||||
|
|
||||||
PDFormXObject form =
|
PDFormXObject form =
|
||||||
layerUtility.importPageAsForm(
|
layerUtility.importPageAsForm(
|
||||||
document, document.getPages().indexOf(originalPage));
|
document, document.getPages().indexOf(originalPage));
|
||||||
|
|
||||||
try (PDPageContentStream contentStream =
|
try (PDPageContentStream contentStream =
|
||||||
new PDPageContentStream(
|
new PDPageContentStream(
|
||||||
subDoc, subPage, AppendMode.APPEND, true, true)) {
|
subDoc, subPage, AppendMode.APPEND, true, true)) {
|
||||||
// Set clipping area and position
|
// Set clipping area and position
|
||||||
float translateX = -subPageWidth * i;
|
float translateX = -subPageWidth * i;
|
||||||
|
|
||||||
|
@ -12,20 +12,20 @@ import stirling.software.common.model.api.PDFFile;
|
|||||||
@EqualsAndHashCode(callSuper = true)
|
@EqualsAndHashCode(callSuper = true)
|
||||||
public class SplitPdfBySectionsRequest extends PDFFile {
|
public class SplitPdfBySectionsRequest extends PDFFile {
|
||||||
@Schema(
|
@Schema(
|
||||||
description = "Pages to be split by section",
|
description = "Pages to be split by section",
|
||||||
defaultValue = "all",
|
defaultValue = "all",
|
||||||
requiredMode = Schema.RequiredMode.REQUIRED)
|
requiredMode = Schema.RequiredMode.REQUIRED)
|
||||||
private String pageNumbers;
|
private String pageNumbers;
|
||||||
|
|
||||||
@Schema(
|
@Schema(
|
||||||
implementation = SplitTypes.class,
|
implementation = SplitTypes.class,
|
||||||
description =
|
description =
|
||||||
"Modes for page split. Valid values are:\n"
|
"Modes for page split. Valid values are:\n"
|
||||||
+ "SPLIT_ALL_EXCEPT_FIRST_AND_LAST: Splits all except the first and the last pages.\n"
|
+ "SPLIT_ALL_EXCEPT_FIRST_AND_LAST: Splits all except the first and the last pages.\n"
|
||||||
+ "SPLIT_ALL_EXCEPT_FIRST: Splits all except the first page.\n"
|
+ "SPLIT_ALL_EXCEPT_FIRST: Splits all except the first page.\n"
|
||||||
+ "SPLIT_ALL_EXCEPT_LAST: Splits all except the last page.\n"
|
+ "SPLIT_ALL_EXCEPT_LAST: Splits all except the last page.\n"
|
||||||
+ "SPLIT_ALL: Splits all pages.\n"
|
+ "SPLIT_ALL: Splits all pages.\n"
|
||||||
+ "CUSTOM: Custom split.\n")
|
+ "CUSTOM: Custom split.\n")
|
||||||
private String splitMode;
|
private String splitMode;
|
||||||
|
|
||||||
@Schema(
|
@Schema(
|
||||||
|
Loading…
Reference in New Issue
Block a user