Example usage for javax.swing JTextPane getUI

List of usage examples for javax.swing JTextPane getUI

Introduction

In this page you can find the example usage for javax.swing JTextPane getUI.

Prototype

public TextUI getUI() 

Source Link

Document

Fetches the user-interface factory for this text-oriented editor.

Usage

From source file:MainClass.java

public void paintToPDF(JTextPane ta) {
    try {/*from   ww  w .  j  av a 2s.  c  o  m*/
        ta.setBounds(0, 0, (int) convertToPixels(612 - 58), (int) convertToPixels(792 - 60));

        Document document = new Document();
        FileOutputStream fos = new FileOutputStream("2.pdf");
        PdfWriter writer = PdfWriter.getInstance(document, fos);

        document.setPageSize(new com.lowagie.text.Rectangle(612, 792));
        document.open();
        PdfContentByte cb = writer.getDirectContent();

        cb.saveState();
        cb.concatCTM(1, 0, 0, 1, 0, 0);

        DefaultFontMapper mapper = new DefaultFontMapper();
        mapper.insertDirectory("c:/windows/fonts");

        Graphics2D g2 = cb.createGraphics(612, 792, mapper, true, .95f);

        AffineTransform at = new AffineTransform();
        at.translate(convertToPixels(20), convertToPixels(20));
        at.scale(pixelToPoint, pixelToPoint);

        g2.transform(at);

        g2.setColor(Color.WHITE);
        g2.fill(ta.getBounds());

        Rectangle alloc = getVisibleEditorRect(ta);
        ta.getUI().getRootView(ta).paint(g2, alloc);

        g2.setColor(Color.BLACK);
        g2.draw(ta.getBounds());

        g2.dispose();
        cb.restoreState();
        document.close();
        fos.flush();
        fos.close();
    } catch (Exception e) {
        e.printStackTrace();
    }
}