• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

how to read .rtf, search and replace text, and write/print .rtf!

 
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
So i am in the process of writing a GUI based app that has multiple jTextField and jTextArea inputs that i wish to put in specific places in 4 separate .rtf files. I have essentially completed the gui, and am now attempting to implement the read file, search file, replace/insert text, write/print file aspects of the app... it is here that i have hit a creative wall (most certainly attributed to my newbness). after many hours of googling and examining much source code i have decided to visit a forum and ask for some guidance. first and foremost, would you all recommend the usage of RTFEditorKit? if so, how would i implement the search and replace feature? i was thinking that in the template .rtfs i would essentially put a code word in each place that had an input coming in to replace it, search for that code word, and then finally replace it with the user input. i hope this is all understandable... thanks for your time!

edit: i'd love to do a similar thing with .doc if that was easier... i have read about apache poi, but wonder if this would be doable... let me know what you think
 
Sheriff
Posts: 22781
131
Eclipse IDE Spring VI Editor Chrome Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Check out http://faq.javaranch.com/java/AccessingFileFormats
 
Rancher
Posts: 43081
77
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I wouldn't recommend RTFEditorKit; it has numerous bugs and hasn't seen significant updates in many years.
If you have ready-made RTFs in which you want to replace placeholders like "${name}" with "Sancho Xavier" or something like that, then regular expression can do that (since RTFs are text files). Even better would be to use a templating engine like Apache Velocity or FreeMarker.

DOC is harder, since that's a binary format. The easiest may be to create RTF, and then use the JODConverter library to create a DOC file from that.
Alternatively, some discussion of replacing text using POI's HWPF module can be found at http://mail-archives.apache.org/mod_mbox/poi-user/200812.mbox/browser in the discussion "POI - HWPFDocument". The code that goes with it is at http://test.javaranch.com/ulf/ParagraphText.java and http://test.javaranch.com/ulf/InsertText.java
 
Sancho Xavier
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
So I have taken your advice and gone with Freemarker. Everything is working out just fine, as I now have an app that can take in a bunch of user input, scan and place text into various templates, and finally spit out a properly formatted .rtf file. Now the final hurdle that i face is enabling the print function. After some googling and searching these forums, I am still a bit mixed up regarding both the possibility and the implementation of printing a formatted .rtf file through a java app. Some methods involve slapping the .rtf to a jEditorPane, and some say it is possible to print directly... Any guidance would be greatly appreciated!
 
Ulf Dittmer
Rancher
Posts: 43081
77
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
With jEditorPane you're limited to those RTF features it can deal with - which is only a subset of RTF, and even parts of those are buggy. If it works with the files you have - great, but if not then the problem is most likely not going to be fixed any time soon.

As to printing directly, you'd need a printer driver or printer that understands RTF; that's not unimaginable, but not a feature I've seen advertised.
 
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,

I am also trying to read an rtf file and replace the place holders using freemarker. But free marker is not properly reading the rtf files. I am getting some junk text instead of my the real content in the file
the junk text looks like this :- {\rtf1\adeflang1025\ansi\ansicpg1252\uc1\adeff0\deff0\stshfdbch0\stshfloch0\stshfhich0\stshfbi0)

Please help.

Thanks,
Bindu
 
Ulf Dittmer
Rancher
Posts: 43081
77
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hard to say what's going on without seeing the original text, and the code that did the replacing.
 
reply
    Bookmark Topic Watch Topic
  • New Topic