บทความนี้อธิบายวิธีการใช้งานภาพหน้าจอของไฟล์ PDF ใน Java แบ่งปันสำหรับการอ้างอิงของคุณดังนี้:
ในเว็บไซต์ที่ฉันสร้างขึ้นเมื่อเร็ว ๆ นี้มีข้อกำหนดในการอัปโหลดไฟล์ PDF แสดงหน้าปก PDF จากนั้นคลิกหน้าปกเพื่ออ่านออนไลน์ ที่นี่ PDFrender ใช้เพื่อถ่ายภาพหน้าจอของ PDF
Public Static Boolean CreatesCreenShoot (แหล่งที่มาของสตริง, เป้าหมายสตริง) {ไฟล์ไฟล์ = ไฟล์ใหม่ (แหล่งที่มา); if (! file.exists ()) {system.err.println ("path [" + source + "] ไฟล์ pdf ที่เกี่ยวข้องไม่มีอยู่!"); กลับเท็จ; } ลอง {RandomAccessFile RAF = new RandomActCessFile (ไฟล์, "r"); FileChannel channel = raf.getChannel (); byteBuffer buf = channel.map (filechannel.mapmode.read_only, 0, channel.size ()); pdffile pdffile = new pdffile (buf); int num = pdffile.getNumpages (); สำหรับ (int i = 1; i <num; i ++) {pdfpage page = pdffile.getPage (1); // รับความกว้างและความสูงสำหรับ DOC ที่ zoom rectangle rect rect = สี่เหลี่ยมใหม่ (0, 0, (int) page.getBbox () .getWidth (), (int) page.getBbox (). getheight ()); // สร้างภาพอิมเมจ img = page.getImage (rect.width, rect.height, // width & rect, // clip rect null, // null สำหรับ imageobserver จริง, // เติมพื้นหลังด้วยสีขาวจริง // บล็อกจนกว่าจะทำการวาด); 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 (Target+I+"JPG"); jpeGimageEncoder encoder = jpegCodec.createJPegenCoder (out); encoder.encode (แท็ก); // jpeg เข้ารหัส out.close (); } return true; } catch (exception e) {e.printstacktrace (); กลับมาจริง; -นอกจากนี้หากคุณต้องการแสดง PDF ออนไลน์คุณต้องตั้งค่าส่วนหัวการตอบกลับ
Response.SetContentType ("Application/PDF");สิ่งที่แนบมา: pdfrender.jar คลิกที่นี่ เพื่อดาวน์โหลดเว็บไซต์นี้
สำหรับเนื้อหาที่เกี่ยวข้องกับ Java เพิ่มเติมผู้อ่านที่สนใจในเว็บไซต์นี้สามารถดูหัวข้อ: "บทสรุปของทักษะการใช้งานภาพ Java", "บทสรุปของ Java Date และ Time Operation Skills", "บทสรุปของทักษะการดำเนินงาน Java Dom Node", "สรุปของไฟล์ Java และทักษะการดำเนินการไดเรกทอรี"
ฉันหวังว่าบทความนี้จะเป็นประโยชน์กับการเขียนโปรแกรม Java ของทุกคน