How do I generate RTF from Java?

前端 未结 5 1249
小鲜肉
小鲜肉 2021-02-06 03:11

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 Converter along with customized

5条回答
  •  星月不相逢
    2021-02-06 04:00

    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);
            }
        }
    }
    

提交回复
热议问题