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

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

這篇具有很好參考價(jià)值的文章主要介紹了JAVA 實(shí)現(xiàn)PDF轉(zhuǎn)圖片(pdfbox版)。希望對(duì)大家有所幫助。如果存在錯(cuò)誤或未考慮完全的地方,請(qǐng)大家不吝賜教,您也可以點(diǎn)擊"舉報(bào)違法"按鈕提交疑問。

依賴:

JAVA 實(shí)現(xiàn)PDF轉(zhuǎn)圖片(pdfbox版),java,pdf,開發(fā)語言

pdf存放路徑

JAVA 實(shí)現(xiàn)PDF轉(zhuǎn)圖片(pdfbox版),java,pdf,開發(fā)語言

正文開始:

pdf轉(zhuǎn)換多張圖片、長圖文章來源地址http://www.zghlxwxcb.cn/news/detail-740548.html

@Test
void pdf2Image() {
        String dstImgFolder = "";
        String PdfFilePath = "";
 
        String relativelyPath=System.getProperty("user.dir");
        PdfFilePath = relativelyPath + "/uploadTest/"+"文檔.pdf";
        dstImgFolder = relativelyPath + "/uploadTest/";
        /* dpi越大轉(zhuǎn)換后越清晰,相對(duì)轉(zhuǎn)換速度越慢 */
        int dpi = 450;
        File file = new File(PdfFilePath);
        PDDocument pdDocument; // 創(chuàng)建PDF文檔
        try {
            String imgPDFPath = file.getParent();
            int dot = file.getName().lastIndexOf('.');
            String imagePDFName = file.getName().substring(0, dot); // 獲取圖片文件名
            String imgFolderPath = null;
            if (dstImgFolder.equals("")) {
                imgFolderPath = imgPDFPath + File.separator;// 獲取圖片存放的文件夾路徑
            } else {
                imgFolderPath = dstImgFolder + File.separator;
            }
            if (createDirectory(imgFolderPath)) {
                pdDocument = PDDocument.load(file);
                PDFRenderer renderer = new PDFRenderer(pdDocument);
                PdfReader reader = new PdfReader(PdfFilePath);
                int pages = reader.getNumberOfPages();
                StringBuffer imgFilePath = null;
               BufferedImage[] bufferedImages = new BufferedImage[pages];
                for (int i = 0; i < pages; i++) {
                    String imgFilePathPrefix = imgFolderPath + File.separator;
                    imgFilePath = new StringBuffer();
                    imgFilePath.append(imgFilePathPrefix);
                    imgFilePath.append("_");
                    imgFilePath.append(i + 1);
                    imgFilePath.append(".png");
                    // File dstFile = new File(imgFilePath.toString());
                    BufferedImage image = renderer.renderImageWithDPI(i, dpi);
                   bufferedImages[i] = image;
                    // ImageIO.write(image, "png", dstFile);
                }
                dstImgFolder = dstImgFolder + imagePDFName + ".png";
                // PDF文件全部頁數(shù)轉(zhuǎn)PNG圖片,若多張展示注釋即可 工具類貼在下面
               ImageMergeUtil.mergeImage(bufferedImages, 2, dstImgFolder);
                System.out.println("PDF文檔轉(zhuǎn)PNG圖片成功!");
            } else {
                System.out.println("PDF文檔轉(zhuǎn)PNG圖片失敗:" + "創(chuàng)建" + imgFolderPath + "失敗");
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
 
    private static boolean createDirectory(String folder) {
        File dir = new File(folder);
        if (dir.exists()) {
            return true;
        } else {
            return dir.mkdirs();
        }
    }

// ImageMergeUtil 圖片的合并,多張圖片合成長圖
import javax.imageio.ImageIO;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
 
public class ImageMergeUtil {
    public static void main(String[] args) throws IOException {
        String filePath = "D:\\temp\\ImageMergeUtil\\";
        String path1 = filePath + "a.png";
        String path2 = filePath + "b.png";
        mergeImage(path1, path2,  2, filePath+"c.png");
    }
 
    /**
     * 圖片拼接
     * @param path1     圖片1路徑
     * @param path2     圖片2路徑
     * @param type      1 橫向拼接, 2 縱向拼接
     * (注意:必須兩張圖片長寬一致)
     */
    public static void mergeImage( String path1, String path2, int type, String targetFile) throws IOException {
        File file1 = new File(path1);
        File file2 = new File(path2);
        //兩張圖片的拼接
        int len = 2;
        BufferedImage[] images = new BufferedImage[len];
        images[0] = ImageIO.read(file1);
        images[1] = ImageIO.read(file2);
        mergeImage(images, type, targetFile);
 
    }
 
    /**
     * 圖片拼接
     * @param images     圖片數(shù)組
     * @param type      1 橫向拼接, 2 縱向拼接
     * (注意:必須兩張圖片長寬一致)
     */
    public static void mergeImage(BufferedImage[] images, int type, String targetFile) throws IOException {
        int len = images.length;
        int[][] ImageArrays = new int[len][];
 
        for (int i = 0; i < len; i++) {
            int width = images[i].getWidth();
            int height = images[i].getHeight();
            ImageArrays[i] = new int[width * height];
            ImageArrays[i] = images[i].getRGB(0, 0, width, height, ImageArrays[i], 0, width);
        }
        int newHeight = 0;
        int newWidth = 0;
        for (int i = 0; i < images.length; i++) {
            // 橫向
            if (type == 1) {
                newHeight = newHeight > images[i].getHeight() ? newHeight : images[i].getHeight();
                newWidth += images[i].getWidth();
            } else if (type == 2) {// 縱向
                newWidth = newWidth > images[i].getWidth() ? newWidth : images[i].getWidth();
                newHeight += images[i].getHeight();
            }
        }
        if (type == 1 && newWidth < 1) {
            return;
        }
        if (type == 2 && newHeight < 1) {
            return;
        }
        // 生成新圖片
        try {
            BufferedImage ImageNew = new BufferedImage(newWidth, newHeight, BufferedImage.TYPE_INT_RGB);
            int height_i = 0;
            int width_i = 0;
            for (int i = 0; i < images.length; i++) {
                if (type == 1) {
                    ImageNew.setRGB(width_i, 0, images[i].getWidth(), newHeight, ImageArrays[i], 0,
                            images[i].getWidth());
                    width_i += images[i].getWidth();
                } else if (type == 2) {
                    ImageNew.setRGB(0, height_i, newWidth, images[i].getHeight(), ImageArrays[i], 0, newWidth);
                    height_i += images[i].getHeight();
                }
            }
 
            //輸出想要的圖片
            ImageIO.write(ImageNew, "png", new File(targetFile));
        } catch (Exception e) {
 
            e.printStackTrace();
        }
    }

展示效果:

JAVA 實(shí)現(xiàn)PDF轉(zhuǎn)圖片(pdfbox版),java,pdf,開發(fā)語言

附加:小程序預(yù)覽wxml代碼

JAVA 實(shí)現(xiàn)PDF轉(zhuǎn)圖片(pdfbox版),java,pdf,開發(fā)語言

依賴:

JAVA 實(shí)現(xiàn)PDF轉(zhuǎn)圖片(pdfbox版),java,pdf,開發(fā)語言

pdf存放路徑

JAVA 實(shí)現(xiàn)PDF轉(zhuǎn)圖片(pdfbox版),java,pdf,開發(fā)語言

正文開始:

pdf轉(zhuǎn)換多張圖片、長圖

到了這里,關(guān)于JAVA 實(shí)現(xiàn)PDF轉(zhuǎn)圖片(pdfbox版)的文章就介紹完了。如果您還想了解更多內(nèi)容,請(qǐng)?jiān)谟疑辖撬阉鱐OY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!

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

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

相關(guān)文章

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

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

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

    2024年02月20日
    瀏覽(39)
  • java利用pdfbox動(dòng)態(tài)生成PDF

    Apache?PDFBox?是一個(gè)用于處理?PDF?文檔的?Java?庫。它提供了許多功能和方法來讀取、創(chuàng)建、操作和提取?PDF?文檔的內(nèi)容。 PDDocument?類 引用源碼中對(duì)PDDocument?類的描述 This?is?the?in-memory?representation?of?the?PDF?document 這是PDF文檔的內(nèi)存表示,在?java?程序中,你可以簡單理解

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

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

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

    Springboot使用pdfbox提取PDF圖片

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

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

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

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

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

    這篇文章,主要介紹PDFBox操作PDF文檔之添加本地圖片、添加網(wǎng)絡(luò)圖片、圖片寬高自適應(yīng)、圖片水平垂直居中對(duì)齊。 目錄 一、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)
  • Java實(shí)現(xiàn)圖片轉(zhuǎn)PDF

    某w*s圖片轉(zhuǎn)PDF還要收費(fèi),簡直不講武德!我啪的一下,很快啊,一段代碼搞定! 引入pom依賴 工具類

    2024年01月15日
    瀏覽(27)
  • JAVA 實(shí)現(xiàn)PDF轉(zhuǎn)圖片(spire.pdf.free版)

    JAVA 實(shí)現(xiàn)PDF轉(zhuǎn)圖片(spire.pdf.free版)

    1. 引入 jar 包 導(dǎo)入方法1: 手動(dòng)引入。將Free Spire.PDF for Java下載到本地,解壓,找到lib文件夾下的Spire.PDF.jar文件。在IDEA中打開如下界面,將本地路徑中的jar文件引入Java程序: ?導(dǎo)入方法2:如果您想通過?Maven安裝,則可以在 pom.xml 文件中添加以下代碼導(dǎo)入 JAR 文件。 1 2 3 4 5

    2024年02月05日
    瀏覽(29)
  • pdf轉(zhuǎn)圖片【java版實(shí)現(xiàn)】

    引入需要導(dǎo)入到項(xiàng)目中的依賴,如下所示: pdf轉(zhuǎn)圖片的工具類如下所示,直接拷貝到項(xiàng)目即可 執(zhí)行工具類中的main方法就行,會(huì)將pdf文件轉(zhuǎn)換成多張圖片到同級(jí)目錄中。

    2024年02月13日
    瀏覽(21)
  • JAVA 實(shí)現(xiàn)PDF轉(zhuǎn)圖片格式

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

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

    2024年02月06日
    瀏覽(17)

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

支付寶掃一掃打賞

博客贊助

微信掃一掃打賞

請(qǐng)作者喝杯咖啡吧~博客贊助

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

二維碼1

領(lǐng)取紅包

二維碼2

領(lǐng)紅包