Help coderanch get a
new server
by contributing to the fundraiser
  • 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

Setting values in PDF Field

 
Ranch Hand
Posts: 53
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Rancher
Posts: 43081
77
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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: 43081
77
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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: 43081
77
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Greenhorn
Posts: 11
1
Eclipse IDE Chrome Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
This code will print out all the PDF values and table names separately, just like you asked.

We can do this by using the String.split() method twice - once to split up all the PDF values-table column pairs and a second time to split up each pair into the PDF value and the table column name.



And here's the output:



This was done using a text file in c:\temp with the following value:

pdfFieldEmployee=Employee; pdfFieldStartDate=StartDate; pdfFieldSalary=Salary; pdfFieldExempt=Exempt

Sources:
https://funnelgarden.com/java_read_file/
https://stackoverflow.com/questions/3481828/how-to-split-a-string-in-java

 
This tiny ad will self destruct in five seconds.
We need your help - Coderanch server fundraiser
https://coderanch.com/t/782867/Coderanch-server-fundraiser
reply
    Bookmark Topic Watch Topic
  • New Topic