2023-01-29 18:06:53 +01:00
|
|
|
package stirling.software.SPDF.controller;
|
|
|
|
|
|
|
|
import java.io.IOException;
|
|
|
|
|
|
|
|
import org.apache.pdfbox.pdmodel.PDDocument;
|
|
|
|
import org.apache.pdfbox.pdmodel.PDPage;
|
|
|
|
import org.apache.pdfbox.pdmodel.PDPageTree;
|
|
|
|
import org.slf4j.Logger;
|
|
|
|
import org.slf4j.LoggerFactory;
|
|
|
|
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;
|
|
|
|
|
|
|
|
import stirling.software.SPDF.utils.PdfUtils;
|
|
|
|
|
|
|
|
@Controller
|
|
|
|
public class RotationController {
|
|
|
|
|
2023-02-11 15:27:15 +01:00
|
|
|
private static final Logger logger = LoggerFactory.getLogger(RotationController.class);
|
2023-01-29 18:06:53 +01:00
|
|
|
|
2023-02-11 15:27:15 +01:00
|
|
|
@GetMapping("/rotate-pdf")
|
|
|
|
public String rotatePdfForm(Model model) {
|
|
|
|
model.addAttribute("currentPage", "rotate-pdf");
|
|
|
|
return "rotate-pdf";
|
|
|
|
}
|
2023-01-29 18:06:53 +01:00
|
|
|
|
2023-02-11 15:27:15 +01:00
|
|
|
@PostMapping("/rotate-pdf")
|
|
|
|
public ResponseEntity<byte[]> rotatePDF(@RequestParam("fileInput") MultipartFile pdfFile, @RequestParam("angle") Integer angle) throws IOException {
|
2023-01-29 18:06:53 +01:00
|
|
|
|
2023-02-11 15:27:15 +01:00
|
|
|
// Load the PDF document
|
|
|
|
PDDocument document = PDDocument.load(pdfFile.getBytes());
|
2023-01-29 18:06:53 +01:00
|
|
|
|
2023-02-11 15:27:15 +01:00
|
|
|
// Get the list of pages in the document
|
|
|
|
PDPageTree pages = document.getPages();
|
2023-01-29 18:06:53 +01:00
|
|
|
|
2023-02-11 15:27:15 +01:00
|
|
|
for (PDPage page : pages) {
|
|
|
|
page.setRotation(page.getRotation() + angle);
|
|
|
|
}
|
2023-01-29 18:06:53 +01:00
|
|
|
|
2023-02-11 15:27:15 +01:00
|
|
|
return PdfUtils.pdfDocToWebResponse(document, pdfFile.getName() + "_rotated.pdf");
|
2023-01-29 18:06:53 +01:00
|
|
|
|
2023-02-11 15:27:15 +01:00
|
|
|
}
|
2023-01-29 18:06:53 +01:00
|
|
|
|
|
|
|
}
|