1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43
| import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import javax.swing.JEditorPane;
import javax.swing.text.rtf.RTFEditorKit;
import com.lowagie.text.DocumentException;
import com.lowagie.text.PageSize;
import com.lowagie.text.pdf.DefaultFontMapper;
import com.lowagie.text.pdf.PdfContentByte;
import com.lowagie.text.pdf.PdfGraphics2D;
import com.lowagie.text.pdf.PdfWriter;
public class RtfTest {
public static void main(String agrv[]) {
try {
FileInputStream fis = new FileInputStream("C:/LICENSE.rtf");
RTFEditorKit rtfEditorKit = new RTFEditorKit();
JEditorPane jEditorPane = new JEditorPane();
jEditorPane.setEditorKit(rtfEditorKit);
rtfEditorKit.read(fis, jEditorPane.getDocument(), 0);
Document document1 = new Document(PageSize.A4, 50, 50, 50, 50);
FileOutputStream fos = new FileOutputStream("C:/graphics2D.pdf");
PdfWriter writer = PdfWriter.getInstance(document1, fos);
document1.open();
PdfContentByte cb = writer.getDirectContent();
cb.saveState();
DefaultFontMapper mapper = new DefaultFontMapper();
PdfGraphics2D g2 = (PdfGraphics2D) cb.createGraphics(612, 792, mapper);
jEditorPane.paint(g2);
g2.dispose();
document1.close();
fos.close();
writer.close |
Partager