mirror of
https://github.com/Stirling-Tools/Stirling-PDF.git
synced 2025-06-18 13:35:03 +00:00

# Description of Changes Introduced a new `common` module for shared libs and commonly used classes. See the screenshot below for the file structure and classes that have been moved. --- <img width="452" alt="Screenshot 2025-05-22 at 11 46 56" src="https://github.com/user-attachments/assets/c9badabc-48f9-4079-b83e-7cfde0fb840f" /> <img width="470" alt="Screenshot 2025-05-22 at 11 47 30" src="https://github.com/user-attachments/assets/e8315b09-2e78-4c50-b9de-4dd9b9b0ecb1" /> ## Checklist ### General - [x] I have read the [Contribution Guidelines](https://github.com/Stirling-Tools/Stirling-PDF/blob/main/CONTRIBUTING.md) - [x] I have read the [Stirling-PDF Developer Guide](https://github.com/Stirling-Tools/Stirling-PDF/blob/main/DeveloperGuide.md) (if applicable) - [x] I have read the [How to add new languages to Stirling-PDF](https://github.com/Stirling-Tools/Stirling-PDF/blob/main/HowToAddNewLanguage.md) (if applicable) - [x] I have performed a self-review of my own code - [x] My changes generate no new warnings ### Documentation - [ ] I have updated relevant docs on [Stirling-PDF's doc repo](https://github.com/Stirling-Tools/Stirling-Tools.github.io/blob/main/docs/) (if functionality has heavily changed) - [ ] I have read the section [Add New Translation Tags](https://github.com/Stirling-Tools/Stirling-PDF/blob/main/HowToAddNewLanguage.md#add-new-translation-tags) (for new translation tags only) ### UI Changes (if applicable) - [x] Screenshots or videos demonstrating the UI changes are attached (e.g., as comments or direct attachments in the PR) ### Testing (if applicable) - [x] I have tested my changes locally. Refer to the [Testing Guide](https://github.com/Stirling-Tools/Stirling-PDF/blob/main/DeveloperGuide.md#6-testing) for more details.
99 lines
3.8 KiB
Java
99 lines
3.8 KiB
Java
package stirling.software.SPDF.controller.api;
|
|
|
|
import java.io.ByteArrayOutputStream;
|
|
import java.io.IOException;
|
|
|
|
import org.apache.pdfbox.multipdf.LayerUtility;
|
|
import org.apache.pdfbox.pdmodel.PDDocument;
|
|
import org.apache.pdfbox.pdmodel.PDPage;
|
|
import org.apache.pdfbox.pdmodel.PDPageContentStream;
|
|
import org.apache.pdfbox.pdmodel.PDPageContentStream.AppendMode;
|
|
import org.apache.pdfbox.pdmodel.common.PDRectangle;
|
|
import org.apache.pdfbox.pdmodel.graphics.form.PDFormXObject;
|
|
import org.springframework.http.ResponseEntity;
|
|
import org.springframework.web.bind.annotation.ModelAttribute;
|
|
import org.springframework.web.bind.annotation.PostMapping;
|
|
import org.springframework.web.bind.annotation.RequestMapping;
|
|
import org.springframework.web.bind.annotation.RestController;
|
|
|
|
import io.swagger.v3.oas.annotations.Operation;
|
|
import io.swagger.v3.oas.annotations.tags.Tag;
|
|
|
|
import lombok.RequiredArgsConstructor;
|
|
|
|
import stirling.software.SPDF.model.api.general.CropPdfForm;
|
|
import stirling.software.common.service.CustomPDFDocumentFactory;
|
|
import stirling.software.common.util.WebResponseUtils;
|
|
|
|
@RestController
|
|
@RequestMapping("/api/v1/general")
|
|
@Tag(name = "General", description = "General APIs")
|
|
@RequiredArgsConstructor
|
|
public class CropController {
|
|
|
|
private final CustomPDFDocumentFactory pdfDocumentFactory;
|
|
|
|
@PostMapping(value = "/crop", consumes = "multipart/form-data")
|
|
@Operation(
|
|
summary = "Crops a PDF document",
|
|
description =
|
|
"This operation takes an input PDF file and crops it according to the given"
|
|
+ " coordinates. Input:PDF Output:PDF Type:SISO")
|
|
public ResponseEntity<byte[]> cropPdf(@ModelAttribute CropPdfForm request) throws IOException {
|
|
PDDocument sourceDocument = pdfDocumentFactory.load(request);
|
|
|
|
PDDocument newDocument =
|
|
pdfDocumentFactory.createNewDocumentBasedOnOldDocument(sourceDocument);
|
|
|
|
int totalPages = sourceDocument.getNumberOfPages();
|
|
|
|
LayerUtility layerUtility = new LayerUtility(newDocument);
|
|
|
|
for (int i = 0; i < totalPages; i++) {
|
|
PDPage sourcePage = sourceDocument.getPage(i);
|
|
|
|
// Create a new page with the size of the source page
|
|
PDPage newPage = new PDPage(sourcePage.getMediaBox());
|
|
newDocument.addPage(newPage);
|
|
PDPageContentStream contentStream =
|
|
new PDPageContentStream(newDocument, newPage, AppendMode.OVERWRITE, true, true);
|
|
|
|
// Import the source page as a form XObject
|
|
PDFormXObject formXObject = layerUtility.importPageAsForm(sourceDocument, i);
|
|
|
|
contentStream.saveGraphicsState();
|
|
|
|
// Define the crop area
|
|
contentStream.addRect(
|
|
request.getX(), request.getY(), request.getWidth(), request.getHeight());
|
|
contentStream.clip();
|
|
|
|
// Draw the entire formXObject
|
|
contentStream.drawForm(formXObject);
|
|
|
|
contentStream.restoreGraphicsState();
|
|
|
|
contentStream.close();
|
|
|
|
// Now, set the new page's media box to the cropped size
|
|
newPage.setMediaBox(
|
|
new PDRectangle(
|
|
request.getX(),
|
|
request.getY(),
|
|
request.getWidth(),
|
|
request.getHeight()));
|
|
}
|
|
|
|
ByteArrayOutputStream baos = new ByteArrayOutputStream();
|
|
newDocument.save(baos);
|
|
newDocument.close();
|
|
sourceDocument.close();
|
|
|
|
byte[] pdfContent = baos.toByteArray();
|
|
return WebResponseUtils.bytesToWebResponse(
|
|
pdfContent,
|
|
request.getFileInput().getOriginalFilename().replaceFirst("[.][^.]+$", "")
|
|
+ "_cropped.pdf");
|
|
}
|
|
}
|