• 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 all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Liutauras Vilda
  • Bear Bibeault
  • Jeanne Boyarsky
  • Tim Cooke
Sheriffs:
  • Knute Snortum
  • Junilu Lacar
  • Devaka Cooray
Saloon Keepers:
  • Ganesh Patekar
  • Tim Moores
  • Carey Brown
  • Stephan van Hulst
  • salvin francis
Bartenders:
  • Ron McLeod
  • Frits Walraven
  • Pete Letkeman

Reading / Writing RTF Files  RSS feed

 
Ranch Hand
Posts: 42
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Java is not my best programming language I am doing a little project for an organization and the last piece is printing certificates. They have provided the format they want and I have that saved in a .rtf file. I am attempting to read that in and replace some tags {Your Name}, etc with the actual data and then write one out for each person.

I am stumped.....

I can read in the .rtf file and save that back out no problem. However in a loop I am only getting one single page when I have several people I am looping through.

I thought this would be the easiest approach but I am second guessing that. Any thoughts or help would be GREATLY appreciated!
 
Sheriff
Posts: 12494
206
Android Debian Eclipse IDE IntelliJ IDE Java Linux Mac Spring Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Can you post some of the code where you think there might be a problem? It's kind of tough to give you any help about code if we can't see the code.
 
Stephanie Challand
Ranch Hand
Posts: 42
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Sure thing. My main issue is that this only generates 1 single certificate vs the 2 I would expect....

 
Stephanie Challand
Ranch Hand
Posts: 42
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It is definitely the RTF....

The log file is generating the records for each certificate correctly.
 
Junilu Lacar
Sheriff
Posts: 12494
206
Android Debian Eclipse IDE IntelliJ IDE Java Linux Mac Spring Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
See the suggestion in this older thread: https://coderanch.com/t/276174/java/add-page-break-java-output

Also, Strings in Java are immutable. You only need to read in the template certificate once, outside of your loop. This is the code I would write:



One thing I don't understand from your code is why you declare the buildCertificatesFile() method as returning a String value, then declare the String s in it, return s but you don't seem to be doing anything useful to s within the method.  All the useful work is being done with the writer.
 
Stephanie Challand
Ranch Hand
Posts: 42
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks. The code is messy because I have been playing around with it for 2 days trying to get something to work. I was just able to get it to write as separate files using that exact same code. It really seems like the RTF formatting is what is stopping each certificate from printing on a separate page......
 
Junilu Lacar
Sheriff
Posts: 12494
206
Android Debian Eclipse IDE IntelliJ IDE Java Linux Mac Spring Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Did you read through the older thread that I cited? It has some suggestions on how you might write a page break to an RTF file. Maybe you can try what was suggested there and see if it makes a difference.
 
Junilu Lacar
Sheriff
Posts: 12494
206
Android Debian Eclipse IDE IntelliJ IDE Java Linux Mac Spring Ubuntu
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Stephanie Challand wrote:The code is messy because I have been playing around with it for 2 days trying to get something to work.


I advise people to keep their code clean no matter what and especially if you're experimenting. It's hard enough to get clean code that you can easily read to work, it's much tougher to wade through messy code to try to understand why it doesn't work.

Uncle Bob Martin says "The only way to go fast is to go well." 
 
Stephanie Challand
Ranch Hand
Posts: 42
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Noted. Well thanks anyway. When you are trying to get something to work you do not always have time to 'clean it up' or I may want to comment it out because parts were working....I will just generate them as separate RTF files as that works fine the way things stand. Thought someone out there may know something about generating them in a single file which would obviously be preferable.
 
Junilu Lacar
Sheriff
Posts: 12494
206
Android Debian Eclipse IDE IntelliJ IDE Java Linux Mac Spring Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Again, I would refer you to this older thread for an idea: https://coderanch.com/t/276174/java/add-page-break-java-output. ; Basically, the suggestion was to use "\page" instead of "\f" to add a page break.
 
Stephanie Challand
Ranch Hand
Posts: 42
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks. I actually tried the /page before posting. The /f works perfectly fine when I start with a basic .rtf file without much/any formatting. I just added a couple of tags {your name}, {dog name}, etc and replace those in the loop and that generates pages just fine. Starting with the .rtf file they provided and that no longer works. I only get one page with one person's data....so the .rtf file seems to be to blame.
 
Junilu Lacar
Sheriff
Posts: 12494
206
Android Debian Eclipse IDE IntelliJ IDE Java Linux Mac Spring Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Stephanie Challand wrote:I actually tried the /page before posting. The /f works perfectly fine when I start with a basic .rtf file without much/any formatting.



Just to be sure, you actually used \page and not /page, right? There would be a difference.
 
Junilu Lacar
Sheriff
Posts: 12494
206
Android Debian Eclipse IDE IntelliJ IDE Java Linux Mac Spring Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I've also seen suggestions to use \pagebb

 
Stephanie Challand
Ranch Hand
Posts: 42
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks. Actually tried that already too....
 
Junilu Lacar
Sheriff
Posts: 12494
206
Android Debian Eclipse IDE IntelliJ IDE Java Linux Mac Spring Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Stephanie Challand wrote:Starting with the .rtf file they provided and that no longer works.


You might want to recreate the template .RTF file then. It might have some non-visible characters in there that are causing problems. Start with something simple that works, then slowly add content to it from the original template file you were given. It's not beyond the realm of possibility that it's some stray non-visible character that is causing you problems.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!