linux 中文乱码 转png_icepdf PDF转图片 Linux下图片乱码问题解决

import java.awt.image.BufferedImage;

import java.io.File;

import java.io.IOException;

import java.io.InputStream;

import java.net.URL;

import java.net.URLConnection;

import javax.imageio.ImageIO;

import org.apache.pdfbox.pdmodel.PDDocument;

import org.apache.pdfbox.rendering.PDFRenderer;

/**

* @author MagicWine

*

*/

public class PdfToImg{

public static void main(String[] args) throws IOException  {

pdfToImage("URL");

}

public static String pdfToImage(String urlStr) throws IOException {

// 构造URL

URL url = new URL(urlStr);

// 打开连接

URLConnection con = url.openConnection();

// 设置请求超时为5s

con.setConnectTimeout(5 * 1000);

// 输入流

InputStream in = null;

PDDocument doc = null;

try {

in = con.getInputStream();

//doc = PDDocument.load(in);

//in = new FileInputStream("D:\\test\\demo.pdf");//可以从本地文件中读取流 也可以从网络中读取

doc = PDDocument.load(in);

PDFRenderer renderer = new PDFRenderer(doc);

int pageCount = doc.getNumberOfPages();

for (int i = 0; i < pageCount; i++) {

BufferedImage image = renderer.renderImageWithDPI(i,200f); // Windows native

// DPI,PdfExchangeDpi是float类型的,越大文件越大,清晰度越高,

File file = new File("D:\\image\\"+i+".png");

if (!file .exists()) {

file .mkdirs();

}

ImageIO.write(image, "PNG", file);

}

} finally {

if (in != null) {

in.close();

}

if (doc != null) {

doc.close();

}

}

return "";

}

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值