I would like to get a JEditorPane to display RTF images like microsoft wordpad. I have a simple program I got from an example off of the web. I created a demo file in wordpad with all of the styles wordpad supports. Strikethrough, super-script, sub-script and highlight are not being read or displayed when reading the file. Highlight would be nice other than that I don't really need the others. The part that is critical is the images in the RTF, they show in wordpad but on in java. I am using:
RTFEditorKit rtf = new RTFEditorKit();
JEditorPane editor = new JEditorPane();
editor.setEditorKit(rtf);
rtf.read(fi, editor.getDocument(), 0);
If RTFEditorKit cannot support images is there a editor kit that can? I can't be the only person to try to get an RTF to work properly. I cannot use an HTML editor kit because I need the images to be embeded like the RTF format.
Related
Here's a code to convert docx to pdf
WordprocessingMLPackage wordMLPackage = WordprocessingMLPackage.load(is);
FOSettings foSettings = Docx4J.createFOSettings();
foSettings.setWmlPackage(wordMLPackage);
Docx4J.toPDF(wordMLPackage, baos);
The problem here is that the generated PDF font is always Times New Roman, which is not the case, the docx template font is actually different, Garamond.
What could be missing or wrong here?
If a font is not installed on the system or embedded in the document, MS Word performs a silent font substitution. To check whether this is happening for your document, open it in Word, go to Word Options > Advanced > Show Document Content and press the Font Substitution button. (Word 2007)
If the font is embedded in the document, or you already have the font installed on your system, then I do not know what to tell you because ideally docx4j should use that font.
If that isn't the case, i.e. Word is using font substitution (which you can determine using the steps above), you can embed the Garmond font in the document using Word Options > Save > Embed Fonts in File. Run through your conversion program and check again.
I have this Desktop email client program that I'm currently working on in Java and I want to show the incoming mail content inside a JEditorPane.
I've created an HTMLEditorKit and set it as an editor kit for the JEditorPane control like this:
editorPane.setEditorKit(htmlEditorKit);
And for some messages, it works fine but for some others, it shows its background as a blue-colored one. I tried to add a stylesheet rule to the kit, but it didn't work.
What could have been the problem?
Any comments will be welcome.
EDIT:
JEditorPane txtContent = new JEditorPane();
txtContent.setEditable(false);
txtContent.setBackground(Color.WHITE);
txtContent.setBounds(316, 110, 598, 545);
kit = new HTMLEditorKit();
txtContent.setEditorKit(kit);
HTMLDocument doc = (HTMLDocument)kit.createDefaultDocument();
txtContent.setDocument(doc);
txtContent.setText("HTML content here.....");
When I put "HTML content here....." part inside an html file with Notepad, it shows the right thing.
But when I put the exact same content inside the JEditorPane like the above, it shows different styling(blue background).
I am using the following code to generate a PDF file of the HTML Report
String url = new File("Test.html").toURI().toURL().toString();
OutputStream os = new FileOutputStream("Test.pdf");
ITextRenderer renderer = new ITextRenderer();
renderer.setDocument(url);
renderer.layout();
renderer.createPDF(os);
os.close();
I was able to use it on sample HTML files to convert to pdf. But when it comes to my real usage, the HTML content consists of various special symbols, like &,<,> that can't be parsed by XML.
I tried using CDATA, while generating HTML itself, but later found that the text around CDATA is not visible in HMTL.
Does anyone have a solution for this?
Have you tried to print to pdf from the browser? Google primo pdf for a program that we'll let you do it.
I don't know if this will help you, but you can use StringEscapeUtils from apache-commons. It has methods for escape and unescape HTML (you may use them to pre-process your HTML before PDF generation).
Basically I have a file called FlowLayout.html and I want to read it into the JEditorPane. I have all the code up to that point but I don't know how to open the file in the pane.
JEditorPane jtfInfo = new JEditorPane();
jtfInfo.setEditable(false);
p3.add(jtfInfo);
public void getFlowLayout(){
jftInfo.setContentType("text/html");
//I dont know this step and setContentType gives me an error in netbeans Cannot find symbol
}
You can just call the setPage() method with URL pointing at the FlowLayout.html file:
public void getFlowLayout(){
jftInfo.setPage(new URL("path to file"));
}
From the Java Tutorial: How to Use Editor Panes and Text Panes:
"The setPage method opens the resource pointed to by the URL and figures out the format of the text (which is HTML in the example). If the text format is known, the editor pane initializes itself with the text found at the URL. A standard editor pane can understand plain text, HTML, and RTF. Note that the page might be loaded asynchronously, which keeps the GUI responsive but means that you should not count on the data being completely loaded after the call to setPage returns."
How can I open an HTML page and show it in a text area? (If I choose the HTML file with the JFileChooser, how can I open that page and show it in the text area?)
URL url = new URL(String s);
JEditorPane pane = JEditorPane(url);
But how can I find the link of the HTML file for inserting as s, here!?
A TextArea is for displaying/editing text, not for showing formatted HTML.
JEditorPane supports HTML markup, but only a rather limited subset.
For full HTML support, you're going to need third-party components. Look at the answers to this question for links.
Format the HTML with a <pre> tag
I guess you can use any properly formatted URL a browser would use e.g.
http://stackoverflow.com/questions/1239454/how-can-i-open-an-html-page-and-show-it-in-a-text-area
But then again Java is very keen on security and you might not be allowed to use certain URLs in your environment.
And like Michael Borgwardt said - the JEditorPane's support of HTML is very limited and some tags (i think <div> is one of them) as well as JavaScript are not supported.
For implementation of a simple browser have a look at this
JEditorPane Tutorial