導航:首頁 > 文檔加密 > javaniopdf

javaniopdf

發布時間:2022-03-16 04:51:19

Ⅰ 用java實現pdf轉jpg圖片的全代碼,我這里附上參考代碼。

學JAVA就到廣州瘋狂JAVA來學習 李剛授課 我是不能。。。

Ⅱ javaweb pdf流轉jpg流怎麼操作

package pdf;
import java.awt.Image;
import java.awt.Rectangle;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.RandomAccessFile;
import java.nio.ByteBuffer;
import java.nio.channels.FileChannel;
import javax.swing.SwingUtilities;
import com.sun.image.codec.jpeg.JPEGCodec;
import com.sun.image.codec.jpeg.JPEGEncodeParam;
import com.sun.image.codec.jpeg.JPEGImageEncoder;
import com.sun.pdfview.PDFFile;
import com.sun.pdfview.PDFPage;
public class PdfToJpg {
public static void setup() throws IOException {
// 載入一個pdf從一個位元組緩沖區
File file = new File("D:\\yangliu\\test.pdf");
RandomAccessFile raf = new RandomAccessFile(file, "r");
FileChannel channel = raf.getChannel();
ByteBuffer buf = channel.map(FileChannel.MapMode.READ_ONLY, 0,
channel.size());
PDFFile pdffile = new PDFFile(buf);
System.out.println("頁數:" + pdffile.getNumPages());
String getPdfFilePath = System.getProperty("user.dir") + "\\pdfPicFile";
System.out.println("getPdfFilePath is :" + getPdfFilePath);

for (int i = 1; i < pdffile.getNumPages(); i++) {
// 畫第一頁到一個圖像
PDFPage page = pdffile.getPage(i);
// 獲得寬度和高度的文件在默認的變焦
Rectangle rect = new Rectangle(0, 0, (int) page.getBBox()
.getWidth(), (int) page.getBBox().getHeight());
// 生成圖像
Image img = page.getImage(rect.width, rect.height, rect, null,
true, true);
BufferedImage tag = new BufferedImage(rect.width, rect.height,
BufferedImage.TYPE_INT_RGB);
tag.getGraphics().drawImage(img, 0, 0, rect.width, rect.height,
null);
// 輸出到文件流
FileOutputStream out = new FileOutputStream(getPdfFilePath + "\\"
+ i + ".jpg");
System.out.println("成功保存圖片到:"+getPdfFilePath+"\\"+i+".jpg");

JPEGImageEncoder encoder = JPEGCodec.createJPEGEncoder(out);
JPEGEncodeParam param = encoder.getDefaultJPEGEncodeParam(tag);
param.setQuality(1f, false); //1f是提高生成的圖片質量
encoder.setJPEGEncodeParam(param);
encoder.encode(tag); // JPEG編碼
out.close();
}
}

public static void main(final String[] args){
SwingUtilities.invokeLater(new Runnable() {
public void run() {
try {
PdfToJpg.setup();
} catch (IOException e) {
e.printStackTrace();
}
}
});
}
}

閱讀全文

與javaniopdf相關的資料

熱點內容
高爾夫電台怎麼添加到文件夾 瀏覽:237
四川麻將一般下哪個app 瀏覽:862
反編譯exe腳本 瀏覽:460
源碼文件夾怎麼編譯到固件中 瀏覽:910
ERp列印伺服器錯誤怎麼弄 瀏覽:111
蚌埠u盤加密軟體有哪些 瀏覽:178
前端如何認證伺服器 瀏覽:554
linux切換db2用戶命令 瀏覽:308
相片如何用電解壓 瀏覽:906
碩士程序員去學校當老師 瀏覽:120
pythonstr提取到字典 瀏覽:818
程序員那麼可愛有人看上陸漓了 瀏覽:878
php正則提取圖片 瀏覽:105
pythonlinuxdjango 瀏覽:562
php中文返回亂碼 瀏覽:91
宿舍裝的電信怎麼加密 瀏覽:745
為什麼壓縮文件解壓後變少了 瀏覽:426
現在安卓充電器普遍是什麼型號 瀏覽:714
9日均線36均線主圖指標源碼 瀏覽:351
程序員阿里文化完整版 瀏覽:98