2

I want to take the screenshot of Jsp page in the browser. I had googled a lot. Everyone is pointing to java.awt.Robot functionality. It is great. But what i need is i want the screenshot of the full web page which is also inside the scrollable area of the browser window. Moreover i want only the webpage content not the status bar and other tabs and menus on the browser. I had used the following code.

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();
}

Is there a way to take the screen shot of the full JSP webpage that is viewing in the browser.(Along the content inside the scrollable window) and then I have to convert this screenshot into PDF. Don't tell me the ways to directly convert it into the PDF using FlyingSaucer as it's not working in my case.

Chirag Kawariya
  • 205
  • 2
  • 4
  • 18
  • you can't do it in java – Madhawa Priyashantha Aug 14 '15 at 14:02
  • What about using java to open a browser on which you have full control (maybe [JXBrowser](http://www.teamdev.com/jxbrowser)?) and [exporting the webpage as image](http://jxbrowser-support.teamdev.com/documentation/taking-screenshots)? – BackSlash Aug 14 '15 at 14:04

1 Answers1

2

This is not possible in pure Java.

However you can add the html2canvas library to your JSP page. You can then use Javascript to submit the canvas image to your servlet and process it as you please.

See the following question and answer that deals with similar problem: How to upload a screenshot using html2canvas?

Community
  • 1
  • 1
MartinTeeVarga
  • 10,478
  • 12
  • 61
  • 98