2015-08-14 38 views
1

我想在浏览器中截取Jsp页面的截图。我搜索了很多。每个人都指向java.awt.Robot功能。太好了。但我需要的是我想要的浏览器窗口的可滚动区域内的完整网页的屏幕截图。此外,我只想要网页内容而不是浏览器上的状态栏和其他选项卡和菜单。我使用了下面的代码。如何截取整个网页?

public class ScreenCapture { 

public void TakeCapture() 
{ 
try 
{ 
     Robot robot = new Robot(); 
     String format = "jpg"; 
     String fileName = "D:\\PDFTest\\PartialScreenshot." + format; 

     Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize(); 
     Rectangle captureRect = new Rectangle(0, 0, screenSize.width , screenSize.height); 
     BufferedImage screenFullImage = robot.createScreenCapture(captureRect); 
     ImageIO.write(screenFullImage, format, new File(fileName)); 


    Document document = new Document(); 
    String input = "D:\\PDFTest\\PartialScreenshot.jpg"; 
    String output = "D:\\PDFTest\\PartialScreenshot.pdf"; 
    try { 
     FileOutputStream fos = new FileOutputStream(output); 
     PdfWriter writer = PdfWriter.getInstance(document, fos); 
     writer.open(); 
     document.open(); 
     document.add(Image.getInstance(input)); 
     document.close(); 
     writer.close(); 
    } 
    catch (Exception e) { 
     e.printStackTrace(); 
    } 
} 

catch (AWTException | IOException ex) { 
     System.err.println(ex); 

    } 
} 

public String getTakeCapture() { 
    return getTakeCapture(); 
} 

有没有办法把正在查看浏览器的全部JSP网页的截屏。(沿滚动窗口里面的内容),然后我有这个截图转换成PDF。不要告诉我使用FlyingSaucer直接将其转换为PDF的方式,因为它不适用于我的情况。

+0

你不能做到这一点在Java中 –

+0

大约用java打开您拥有完全控制浏览器(也许[JXBrowser(HTTP什么://www.teamdev.com/jxbrowser)?)和[将网页导出为图片](http://jxbrowser-support.teamdev.com/documentation/taking-screenshots)? – BackSlash

回答

1

这在纯Java中是不可能的。

但是,您可以将html2canvas库添加到JSP页面。然后,您可以使用Javascript将画布图像提交给您的servlet并根据需要进行处理。

请参见下面的问题和答案,与类似的问题涉及:How to upload a screenshot using html2canvas?