精华内容
下载资源
问答
  • java Pdf转换图片

    2019-04-19 08:57:08
    javaPDF转图片 能进行缩放 进行将多页图片合成为一张图片
  • java PDF转图片

    2016-06-20 14:40:55
    PDF转换图片(多页多张) java实现多页PDF转换多张图片
  • javaPDF转图片

    2015-07-07 18:07:07
    java实现 PDF提取图片生成图片 PDF转换各种格式图片
  • java pdf转图片

    2013-08-23 15:57:15
    java工程 pdf转图片 可指定转换某一页
  • Java PDF转图片

    2019-09-24 05:19:05
    "PDF文档PNG图片失败:" + "创建" + imgFolderPath + "失败" ); } } catch (IOException e) { e.printStackTrace(); } } private static boolean createDirectory(String folder) { File dir = ...

    maven依赖:

          <dependency>
                <groupId>org.apache.pdfbox</groupId>
                <artifactId>pdfbox</artifactId>
                <version>2.0.4</version>
                <scope>test</scope>
            </dependency>
            <dependency>
                <groupId>com.lowagie</groupId>
                <artifactId>itext</artifactId>
                <version>2.1.7</version>
            </dependency>    
    import java.awt.image.BufferedImage;
    import java.io.File;
    import java.io.IOException;
    
    import javax.imageio.ImageIO;
    
    import org.apache.pdfbox.pdmodel.PDDocument;
    import org.apache.pdfbox.rendering.PDFRenderer;
    
    import com.lowagie.text.pdf.PdfReader;
    public class test1 {
    
        public static void main(String[] args) {
                pdf2Image("D:/1.pdf", "D:/pdf", 130);
            }
    
            /***
             * PDF文件转PNG图片,全部页数
             *
             * @param PdfFilePath pdf完整路径
             * @param imgFilePath 图片存放的文件夹
             * @param dpi dpi越大转换后越清晰,相对转换速度越慢
             * @return
             */
            public static void pdf2Image(String PdfFilePath, String dstImgFolder, int dpi) {
                File file = new File(PdfFilePath);
                PDDocument pdDocument;
                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 + imagePDFName;// 获取图片存放的文件夹路径
                    } else {
                        imgFolderPath = dstImgFolder + File.separator + imagePDFName;
                    }
    
                    if (createDirectory(imgFolderPath)) {
    
                        pdDocument = PDDocument.load(file);
                        PDFRenderer renderer = new PDFRenderer(pdDocument);
                    /* dpi越大转换后越清晰,相对转换速度越慢 */
                        PdfReader reader = new PdfReader(PdfFilePath);
                        int pages = reader.getNumberOfPages();
                        StringBuffer imgFilePath = null;
                        for (int i = 0; i < pages; i++) {
                            String imgFilePathPrefix = imgFolderPath + File.separator + imagePDFName;
                            imgFilePath = new StringBuffer();
                            imgFilePath.append(imgFilePathPrefix);
                            imgFilePath.append("_");
                            imgFilePath.append(String.valueOf(i + 1));
                            imgFilePath.append(".png");
                            File dstFile = new File(imgFilePath.toString());
                            BufferedImage image = renderer.renderImageWithDPI(i, dpi);
                            ImageIO.write(image, "png", dstFile);
                        }
                        System.out.println("PDF文档转PNG图片成功!");
    
                    } else {
                        System.out.println("PDF文档转PNG图片失败:" + "创建" + 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();
                }
            }
    
    
    }

     

    转载于:https://www.cnblogs.com/haciont/p/10592692.html

    展开全文
  • Java PDF 转图片

    2017-11-16 16:58:04
    package com.hbzx.utils; import java.awt.image.BufferedImage; import java.io.File; import java.io.IOException; import java.util.ArrayList; import java.util.List; import org.apache.pdfbox.pdmodel.PDDo
    package com.hbzx.utils;
    
    import java.awt.image.BufferedImage;
    import java.io.File;
    import java.io.IOException;
    import java.util.ArrayList;
    import java.util.List;
    
    import org.apache.pdfbox.pdmodel.PDDocument;
    import org.apache.pdfbox.pdmodel.PDPage;
    import org.apache.pdfbox.pdmodel.encryption.InvalidPasswordException;
    import org.apache.pdfbox.rendering.ImageType;
    import org.apache.pdfbox.rendering.PDFRenderer;
    import org.apache.pdfbox.tools.imageio.ImageIOUtil;
    import org.junit.Test;
    
    public class PDFToJpg {
    
    	@Test
    	public void pdfToJpg() throws InvalidPasswordException, IOException {
    		String pdfFilename = "C:\\Users\\aimil\\Desktop\\2.pdf";
    		PDDocument document = PDDocument.load(new File(pdfFilename));
    		PDFRenderer pdfRenderer = new PDFRenderer(document);
    		List<BufferedImage> images = new ArrayList<>();
    		int pageCounter = 0;
    		for (PDPage page : document.getPages()) {
    			// note that the page number parameter is zero based
    			BufferedImage bim = pdfRenderer.renderImageWithDPI(pageCounter++, 300, ImageType.RGB);
    			images.add(bim);
    		}
    		
    		BufferedImage result = mergeImage(false,images.toArray(new BufferedImage[0]));
    		ImageIOUtil.writeImage(result, pdfFilename + "-" + (pageCounter++) + ".png", 300);
    		document.close();
    	}
    
    	/**
    	 * 合并任数量的图片成一张图片
    	 * 
    	 * @param isHorizontal
    	 *            true代表水平合并,fasle代表垂直合并
    	 * @param imgs
    	 *            欲合并的图片数组
    	 * @return
    	 * @throws IOException
    	 */
    	public static BufferedImage mergeImage(boolean isHorizontal, BufferedImage... imgs) throws IOException {
    		// 生成新图片
    		BufferedImage destImage = null;
    
    		// 计算新图片的长和高
    		int allw = 0, allh = 0, allwMax = 0, allhMax = 0;
    		for (BufferedImage img : imgs) {
    			allw += img.getWidth();
    			allh += img.getHeight();
    			if (img.getWidth() > allwMax) {
    				allwMax = img.getWidth();
    			}
    			if (img.getHeight() > allhMax) {
    				allhMax = img.getHeight();
    			}
    		}
    		// 创建新图片
    		if (isHorizontal) {
    			destImage = new BufferedImage(allw, allhMax, BufferedImage.TYPE_INT_RGB);
    		} else {
    			destImage = new BufferedImage(allwMax, allh, BufferedImage.TYPE_INT_RGB);
    		}
    
    		// 合并所有子图片到新图片
    		int wx = 0, wy = 0;
    		for (int i = 0; i < imgs.length; i++) {
    			BufferedImage img = imgs[i];
    			int w1 = img.getWidth();
    			int h1 = img.getHeight();
    			// 从图片中读取RGB
    			int[] ImageArrayOne = new int[w1 * h1];
    			ImageArrayOne = img.getRGB(0, 0, w1, h1, ImageArrayOne, 0, w1); // 逐行扫描图像中各个像素的RGB到数组中
    			if (isHorizontal) { // 水平方向合并
    				destImage.setRGB(wx, 0, w1, h1, ImageArrayOne, 0, w1); // 设置上半部分或左半部分的RGB
    			} else { // 垂直方向合并
    				destImage.setRGB(0, wy, w1, h1, ImageArrayOne, 0, w1); // 设置上半部分或左半部分的RGB
    			}
    			wx += w1;
    			wy += h1;
    		}
    		return destImage;
    	}
    }
    

    展开全文
  • Java pdf 图片

    2019-03-22 14:06:00
    * @return pdf 图片流 */ public static byte[] getImageFromPdf(File pdf, String format) { return pdfHomePageToImage(getFromFile(pdf), format); } /** * Pdf -> Image (指定页) * * @param pdf pdf文件...

    maven 依赖:

    <dependency>
      <groupId>org.apache.pdfbox</groupId>
      <artifactId>pdfbox</artifactId>
      <version>2.0.8</version>
    </dependency>
    <dependency>
      <groupId>org.apache.pdfbox</groupId>
      <artifactId>pdfbox-tools</artifactId>
      <version>2.0.8</version>
    </dependency>
    

    代码示例:

    private static final int HOME_PAGE_INDEX = 0;
    
    /**
     * Pdf -> Image (首页)
     *
     * [@param](https://my.oschina.net/u/2303379) pdf    pdf流
     * [@param](https://my.oschina.net/u/2303379) format 图片格式
     * [@return](https://my.oschina.net/u/556800) pdf 图片流
     */
    public static byte[] getImageFromPdf(byte[] pdf, String format) {
        return pdfHomePageToImage(getFromByteArray(pdf), format);
    }
    
    /**
     * Pdf -> Image (首页)
     *
     * [@param](https://my.oschina.net/u/2303379) pdf    pdf文件
     * [@param](https://my.oschina.net/u/2303379) format 图片格式
     * @return pdf 图片流
     */
    public static byte[] getImageFromPdf(File pdf, String format) {
        return pdfHomePageToImage(getFromFile(pdf), format);
    }
    
    /**
     * Pdf -> Image (指定页)
     *
     * @param pdf       pdf文件
     * @param pageIndex 页号(页号从0开始)
     * @param format    图片格式
     * @return pdf 图片流
     */
    public static byte[] getImageFromPdf(File pdf, int pageIndex, String format) {
        return pdfToImageWithIndex(getFromFile(pdf), pageIndex, pageIndex, format).get(0);
    }
    
    /**
     * Pdf -> Image (指定页)
     *
     * @param pdf       pdf流
     * @param pageIndex 起始页号(页号从0开始)
     * @param format    图片格式
     * @return pdf 图片流
     */
    public static byte[] getImageFromPdf(byte[] pdf, int pageIndex, String format) {
        return pdfToImageWithIndex(getFromByteArray(pdf), pageIndex, pageIndex, format).get(0);
    }
    
    /**
     * Pdf -> Image (指定页)
     *
     * @param pdf        pdf文件
     * @param startIndex 起始页号(页号从0开始)
     * @param endIndex   终止页号
     * @param format     图片格式
     * @return pdf 图片流
     */
    public static List<byte[]> getImageFromPdf(File pdf, int startIndex, int endIndex, String format) {
        return pdfToImageWithIndex(getFromFile(pdf), startIndex, endIndex, format);
    }
    
    /**
     * Pdf -> Image (指定页)
     *
     * @param pdf        pdf流
     * @param startIndex 起始页号(页号从0开始)
     * @param endIndex   终止页号
     * @param format     图片格式
     * @return pdf 图片流
     */
    public static List<byte[]> getImageFromPdf(byte[] pdf, int startIndex, int endIndex, String format) {
        return pdfToImageWithIndex(getFromByteArray(pdf), startIndex, endIndex, format);
    }
    
    private static List<byte[]> pdfToImageWithIndex(PDDocument document, int startIndex, int endIndex, String format) {
        List<byte[]> pdfImages = new ArrayList<>();
        if (startIndex > endIndex) {
            throw new IllegalArgumentException("The param startIndex cannot be greater than endIndex");
        }
        if (endIndex > pdfTotalPages(document)) {
            throw new IllegalArgumentException(String.format("The pdf max page index is [%s], But the endIndex you input is [%s]", pdfTotalPages(document), endIndex));
        }
        for (int i = startIndex; i < endIndex; i++) {
            pdfImages.add(pdfToImage(document, i, endIndex, format));
        }
        return pdfImages;
    }
    
    /**
     * Pdf -> Image (所有页)
     *
     * @param pdf    pdf byte[]
     * @param format 图片格式
     * @return 图片 byte[]
     */
    public static List<byte[]> getImageFromPdfAllPages(byte[] pdf, String format) {
        return pdfToImageForAllPages(getFromByteArray(pdf), format);
    }
    
    /**
     * Pdf -> Image (所有页)
     *
     * @param pdf    pdf文件
     * @param format 图片格式
     * @return 图片 byte[]
     */
    public static List<byte[]> getImageFromPdfAllPages(File pdf, String format) {
        return pdfToImageForAllPages(getFromFile(pdf), format);
    }
    
    private static List<byte[]> pdfToImageForAllPages(PDDocument document, String format) {
        List<byte[]> pdfImages = new ArrayList<>();
        int totalPages = pdfTotalPages(document);
        for (int i = 0; i < totalPages; i++) {
            pdfImages.add(pdfToImage(document, i, totalPages, format));
        }
        return pdfImages;
    }
    
    /**
     * pdf首页 -> image
     *
     * @param document PDDocument
     * @param format   图片格式
     * @return 图片 byte[]
     */
    private static byte[] pdfHomePageToImage(PDDocument document, String format) {
        return pdfToImage(document, HOME_PAGE_INDEX, HOME_PAGE_INDEX, format);
    }
    
    
    /**
     * pdf -> image
     *
     * @param document   PDDocument
     * @param startIndex 页号
     * @param endIndex   页数
     * @param format     图片格式
     * @return 图片 byte[]
     */
    private static byte[] pdfToImage(PDDocument document, int startIndex, int endIndex, String format) {
        try {
            PDFRenderer pdfRenderer = new PDFRenderer(document);
            BufferedImage bufferedImage = pdfRenderer.renderImageWithDPI(startIndex, 100);
            return imageToBytes(bufferedImage, format);
        } catch (Exception e) {
            log.warn("Pdf Read Error:", e);
        } finally {
            // 开发者需要的页数读取完再关闭
            if (startIndex == endIndex) {
                try {
                    document.close();
                } catch (IOException e) {
                    log.warn("IO Exception", e);
                }
            }
    
        }
        return null;
    }
    
    /**
     * 获取pdf总页数
     *
     * @param document pdf
     * @return pdf总页数
     */
    private static int pdfTotalPages(PDDocument document) {
        return document.getNumberOfPages();
    }
    
    /**
     * PDDocument
     *
     * @param pdf pdf文件
     * @return 图片 byte[]
     */
    private static PDDocument getFromFile(File pdf) {
        try {
            return PDDocument.load(pdf);
        } catch (IOException e) {
            throw new RuntimeException(String.format("Load pdf %s, The File must be a pdf.", e.getMessage()));
        }
    }
    
    /**
     * PDDocument
     *
     * @param pdf pdf文件 byte[]
     * @return 图片 byte[]
     */
    private static PDDocument getFromByteArray(byte[] pdf) {
        try {
            return PDDocument.load(pdf);
        } catch (IOException e) {
            e.printStackTrace();
            throw new RuntimeException("Load pdf error: The File must be a pdf.");
        }
    
    }
    
    /**
     * 转换BufferedImage 数据为byte数组
     *
     * @param bImage Image对象
     * @param format image格式字符串.如"gif","png"
     * @return byte数组
     */
    private static byte[] imageToBytes(BufferedImage bImage, String format) {
        ByteArrayOutputStream out = new ByteArrayOutputStream();
        try {
            ImageIO.write(bImage, format, out);
        } catch (IOException e) {
            log.warn("IO Exception", e);
        }
        return out.toByteArray();
    }
    

    转载于:https://my.oschina.net/u/3757402/blog/3026251

    展开全文
  • 基于apache pdfbox库实现Java PDF转图片,带源码和pom文件; 开发环境导入maven工程,PdfToImg为测试类,修改其中test.pdf和test.png的路径即可运行; PDFUtil为独立工具类,可放置任何工程中运行。 如果在linux...
  • pdf转图片、office转pdf例子,commons-cli-1.4.jar,commons-io-1.3.2.jar,commons-logging-1.2.jar,icepdf-core-4.1.1.jar,icepdf-viewer-4.1.1-taverna.jar,jodconverter-2.1.1.jar、jodconverter-cli-2.2.2....
  • Java PDF转图片 pdfbox

    2021-03-01 20:49:42
    <version>2.0.15version> dependency> 代码示例 pdfStream:pdf文件的输入流 imgPath:导出图片的路径 返回文件的全路径集合 public static List<String> pdfToImages(InputStream pdfStream, String imagePath) { ...

    引入Maven依赖

            <dependency>
                <groupId>org.apache.pdfbox</groupId>
                <artifactId>pdfbox</artifactId>
                <version>2.0.15</version>
            </dependency>
            <dependency>
                <groupId>org.apache.pdfbox</groupId>
                <artifactId>fontbox</artifactId>
                <version>2.0.15</version>
            </dependency>
    

    代码示例

    pdfStream:pdf文件的输入流

    imgPath:导出图片的路径

    返回文件的全路径集合

        public static List<String> pdfToImages(InputStream pdfStream, String imagePath) {
            List<String> result = new ArrayList<>();
            try {
                PDDocument readPdf = PDDocument.load(pdfStream);
                PDFRenderer pdfRenderer = new PDFRenderer(readPdf);
                int totalPage = readPdf.getNumberOfPages();
                int startPage = 0;
                String savePath;
                for (int i = startPage; i < totalPage; i++) {
                    String fileName = StringUtil.UUID() + ".jpg";
                    savePath = imagePath + fileName;
                    BufferedImage bufferedImage = pdfRenderer.renderImageWithDPI(i, 144);
                    ImageIO.write(bufferedImage, "JPG", new File(savePath));
                    result.add(fileName);
                }
                readPdf.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
            return result;
        }
    

    pdf中文乱码解决方案Linux,针对字体库缺失导致的乱码

    2021-03-01 09:59:03.590  WARN 380170 --- [   scheduling-1] o.a.pdfbox.pdmodel.font.PDCIDFontType0   : Using fallback LiberationSans for CID-keyed font AdobeKaitiStd-Regular
    2021-03-01 09:59:03.591  WARN 380170 --- [   scheduling-1] o.a.pdfbox.rendering.CIDType0Glyph2D     : No glyph for 3206 (CID 0c86) in font AdobeKaitiStd-Regular
    2021-03-01 09:59:03.592  WARN 380170 --- [   scheduling-1] o.a.pdfbox.rendering.CIDType0Glyph2D     : No glyph for 1875 (CID 0753) in font AdobeKaitiStd-Regular
    2021-03-01 09:59:03.593  WARN 380170 --- [   scheduling-1] o.a.pdfbox.rendering.CIDType0Glyph2D     : No glyph for 3684 (CID 0e64) in font AdobeKaitiStd-Regular
    2021-03-01 09:59:03.594  WARN 380170 --- [   scheduling-1] o.a.pdfbox.rendering.CIDType0Glyph2D     : No glyph for 4162 (CID 1042) in font AdobeKaitiStd-Regular
    2021-03-01 09:59:03.595  WARN 380170 --- [   scheduling-1] o.a.pdfbox.rendering.CIDType0Glyph2D     : No glyph for 1599 (CID 063f) in font AdobeKaitiStd-Regular
    2021-03-01 09:59:03.596  WARN 380170 --- [   scheduling-1] o.a.pdfbox.rendering.CIDType0Glyph2D     : No glyph for 3024 (CID 0bd0) in font AdobeKaitiStd-Regular
    2021-03-01 09:59:03.597  WARN 380170 --- [   scheduling-1] o.a.pdfbox.rendering.CIDType0Glyph2D     : No glyph for 2131 (CID 0853) in font AdobeKaitiStd-Regular
    2021-03-01 09:59:03.597  WARN 380170 --- [   scheduling-1] o.a.pdfbox.rendering.CIDType0Glyph2D     : No glyph for 4549 (CID 11c5) in font AdobeKaitiStd-Regular
    2021-03-01 09:59:03.598  WARN 380170 --- [   scheduling-1] o.a.pdfbox.rendering.CIDType0Glyph2D     : No glyph for 4451 (CID 1163) in font AdobeKaitiStd-Regular
    2021-03-01 09:59:03.599  WARN 380170 --- [   scheduling-1] o.a.pdfbox.rendering.CIDType0Glyph2D     : No glyph for 2117 (CID 0845) in font AdobeKaitiStd-Regular
    2021-03-01 09:59:03.599  WARN 380170 --- [   scheduling-1] o.a.pdfbox.rendering.CIDType0Glyph2D     : No glyph for 3493 (CID 0da5) in font AdobeKaitiStd-Regular
    2021-03-01 09:59:03.600  WARN 380170 --- [   scheduling-1] o.a.pdfbox.rendering.CIDType0Glyph2D     : No glyph for 3840 (CID 0f00) in font AdobeKaitiStd-Regular
    2021-03-01 09:59:03.600  WARN 380170 --- [   scheduling-1] o.a.pdfbox.rendering.CIDType0Glyph2D     : No glyph for 4665 (CID 1239) in font AdobeKaitiStd-Regular
    2021-03-01 09:59:03.600  WARN 380170 --- [   scheduling-1] o.a.pdfbox.rendering.CIDType0Glyph2D     : No glyph for 2309 (CID 0905) in font AdobeKaitiStd-Regular
    

    出现上方类似日志,则表示缺少对应字体,上方日志表示为缺少AdobeKaitiStd-Regular这个字体。从百度或其他方式找打对应字体文件,在linux执行以下操作

    #cd /usr/share/fonts/   // 进入系统自带的字体目录
    #mkdir myfonts  // myfonts 是你自己随便取得文件夹名字,一定要有这一步
    #将字体文件拷贝到这个文件夹下,在cd /usr/share/fonts/目录下执行以下命令
    #mkfontscale   
    #mkfontdir
    #fc-cache -fv           //更新字体缓存
    #fc-list    // 查看系统中所有得字体,可用于测试是否安装字体成功
    

    注:项目需重新启动

    展开全文
  • Java pdf转word分为几步? 第一步: 下载jar包,引入,下载地址:https://www.lanzous.com/i7zwcqh 第二步:贴上下面这段代码 import java.awt.Image; import java.awt.Rectangle; import java.awt.image....
  • [PDF]JavaPDF转图片处理

    千次阅读 2018-07-02 09:15:19
    原文转自:https://www.cnblogs.com/pcheng/p/5704470.html*****************************************************************JAVA中实现pdf转图片可以通过...
  • java pdf转图片工具类

    2021-03-12 11:15:42
    @Slf4j public class PDFToImage { public static void batchPic(List<BufferedImage> piclist, File response) {// 纵向处理图片 if (CollectionUtils.isEmpty(piclist)) { log.error("图片数组为空!"); return; }...
  • ICEpdf-pro-5.0.6-P01-bin-trial 最新官方jar包 带字库 java PDF转图片工具
  • java PDF转换图片(IMG)

    千次阅读 2019-08-20 15:49:26
    public static void pdfImg(String inputPdf, long ItemId, String outPut){ //加载PDF文件 PdfDocument doc = new PdfDocument(); doc.loadFromFile(inputPdf);... //保存PDF的每一页到图片 ...
  • java 使用pdfbox pdf转图片时候,有些pdf文件转图片报错java.lang.UnsupportedOperationException: OTF fonts do not have a glyf table 使用的是pdfbox-2.0.4.jar jar包跟新到pdfbox-2.0.9.jar后正常 ...
  • 使用Icepdf 5.0.6 P01最新版PDF转图片,附带jar包和转换demo实例工具类,并且完美去水印。详细去水印方法请看我的博客文章http://blog.csdn.net/wangpeng468/article/details/37810445
  • itextpdf 实现html转pdf中中文及图片base64的解决方法:1. 中文解决方案:实现FontProvider接口:package com.mumu.image2pdf;import com.itextpdf.text.BaseColor;import com.itextpdf.text.Font;import ...
  • JavaPDF转图片

    2017-03-01 23:19:43
    JavaPDF转图片
  • Java pdf转高清图片-附件资源

空空如也

空空如也

1 2 3 4 5 ... 20
收藏数 1,099
精华内容 439
关键字:

javapdf转图片

java 订阅