Priting a WebPage in Java

T

tascien

this code downloads a web page and makes it a jpeg:

----------------------------------------

import java.net.URL;
import java.awt.image.BufferedImage;
import javax.imageio.ImageIO;
import java.beans.PropertyChangeListener;
import java.beans.PropertyChangeEvent;
import javax.swing.text.html.*;
import java.awt.geom.AffineTransform;
import java.awt.image.AffineTransformOp;
import java.util.*;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import java.io.*;
import javax.print.*;
import javax.print.attribute.*;
import javax.print.attribute.standard.*;
import java.awt.print.*;


public class EBXPrinterWebPageToJPEG
{
private BufferedImage image = null;

public BufferedImage Download(String webpageurl)
{
try
{
URL url = new URL(webpageurl);
final JEditorPane jep = new JEditorPane();
jep.setContentType("text/html");
((HTMLDocument)jep.getDocument()).setBase(url);
jep.setEditable(false);
jep.setBounds(0,0,1024,1768);
jep.addPropertyChangeListener("page",new
PropertyChangeListener() {
public void propertyChange(PropertyChangeEvent e)
{
try
{
image = new
BufferedImage(1024,1768,BufferedImage.TYPE_INT_RGB);
Graphics g = image.getGraphics();
Graphics2D graphics = (Graphics2D) g;


graphics.setRenderingHint(RenderingHints.KEY_ANTIALIASING,
RenderingHints.VALUE_ANTIALIAS_ON);
jep.paint(graphics);
//ImageIO.write(image,"jpg",new
File("webpage.jpg"));
}
catch (Exception re)
{
re.printStackTrace();
}
}});
jep.setPage(url);

}
catch (Exception e)
{
e.printStackTrace();
}

return image;
}


}


----------------------------------------

this code prints a page:

-----------------------------------------

import java.awt.*;
import java.awt.print.*;
/**
* This example shows how to use the PrinterJob
* and Book classes.
*/
public class EBXPrinterJob implements Printable {
/**
* Print a single, blank page.
*/
private Image pImage = null;
private String pText = "EBXPrinter 1.0";
private int pTextX = 100;
private int pTextY = 100;

public void Start() {
/* Get the representation of the current printer and
* the current print job.
*/

PrinterJob printerJob = PrinterJob.getPrinterJob();
/* Build a book containing pairs of page painters (Printables)
* and PageFormats. This example has a single page.
*/
Book book = new Book();
book.append(new EBXPrinterJob(), new PageFormat());
/* Set the object to be printed (the Book) into the PrinterJob.
* Doing this before bringing up the print dialog allows the
* print dialog to correctly display the page range to be printed
* and to dissallow any print settings not appropriate for the
* pages to be printed.
*/
printerJob.setPageable(book);



/* Show the print dialog to the user. This is an optional step
* and need not be done if the application wants to perform
* 'quiet' printing. If the user cancels the print dialog then false
* is returned. If true is returned we go ahead and print.
*/
//boolean doPrint = printerJob.printDialog();
boolean doPrint = true;
if (doPrint) {

try {
printerJob.print();
} catch (PrinterException exception) {
System.err.println("Printing error: " + exception);
}
}
}
/**
* Print a blank page.
*/
public int print(Graphics g, PageFormat format, int pageIndex) {
/* Do the page drawing here. This example does not do any
* drawing and therefore blank pages are generated:
* "This Page Intentionally Left Blank"
*/


Graphics2D g2d = (Graphics2D) g;
/* Move the origin from the corner of the Paper to the corner
* of the imageable area.
*/
g2d.translate(format.getImageableX(), format.getImageableY());

/* Set the text color. */
g2d.setPaint(Color.black);

/* draw text string . */
if(pText != ""){
g.drawString("Image width: " + pImage.getWidth(null), pTextX,
pTextY);
g.drawString(pText, pTextX, pTextY);
}


return Printable.PAGE_EXISTS;
}
}


-----------------------------------------

Can anybody put it together and make it print a downloaded webpage,
without user seeing the webpage being downloaded?

Thanks.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,770
Messages
2,569,583
Members
45,075
Latest member
MakersCBDBloodSupport

Latest Threads

Top