Example usage for javax.swing JEditorPane printAll

List of usage examples for javax.swing JEditorPane printAll

Introduction

In this page you can find the example usage for javax.swing JEditorPane printAll.

Prototype

public void printAll(Graphics g) 

Source Link

Document

Invoke this method to print the component.

Usage

From source file:embedding.ExampleJava2D2PDF.java

/**
 * Creates a PDF file. The contents are painted using a Graphics2D implementation that
 * generates an PDF file.// w  w w . ja  va 2 s  .c  om
 * @param outputFile the target file
 * @throws IOException In case of an I/O error
 * @throws ConfigurationException if an error occurs configuring the PDF output
 */
public void generatePDF(File outputFile) throws IOException, ConfigurationException {
    OutputStream out = new java.io.FileOutputStream(outputFile);
    out = new java.io.BufferedOutputStream(out);
    try {

        //Instantiate the PDFDocumentGraphics2D instance
        PDFDocumentGraphics2D g2d = new PDFDocumentGraphics2D(false);
        g2d.setGraphicContext(new org.apache.xmlgraphics.java2d.GraphicContext());

        //Configure the G2D with the necessary fonts
        configure(g2d, createAutoFontsConfiguration());

        //Set up the document size
        Dimension pageSize = new Dimension((int) Math.ceil(UnitConv.mm2pt(210)),
                (int) Math.ceil(UnitConv.mm2pt(297))); //page size A4 (in pt)
        g2d.setupDocument(out, pageSize.width, pageSize.height);
        g2d.translate(144, 72); //Establish some page borders

        //A few rectangles rotated and with different color
        Graphics2D copy = (Graphics2D) g2d.create();
        int c = 12;
        for (int i = 0; i < c; i++) {
            float f = ((i + 1) / (float) c);
            Color col = new Color(0.0f, 1 - f, 0.0f);
            copy.setColor(col);
            copy.fillRect(70, 90, 50, 50);
            copy.rotate(-2 * Math.PI / c, 70, 90);
        }
        copy.dispose();

        //Some text
        g2d.rotate(-0.25);
        g2d.setColor(Color.RED);
        g2d.setFont(new Font("sans-serif", Font.PLAIN, 36));
        g2d.drawString("Hello world!", 140, 140);
        g2d.setColor(Color.RED.darker());
        g2d.setFont(new Font("serif", Font.PLAIN, 36));
        g2d.drawString("Hello world!", 140, 180);

        pageSize = new Dimension(pageSize.height, pageSize.width);
        g2d.nextPage(pageSize.width, pageSize.height);

        //Demonstrate painting rich text
        String someHTML = "<html><body style=\"font-family:Verdana\">" + "<p>Welcome to <b>page 2!</b></p>"
                + "<h2>PDFDocumentGraphics2D Demonstration</h2>"
                + "<p>We can <i>easily</i> paint some HTML here!</p>" + "<p style=\"color:green;\">"
                + "Lorem ipsum dolor sit amet, consectetur adipiscing elit. Proin accumsan"
                + " condimentum ullamcorper. Sed varius quam id arcu fermentum luctus. Praesent"
                + " nisi ligula, cursus sed vestibulum vel, sodales sed lectus.</p>" + "</body></html>";
        JEditorPane htmlComp = new JEditorPane();
        htmlComp.setContentType("text/html");
        htmlComp.read(new StringReader(someHTML), null);
        htmlComp.setSize(new Dimension(pageSize.width - 72, pageSize.height - 72));
        //htmlComp.setBackground(Color.ORANGE);
        htmlComp.validate();
        htmlComp.printAll(g2d);

        //Cleanup
        g2d.finish();
    } finally {
        IOUtils.closeQuietly(out);
    }
}