Hi,
First I would like to thank Kishore for answering my question,
but I think I did not state my problem clearly. So I am going to try one more
time.
1. We have a predefined PDF form, with text fields in it.
a. We used Adobe Acrobat Professional to create the PDF form.
2. The font set in the predefined fields are COURIER
a. We choose COURIER because of its mono-spaced feature
3. We want to set the values in predefined form fields.
So we used pdfbox
1. Load the pdf form
2. Set the value
But the problem now is the original font got changed after setting the value.
The link Kishore referenced is trying to create new field with font and place
it on the pdf, but our requirement is setting values on existing fields and
retain original font.
The original files and tested file:
http://www.2shared.com/document/auMD40In/PRP.html
http://www.2shared.com/document/SdnqJEFO/forms26916.html
The code:
package lab;
import java.io.File;
import org.apache.pdfbox.pdmodel.PDDocument;
import org.apache.pdfbox.pdmodel.PDDocumentCatalog;
import org.apache.pdfbox.pdmodel.interactive.form.PDAcroForm;
public class TestBed1 {
public static void main(String[] args) {
try {
PDDocument pdfDocument = PDDocument.load("C:/Temp/pdf/PRP.pdf");
PDDocumentCatalog docCatalog = pdfDocument.getDocumentCatalog();
PDAcroForm acroForm = docCatalog.getAcroForm();
acroForm.getField("officeAfterHourAndTollFree").setValue(
"Phone: (805) 777-7666 Fax: (805) 777-9876");
File file = File.createTempFile("forms", ".pdf");
pdfDocument.save(file.getCanonicalPath());
pdfDocument.close();
System.out.println(file.getCanonicalPath());
} catch (Exception e) {
e.printStackTrace();
}
}
}
Regards,
From: Kishore Babu [mailto:[email protected]]
Sent: Thursday, October 11, 2012 4:34 AM
To: [email protected]
Subject: RE: Font being changed after form field is set
Check the following links, may be helpful to set the fonts
http://mail-archives.apache.org/mod_mbox/pdfbox-users/201205.mbox/%[email protected]%3E
http://stackoverflow.com/questions/8129451/write-text-into-ploygon-with-pdfbox
[cid:[email protected]]<http://www.envistacorp.com/>
Kishore Babu I Developer
email: [email protected]<mailto:[email protected]>
office: 040.66417681
www.envistacorp.com<http://www.envistacorp.com>
Subscribe<http://pages.exacttarget.com/page.aspx?QS=472529ec60bdf32a36ac1f221ebc1706b66778f96833e451f77fe13f8e4cf0db>
to enVista's Newsletter!
[cid:[email protected]]<http://www.facebook.com/home.php?#%21/pages/enVista/281494945342?ref=search>
[cid:[email protected]] <http://twitter.com/enVistaSpplyChn>
[cid:[email protected]] <http://www.linkedin.com/companies/envista>
[cid:[email protected]]<http://www.inc.com/inc5000/profile/envista>
From: Eugene Chang [mailto:[email protected]]
Sent: Wednesday, 10 October, 2012 8:38 PM
To: [email protected]<mailto:[email protected]>
Subject: RE: Font being changed after form field is set
Sure, attached are the file.
PRP.pdf
is the original file with Courier font
forms26916.pdf
is the data populated form, and font got changed
Actually I attached the two files in my original email, in case you cannot see
the attached files, I have uploaded to a file sharing site, below are the links:
http://www.2shared.com/document/auMD40In/PRP.html
http://www.2shared.com/document/SdnqJEFO/forms26916.html
Regards,
From: Kishore Babu [mailto:[email protected]]
Sent: Wednesday, October 10, 2012 6:13 AM
To: [email protected]<mailto:[email protected]>
Subject: RE: Font being changed after form field is set
Could you please send the original pdf file?
Regards,
[cid:[email protected]]<http://www.envistacorp.com/>
Kishore Babu I Developer
email: [email protected]<mailto:[email protected]>
office: 040.66417681
www.envistacorp.com<http://www.envistacorp.com>
Subscribe<http://pages.exacttarget.com/page.aspx?QS=472529ec60bdf32a36ac1f221ebc1706b66778f96833e451f77fe13f8e4cf0db>
to enVista's Newsletter!
[cid:[email protected]]<http://www.facebook.com/home.php?#%21/pages/enVista/281494945342?ref=search>
[cid:[email protected]] <http://twitter.com/enVistaSpplyChn>
[cid:[email protected]] <http://www.linkedin.com/companies/envista>
[cid:[email protected]]<http://www.inc.com/inc5000/profile/envista>
From: Eugene Chang [mailto:[email protected]]
Sent: Saturday, 06 October, 2012 1:57 AM
To: [email protected]<mailto:[email protected]>
Subject: Font being changed after form field is set
Hi,
We have encounter a problem when using pdfbox.
We have a pdf form with fields and font set to Courier (please see attached pdf
file PRP.pdf).
After running the program below which tries to set the value of one of the
fields, font was being changed to Helvetica (please see attached pdf file
forms26916.pdf).
Is there any way we can set the values and retain the original font?
Regards,
Eugene
package lab;
import java.io.File;
import org.apache.pdfbox.pdmodel.PDDocument;
import org.apache.pdfbox.pdmodel.PDDocumentCatalog;
import org.apache.pdfbox.pdmodel.interactive.form.PDAcroForm;
public class TestBed1 {
public static void main(String[] args) {
try {
PDDocument pdfDocument = PDDocument.load("C:/Temp/pdf/PRP.pdf");
PDDocumentCatalog docCatalog = pdfDocument.getDocumentCatalog();
PDAcroForm acroForm = docCatalog.getAcroForm();
acroForm.getField("officeAfterHourAndTollFree").setValue(
"Phone: (805) 777-7666 Fax: (805) 777-9876");
File file = File.createTempFile("forms", ".pdf");
pdfDocument.save(file.getCanonicalPath());
pdfDocument.close();
System.out.println(file.getCanonicalPath());
} catch (Exception e) {
e.printStackTrace();
}
}
}