List of usage examples for javax.swing.text.rtf RTFEditorKit read
public void read(Reader in, Document doc, int pos) throws IOException, BadLocationException
From source file:com.mirth.connect.server.userutil.FileUtil.java
/** * Converts an RTF into plain text using the Swing RTFEditorKit. * //from ww w . j a v a 2 s. co m * @param message * The RTF message to convert. * @param replaceLinebreaksWith * If not null, any line breaks in the converted message will be replaced with this * string. * @return The converted plain text message. * @throws IOException * @throws BadLocationException */ public static String rtfToPlainText(String message, String replaceLinebreaksWith) throws IOException, BadLocationException { String convertedPlainText; // Reading the RTF content string Reader in = new StringReader(message); // creating a default blank styled document DefaultStyledDocument styledDoc = new DefaultStyledDocument(); // Creating a RTF Editor kit RTFEditorKit rtfKit = new RTFEditorKit(); // Populating the contents in the blank styled document rtfKit.read(in, styledDoc, 0); // Getting the root document Document doc = styledDoc.getDefaultRootElement().getDocument(); convertedPlainText = doc.getText(0, doc.getLength()); if (replaceLinebreaksWith != null) { convertedPlainText = convertedPlainText.replaceAll("\\n", replaceLinebreaksWith); } return convertedPlainText; }
From source file:com.mirth.connect.server.util.FileUtil.java
public static String rtfToPlainText(String message, String replaceLinebreaksWith) throws IOException, BadLocationException { String convertedPlainText;//from w w w . j a v a 2 s .c o m // Reading the RTF content string Reader in = new StringReader(message); // creating a default blank styled document DefaultStyledDocument styledDoc = new DefaultStyledDocument(); // Creating a RTF Editor kit RTFEditorKit rtfKit = new RTFEditorKit(); // Populating the contents in the blank styled document rtfKit.read(in, styledDoc, 0); // Getting the root document Document doc = styledDoc.getDefaultRootElement().getDocument(); convertedPlainText = doc.getText(0, doc.getLength()); if (replaceLinebreaksWith != null) { convertedPlainText = convertedPlainText.replaceAll("\\n", replaceLinebreaksWith); } return convertedPlainText; }
From source file:com.liferay.portal.util.LuceneFields.java
public static Field getFile(String field, File file, String fileExt) throws IOException { fileExt = fileExt.toLowerCase();/* w w w.j a va 2s .co m*/ FileInputStream fis = new FileInputStream(file); Reader reader = new BufferedReader(new InputStreamReader(fis)); String text = null; if (fileExt.equals(".doc")) { try { WordDocument wordDocument = new WordDocument(fis); StringWriter stringWriter = new StringWriter(); wordDocument.writeAllText(stringWriter); text = stringWriter.toString(); stringWriter.close(); } catch (Exception e) { _log.error(e.getMessage()); } } else if (fileExt.equals(".htm") || fileExt.equals(".html")) { try { DefaultStyledDocument dsd = new DefaultStyledDocument(); HTMLEditorKit htmlEditorKit = new HTMLEditorKit(); htmlEditorKit.read(reader, dsd, 0); text = dsd.getText(0, dsd.getLength()); } catch (Exception e) { _log.error(e.getMessage()); } } else if (fileExt.equals(".pdf")) { try { PDFParser parser = new PDFParser(fis); parser.parse(); PDDocument pdDoc = parser.getPDDocument(); StringWriter stringWriter = new StringWriter(); PDFTextStripper stripper = new PDFTextStripper(); stripper.setLineSeparator("\n"); stripper.writeText(pdDoc, stringWriter); text = stringWriter.toString(); stringWriter.close(); pdDoc.close(); } catch (Exception e) { _log.error(e.getMessage()); } } else if (fileExt.equals(".rtf")) { try { DefaultStyledDocument dsd = new DefaultStyledDocument(); RTFEditorKit rtfEditorKit = new RTFEditorKit(); rtfEditorKit.read(reader, dsd, 0); text = dsd.getText(0, dsd.getLength()); } catch (Exception e) { _log.error(e.getMessage()); } } else if (fileExt.equals(".xls")) { try { XLSTextStripper stripper = new XLSTextStripper(fis); text = stripper.getText(); } catch (Exception e) { _log.error(e.getMessage()); } } if (text != null) { return new Field(field, text, Field.Store.YES, Field.Index.NOT_ANALYZED); } else { return new Field(field, reader); } }
From source file:framework.retrieval.engine.index.create.impl.file.parse.RTFFileContentParser.java
public String getContent(RFileDocument document, String charsetName) { String content = ""; InputStream fileInputStream = null; try {//from ww w . ja v a 2 s . c o m fileInputStream = new FileInputStream(document.getFile()); DefaultStyledDocument styledDoc = new DefaultStyledDocument(); RTFEditorKit rtfEditorKit = new RTFEditorKit(); rtfEditorKit.read(fileInputStream, styledDoc, 0); content = styledDoc.getText(0, styledDoc.getLength()); } catch (Exception e) { RetrievalUtil.errorLog(log, document.getFile().getAbsolutePath(), e); } finally { try { if (fileInputStream != null) { fileInputStream.close(); } } catch (Exception e) { RetrievalUtil.errorLog(log, e); } } return content; }
From source file:Main.java
public Main() throws Exception { setSize(400, 240);//from ww w. ja v a2 s.c om JPanel topPanel = new JPanel(); topPanel.setLayout(new BorderLayout()); getContentPane().add(topPanel, BorderLayout.CENTER); RTFEditorKit rtf = new RTFEditorKit(); JEditorPane editor = new JEditorPane(); editor.setEditorKit(rtf); JScrollPane scroller = new JScrollPane(); scroller.getViewport().add(editor); topPanel.add(scroller, BorderLayout.CENTER); FileInputStream fi = new FileInputStream("test.rtf"); rtf.read(fi, editor.getDocument(), 0); }
From source file:edu.ur.ir.index.DefaultRtfTextExtractor.java
/** * Extract text from the Rich text file document * @throws Exception //w ww .j ava 2 s. com * * @see edu.ur.ir.index.FileTextExtractor#getText(java.io.File) */ public String getText(File f) throws Exception { String text = null; // don't even try if the file is too large if (isFileTooLarge(f) || f.length() <= 0l) { return text; } DefaultStyledDocument styledDoc = new DefaultStyledDocument(); RTFEditorKit editorKit = new RTFEditorKit(); FileInputStream inputStream = null; try { inputStream = new FileInputStream(f); editorKit.read(inputStream, styledDoc, 0); String myText = styledDoc.getText(0, styledDoc.getLength()); if (myText != null && !myText.trim().equals("")) { text = myText; } } catch (OutOfMemoryError oome) { text = null; log.error("could not extract text", oome); throw (oome); } catch (Exception e) { text = null; log.error("could not get text for rich text document " + f.getAbsolutePath(), e); throw (e); } finally { closeInputStream(inputStream); editorKit = null; } return text; }
From source file:RTFView.java
public RTFView() { setTitle("RTF Text Application"); setSize(400, 240);/*from w ww . ja va 2 s . c o m*/ setBackground(Color.gray); getContentPane().setLayout(new BorderLayout()); JPanel topPanel = new JPanel(); topPanel.setLayout(new BorderLayout()); getContentPane().add(topPanel, BorderLayout.CENTER); // Create an RTF editor window RTFEditorKit rtf = new RTFEditorKit(); JEditorPane editor = new JEditorPane(); editor.setEditorKit(rtf); editor.setBackground(Color.white); // This text could be big so add a scroll pane JScrollPane scroller = new JScrollPane(); scroller.getViewport().add(editor); topPanel.add(scroller, BorderLayout.CENTER); // Load an RTF file into the editor try { FileInputStream fi = new FileInputStream("test.rtf"); rtf.read(fi, editor.getDocument(), 0); } catch (FileNotFoundException e) { System.out.println("File not found"); } catch (IOException e) { System.out.println("I/O error"); } catch (BadLocationException e) { } }
From source file:com.aurel.track.lucene.index.associatedFields.textExctractor.RTFExtractor.java
/** * Gets the text from file content //from w w w .ja v a2 s. co m * @param file * @param fileExtension * @return */ @Override public String getText(File file, String fileExtension) { FileInputStream fis = null; Reader reader = null; try { DefaultStyledDocument dsd = new DefaultStyledDocument(); RTFEditorKit rtfEditorKit = new RTFEditorKit(); try { fis = new FileInputStream(file); } catch (FileNotFoundException e) { LOGGER.info("File " + file.getName() + " not found. " + e.getMessage()); LOGGER.debug(ExceptionUtils.getStackTrace(e)); return null; } reader = new BufferedReader(new InputStreamReader(fis)); rtfEditorKit.read(reader, dsd, 0); return dsd.getText(0, dsd.getLength()); } catch (Exception e) { if (LOGGER.isDebugEnabled()) { LOGGER.debug( "Extracting text from the .rtf file " + file.getName() + " failed with " + e.getMessage()); LOGGER.debug(ExceptionUtils.getStackTrace(e)); } return null; } finally { if (reader != null) { try { reader.close(); } catch (IOException e) { LOGGER.debug( "Closing the reader for file " + file.getName() + " failed with " + e.getMessage()); LOGGER.debug(ExceptionUtils.getStackTrace(e)); } } if (fis != null) { try { fis.close(); } catch (IOException e) { LOGGER.debug("Closing the FileInputStream for file " + file.getName() + " failed with " + e.getMessage()); LOGGER.debug(ExceptionUtils.getStackTrace(e)); } } } }
From source file:org.alder.fotobuchconvert.scribus.RtfToScribusConverter.java
public void convert(XmlBuilder xml, String input, ScribusWriter scribus) throws IOException, BadLocationException { if (input == null) return;//from www.j a v a 2 s .com log.debug("RTF input: " + input); CharArrayReader rd = new CharArrayReader(input.toCharArray()); RTFEditorKit kit = new RTFEditorKit(); DefaultStyledDocument doc = (DefaultStyledDocument) kit.createDefaultDocument(); kit.read(rd, doc, 0); output(xml, doc, scribus); }
From source file:org.nuxeo.ecm.core.convert.plugins.text.extractors.RTF2TextConverter.java
@Override public BlobHolder convert(BlobHolder blobHolder, Map<String, Serializable> parameters) throws ConversionException { File f = null;//from ww w . j a v a 2 s. c o m try { RTFEditorKit rtfParser = new RTFEditorKit(); Document document = rtfParser.createDefaultDocument(); rtfParser.read(blobHolder.getBlob().getStream(), document, 0); String text = document.getText(0, document.getLength()); f = Framework.createTempFile("swing-rtf2text", ".txt"); FileUtils.writeStringToFile(f, text); Blob blob; try (InputStream in = new FileInputStream(f)) { blob = Blobs.createBlob(in, "text/plain"); } return new SimpleCachableBlobHolder(blob); } catch (IOException | BadLocationException e) { throw new ConversionException("Error during Word2Text conversion", e); } finally { if (f != null) { f.delete(); } } }