将PDF文档中的页面生成图片,导出。
- /**浏览页面,生成页面的图片字节数据
- *
- *@paramfileName文档名称及路径
- *@parampage
- *@paramtype
- *@return
- */
- publicbyte[]ViewPage(StringfileName,intpage,Stringtype){
- if(fileName==null||fileName.length()<=0)
- returnnull;
- //if(!fileName.endsWith("pdf")||!fileName.endsWith("PDF"))
- //returnnull;
- try
- {
- BitImgTypeimgType=BitImgType.valueOf(type.toUpperCase());
- if(imgType==null)returnnull;
- PDFFilepdfFile=this.getPdfFile(fileName);
- if(pdfFile==null)
- returnnull;
- PDFPagepdfPage=pdfFile.getPage(page);
- if(pdfPage==null)returnnull;
- //getthewidthandheightforthedocatthedefaultzoom
- Rectanglerect=newRectangle(0,0,
- (int)pdfPage.getBBox().getWidth(),
- (int)pdfPage.getBBox().getHeight());
- //generatetheimage
- Imageimg=pdfPage.getImage(
- rect.width,rect.height,//width&height
- rect,//cliprect
- null,//nullfortheImageObserver
- true,//fillbackgroundwithwhite
- true//blockuntildrawingisdone
- );
- ByteArrayOutputStreambaos=newByteArrayOutputStream();
- BufferedImagebImg=(BufferedImage)img;
- ImageIO.write(bImg,imgType.toString(),baos);
- returnbaos.toByteArray();
- }
- catch(Exceptionex)
- {
- ex.printStackTrace();
- }
- returnnull;
- }
- /**建立PDF文档读取类
- *
- *@paramfilePathPDF文件的路径
- *@returnnull或者PDFFileinstance
- */
- privatePDFFilegetPdfFile(StringfilePath)
- {
- try
- {
- Filefile=newFile(filePath);
- RandomAccessFileraf=newRandomAccessFile(file,"r");
- FileChannelchannel=raf.getChannel();
- ByteBufferbuf=channel.map(FileChannel.MapMode.READ_ONLY,0,channel.size());
- PDFFilepdffile=newPDFFile(buf);
- returnpdffile;
- }
- catch(Exceptionex)
- {
- ex.printStackTrace();
- }
- returnnull;
- }
下面的代码从PDFRenderer 的example而来
- publicclassPDFViewer{
- publicstaticvoidsetup()throwsIOException{
- //setuptheframeandpanel
- JFrameframe=newJFrame("PDFTest");
- frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
- PagePanelpanel=newPagePanel();
- frame.add(panel);
- frame.pack();
- frame.setVisible(true);
- //loadapdffromabytebuffer
- StringurlPath="E:\\ArcGIS应用\\ESRI技术白皮书\\ESRIData&Maps9.3.pdf";
- Filefile=newFile(urlPath);
- RandomAccessFileraf=newRandomAccessFile(file,"r");
- FileChannelchannel=raf.getChannel();
- ByteBufferbuf=channel.map(FileChannel.MapMode.READ_ONLY,
- 0,channel.size());
- PDFFilepdffile=newPDFFile(buf);
- //showthefirstpage
- PDFPagepage=pdffile.getPage(0);
- panel.showPage(page);
- }
- publicstaticvoidmain(finalString[]args){
- SwingUtilities.invokeLater(newRunnable(){
- publicvoidrun(){
- try{
- PDFViewer.setup();
- }catch(IOExceptionex){
- ex.printStackTrace();
- }
- }
- });
- }}
来自: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之上绘图,并可以在网络查看器中进行标注。
提供PDF文件的打印预览。
将PDF合并到3D场景中。
在PDF之上绘图,并可以在网络查看器中进行标注。