Change BaseFont size in ItextPdf - java

I am using arial font using BaseFont in ItextPdf in my Java app. I am updating the acro fields of an editable pdf and regenerating. It's working all fine.
I am creating it as:
ArrayList<BaseFont> fonts = new ArrayList<BaseFont>();
BaseFont baseFont = BaseFont.createFont("/path/to/arial.ttf",BaseFont.IDENTITY_H,BaseFont.EMBEDDED);
fonts.add(baseFont);
and within my acroform I am attaching that font like this:
acroForm.setSubstitutionFonts(fonts);
How can I change the size of this BaseFont?

Related

HTML to PDF using flying saucer: bold text is blurry

I am using flying saucer (xhtmlrenderer) 9.1.22 to create PDF files from HTML content,
but the bold text in PDF looks blurry.
this is my code snippet:
ITextRenderer renderer = new ITextRenderer();
renderer.getFontResolver().addFont("arial.ttf", BaseFont.CP1252, BaseFont.EMBEDDED);
renderer.setDocumentFromString(htmlAsString);
renderer.layout():
renderer.createPDF(output);

Setting substitution fonts in AcroForm itext7

I have a pdf with AcroForm and need to fill it with string that contains different languages glyphs (English, Chinese, Korean, Khmer).
In iText5 I've used:
AcroFields form = stamper.getAcroFields();
form.addSubstitutionFont(arialFont);
form.addSubstitutionFont(khmerFont);
And it worked fine for Chinese and Korean, but I've faced an issue with Khmer ligatures not being rendered. Found out that I need pdfCalligraph addon to make ligatures work, but it comes with iText7 only. I've managed to add paragraphs with proper Khmer ligatures rendering (requiring typography as a dependency and loading a license key). But in AcroForm it won't do it automatically. I'm struggling to find an iText7 version of addSubstitutionFont and make it work with pdfCalligraph.
Code I've used with iText7:
PdfReader reader = new PdfReader(templatePath);
PdfDocument pdf = new PdfDocument(reader, new PdfWriter(outputPath));
Document document = new Document(pdf);
PdfAcroForm form = PdfAcroForm.getAcroForm(pdf, true);
PdfFont khmerFont = PdfFontFactory.createFont(pathToKhmerFont, PdfEncodings.IDENTITY_H, true);
PdfFont font = PdfFontFactory.createFont(pathToArialUnicodeFont, PdfEncodings.IDENTITY_H, true);
pdf.addFont(khmerFont);
pdf.addFont(font);
FontSet set = new FontSet();
set.addFont(pathToKhmerFont);
set.addFont(pathToArialUnicodeFont);
document.setFontProvider(new FontProvider(set));
document.setProperty(Property.FONT, "Arial");
form.setNeedAppearances(true);
String content = "khmer ថ្ងៃឈប់សម្រាក and chinese 假日 and korean 휴일";
PdfFormField tf = form.getField("Text3");
tf.setValue(content);
// tf.setFont(khmerFont);
tf.regenerateField();
// add a paragraph just to check pdfCalligraph works
document.add(new Paragraph(content));
pdf.close();
String used to test proper rendering: "khmer ថ្ងៃឈប់សម្រាក and chinese 假日 and korean 휴일"
iText5 in form field without pdfCalligraph, but with substitution fonts:
iText7 in form field with pdfCalligraph loaded (set arial font field.setFont(arialFont)):
iText7 in form field with pdfCalligraph loaded (set khmer font field.setFont(khmerFont)):
iText7 same document but in a paragraph instead of form field with pdfCalligraph loaded (It is an expected resut, so it does use pdfCalligraph for paragraphs, but not for form fields):
So, as you can see there're basically 2 issues:
How do I addSubstitutionFont in iText7?
How do I use pdfCalligraph in PdfFormField appearance?
I've also checked if pdfCalligraph works in text form and it looks like it does not. Here is a code I've used to check it:
LicenseKey.loadLicenseFile(path_to_license);
String outputPath = path_to_output_doc;
PdfDocument pdf = new PdfDocument(new PdfWriter(outputPath));
Document document = new Document(pdf);
// prepare fonts for pdfCalligraph to use
FontSet set = new FontSet();
set.addFont("/path_to/Khmer.ttf");
set.addFont("/path_to/ArialUnicodeMS.ttf");
FontProvider fontProvider = new FontProvider(set);
document.setFontProvider(fontProvider);
document.setProperty(Property.FONT, "Arial");
String content = "khmer ថ្ងៃឈប់សម្រាក and chinese 假日 and korean 휴일";
// Add a paragraph to check if pdfCalligraph works
document.add(new Paragraph(content));
// Add a form text field
PdfAcroForm form = PdfAcroForm.getAcroForm(pdf, true);
PdfTextFormField field = PdfFormField.createText(pdf, new Rectangle(36, 700, 400, 30), "test");
field.setValue(content);
form.addField(field);
document.close();
Output with pdfCalligraph dependency loaded (as you can see paragraph rendered properly, but in form all non-halvetica chars just ignored:
Output without pdfCalligraph dependency loaded (as you can see paragraph is not rendered properly which is expected, the form field looks same as with loaded pdfCalligraph):
Am I missing something?

PDFTable Itext arabic

I have coded java code and I wanted Arabic words to be displayed at PdfPTable which was asses to itext document to create PDF document
as attached picture "???" is Arabic code '
PdfPTable header = new PdfPTable(6);
PdfPTable tbame = new PdfPTable(1);
tbame.addCell(" >>>>>> " + install.getCustId().getFullName() + " <<<<<<");
tbame.setHorizontalAlignment(PdfPTable.ALIGN_CENTER);
tbame.setLockedWidth(false);
tbame.setExtendLastRow(false);
tbame.setWidthPercentage(100);
header.addCell("End");
header.addCell("Start");
Please read the documentation and you'll find out that the addCell(String content) method can not be used to add Arabic text for two reasons:
When you use this method, the default font Helvetica is used. You need to use a font that knows how to draw Arabic shapes. This is explained in the answer to this question: Itext Arabic Font coming as question marks
Arabic is written from right to left, which means that you need to change the run direction of the content of the cell as is explained in my answer to this question: RTL not working in pdf generation with itext 5.5 for Arabic text
A code snippet:
BaseFont bf = BaseFont.createFont("c:/WINDOWS/Fonts/arialuni.ttf",
BaseFont.IDENTITY_H, BaseFont.EMBEDDED);
Font font = new Font(bf, 12);
Phrase phrase = new Phrase(
"\u0644\u0648\u0631\u0627\u0646\u0633 \u0627\u0644\u0639\u0631\u0628", font);
PdfPCell cell = new PdfPCell(phrase);
cell.setRunDirection(PdfWriter.RUN_DIRECTION_RTL);
table.addCell(cell);
If you don't have access to the font arialuni.ttf, you'll have to find another font that contains Arabic glyphs.

Using a primary and a fallbackfont in Flying Saucer PDF generator

I'm having trouble getting flying saucer to use a secondary font for the glyphs/charachters which are not present in my main font.
The Java code I'm using for this purpose is more or less:
String result = getPrintHtmlContent(urlString);
result = CharacterConverter.replaceInvalidCharacters(result);
ITextRenderer renderer = new ITextRenderer();
renderer.getFontResolver();
renderer.getFontResolver().addFont(FONTS_DIR_PATH + "ARIALUNI.TTF", BaseFont.IDENTITY_H, BaseFont.EMBEDDED);
renderer.getFontResolver().addFont(FONTS_DIR_PATH + "droidsans/DroidSans.ttf", BaseFont.IDENTITY_H, BaseFont.EMBEDDED);
renderer.getFontResolver().addFont(FONTS_DIR_PATH + "droidsans/DroidSansBold.ttf", BaseFont.IDENTITY_H, BaseFont.EMBEDDED);
renderer.setDocumentFromString(result, "http://" + frontendHost + ":" + frontendPort + frontendContextRoot);
renderer.layout();
renderer.createPDF(os);
And the css:
body {
font-family: "Droid Sans", "Arial Unicode MS";
}
I have also included the fonts in the css by using the #font-face rule.
I am able to get this to work using either of the fonts separately, so there seems to be no problem with flying saucer finding the fonts or the css not rendering correctly.
If I on the other hand do as above and try to use both fonts the output PDF is only using Droid Sans...
Is it even possible to use a "fallback font" in flying saucer, as it is on websites?
I asked the same question on Flying Saucer developer community and got a reply:
https://groups.google.com/forum/#!topic/flying-saucer-dev/5p00ISwnxiw
In short the answer is NO, it is not possible to use a secondary font.

How to get less-than-equal-to to show in iTextRenderer conversion

I've tried everything I know but still cannot get the ≥ and ≤ characters to show up in my PDF generated by Flying Saucer's iTextRenderer from xHTML. They display fine in my browser as HTML of course. I've tried different xml encoding types, doctypes, and using the decimal and hex values instead of the shortcut names. Still, no errors display, but I get a blank space where the characters should be.
Has anyone successfully converted an xhtml to pdf using iTextRenderer with the ≤ and ≥ special symbols, and if so, how?
SOLUTION: Okay, I got this one solved. Turns out I did need to embed the Verdana font into the PDF using the follow lines of Java code:
renderer.getFontResolver().addFont("/Library/Fonts/Verdana.ttf", BaseFont.IDENTITY_H, BaseFont.EMBEDDED);
renderer.getFontResolver().addFont("/Library/Fonts/Verdana Bold.ttf", BaseFont.IDENTITY_H, BaseFont.EMBEDDED);
renderer.getFontResolver().addFont("/Library/Fonts/Verdana Italic.ttf", BaseFont.IDENTITY_H, BaseFont.EMBEDDED);
renderer.getFontResolver().addFont("/Library/Fonts/Verdana Bold Italic.ttf", BaseFont.IDENTITY_H, BaseFont.EMBEDDED);
SOLUTION: Okay, I got this one solved. Turns out I did need to embed the Verdana font into the PDF using the follow lines of Java code:
renderer.getFontResolver().addFont("/Library/Fonts/Verdana.ttf", BaseFont.IDENTITY_H, BaseFont.EMBEDDED);
renderer.getFontResolver().addFont("/Library/Fonts/Verdana Bold.ttf", BaseFont.IDENTITY_H, BaseFont.EMBEDDED);
renderer.getFontResolver().addFont("/Library/Fonts/Verdana Italic.ttf", BaseFont.IDENTITY_H, BaseFont.EMBEDDED);
renderer.getFontResolver().addFont("/Library/Fonts/Verdana Bold Italic.ttf", BaseFont.IDENTITY_H, BaseFont.EMBEDDED);

Categories