Example usage for javax.swing JTextPane getBounds

List of usage examples for javax.swing JTextPane getBounds

Introduction

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

Prototype

public Rectangle getBounds() 

Source Link

Document

Gets the bounds of this component in the form of a Rectangle object.

Usage

From source file:MainClass.java

protected Rectangle getVisibleEditorRect(JTextPane ta) {
    Rectangle alloc = ta.getBounds();
    if ((alloc.width > 0) && (alloc.height > 0)) {
        alloc.x = alloc.y = 0;//from  www. java2s.c o  m
        Insets insets = ta.getInsets();
        alloc.x += insets.left;
        alloc.y += insets.top;
        alloc.width -= insets.left + insets.right;
        alloc.height -= insets.top + insets.bottom;
        return alloc;
    }
    return null;
}

From source file:MainClass.java

public void paintToPDF(JTextPane ta) {
    try {//from   w w w.  j a va  2s .c om
        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();
    }
}