国产 无码 综合区,色欲AV无码国产永久播放,无码天堂亚洲国产AV,国产日韩欧美女同一区二区

Java 利用pdfbox將圖片和成到pdf指定位置

這篇具有很好參考價(jià)值的文章主要介紹了Java 利用pdfbox將圖片和成到pdf指定位置。希望對大家有所幫助。如果存在錯誤或未考慮完全的地方,請大家不吝賜教,您也可以點(diǎn)擊"舉報(bào)違法"按鈕提交疑問。

業(yè)務(wù)背景:用戶在手機(jī)APP上進(jìn)行簽名,前端將簽完名字的圖片傳入后端,后端合成新的pdf.

廢話不多說,上代碼:

//控制層代碼
    @PostMapping("/imageToPdf")
    public Result imageToPdf(@RequestParam("linkName") String name, @RequestParam("file") 
                                               MultipartFile file) throws IOException {
        try {
            String format = LocalDateTimeUtil.format(LocalDateTime.now(), 
            DatePattern.PURE_DATETIME_MS_PATTERN);
            File f = convertMultipartFileToFile(file);
            //縮放圖片
            ImgUtil.scale(FileUtil.file(f), FileUtil.file("/hetong/dev/image/" + format + 
              ".png"), 0.06f);
            // 旋轉(zhuǎn)270度
            BufferedImage image = (BufferedImage) 
//此處利用hutool工具類進(jìn)行縮放
            ImgUtil.rotate(ImageIO.read(FileUtil.file("/hetong/dev/image/" + format + 
            ".png")), 270);
            ImgUtil.write(image, FileUtil.file("/hetong/dev/image/" + format + "_result" + 
            ".png"));
            String imagePath = "/hetong/dev/image/" + format + "_result" + ".png";
            String fileUrl = name; // 要下載的文件的HTTPS鏈接
            String localFilePath = "/hetong/dev/" + format + ".pdf"; // 下載文件保存到本地的路徑和名稱
            //String localFilePath = "D:\\soft\\ceshi\\" + format + ".pdf"; // 下載文件保存到本地的路徑和名稱
            downloadFile(fileUrl, localFilePath);
            String pdfPath = localFilePath;
            String url = imageToPdfUtil.addImageToPDF(imagePath, pdfPath, format);
            return new Result(ResultCode.SUCCESS, url);
        } catch (IOException e) {
            e.printStackTrace();
        }
        return new Result(ResultCode.IMAGE_MERGE_FAILED, "");
    }


//工具類
@Component
public class ImageToPdfUtil {
    private static Logger logger = LoggerFactory.getLogger(WordUtil.class);
    @Autowired
    private UploadManager uploadManager;
    @Autowired
    private Auth auth;

    @Value("${qiniu.bucketName}")
    private String bucketName;
    @Value("${qiniu.path}")
    private String url;

    /**
     * 添加圖片至pdf指定位置
     *
     * @param imagePath
     * @param pdfPath
     * @throws IOException
     */
    public String addImageToPDF(String imagePath, String pdfPath, String format) throws IOException {
        // 加載圖片
        PDDocument document = PDDocument.load(new File(pdfPath));
        PDPage lastPage = document.getPage(document.getNumberOfPages() - 1);
        PDImageXObject image = PDImageXObject.createFromFile(imagePath, document);

        // 獲取圖片寬度和高度
        float imageWidth = image.getWidth();
        float imageHeight = image.getHeight();

        try (PDPageContentStream contentStream = new PDPageContentStream(document, lastPage, PDPageContentStream.AppendMode.APPEND, true, true)) {
            // 設(shè)置圖片位置
            float x = (lastPage.getMediaBox().getWidth() / 2f - imageWidth / 2f) * 0.88f;
            float y = (lastPage.getMediaBox().getHeight() / 2f - imageHeight / 2f) * 1.5f;

            // 添加圖片到指定位置
            contentStream.drawImage(image, x, y, imageWidth, imageHeight);
        }

        // 保存修改后的 PDF
        document.save(pdfPath);
        document.close();
        return uploadQiniu(format);
    }

    /**
     * 上傳到七牛云
     *
     * @param fileName
     * @return
     * @throws FileNotFoundException
     * @throws QiniuException
     */
    public String uploadQiniu(String fileName) throws FileNotFoundException, QiniuException {
        //這里是上傳到服務(wù)器路徑下的,已經(jīng)填充完數(shù)據(jù)的word
        File file = ResourceUtils.getFile("/hetong/dev/" + fileName + ".pdf");
        //File file = ResourceUtils.getFile("D:\\soft\\ceshi\\" + fileName + ".pdf");
        //FileInputStream uploadFile = (FileInputStream) file.getInputStream();
        // 獲取文件輸入流
        FileInputStream inputStream = new FileInputStream(file);

        //這個是已經(jīng)填充完整的數(shù)據(jù)后上傳至七牛云鏈接
        String path = "http://" + upload(inputStream, fileName);
        logger.info("上傳至七牛云的合同路徑==path==" + path);
        return path;
    }

    public String upload(FileInputStream file, String fileName) throws QiniuException {
        String token = auth.uploadToken(bucketName);
        Response res = uploadManager.put(file, fileName, token, null, null);
        if (!res.isOK()) {
            throw new RuntimeException("上傳七牛云出錯:" + res);
        }
        return url + "/" + fileName;
    }

    public static void main(String[] args) {
        try {
            // 指定原始圖片路徑
            File originalImage = new File("D:\\soft\\ceshi\\20230901204201606.png");

            // 指定壓縮后保存的目標(biāo)路徑
            File compressedImage = new File("D:\\soft\\ceshi\\20230901204201606_result.png");

            // 壓縮和縮放圖片
            Thumbnails.of(originalImage)
                    .scale(0.1) // 縮放比例為50%
                    .outputQuality(0.9) // 圖片質(zhì)量為80%
                    .toFile(compressedImage);

            System.out.println("圖片壓縮和縮放完成!");
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    /**
     * MultipartFile 轉(zhuǎn)file
     *
     * @param multipartFile
     * @return
     * @throws IOException
     */
    public static File convertMultipartFileToFile(MultipartFile multipartFile) throws IOException {
        byte[] fileBytes = multipartFile.getBytes();

        File tempFile = File.createTempFile("temp", null);
        try (OutputStream outputStream = new FileOutputStream(tempFile)) {
            outputStream.write(fileBytes);
        }
        return tempFile;
    }

    /**
     * 根號https鏈接下載文檔
     *
     * @param fileUrl
     * @param localFilePath
     * @throws IOException
     */
    public static void downloadFile(String fileUrl, String localFilePath) throws IOException {
        URL url = new URL(fileUrl);
        HttpURLConnection connection = (HttpURLConnection) url.openConnection();
        connection.setRequestMethod("GET");

        BufferedInputStream inputStream = new BufferedInputStream(connection.getInputStream());
        FileOutputStream outputStream = new FileOutputStream(localFilePath);

        byte[] buffer = new byte[1024];
        int bytesRead;
        while ((bytesRead = inputStream.read(buffer)) != -1) {
            outputStream.write(buffer, 0, bytesRead);
        }
        outputStream.close();
        inputStream.close();
        connection.disconnect();
    }

}

注意:前端傳過來的圖片必須是透明的,否則合成的時候簽名處會有邊框
? ? ?

?文章來源地址http://www.zghlxwxcb.cn/news/detail-703502.html

到了這里,關(guān)于Java 利用pdfbox將圖片和成到pdf指定位置的文章就介紹完了。如果您還想了解更多內(nèi)容,請?jiān)谟疑辖撬阉鱐OY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!

本文來自互聯(lián)網(wǎng)用戶投稿,該文觀點(diǎn)僅代表作者本人,不代表本站立場。本站僅提供信息存儲空間服務(wù),不擁有所有權(quán),不承擔(dān)相關(guān)法律責(zé)任。如若轉(zhuǎn)載,請注明出處: 如若內(nèi)容造成侵權(quán)/違法違規(guī)/事實(shí)不符,請點(diǎn)擊違法舉報(bào)進(jìn)行投訴反饋,一經(jīng)查實(shí),立即刪除!

領(lǐng)支付寶紅包贊助服務(wù)器費(fèi)用

相關(guān)文章

  • JAVA 實(shí)現(xiàn)PDF轉(zhuǎn)圖片(pdfbox版)

    JAVA 實(shí)現(xiàn)PDF轉(zhuǎn)圖片(pdfbox版)

    依賴: pdf存放路徑 正文開始: pdf轉(zhuǎn)換多張圖片、長圖 展示效果: 附加:小程序預(yù)覽wxml代碼 依賴: pdf存放路徑 正文開始: pdf轉(zhuǎn)換多張圖片、長圖

    2024年02月06日
    瀏覽(26)
  • Java使用pdfbox進(jìn)行pdf和圖片之間的轉(zhuǎn)換

    Java使用pdfbox進(jìn)行pdf和圖片之間的轉(zhuǎn)換

    pdfbox是Apache開源的一個項(xiàng)目,支持pdf文檔操作功能。 官網(wǎng)地址:?Apache PDFBox | A Java PDF Library 支持的功能如下圖. 引入依賴

    2024年02月06日
    瀏覽(25)
  • java中pdfbox處理pdf常用方法(讀取、寫入、合并、拆分、寫文字、寫圖片)

    java中pdfbox處理pdf常用方法(讀取、寫入、合并、拆分、寫文字、寫圖片)

    方法代碼: 測試用例: 2.1寫文字 方法代碼: 測試用例: A.pdf: A2.pdf: 2.2寫圖片 方法代碼: 測試用例: A.pdf: pic.jpg: A2.pdf: 方法代碼: 測試用例: 方法代碼: 測試用例: 引用鏈接: (17條消息) 使用Apache PDFBox實(shí)現(xiàn)拆分、合并PDF_似有風(fēng)中泣的博客-CSDN博客 (17條消息) Java使用P

    2024年02月11日
    瀏覽(32)
  • 【PDFBox】PDFBox操作PDF文檔之讀取指定頁面文本內(nèi)容、讀取所有頁面文本內(nèi)容、根據(jù)模板文件生成PDF文檔

    【PDFBox】PDFBox操作PDF文檔之讀取指定頁面文本內(nèi)容、讀取所有頁面文本內(nèi)容、根據(jù)模板文件生成PDF文檔

    這篇文章,主要介紹PDFBox操作PDF文檔之讀取指定頁面文本內(nèi)容、讀取所有頁面文本內(nèi)容、根據(jù)模板文件生成PDF文檔。 目錄 一、PDFBox操作文本 1.1、讀取所有頁面文本內(nèi)容 1.2、讀取指定頁面文本內(nèi)容 1.3、寫入文本內(nèi)容 1.4、替換文本內(nèi)容 (1)自定義PDTextStripper類 (2)創(chuàng)建Key

    2024年02月16日
    瀏覽(23)
  • 【Java】OpenPDF、iText、PDFBox 是三種常用的 PDF 處理庫

    OpenPDF、iText、PDFBox 是三種常用的 PDF 處理庫,它們各自具有獨(dú)特的優(yōu)勢和特點(diǎn),同時也存在一些局限性和差異。本文將對這四種庫進(jìn)行詳細(xì)的比較,并通過代碼示例來展示它們的使用。 1、OpenPDF OpenPDF 是一個用于創(chuàng)建和編輯 PDF 文檔的 Java 庫,它基于 iText 庫的一個分支,提供

    2024年02月09日
    瀏覽(34)
  • python 利用word中占位符號實(shí)現(xiàn)按word指定位置插入圖片

    from docx import Document from docx.shared import Inches from docx.oxml.ns import qn from docx.enum.text import WD_ALIGN_PARAGRAPH def center_insert_img(doc, img): ? ? \\\"\\\"\\\"插入圖片\\\"\\\"\\\" ? ? for paragraph in doc.paragraphs: ? ? ? ? # 根據(jù)文檔中的占位符定位圖片插入的位置 ? ? ? ? if \\\'img1\\\' in paragraph.text: ? ? ? ? ? ? # 把占

    2024年02月11日
    瀏覽(29)
  • 如何通過Java的Apache PDFBox庫制作一個PDF表格模板并填充數(shù)據(jù)

    要使用Java的Apache PDFBox庫制作一個PDF表格模板并填充數(shù)據(jù),你需要遵循以下步驟: 添加依賴 :首先,確保你的項(xiàng)目中包含了Apache PDFBox的依賴。如果你使用Maven,可以在你的 pom.xml 文件中添加以下依賴: 創(chuàng)建PDF模板 :你可以使用PDFBox創(chuàng)建一個簡單的PDF模板,或者使用其他工具

    2024年02月22日
    瀏覽(24)
  • 【PDFBox】PDFBox操作PDF文檔之添加本地圖片、添加網(wǎng)絡(luò)圖片、圖片寬高自適應(yīng)、圖片水平垂直居中對齊

    【PDFBox】PDFBox操作PDF文檔之添加本地圖片、添加網(wǎng)絡(luò)圖片、圖片寬高自適應(yīng)、圖片水平垂直居中對齊

    這篇文章,主要介紹PDFBox操作PDF文檔之添加本地圖片、添加網(wǎng)絡(luò)圖片、圖片寬高自適應(yīng)、圖片水平垂直居中對齊。 目錄 一、PDFBox操作圖片 1.1、添加本地圖片 (1)案例代碼 (2)運(yùn)行效果 (3)方法介紹 1.2、添加網(wǎng)絡(luò)圖片 (1)案例代碼 (2)運(yùn)行效果 1.3、圖片寬高自適應(yīng)(

    2024年02月16日
    瀏覽(40)
  • Springboot使用pdfbox提取PDF圖片

    Springboot使用pdfbox提取PDF圖片

    PDFBox是一個用于創(chuàng)建和處理PDF文檔的Java庫。它可以使用Java代碼創(chuàng)建、讀取、修改和提取PDF文檔中的內(nèi)容。 PDFBox的功能: Extract Text - 使用PDFBox,您可以從PDF文件中提取Unicode文本。 Split Merge - 使用PDFBox,您可以將單個PDF文件分成多個文件,并將它們合并為一個文件。 Fill Forms

    2024年02月10日
    瀏覽(22)
  • Java實(shí)現(xiàn)自動化pdf打水印小項(xiàng)目 使用技術(shù)pdfbox、Documents4j

    Java實(shí)現(xiàn)自動化pdf打水印小項(xiàng)目 使用技術(shù)pdfbox、Documents4j

    博主介紹:?目前全網(wǎng)粉絲2W+,csdn博客專家、Java領(lǐng)域優(yōu)質(zhì)創(chuàng)作者,博客之星、阿里云平臺優(yōu)質(zhì)作者、專注于Java后端技術(shù)領(lǐng)域。 涵蓋技術(shù)內(nèi)容:Java后端、算法、分布式微服務(wù)、中間件、前端、運(yùn)維、ROS等。 博主所有博客文件目錄索引:博客目錄索引(持續(xù)更新) 視頻平臺:

    2024年02月20日
    瀏覽(39)

覺得文章有用就打賞一下文章作者

支付寶掃一掃打賞

博客贊助

微信掃一掃打賞

請作者喝杯咖啡吧~博客贊助

支付寶掃一掃領(lǐng)取紅包,優(yōu)惠每天領(lǐng)

二維碼1

領(lǐng)取紅包

二維碼2

領(lǐng)紅包