aspose file tools*
The moose likes Java in General and the fly likes Java Code to Convert RTF to JPG Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Java » Java in General
Bookmark "Java Code to Convert RTF to JPG" Watch "Java Code to Convert RTF to JPG" New topic
Author

Java Code to Convert RTF to JPG

Steve Dyke
Ranch Hand

Joined: Nov 16, 2004
Posts: 1466
    
    1
Is there a way to read a rtf file and convert it to jpg? I find several software solutions on the web but I need an API to add to my web app or help with my own code to accomplish this task. This is a dynamic web application using JSP, Servlets, Classes and outputing reports using JasperReports.
Ulf Dittmer
Marshal

Joined: Mar 22, 2005
Posts: 42289
    
  64
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.

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).


Ping & DNS - my free Android networking tools app
Joe Ess
Bartender

Joined: Oct 29, 2001
Posts: 8927
    
    9

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).


You are correct: Headless Mode


"blabbing like a narcissistic fool with a superiority complex" ~ N.A.
[How To Ask Questions On JavaRanch]
Steve Dyke
Ranch Hand

Joined: Nov 16, 2004
Posts: 1466
    
    1
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.



Any chance on example code or directing me to an example?

Joe Ess
Bartender

Joined: Oct 29, 2001
Posts: 8927
    
    9

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.
Steve Dyke
Ranch Hand

Joined: Nov 16, 2004
Posts: 1466
    
    1
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.


I am here so far but not sure if this is right and where to go next.

[code]

public void setRTFtoJPG(){

//this will be a dynamic path(parameter sent to class)
String filePath = "http://gvsvr03/pre_pdf/sourcefiles/861000/861237F1.rtf";

JEditorPane editorPane = new JEditorPane();
editorPane.setEditable(false);

try{
URL url = new URL ( filePath );

try{

if(url.openConnection().getContentLength() > 4040){
editorPane.setPage(url);

BufferedImage bii = null;

bii = new BufferedImage(50, 50, BufferedImage.TYPE_INT_RGB);

bii.getGraphics();
}
}
catch(IOException ie){

}


}
catch(MalformedURLException me){
System.out.println(me);
this.errorString = "Error";
}

}

[code]
Joe Ess
Bartender

Joined: Oct 29, 2001
Posts: 8927
    
    9

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.
Steve Dyke
Ranch Hand

Joined: Nov 16, 2004
Posts: 1466
    
    1
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.


This is my latest attempt but get compile error on

this line


Joe Ess
Bartender

Joined: Oct 29, 2001
Posts: 8927
    
    9

JavaDoc says:
public void paintComponents(Graphics g)
Paints each of the components in this container.


Note paintComponents does not return a value.





Steve Dyke
Ranch Hand

Joined: Nov 16, 2004
Posts: 1466
    
    1
Joe Ess wrote:
Next, get the Graphics instance from your BufferedImage and pass it to the EditorPane's paint() method.


I have changed the code but still have problems. So how do I get this to work? Somehow I have to tie the editorPane to the bufferedImage don't I?

Joe Ess
Bartender

Joined: Oct 29, 2001
Posts: 8927
    
    9

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.
Steve Dyke
Ranch Hand

Joined: Nov 16, 2004
Posts: 1466
    
    1
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.


Ok. I have adjused the code and got it to compile but now I says the file can not be found. However, if I place the URL in an address bar it opens it fine.

Here is the console output:

http://gvsvr03/pre_pdf/sourcefiles/861000/861237F1.rtf
SystemOut O Not Found
SRVE0068E: Could not invoke the service() method on servlet ViewDrawing6. Exception thrown : java.lang.IllegalArgumentException: output == null!


Joe Ess
Bartender

Joined: Oct 29, 2001
Posts: 8927
    
    9

You may want to work with a local file until you get the conversion working. Solve one problem at a time.
Steve Dyke
Ranch Hand

Joined: Nov 16, 2004
Posts: 1466
    
    1
Joe Ess wrote:You may want to work with a local file until you get the conversion working. Solve one problem at a time.


When I try to do the



If I use .rtf if cannot find but if I use .pdf it does.
Joe Ess
Bartender

Joined: Oct 29, 2001
Posts: 8927
    
    9

How big is the RTF?
Steve Dyke
Ranch Hand

Joined: Nov 16, 2004
Posts: 1466
    
    1
Joe Ess wrote:How big is the RTF?


RTF = 2K
PDF = 69,000K
Steve Dyke
Ranch Hand

Joined: Nov 16, 2004
Posts: 1466
    
    1
Joe Ess wrote:How big is the RTF?


I now have it producing a jpg file however, it is just producing a black square.

This is the rendered value

BufferedImage@2df9ec1d: type = 1 DirectColorModel: rmask=ff0000 gmask=ff00 bmask=ff amask=0 IntegerInterleavedRaster: width = 100 height = 100 #Bands = 3 xOff = 0 yOff = 0 dataOffset[0] 0
Joe Ess
Bartender

Joined: Oct 29, 2001
Posts: 8927
    
    9

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.
Steve Dyke
Ranch Hand

Joined: Nov 16, 2004
Posts: 1466
    
    1
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.


Can you help decipher how to run this class in headless mode while leaving the rest of the app the way it is?
Joe Ess
Bartender

Joined: Oct 29, 2001
Posts: 8927
    
    9

From the article i posted:

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
Marshal

Joined: Mar 22, 2005
Posts: 42289
    
  64
I added an example code for this in the CodeBarn: http://faq.javaranch.com/java/RTF2Image
Joe Ess
Bartender

Joined: Oct 29, 2001
Posts: 8927
    
    9

Looks good, but how does it handle multiple page documents
Steve Dyke
Ranch Hand

Joined: Nov 16, 2004
Posts: 1466
    
    1
Ulf Dittmer wrote:I added an example code for this in the CodeBarn: http://faq.javaranch.com/java/RTF2Image


This looks like it will work for me except one problem. How do I use the URL of the file I need to convert?



needs to be



But if I use url.getfile() I only get part of the path(all but "http://gvsvr03/drawings". The files I need to access are stored in virual directories.
Joe Ess
Bartender

Joined: Oct 29, 2001
Posts: 8927
    
    9

Does this work?
Steve Dyke
Ranch Hand

Joined: Nov 16, 2004
Posts: 1466
    
    1
Joe Ess wrote:Does this work?


It is now producing a file again. However, it is still just a black square.
Joe Ess
Bartender

Joined: Oct 29, 2001
Posts: 8927
    
    9

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
Ranch Hand

Joined: Nov 16, 2004
Posts: 1466
    
    1
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
Bartender

Joined: Oct 29, 2001
Posts: 8927
    
    9

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?
Steve Dyke
Ranch Hand

Joined: Nov 16, 2004
Posts: 1466
    
    1
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.


I cannot get a file to attach.
Steve Dyke
Ranch Hand

Joined: Nov 16, 2004
Posts: 1466
    
    1
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?


I get a message when tring to attach my file that .rtf is not allowed.

And no, I do not get a popup.
Joe Ess
Bartender

Joined: Oct 29, 2001
Posts: 8927
    
    9

Ah, "Files with the extension .rtf are not allowed as attachment in the message".
Let's try a changing the extension to "jpg". . .


[test.jpg]

Steve Dyke
Ranch Hand

Joined: Nov 16, 2004
Posts: 1466
    
    1
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". . .


Here is another attempt at posting my .rtf file(changed to .jpg)




[861237F1.jpg]

Joe Ess
Bartender

Joined: Oct 29, 2001
Posts: 8927
    
    9

Works For Me ™


[Thumbnail for example.jpg]

Steve Dyke
Ranch Hand

Joined: Nov 16, 2004
Posts: 1466
    
    1
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?
Joe Ess
Bartender

Joined: Oct 29, 2001
Posts: 8927
    
    9

Graphics2D.setBackground(java.awt.Color) sounds like it would work.
Steve Dyke
Ranch Hand

Joined: Nov 16, 2004
Posts: 1466
    
    1
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?


It was working fairly good until I an .rtf that contains a graphic and is 3Megs plus. It only converts the text part. Is there any other format that might be clearer when displayed than .jpg?
Steve Dyke
Ranch Hand

Joined: Nov 16, 2004
Posts: 1466
    
    1
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?


After I open my IDE and run the RTF to JPG code the first time I get the following in the console. It does not happen if I run it again. Any ideas?

After a little more testing I think it is breaking on this line of code:



Joe Ess
Bartender

Joined: Oct 29, 2001
Posts: 8927
    
    9

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.

Do images usually work (I haven't tried them). The API docs make a statement to the effect "don't blame us [the swing team] for this code"
With such a large file, my first guess (again) would be a timing problem.

Steve Dyke wrote:
Is there any other format that might be clearer when displayed than .jpg?


Is the RTF as presented by the editor pane clear? I remember thinking the one I did above was a little grainy.
The Java 2D API has some image transforming capabilities. You could also try saving the image as a GIF, as GIF does not lose information in compression, as JPG does.

What IDE are you using?
Steve Dyke
Ranch Hand

Joined: Nov 16, 2004
Posts: 1466
    
    1
Joe Ess wrote:
With such a large file, my first guess (again) would be a timing problem.

What IDE are you using?


How can I allow more time for the process?

I am using Websphere Development Studio.

Any ideas on the error message issue?
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Java Code to Convert RTF to JPG