Ulf Dittmer wrote:
I'm fairly certain that it's possible to use Swing components even on an otherwise GUI-less server (provided the X11 libraries are present if you're on Unix).
Ulf Dittmer wrote:There's a Swing component that can render RTF (RTFEditorToolkit or something like that). If you render that into a BufferedImage then you can use the ImageIO class to save it to a JPEG.
Joe Ess wrote:It's not complex, but you will have to put a couple of unrelated things together. First, display the RTF in an EditorPane.
Then create a BufferedImage like this.
Next, get the Graphics instance from your BufferedImage and pass it to the EditorPane's paint() method.
Finally you can use javax.imageio.ImageIO class to write the Image to a file.
Joe Ess wrote:This is a bad idea:
If you get an exception, you'll never know it.
As for your question, here's a hint: bii.getGraphics() returns some value.
public void paintComponents(Graphics g)
Paints each of the components in this container.
Joe Ess wrote:
Next, get the Graphics instance from your BufferedImage and pass it to the EditorPane's paint() method.
Joe Ess wrote:Think of it this way: the graphics object is the "writable" part of the buffered image. You are taking a reference to that writable part, handing it to the editor pane and telling it to draw on it. No return value needed because the graphics never really leaves the buffered image. You are only exposing it.
Joe Ess wrote:You may want to work with a local file until you get the conversion working. Solve one problem at a time.
Joe Ess wrote:How big is the RTF?
Joe Ess wrote:How big is the RTF?
Joe Ess wrote:I believe the problem is that you either have to run the program in "headless" mode (see my link above) or display the rtf to the screen. If the page isn't rendered by something, there's nothing to write onto the graphics context and no data to write to file.
To set up headless mode, set the appropriate system property by using the setProperty() method. This method enables you to set the desired value for the system property that is indicated by the specific key.
System.setProperty("java.awt.headless", "true");
You can also use the following command line if you plan to run the same application in both a headless and a traditional environment:
java -Djava.awt.headless=true
Ulf Dittmer wrote:I added an example code for this in the CodeBarn: http://faq.javaranch.com/java/RTF2Image
Joe Ess wrote:Does this work?
Joe Ess wrote:I tried Ulf's code and it Works For Me ™
I suspect that the RTF hasn't loaded when you are trying to generate the image.
Try with the simplest RTF you can find (MS Word can save files as RTF, as can WordPad). If that works, it's probably a timing issue.
Can you supply us with an RTF that doesn't work? We don't want anything with sensitive data on it.
Steve Dyke wrote:
Here is the file I have been testing.
Steve Dyke wrote:
Joe Ess wrote:I tried Ulf's code and it Works For Me ™
I suspect that the RTF hasn't loaded when you are trying to generate the image.
Try with the simplest RTF you can find (MS Word can save files as RTF, as can WordPad). If that works, it's probably a timing issue.
Can you supply us with an RTF that doesn't work? We don't want anything with sensitive data on it.
Here is the file I have been testing.
Joe Ess wrote:
Steve Dyke wrote:
Here is the file I have been testing.
Where?
One more question: do you see the RTF displayed in a pop-up window?
Joe Ess wrote:Ah, "Files with the extension .rtf are not allowed as attachment in the message".
Let's try a changing the extension to "jpg". . .
Joe Ess wrote:Works For Me ™
Steve Dyke wrote:
Joe Ess wrote:Works For Me ™
Thanks for all the help.
I finally have it working now. Any chance of getting rid of the black bar on the right and bottom?
Steve Dyke wrote:
Joe Ess wrote:Works For Me ™
Thanks for all the help.
I finally have it working now. Any chance of getting rid of the black bar on the right and bottom?
Steve Dyke wrote:
It was working fairly good until I an .rtf that contains a graphic and is 3Megs plus. It only converts the text part.
Steve Dyke wrote:
Is there any other format that might be clearer when displayed than .jpg?
Joe Ess wrote:
With such a large file, my first guess (again) would be a timing problem.
What IDE are you using?
If you believe you can tell me what to think, I believe I can tell you where to go. Go read this tiny ad!
Smokeless wood heat with a rocket mass heater
https://woodheat.net
|