如何从 Java 生成 RTF?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/424119/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-11 14:27:40  来源:igfitidea点击:

How do I generate RTF from Java?

javaxmlxsltrtf

提问by Ingvald

I work on a web-based tool where we offer customized prints.

我在一个基于网络的工具上工作,我们提供定制的打印。

Currently we build an XML structure with Java, feed it to the XMLmind XSL-FO Converteralong with customized XSL-FO, which then produces an RTF document.

目前,我们使用 Java 构建了一个 XML 结构,将它与定制的 XSL-FO 一起提供给XMLmind XSL-FO 转换器,然后生成一个 RTF 文档。

This works fine on simple layouts, but there's some problem areas where I'd like greater control, or where I can't do what I want at all. F.ex: tables in header, footers (e.g., page numbers), columns, having a separate column setup or different page number info on the first page, etc.

这在简单的布局上效果很好,但在一些问题区域我想要更好的控制,或者我根本无法做我想做的事情。F.ex:页眉,页脚(例如,页码),列中的表格,在第一页上有单独的列设置或不同的页码信息等。

Do any of you know of better alternatives, either to XMLmind or to the way we get from data to RTF, i.e., Java-> XML, XML+XSL-> RTF? (The only practical limitation for us is the JVM.)

你们有没有人知道更好的替代方案,无论是 XMLmind 还是我们从数据到 RTF 的方式,即 Java-> XML、XML+XSL-> RTF?(对我们来说唯一的实际限制是 JVM。)

采纳答案by splattne

If you could afford spending some money, you could use Aspose.Words, a professional library for creating Word and RTF documents for Java and .NET.

如果你能负担得起花一些钱,你可以使用Aspose.Words,一个用于为 Java 和 .NET 创建 Word 和 RTF 文档的专业库。

回答by joel.neely

iTextsupports RTF.

iText支持 RTF。

回答by Andrzej Doyle

Have you had a look at the iTextlibrary? It's touted primarily as a PDF generator, though it can also generate RTF. I haven't had cause to use it personally, but the general feeling I get is that it's good, and the interface looks comprehensive and easy to work to in the abstract. Whether it would fit in well with your existing data model is another question.

你有没有看过iText库?它主要被吹捧为 PDF 生成器,但它也可以生成 RTF。我个人没有理由使用它,但我得到的总体感觉是它很好,并且界面看起来很全面并且抽象地易于使用。它是否适合您现有的数据模型是另一个问题。

回答by Christian Ullenboom

You can take a look at a new library called jRTF. It allows you to create new RTF documents and to fill RTF templates.

您可以查看一个名为jRTF的新库。它允许您创建新的 RTF 文档并填充 RTF 模板。

回答by user1491821

import com.lowagie.text.*;
import com.lowagie.text.html.simpleparser.HTMLWorker;
import com.lowagie.text.html.simpleparser.StyleSheet;
import com.lowagie.text.rtf.*;

import java.io.*;
import java.util.ArrayList;

public class HTMLtoRTF {
    public static void main(String[] args) throws DocumentException {
        Document document = new Document();

        try {
            Reader htmlreader = new BufferedReader((new InputStreamReader((new FileInputStream("C:\Users\asrikantan\Desktop\sample.htm")))));

            RtfWriter2 rtfWriter = RtfWriter2.getInstance(document, new FileOutputStream(("C:\Users\asrikantan\Desktop\sample12.rtf")));
            document.open();
            document.add(new Paragraph("Testing simple paragraph addition."));
            //ByteArrayOutputStream out = new ByteArrayOutputStream();

            StyleSheet styles = new StyleSheet();
            styles.loadTagStyle("body", "font", "Bitstream Vera Sans");
            ArrayList htmlParser = HTMLWorker.parseToList(htmlreader, styles);
            //fetch HTML line by line

            for (int htmlDatacntr = 0; htmlDatacntr < htmlParser.size(); htmlDatacntr++) {
                Element htmlDataElement = (Element) htmlParser.get(htmlDatacntr);
                document.add((htmlDataElement));
            }
            htmlreader.close();
            document.close();

        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (Exception e) {
            System.out.println(e);
        }
    }
}