Itext embed font in a PDF
Solution 1
I'm almost sure that you got an answer by now, but maybe others would like to get a detailed view on the solution. Below is the sample java code I used to embed fonts in the generated PDF (useful only in some cases, as the size of the documents increases dramatically). As a free tool to create the PDF forms, I have used the OpenOffice writer, by adding forms inside the documents and exporting the documents as PDF files :
PdfReader pdfTemplate = new PdfReader(templateName);
ByteArrayOutputStream out = new ByteArrayOutputStream();
BaseFont unicode = BaseFont.createFont(unicodeFontPath, BaseFont.IDENTITY_H, BaseFont.EMBEDDED);
PdfStamper stamper = new PdfStamper(pdfTemplate, out);
//assigning the font to the field
stamper.getAcroFields().setFieldProperty("myField", "textfont", unicode, null);
stamper.getAcroFields().setField("myField", someValue);
stamper.close();
pdfTemplate.close();
Solution 2
PdfContentByte ab = yourPDFWriter.getDirectContent();
// field: PdfFormField.createTextField(...);
PdfAppearance ap = ab.createAppearance(320, 30); // size of field
field.setDefaultAppearanceString(ap);
That should do the trick.
user13842621
Updated on June 04, 2022Comments
-
user13842621 almost 2 years
The following code creates the path:
/Users/michellegautier/ACHFiles/26207428-8f40-11e7-8d57-f354c8f15cfa/outbox/PPSACHCOMSpec/mt.ach.06302020.??????_8172363248911097723
, or similar.We are waiting for this file to be created. In the initial
time.sleep(1)
, if I change it to10
, theif not os.path.isfile(filepath)
statement returns false as because it finds the file and everything works great.BUT, if I shorten this time to
1
and the file isn't there yet, I'm trying to find it after it is created. Even if I add large pauses and ensure the file is created, the followingif os.path.isfile(filepath):
in the for loop NEVER passes and I don't know why.I've tried adjusting the statements to exists, check for length of file, etc but no matter what, if it doesn't find it in the first check, it never finds the file in subsequent checks even after it is added. Please help!
home = str(Path.home()) file_date = strftime('%m%d%Y') if company_profile == 'from_storage': company_profile = context.storage['company_profile_id'] filepath = home + '/ACHFiles/' + context.providerID + '/outbox/PPSACHCOMSpec/mt.ach.' + file_date + '.??????_{}'.format( company_profile) time.sleep(int(1)) for name in glob.glob(filepath): filepath = name if not os.path.isfile(filepath): for i in range(3): time.sleep(3) if os.path.isfile(filepath): print('File found: ', filepath) break
-
roganjosh almost 4 yearsPretty sure you're misdiagnosing the problem here.
filepath = name
just overwrites thefilepath
on each iteration and all you're left with is the last one. I don't see where you're creating the file -
roganjosh almost 4 yearsPlease edit the post to get the indentation correct and make sure we have a full minimal reproducible example
-
user13842621 almost 4 years@roganjosh, thank you for the feedback. I am not creating the file in my code. I've launched a process that does that and in this code I'm ready to read that file.
-
Jan Christoph Terasa almost 4 yearsThere is no retry loop.
-
Barmar almost 4 years@JanChristophTerasa
for i in range(3):
is the retry loop. -
Jan Christoph Terasa almost 4 yearsWhat if three iterations aren't enough? Is it intentional to just move on after 9 seconds?
-
-
Elderry over 8 yearsI'm met almost the same problem and I have done the same as you, but I still can't embed the font for those text I input using Java, can you have a look at my question? Thanks in advance!
-
user13842621 almost 4 yearsThat fixed it. Thank you so much for explaining it!