PDFRenderer 生成图片

将PDF文档中的页面生成图片,导出。

Java代码 收藏代码
  1. /**浏览页面,生成页面的图片字节数据
  2. *
  3. *@paramfileName文档名称及路径
  4. *@parampage
  5. *@paramtype
  6. *@return
  7. */
  8. publicbyte[]ViewPage(StringfileName,intpage,Stringtype){
  9. if(fileName==null||fileName.length()<=0)
  10. returnnull;
  11. //if(!fileName.endsWith("pdf")||!fileName.endsWith("PDF"))
  12. //returnnull;
  13. try
  14. {
  15. BitImgTypeimgType=BitImgType.valueOf(type.toUpperCase());
  16. if(imgType==null)returnnull;
  17. PDFFilepdfFile=this.getPdfFile(fileName);
  18. if(pdfFile==null)
  19. returnnull;
  20. PDFPagepdfPage=pdfFile.getPage(page);
  21. if(pdfPage==null)returnnull;
  22. //getthewidthandheightforthedocatthedefaultzoom
  23. Rectanglerect=newRectangle(0,0,
  24. (int)pdfPage.getBBox().getWidth(),
  25. (int)pdfPage.getBBox().getHeight());
  26. //generatetheimage
  27. Imageimg=pdfPage.getImage(
  28. rect.width,rect.height,//width&height
  29. rect,//cliprect
  30. null,//nullfortheImageObserver
  31. true,//fillbackgroundwithwhite
  32. true//blockuntildrawingisdone
  33. );
  34. ByteArrayOutputStreambaos=newByteArrayOutputStream();
  35. BufferedImagebImg=(BufferedImage)img;
  36. ImageIO.write(bImg,imgType.toString(),baos);
  37. returnbaos.toByteArray();
  38. }
  39. catch(Exceptionex)
  40. {
  41. ex.printStackTrace();
  42. }
  43. returnnull;
  44. }
  45. /**建立PDF文档读取类
  46. *
  47. *@paramfilePathPDF文件的路径
  48. *@returnnull或者PDFFileinstance
  49. */
  50. privatePDFFilegetPdfFile(StringfilePath)
  51. {
  52. try
  53. {
  54. Filefile=newFile(filePath);
  55. RandomAccessFileraf=newRandomAccessFile(file,"r");
  56. FileChannelchannel=raf.getChannel();
  57. ByteBufferbuf=channel.map(FileChannel.MapMode.READ_ONLY,0,channel.size());
  58. PDFFilepdffile=newPDFFile(buf);
  59. returnpdffile;
  60. }
  61. catch(Exceptionex)
  62. {
  63. ex.printStackTrace();
  64. }
  65. returnnull;
  66. }

下面的代码从PDFRenderer 的example而来
Java代码 收藏代码
  1. publicclassPDFViewer{
  2. publicstaticvoidsetup()throwsIOException{
  3. //setuptheframeandpanel
  4. JFrameframe=newJFrame("PDFTest");
  5. frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  6. PagePanelpanel=newPagePanel();
  7. frame.add(panel);
  8. frame.pack();
  9. frame.setVisible(true);
  10. //loadapdffromabytebuffer
  11. StringurlPath="E:\\ArcGIS应用\\ESRI技术白皮书\\ESRIData&Maps9.3.pdf";
  12. Filefile=newFile(urlPath);
  13. RandomAccessFileraf=newRandomAccessFile(file,"r");
  14. FileChannelchannel=raf.getChannel();
  15. ByteBufferbuf=channel.map(FileChannel.MapMode.READ_ONLY,
  16. 0,channel.size());
  17. PDFFilepdffile=newPDFFile(buf);
  18. //showthefirstpage
  19. PDFPagepage=pdffile.getPage(0);
  20. panel.showPage(page);
  21. }
  22. publicstaticvoidmain(finalString[]args){
  23. SwingUtilities.invokeLater(newRunnable(){
  24. publicvoidrun(){
  25. try{
  26. PDFViewer.setup();
  27. }catch(IOExceptionex){
  28. ex.printStackTrace();
  29. }
  30. }
  31. });
  32. }}

来自:http://lozpeng.iteye.com/blog/403559

==========================================================================================================

1)下载一个 PDFRenderer.jar的包
2)在这个官网上还有几个sample ,大家可以看一下 : https://pdf-renderer.dev.java.net/examples.html

3)下面一段代码就是将一个pdf生成png格式的图片

public void Pdf_Png(int pageNumber ) {
int pagen= pageNumber;
File file = new File("D:/test.pdf");
PDFFile pdffile=null;
// set up the PDF reading
try{
RandomAccessFile raf = new RandomAccessFile(file, "r");
FileChannel channel = raf.getChannel();
ByteBuffer buf = channel.map(FileChannel.MapMode.READ_ON<wbr style="line-height:24px">LY, 0, channel.size());</wbr>
pdffile = new PDFFile(buf);
}
catch(Exception e){}
if(pagen<pdffile.getNumPages())
return;
//print出该pdf文档的页数
System.out.println(pdffile.getNumPages());
//设置将第pagen也生成png图片
PDFPage page = pdffile.getPage(pagen);

// create and configure a graphics object
int width = (int)page.getBBox().getWidth();
int height =(int)page.getBBox().getHeight();
BufferedImage img = new BufferedImage(width, height, BufferedImage.TYPE_INT_ARGB);
Graphics2D g2 = img.createGraphics();
g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);

// do the actual drawing
PDFRenderer renderer = new PDFRenderer(page, g2,
new Rectangle(0, 0, width, height), null, Color.RED);
try{
page.waitForFinish();
}catch(Exception e){
e.printStackTrace();
}
renderer.run();

g2.dispose();
try{
ImageIO.write(img, "png", new File("D:/Image.png"));
}
catch(Exception ex){}


除了生成PNG图片外,
PDF Renderer还有一下功能
在用户自己的应用中查看PDF。

  提供PDF文件的打印预览。

  将PDF合并到3D场景中。

  在PDF之上绘图,并可以在网络查看器中进行标注。





















































  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值