mirror of
				https://github.com/Frooodle/Stirling-PDF.git
				synced 2025-11-01 01:21:18 +01:00 
			
		
		
		
	removal
This commit is contained in:
		
							parent
							
								
									ef07963d79
								
							
						
					
					
						commit
						9ece6dacbd
					
				@ -4,6 +4,9 @@ import java.io.ByteArrayInputStream;
 | 
				
			|||||||
import java.io.ByteArrayOutputStream;
 | 
					import java.io.ByteArrayOutputStream;
 | 
				
			||||||
import java.io.IOException;
 | 
					import java.io.IOException;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					import org.apache.pdfbox.pdmodel.PDDocument;
 | 
				
			||||||
 | 
					import org.apache.pdfbox.pdmodel.PDPage;
 | 
				
			||||||
 | 
					import org.apache.pdfbox.pdmodel.common.PDRectangle;
 | 
				
			||||||
import org.slf4j.Logger;
 | 
					import org.slf4j.Logger;
 | 
				
			||||||
import org.slf4j.LoggerFactory;
 | 
					import org.slf4j.LoggerFactory;
 | 
				
			||||||
import org.springframework.http.ResponseEntity;
 | 
					import org.springframework.http.ResponseEntity;
 | 
				
			||||||
@ -25,6 +28,26 @@ import io.swagger.v3.oas.annotations.Parameter;
 | 
				
			|||||||
import io.swagger.v3.oas.annotations.media.Schema;
 | 
					import io.swagger.v3.oas.annotations.media.Schema;
 | 
				
			||||||
import io.swagger.v3.oas.annotations.tags.Tag;
 | 
					import io.swagger.v3.oas.annotations.tags.Tag;
 | 
				
			||||||
import stirling.software.SPDF.utils.WebResponseUtils;
 | 
					import stirling.software.SPDF.utils.WebResponseUtils;
 | 
				
			||||||
 | 
					import org.apache.commons.io.IOUtils;
 | 
				
			||||||
 | 
					import org.apache.pdfbox.pdmodel.PDDocument;
 | 
				
			||||||
 | 
					import org.apache.pdfbox.pdmodel.PDPage;
 | 
				
			||||||
 | 
					import org.apache.pdfbox.pdmodel.PDPageContentStream;
 | 
				
			||||||
 | 
					import org.apache.pdfbox.pdmodel.font.PDFont;
 | 
				
			||||||
 | 
					import org.apache.pdfbox.pdmodel.font.PDType0Font;
 | 
				
			||||||
 | 
					import org.apache.pdfbox.pdmodel.font.PDType1Font;
 | 
				
			||||||
 | 
					import org.apache.pdfbox.pdmodel.graphics.image.LosslessFactory;
 | 
				
			||||||
 | 
					import org.apache.pdfbox.pdmodel.graphics.image.PDImageXObject;
 | 
				
			||||||
 | 
					import org.apache.pdfbox.pdmodel.graphics.state.PDExtendedGraphicsState;
 | 
				
			||||||
 | 
					import org.apache.pdfbox.util.Matrix;
 | 
				
			||||||
 | 
					import org.apache.pdfbox.pdmodel.graphics.color.PDColorSpace;
 | 
				
			||||||
 | 
					import org.apache.pdfbox.pdmodel.graphics.color.PDICCBased;
 | 
				
			||||||
 | 
					import org.apache.pdfbox.pdmodel.graphics.form.PDFormXObject;
 | 
				
			||||||
 | 
					import org.apache.pdfbox.pdmodel.graphics.image.PDImageXObject;
 | 
				
			||||||
 | 
					import org.apache.pdfbox.pdmodel.graphics.optionalcontent.PDOptionalContentGroup;
 | 
				
			||||||
 | 
					import org.apache.pdfbox.pdmodel.graphics.optionalcontent.PDOptionalContentProperties;
 | 
				
			||||||
 | 
					import stirling.software.SPDF.utils.WebResponseUtils;
 | 
				
			||||||
 | 
					import org.apache.pdfbox.pdmodel.PDPageContentStream;
 | 
				
			||||||
 | 
					import org.apache.pdfbox.multipdf.LayerUtility;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
@RestController
 | 
					@RestController
 | 
				
			||||||
@Tag(name = "General", description = "General APIs")
 | 
					@Tag(name = "General", description = "General APIs")
 | 
				
			||||||
@ -32,7 +55,6 @@ public class CropController {
 | 
				
			|||||||
 | 
					
 | 
				
			||||||
	private static final Logger logger = LoggerFactory.getLogger(CropController.class);
 | 
						private static final Logger logger = LoggerFactory.getLogger(CropController.class);
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					 | 
				
			||||||
	@PostMapping(value = "/crop", consumes = "multipart/form-data")
 | 
						@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")
 | 
						@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(
 | 
						public ResponseEntity<byte[]> cropPdf(
 | 
				
			||||||
@ -40,39 +62,55 @@ public class CropController {
 | 
				
			|||||||
			@Parameter(description = "The x-coordinate of the top-left corner of the crop area", required = true, schema = @Schema(type = "number")) @RequestParam("x") float x,
 | 
								@Parameter(description = "The x-coordinate of the top-left corner of the crop area", required = true, schema = @Schema(type = "number")) @RequestParam("x") float x,
 | 
				
			||||||
			@Parameter(description = "The y-coordinate of the top-left corner of the crop area", required = true, schema = @Schema(type = "number")) @RequestParam("y") float y,
 | 
								@Parameter(description = "The y-coordinate of the top-left corner of the crop area", required = true, schema = @Schema(type = "number")) @RequestParam("y") float y,
 | 
				
			||||||
			@Parameter(description = "The width of the crop area", required = true, schema = @Schema(type = "number")) @RequestParam("width") float width,
 | 
								@Parameter(description = "The width of the crop area", required = true, schema = @Schema(type = "number")) @RequestParam("width") float width,
 | 
				
			||||||
	        @Parameter(description = "The height of the crop area", required = true, schema = @Schema(type = "number")) @RequestParam("height") float height) throws IOException {
 | 
								@Parameter(description = "The height of the crop area", required = true, schema = @Schema(type = "number")) @RequestParam("height") float height)
 | 
				
			||||||
	    byte[] bytes = file.getBytes();
 | 
								throws IOException {
 | 
				
			||||||
	    System.out.println("x=" + x + ", " + "y=" + y + ", " + "width=" + width + ", " +"height=" + height  );
 | 
					
 | 
				
			||||||
	    PdfReader reader = new PdfReader(new ByteArrayInputStream(bytes));
 | 
					
 | 
				
			||||||
	    PdfDocument pdfDoc = new PdfDocument(reader);
 | 
					
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					PDDocument sourceDocument = PDDocument.load(new ByteArrayInputStream(file.getBytes()));
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					PDDocument newDocument = new PDDocument();
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					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);
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    // Import the source page as a form XObject
 | 
				
			||||||
 | 
					    PDFormXObject formXObject = layerUtility.importPageAsForm(sourceDocument, i);
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    contentStream.saveGraphicsState();
 | 
				
			||||||
 | 
					    
 | 
				
			||||||
 | 
					    // Define the crop area
 | 
				
			||||||
 | 
					    contentStream.addRect(x, y, width, height);
 | 
				
			||||||
 | 
					    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(x, y, width, height));
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
ByteArrayOutputStream baos = new ByteArrayOutputStream();
 | 
					ByteArrayOutputStream baos = new ByteArrayOutputStream();
 | 
				
			||||||
	    PdfWriter writer = new PdfWriter(baos);
 | 
					newDocument.save(baos);
 | 
				
			||||||
	    PdfDocument outputPdf = new PdfDocument(writer);
 | 
					newDocument.close();
 | 
				
			||||||
 | 
					sourceDocument.close();
 | 
				
			||||||
 | 
					
 | 
				
			||||||
	    int totalPages = pdfDoc.getNumberOfPages();
 | 
					 | 
				
			||||||
	
 | 
					 | 
				
			||||||
	    for (int i = 1; i <= totalPages; i++) {
 | 
					 | 
				
			||||||
	        PdfPage page = outputPdf.addNewPage(new PageSize(width, height));
 | 
					 | 
				
			||||||
	        PdfCanvas pdfCanvas = new PdfCanvas(page);
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
	        PdfFormXObject formXObject = pdfDoc.getPage(i).copyAsFormXObject(outputPdf);
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
	        // Save the graphics state, apply the transformations, add the object, and then
 | 
					 | 
				
			||||||
	        // restore the graphics state
 | 
					 | 
				
			||||||
	        pdfCanvas.saveState();
 | 
					 | 
				
			||||||
	        pdfCanvas.rectangle(x, y, width, height);
 | 
					 | 
				
			||||||
	        pdfCanvas.clip();
 | 
					 | 
				
			||||||
	        pdfCanvas.addXObject(formXObject, -x, -y);
 | 
					 | 
				
			||||||
	        pdfCanvas.restoreState();
 | 
					 | 
				
			||||||
	    }
 | 
					 | 
				
			||||||
	
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
	    outputPdf.close();
 | 
					 | 
				
			||||||
byte[] pdfContent = baos.toByteArray();
 | 
					byte[] pdfContent = baos.toByteArray();
 | 
				
			||||||
		pdfDoc.close();
 | 
					return WebResponseUtils.bytesToWebResponse(pdfContent, file.getOriginalFilename().replaceFirst("[.][^.]+$", "") + "_cropped.pdf");
 | 
				
			||||||
		return WebResponseUtils.bytesToWebResponse(pdfContent,
 | 
					 | 
				
			||||||
				file.getOriginalFilename().replaceFirst("[.][^.]+$", "") + "_cropped.pdf");
 | 
					 | 
				
			||||||
	}
 | 
						}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
				
			|||||||
		Loading…
	
		Reference in New Issue
	
	Block a user