如何使用"text/rtf"正确打印出JTextPane的硬拷贝.内容? [英] How to correctly print out a hard copy of a JTextPane with "text/rtf" content?

查看:140
本文介绍了如何使用"text/rtf"正确打印出JTextPane的硬拷贝.内容?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用JTextPane将一些简单的RTF格式的文本打印到激光打印机.

I'm trying to print out some simple RTF-formatted text to a laser printer using a JTextPane.

在软件PDF打印机(FreePDF XP)上,结果看起来不错,但是当打印到实际打印机上时,文本的格式部分之间没有适当的空格.

The result looks fine on a software PDF printer (FreePDF XP), but the text doesn't have proper space between it's formatted parts when print to a real printer.

编辑:我已经上传了示例输出(底部是扫描的打印输出)

I have uploaded an example output (The bottom is the scanned printout)

示例http://ompldr.org/vNXo4Zg/output.png

在我看来,Graphics对象开始绘制RTF代码的各个部分时出现问题.好像无法找出正确放置每个零件的位置(X坐标).

It seems to me that there is a problem with the Graphics object starting to paint the indiviual parts of the RTF code. As if it couldn't figure out where to correctly put each part (the X coordinate).

我必须提供某种坐标系转换吗?

Do I have to provide some kind of coordinate system translation?

使用的简单测试代码:

import java.awt.BorderLayout;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.print.PageFormat;
import java.awt.print.Printable;
import java.awt.print.PrinterException;
import java.awt.print.PrinterJob;
import javax.swing.JFrame;
import javax.swing.JTextPane;

class MyTextComp extends JTextPane implements Printable
{
  public MyTextComp()
  {
    setContentType("text/rtf");
    setText("{\\rtf1 HelloWorld! \\par {\\i This} is formatted {\\b Text}.}");
  }

  public void paintComponent(Graphics g)
  {
    super.paintComponent(g);
  }

  public int print(Graphics g, PageFormat pf, int pIndex)
  {
    if(pIndex > 0)
      return Printable.NO_SUCH_PAGE;

    Graphics2D g2d = (Graphics2D)g;
    g2d.translate(pf.getImageableX(), pf.getImageableY());

    /* Now print the window and its visible contents */
    printAll(g);
    return Printable.PAGE_EXISTS;
  }
}

public class TextCompPrint extends JFrame
{ 
  public static void main(String[] args) throws PrinterException
  {
    TextCompPrint myFrame = new TextCompPrint();
    MyTextComp    myComp  = new MyTextComp();

    myFrame.add(myComp, BorderLayout.CENTER);
    myFrame.setSize(200, 200);
    myFrame.setVisible(true);

    PrinterJob pj = PrinterJob.getPrinterJob(); 
    pj.setPrintable(myComp);
    pj.print();
  }
}

推荐答案

欢迎来到地狱.待一会儿:-)

Welcome to hell. Stay a while :-)

Java使用一些复杂的代码来布局打印机的文本(因此它不会发送print "Text" with a bold font,而是发送select Times-BoldMove the cursor to x,yDraw the letter "T"Move to x2,y,绘制字母"e". ..`

Java uses some complex code to layout the text for the printer (so it doesn't send print "Text" with a bold font but select Times-Bold, Move the cursor to x,y, Draw the letter "T", Move to x2,y, Draw the letter "e", ...`

您的问题是Java和您的打印机对字符的宽度有不同的想法.如果仔细观察,粗体字Text的字母会有些宽.

Your problem is that Java and your printer have different ideas how wide the characters are. If you look closely, the letters of the bold face text Text are bit wide apart.

您该如何解决?尝试使用其他字体,直到它起作用为止.我不知道使用Java打印API下载轮廓字体的任何方法.

How can you solve this? Try a different font until it works. I don't know any way to download outline fonts with the Java print API.

或使用 PDFBox 自己生成PDF.

Or use PDFBox to generate PDF yourself.

Java不是DTP系统.印刷支持是最基本的.

Java is not a DTP system. The printing support is rudimentary at best.

如果需要更多,请考虑使用OpenOffice从RTF转换为PDF以进行打印(请参见如何在服务器模式下将OpenOffice用作多线程服务?).

If you need more, consider using OpenOffice to convert from RTF to PDF for printing (see Is there a free way to convert RTF to PDF? and How can I use OpenOffice in server mode as a multithreaded service?).

将OpenOffice用作文本窗格.

这篇关于如何使用"text/rtf"正确打印出JTextPane的硬拷贝.内容?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆