Java实现图片保存到pdf的某个位置
1、依赖–maven
< dependency> < groupId> com.itextpdf</ groupId> < artifactId> itextpdf</ artifactId> < version> 5.5.13</ version> </ dependency>
2、上代码
package com. hxlinks. hxiot. controller ; import com. fasterxml. jackson. databind. JsonNode ;
import com. fasterxml. jackson. databind. ObjectMapper ;
import com. itextpdf. text. DocumentException ;
import com. itextpdf. text. Image ;
import com. itextpdf. text. pdf. PdfContentByte ;
import com. itextpdf. text. pdf. PdfImportedPage ;
import com. itextpdf. text. pdf. PdfReader ;
import com. itextpdf. text. pdf. PdfStamper ;
import org. springframework. http. HttpStatus ;
import org. springframework. http. ResponseEntity ;
import org. springframework. stereotype. Controller ;
import org. springframework. web. bind. annotation. PostMapping ;
import org. springframework. web. bind. annotation. RequestBody ;
import org. springframework. web. bind. annotation. RestController ; import java. awt. geom. Point2D ;
import java. io. * ;
import java. nio. file. Files ;
import java. nio. file. Path ;
import java. nio. file. Paths ;
import java. nio. file. StandardCopyOption ;
import java. util. Base64 ; @Controller
public class PdfApiController { @PostMapping ( "/api/save-image-to-pdf" ) public ResponseEntity < String > saveImageToPdf ( @RequestBody JsonNode jsonNode) { try { int pageNumber = jsonNode. get ( "pageNumber" ) . asInt ( ) ; Point2D. Float position = new Point2D. Float ( jsonNode. get ( "imagePosition" ) . get ( "x" ) . floatValue ( ) , jsonNode. get ( "imagePosition" ) . get ( "y" ) . floatValue ( ) ) ; String base64Image = jsonNode. get ( "base64Image" ) . toString ( ) ; byte [ ] imageBytes = Base64 . getDecoder ( ) . decode ( base64Image. split ( "," ) [ 1 ] ) ; File originalPdf = new File ( "D:\\templateFilePath\\测试.pdf" ) ; File tempPdf = File . createTempFile ( "temp" , ".pdf" ) ; insertImageIntoPdf ( originalPdf, tempPdf, pageNumber, position, imageBytes) ; downloadPdf ( tempPdf, "modified_pdf_page_" + pageNumber + ".pdf" ) ; return ResponseEntity . ok ( "图片已成功添加到PDF并保存到D盘。" ) ; } catch ( IOException | DocumentException e) { e. printStackTrace ( ) ; return ResponseEntity . status ( HttpStatus . INTERNAL_SERVER_ERROR ) . body ( "处理PDF时出错。" ) ; } } private void insertImageIntoPdf ( File originalPdf, File tempPdf, int pageNumber, Point2D. Float position, byte [ ] imageBytes) throws IOException , DocumentException { PdfReader reader = new PdfReader ( new FileInputStream ( originalPdf) ) ; PdfStamper stamper = new PdfStamper ( reader, new FileOutputStream ( tempPdf) ) ; PdfContentByte canvas = stamper. getOverContent ( pageNumber) ; Image img = Image . getInstance ( imageBytes) ; img. setAbsolutePosition ( position. x, position. y) ; img. scaleToFit ( 100 , 100 ) ; canvas. addImage ( img) ; stamper. close ( ) ; reader. close ( ) ; } private void downloadPdf ( File tempPdf, String fileName) throws IOException { Path sourcePath = tempPdf. toPath ( ) ; Path targetDir = Paths . get ( "D:/" ) ; Path targetPath = targetDir. resolve ( System . currentTimeMillis ( ) + ".pdf" ) ; if ( Files . exists ( targetPath) ) { throw new IOException ( "目标文件已存在:" + targetPath) ; } Files . createDirectories ( targetDir) ; Files . move ( sourcePath, targetPath, StandardCopyOption . REPLACE_EXISTING ) ; } }
3、jsonNode参数,根据需要调整
{ "pageNumber" : 2 , "base64Image" : "data:image/png;base64,i..................." , "imagePosition" : { "x" : 100 , "y" : 0 } , "imageSize" : { "width" : 100 , "height" : 100 }
}