JTextPane和PDF : 转换成PDF « PDF « Java 教程

En
Java 教程
1. 语言基础
2. 数据类型
3. 操作符
4. 流程控制
5. 类定义
6. 开发相关
7. 反射
8. 正则表达式
9. 集合
10. 线
11. 文件
12. 泛型
13. 本土化
14. Swing
15. Swing事件
16. 二维图形
17. SWT
18. SWT 二维图形
19. 网络
20. 数据库
21. Hibernate
22. JPA
23. JSP
24. JSTL
25. Servlet
26. Web服务SOA
27. EJB3
28. Spring
29. PDF
30. 电子邮件
31. 基于J2ME
32. J2EE应用
33. XML
34. 设计模式
35. 日志
36. 安全
37. Apache工具
38. 蚂蚁编译
39. JUnit单元测试
Java
Java 教程 » PDF » 转换成PDF 
29. 77. 2. JTextPane和PDF
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Graphics2D;
import java.awt.Insets;
import java.awt.Rectangle;
import java.awt.Toolkit;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.awt.geom.AffineTransform;
import java.io.FileOutputStream;

import javax.swing.Action;
import javax.swing.JButton;
import javax.swing.JEditorPane;
import javax.swing.JFrame;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTextPane;
import javax.swing.text.MutableAttributeSet;
import javax.swing.text.SimpleAttributeSet;
import javax.swing.text.StyleConstants;
import javax.swing.text.StyledEditorKit;

import com.lowagie.text.Document;
import com.lowagie.text.pdf.DefaultFontMapper;
import com.lowagie.text.pdf.PdfContentByte;
import com.lowagie.text.pdf.PdfWriter;

public class MainClass {

  int inch = Toolkit.getDefaultToolkit().getScreenResolution();

  float pixelToPoint = (float72 (floatinch;

  JTextPane textPane;

  public MainClass() {
    JFrame frame = new JFrame();
    textPane = new JTextPane();
    JScrollPane scrollPane = new JScrollPane(textPane);

    JPanel north = new JPanel();
    JButton print = new JButton("Print");
    print.addActionListener(new ActionListener() {
      public void actionPerformed(ActionEvent ae) {
        paintToPDF(textPane);
      }
    });
    JMenuBar menu = new JMenuBar();
    JMenu styleMenu = new JMenu();
    styleMenu.setText("Style");

    Action boldAction = new BoldAction();
    boldAction.putValue(Action.NAME, "Bold");
    styleMenu.add(boldAction);

    Action italicAction = new ItalicAction();
    italicAction.putValue(Action.NAME, "Italic");
    styleMenu.add(italicAction);

    menu.add(styleMenu);

    north.add(menu);
    north.add(print);
    frame.getContentPane().setLayout(new BorderLayout());
    frame.getContentPane().add(north, BorderLayout.NORTH);
    frame.getContentPane().add(scrollPane, BorderLayout.CENTER);
    frame.setSize(800500);
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    frame.setVisible(true);
  }

  public static void main(String[] args) {
    new MainClass();
  }

  public void paintToPDF(JTextPane ta) {
    try {
      ta.setBounds(00(intconvertToPixels(612 58)(intconvertToPixels(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(612792));
      document.open();
      PdfContentByte cb = writer.getDirectContent();

      cb.saveState();
      cb.concatCTM(100100);

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

      Graphics2D g2 = cb.createGraphics(612792, 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();
    }
  }

  public float convertToPoints(int pixels) {
    return (float) (pixels * pixelToPoint);
  }

  public float convertToPixels(int points) {
    return (float) (points / pixelToPoint);
  }

  protected Rectangle getVisibleEditorRect(JTextPane ta) {
    Rectangle alloc = ta.getBounds();
    if ((alloc.width > 0&& (alloc.height > 0)) {
      alloc.x = alloc.y = 0;
      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;
  }

}

class BoldAction extends StyledEditorKit.StyledTextAction {
  private static final long serialVersionUID = 9174670038684056758L;

  public BoldAction() {
    super("font-bold");
  }

  public String toString() {
    return "Bold";
  }

  public void actionPerformed(ActionEvent e) {
    JEditorPane editor = getEditor(e);
    if (editor != null) {
      StyledEditorKit kit = getStyledEditorKit(editor);
      MutableAttributeSet attr = kit.getInputAttributes();
      boolean bold = (StyleConstants.isBold(attr)) false true;
      SimpleAttributeSet sas = new SimpleAttributeSet();
      StyleConstants.setBold(sas, bold);
      setCharacterAttributes(editor, sas, false);

    }
  }
}

class ItalicAction extends StyledEditorKit.StyledTextAction {

  private static final long serialVersionUID = -1428340091100055456L;

  public ItalicAction() {
    super("font-italic");
  }

  public String toString() {
    return "Italic";
  }

  public void actionPerformed(ActionEvent e) {
    JEditorPane editor = getEditor(e);
    if (editor != null) {
      StyledEditorKit kit = getStyledEditorKit(editor);
      MutableAttributeSet attr = kit.getInputAttributes();
      boolean italic = (StyleConstants.isItalic(attr)) false true;
      SimpleAttributeSet sas = new SimpleAttributeSet();
      StyleConstants.setItalic(sas, italic);
      setCharacterAttributes(editor, sas, false);
    }
  }
}
29. 77. 转换成PDF
29. 77. 1. 创建PDF文件的表格
29. 77. 2. JTextPane和PDF
www.java2java.com | Contact Us
Copyright 2010 - 2030 Java Source and Support. All rights reserved.
All other trademarks are property of their respective owners.