pdf多页合成一张pdf图片
生活随笔
收集整理的這篇文章主要介紹了
pdf多页合成一张pdf图片
小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.
//使用架包pdfbox-1.71 ?fontbox-1.71
public String[] ?yPic(String pdfPath) ?{//strs是傳過來的pdf文件路徑
String[] str = null;//定義一個(gè) 數(shù)組,用來存放輸出的圖片的路徑try { ?
? ? ? ?InputStream is = new FileInputStream(pdfPath); ?
? ? ? ?PDDocument pdf = PDDocument.load(is, true); ?
? ? ? ?List<PDPage> pages = pdf.getDocumentCatalog().getAllPages(); ?
? ? ? ?int actSize = pages.size(); // actSize是PDP文件中實(shí)際的頁數(shù) ?
? ? ? ?str = new String[actSize];
? ? ? ?for(int i =0;i<actSize;i++){//循環(huán)輸出一頁pdf文件轉(zhuǎn)化的圖片
? ? ? ? Document document = new Document();
? ? ? ? document.setFile(pdfPath);
? ? ? ? float scale = 1f;
? ? ? ? float rotation = 0f;
? ? ? ? Page page = document.getPageTree().getPage(i);
? ? ? ? page.init();
? ? ? ? PDimension sz = page.getSize(Page.BOUNDARY_CROPBOX, rotation, scale);
? ? ? ? pageHeight = (int) sz.getHeight();
? ? ? ? pageWidth = (int) sz.getWidth();
? ? ? ? BufferedImage image = new BufferedImage(pageWidth,
? ? ? ? pageHeight,
? ? ? ? BufferedImage.TYPE_INT_RGB);
? ? ? ? Graphics g = image.createGraphics();
? ? ? ? page.paint(g, GraphicsRenderingHints.PRINT,
? ? ? ? Page.BOUNDARY_CROPBOX, rotation, scale);
? ? ? ? g.dispose();
? ? ? ? //防止文件重名
? ? ? ? int index = pdfPath.lastIndexOf("\\");
? ? ? ? String path = pdfPath.substring(0, index);
? ? ? ? index = path.lastIndexOf("\\");
? ? ? ? String imgPath = path.substring(0, index);
? ? ? ? imgPath = imgPath.replace("\\", "/");
? ? ? ?
? ? ? ? SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMddhhmmssSSS");
? ? ? ? Date dt = new Date();
? ? ? ? String fileName = sdf.format(dt);
? ? ? ? File file = new File(imgPath + "/imgFiles/" + fileName + ".png");
? ? ? ? if(!file.exists()){
? ? ? ? try {
? ? ? ? file.createNewFile();
? ? ? ? ImageIO.write(image, "png", file);
? ? ? ? } catch (IOException e) {
? ? ? ? // TODO 自動(dòng)生成的 catch 塊
? ? ? ? e.printStackTrace();
? ? ? ? }
? ? ? ? image.flush();
? ? ? ? }
? ? ? ? str[i] = imgPath + "/imgFiles/" + fileName + ".png";//輸出文件到1個(gè)路徑,把這個(gè)路徑放到strs中
? ? ? ? document.dispose();
? ? ? ?}?
? ?} catch (IOException e) { ?
? ? ? ?e.printStackTrace(); ?
? ?}
}
@Bizlet
public String[] createPicTwo2(String [] strs){//strs是傳過來的pdf文件路徑,放到數(shù)組中
String path="";?
String[] strpath = null;
try{
if(strs.length>=2){
strpath = new String[strs.length*2-1];
for(int i=0;i<strs.length;i++){
strpath[i] = strs[i];
}
for(int i=1;i<strs.length;i++){//循環(huán)從1開始
strs[i] = strs[i].replace("/", "\\");
int index = strs[i].lastIndexOf("\\");
? ? ? ? String paths = strs[i].substring(0, index);
? ? ? ? index = paths.lastIndexOf("\\");
? ? ? ? String imgPath = paths.substring(0, index);
? ? ? ? imgPath = imgPath.replace("\\", "/");
? ? ? ? imgPath +="/imgFiles/";
File outFile = new File(imgPath);
File fileOne = new File(strs[i]);
if(i==1){
BufferedImage image1 = ImageIO.read(new File(strs[i-1])); ?
BufferedImage image2 = ImageIO.read(fileOne); ?
//因?yàn)閜df轉(zhuǎn)換的圖片寬和高 都一樣,合成新文件的寬度的就是循環(huán)的次數(shù)+1,高度不變,注掉的是上下合成的分方法。本方法是左右合成
//BufferedImage combined = new BufferedImage(image1.getWidth(), image2.getHeight()*(i+1), BufferedImage.TYPE_INT_RGB); ?
BufferedImage combined = new BufferedImage(image2.getWidth()*(i+1), image2.getHeight(), BufferedImage.TYPE_INT_RGB);
Graphics g = combined.getGraphics(); ?
g.drawImage(image1, 0, 0, null); ?
//g.drawImage(image2, 0, image1.getHeight(), null);?
g.drawImage(image2, image1.getWidth(), 0, null);
SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMddhhmmssSSS");
Date dt = new Date();
String fileName = sdf.format(dt);
path = imgPath + fileName + ".png";
outFile = new File(outFile +"\\"+ fileName + ".png");
ImageIO.write(combined, "png", outFile);
}else{
BufferedImage image1 = ImageIO.read(new File(path)); ?
BufferedImage image2 = ImageIO.read(fileOne); ?
//BufferedImage combined = new BufferedImage(image1.getWidth(), image2.getHeight()*(i+1), BufferedImage.TYPE_INT_RGB); ?
BufferedImage combined = new BufferedImage(image2.getWidth()*(i+1), image2.getHeight(), BufferedImage.TYPE_INT_RGB); ?
Graphics g = combined.getGraphics(); ?
g.drawImage(image1, 0, 0, null); ?
//g.drawImage(image2, 0, image1.getHeight(), null);
g.drawImage(image2, image1.getWidth(), 0, null);
SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMddhhmmssSSS");
Date dt = new Date();
String fileName = sdf.format(dt);
path = imgPath + fileName + ".png";
outFile = new File(outFile +"\\"+ fileName + ".png");
ImageIO.write(combined, "png", outFile);
}
strpath[strs.length+i-1] = path;
}
}else{
path = strs[0];
strpath = strs;
}
? ? ? }catch(Exception e){
? e.printStackTrace();
? ? ? }
return strpath;
}
總結(jié)
以上是生活随笔為你收集整理的pdf多页合成一张pdf图片的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: js 上传文件
- 下一篇: 推荐周立功先生的一本书