導航:首頁 > 文檔加密 > 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相關的資料

熱點內容
程序員看不懂怎麼辦 瀏覽:271
linux操作系統題 瀏覽:765
單片機無符號數加法 瀏覽:227
應用隱藏加密怎麼關閉 瀏覽:269
汽車空調的壓縮機電線有什麼用 瀏覽:429
電腦加密圖片如何取消加密 瀏覽:340
慧凈電子51單片機視頻 瀏覽:343
javamap賦值 瀏覽:165
什麼app可以玩掌機游戲 瀏覽:46
java簡單聊天室 瀏覽:462
通用汽車編程軟體 瀏覽:432
一級抗震框架梁箍筋加密區規定是多少 瀏覽:974
教你如何把安卓手機變成蘋果 瀏覽:11
app編譯分類 瀏覽:323
怎麼用伺服器的資源包 瀏覽:199
oa軟體手機登陸伺服器地址 瀏覽:289
androidrtp打包 瀏覽:723
信息被加密碼了怎麼辦 瀏覽:420
彈出光碟命令 瀏覽:517
kdj公式源碼分享 瀏覽:355