• Post Reply Bookmark Topic Watch Topic
  • New Topic

Setting values in PDF Field  RSS feed

 
Alissa Horner
Ranch Hand
Posts: 53
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I have a PDF file and I have a text file that contains this data. It has the name of a PDF field along with a database table column name.

Text file:
pdfFieldEmployee=Employee; pdfFieldStartDate=StartDate; pdfFieldSalary=Salary; pdfFieldExempt=Exempt

I also have this hash map that contains the database table column names along with their corresponding values:
Employee, Don Parker
StartDate, 5/6/2000
Salary, $55000
Exempt, N

How would I read through the text file and the hash map and at the same time do this:

if the pdf field is equal to pdfFieldEmployee, then set pdfFieldEmployee equal to Don Parker.
if the pdf field is equal to pdfFieldStartDate, then set pdfFieldStartDate equal to 5/6/2000.

and so on and so on. I would prefer to do this with a loop because I think it would take less code than writing a bunch of if statements.
 
Ulf Dittmer
Rancher
Posts: 42972
73
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Welcome to the Ranch.

Is this question primarily about how to iterate over a HashMap, about reading a text file, or about altering a PDF file? If the latter, then the PDFBox library can do that. Grab the file pdfbox-app-1.8.7.jar from http://pdfbox.apache.org/download.cgi and include it in your classpath. An example of how to use the library to change form fields can be found here.
 
Alissa Horner
Ranch Hand
Posts: 53
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I would like to know how to iterate over a HashMap and read a text file.
 
Ulf Dittmer
Rancher
Posts: 42972
73
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
For reading a file, check out http://www.mkyong.com/java/how-to-read-file-from-java-bufferedreader-example/

For iterating over a HashMap, something like this should do (assuming that both key and value are strings, but it sounds like they would be).
 
Alissa Horner
Ranch Hand
Posts: 53
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank you for the sample code that iterates over the HashMap.

I went to the link for reading a file. That web page discusses how to read one line at a time.

If I have this line in my text file, how would I get every string that precedes the equals sign and print it out?
pdfFieldEmployee=Employee; pdfFieldStartDate=StartDate; pdfFieldSalary=Salary; pdfFieldExempt=Exempt

 
Ulf Dittmer
Rancher
Posts: 42972
73
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You can use String.split (with a semicolon) to get the constituent parts, and then grab the pieces before and after the equals sign using the String.indexOf and String.substring methods. String.trim would also be useful to remove any unwanted spaces.
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!