2023-01-27 19:23:40 +01:00
|
|
|
package stirling.software.SPDF.controller;
|
|
|
|
|
|
|
|
import java.io.ByteArrayOutputStream;
|
|
|
|
import java.io.IOException;
|
|
|
|
import java.util.ArrayList;
|
|
|
|
import java.util.List;
|
|
|
|
|
|
|
|
import org.apache.pdfbox.pdmodel.PDDocument;
|
|
|
|
import org.apache.pdfbox.pdmodel.PDPage;
|
|
|
|
import org.slf4j.Logger;
|
|
|
|
import org.slf4j.LoggerFactory;
|
|
|
|
import org.springframework.http.HttpHeaders;
|
|
|
|
import org.springframework.http.HttpStatus;
|
|
|
|
import org.springframework.http.MediaType;
|
|
|
|
import org.springframework.http.ResponseEntity;
|
|
|
|
import org.springframework.stereotype.Controller;
|
|
|
|
import org.springframework.ui.Model;
|
|
|
|
import org.springframework.web.bind.annotation.GetMapping;
|
|
|
|
import org.springframework.web.bind.annotation.PostMapping;
|
|
|
|
import org.springframework.web.bind.annotation.RequestParam;
|
|
|
|
import org.springframework.web.multipart.MultipartFile;
|
|
|
|
|
2023-02-03 21:26:35 +01:00
|
|
|
import stirling.software.SPDF.utils.PdfUtils;
|
|
|
|
|
2023-01-27 19:23:40 +01:00
|
|
|
@Controller
|
|
|
|
public class RearrangePagesPDFController {
|
|
|
|
|
|
|
|
private static final Logger logger = LoggerFactory.getLogger(RearrangePagesPDFController.class);
|
|
|
|
|
|
|
|
@GetMapping("/pdf-organizer")
|
|
|
|
public String pageOrganizer(Model model) {
|
2023-01-29 18:06:53 +01:00
|
|
|
model.addAttribute("currentPage", "pdf-organizer");
|
2023-01-27 19:23:40 +01:00
|
|
|
return "pdf-organizer";
|
|
|
|
}
|
|
|
|
|
2023-02-03 21:26:35 +01:00
|
|
|
@GetMapping("/delete-pages")
|
|
|
|
public String pageDeleter(Model model) {
|
|
|
|
model.addAttribute("currentPage", "delete-pages");
|
|
|
|
return "delete-pages";
|
|
|
|
}
|
|
|
|
|
|
|
|
@PostMapping("/delete-pages")
|
|
|
|
public ResponseEntity<byte[]> deletePages(@RequestParam("fileInput") MultipartFile pdfFile,
|
|
|
|
@RequestParam("pagesToDelete") String pagesToDelete) throws IOException {
|
|
|
|
|
|
|
|
PDDocument document = PDDocument.load(pdfFile.getBytes());
|
|
|
|
|
|
|
|
// Split the page order string into an array of page numbers or range of numbers
|
|
|
|
String[] pageOrderArr = pagesToDelete.split(",");
|
|
|
|
|
|
|
|
List<Integer> pagesToRemove = pageOrderToString(pageOrderArr, document.getNumberOfPages());
|
|
|
|
|
|
|
|
for (int i = pagesToRemove.size() - 1; i >= 0; i--) {
|
|
|
|
int pageIndex = pagesToRemove.get(i);
|
|
|
|
document.removePage(pageIndex);
|
|
|
|
}
|
|
|
|
|
|
|
|
return PdfUtils.pdfDocToWebResponse(document, pdfFile.getName() + "_removed_pages.pdf");
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
private List<Integer> pageOrderToString(String[] pageOrderArr, int totalPages) {
|
|
|
|
List<Integer> newPageOrder = new ArrayList<Integer>();
|
|
|
|
// loop through the page order array
|
|
|
|
for (String element : pageOrderArr) {
|
|
|
|
// check if the element contains a range of pages
|
|
|
|
if (element.contains("-")) {
|
|
|
|
// split the range into start and end page
|
|
|
|
String[] range = element.split("-");
|
|
|
|
int start = Integer.parseInt(range[0]);
|
|
|
|
int end = Integer.parseInt(range[1]);
|
|
|
|
// check if the end page is greater than total pages
|
|
|
|
if (end > totalPages) {
|
|
|
|
end = totalPages;
|
|
|
|
}
|
|
|
|
// loop through the range of pages
|
|
|
|
for (int j = start; j <= end; j++) {
|
|
|
|
// print the current index
|
|
|
|
newPageOrder.add(j - 1);
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
// if the element is a single page
|
|
|
|
newPageOrder.add(Integer.parseInt(element) - 1);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return newPageOrder;
|
|
|
|
}
|
|
|
|
|
2023-01-27 19:23:40 +01:00
|
|
|
@PostMapping("/rearrange-pages")
|
|
|
|
public ResponseEntity<byte[]> rearrangePages(@RequestParam("fileInput") MultipartFile pdfFile,
|
|
|
|
@RequestParam("pageOrder") String pageOrder) {
|
|
|
|
try {
|
|
|
|
// Load the input PDF
|
|
|
|
PDDocument document = PDDocument.load(pdfFile.getInputStream());
|
|
|
|
|
|
|
|
// Split the page order string into an array of page numbers or range of numbers
|
|
|
|
String[] pageOrderArr = pageOrder.split(",");
|
2023-02-03 21:26:35 +01:00
|
|
|
// int[] newPageOrder = new int[pageOrderArr.length];
|
2023-01-27 19:23:40 +01:00
|
|
|
int totalPages = document.getNumberOfPages();
|
|
|
|
|
2023-02-03 21:26:35 +01:00
|
|
|
List<Integer> newPageOrder = pageOrderToString(pageOrderArr, totalPages);
|
|
|
|
|
2023-01-27 19:23:40 +01:00
|
|
|
// Create a new list to hold the pages in the new order
|
|
|
|
List<PDPage> newPages = new ArrayList<>();
|
|
|
|
for (int i = 0; i < newPageOrder.size(); i++) {
|
|
|
|
newPages.add(document.getPage(newPageOrder.get(i)));
|
|
|
|
}
|
|
|
|
|
|
|
|
// Remove all the pages from the original document
|
|
|
|
for (int i = document.getNumberOfPages() - 1; i >= 0; i--) {
|
|
|
|
document.removePage(i);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Add the pages in the new order
|
|
|
|
for (PDPage page : newPages) {
|
|
|
|
document.addPage(page);
|
|
|
|
}
|
|
|
|
|
2023-02-03 21:26:35 +01:00
|
|
|
return PdfUtils.pdfDocToWebResponse(document, pdfFile.getName() + "_rearranged.pdf");
|
2023-01-27 19:23:40 +01:00
|
|
|
} catch (IOException e) {
|
|
|
|
|
|
|
|
logger.error("Failed rearranging documents", e);
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|