Regd. The font 'ABCDEE+Calibri' contains bad/widts on Unix/Linux - java

Greetings for the Day :)
I am working on PdfBox and generated PDF successfully as per requirement on Windows. But when running my solution on Unix/Linux and it is generating the PDFs as per requirement. When we tried to open the PDF with Adobe Acrobat Reader DC version it is giving Pop-up The font 'ABCDEE+Calibri' contains bad/widts. I moved the PDF to windows from linux and tried to open the pdf , now its poping up on windows as well. I have not used calibri anywhere on my pdf. I used TrueTypeFont to load.ttc file as .ttf file. Apart from that there is no great logic as well.
Map<String, PDFont> suppFonts = new HashMap<>();
PDFont arial = PDType0Font.load(pddoc, <<className>>.class.getResourceAsStream("/path/to/the/file.ttf").getInputStream());
PDFont mingliu = PDType0Font.load(pddoc, new TrueTypeCollection(<<className>>.class.getResourceAsStream("/path/to/the/file.ttc").getInputStream());
suppFonts .put("arial",arial);
suppFonts .put("mingliu",mingliu);
we used this HashMap and retrieved font with the name we have given such as arial,mingliu..etc.
Please help me if you have faced such kind of issue earlier.

Related

Font different in MS Edge than Chrome for PDF when using PDFBox

I have a PDF template created in Acrobat Reader DC which contains a field that I trying to fill with some text. The field has a specific font that I want to keep. I am able to obtain the field and change the value.
However, when I open the PDF in Internet Explorer the font is a default font. The confusing part is that if I open it in Chrome then it shows the correct font. Not sure why that is, any help is appreciated. I am using PDFBox version 2.
(The font works if I don't use Java to edit the file, if I just manually change it inside Acrobat and save the file then it shows correctly.)
See below for the code used.
File file = new File("PDFToReadFrom.pdf");
PDDocument pdDoc = PDDocument.load(file);
PDDocumentCatalog pdCatalog = pdDoc.getDocumentCatalog();
PDAcroForm pdAcroForm = pdCatalog.getAcroForm();
for(PDField pdField : pdAcroForm.getFields()){
pdField.setValue("value");
}
pdDoc.save(new File("test.pdf"));
pdDoc.close();
I suggest you'd better to compare the PDF file (Use Java edited file and the Acrobat generated file), whether they are using the same font.
According to this article, it seems that we could set the font when using PDFBox to create a PDF file.

iText PDF values not shown on iOS

We create PDF files with iText (in Java) which are working fine on Windows / Android, however when we open such file on an iPhone the acroFields that were set in the code are empty.
We are using the following code and the setGenerateAppearances is already set to true.
PdfStamper pdfStamper = new PdfStamper(templateReader, pagePdf);
AcroFields acroFields = pdfStamper.getAcroFields();
acroFields.setGenerateAppearances(true);
acroFields.setField("creationdate", creationDate);
Any idea why this isn't working on the iPhone but it does work on all other devices?
Thanks
As stated by Bruno Lowagie, the issue can be resolved by using the flatten option, which should be set before the pdfStamper.close() has been called:
// flatten the PDF (so the values are visible when PDF is downloaded on iOS / OS X)
pdfStamper.setFormFlattening(true);

Embed non-embedded fonts in PDF with IText

So I have the following problem. I receive a PDF file which contains a set of fonts. These fonts are not embedded into the file. Here is a simple example:
I would like to embed these fonts inside the PDF, so they're self-contained and always available. But things don't seem that simple. I'm using IText to do my PDF processing.
I have read and tried the following questions/answers:
how-to-create-pdf-with-font-information-and-embed-actual-font-when-merging-them
embed-truetype-fonts-in-existing-pdf
embed-font-into-pdf-file-by-using-itext
how-to-check-that-all-used-fonts-are-embedded-in-pdf-with-java-itext
Chapter 16.1.4 Replacing a font of the book iText in Action - 2nd Edition
...
But what had gotten me closest was the following example: EmbedFontPostFacto.java (which comes from the book). I was able to embed the Arial font when providing the Arial.ttf file.
But with this, like with other examples, I need the source file of the font in order to embed it. In my case, I don't have the source file. But I might have them on the system however. So I'd like to query my available fonts on the system and see if it corresponds to the given font.
Something of the likes as
GraphicsEnvironment e = GraphicsEnvironment.getLocalGraphicsEnvironment();
java.awt.Font[] fonts = e.getAllFonts();
for(java.awt.Font f : fonts){
System.out.println(f.getFontName());
}
But I cannot transform the given java.awt.Font into a RandomAccessFile or a byte[] to be used in order to embed the font file itself. Is there another way for embedding fonts into a PDF, without having the source file of the font itself?
For Windows C:\Windows\Fonts or such contain all font files, and in the explorer shows also font names. So a manual search is feasible.
In java, you have GraphicsEnvironment.getAvailableFontFamilyNames() and Font.getFamilyName() to check for a name from the PDF like "Arial MT."
However a getter for the file is missing from Font.
So list all files of the font directory, and load each file consecutively as Font.
GraphicsEnvironment ge = GraphicsEnvironment.getLocalGraphicsEnvironment();
Font font = Font.createFont(Font.TRUETYPE_FONT, ttfFile);
ge.registerFont(font); // If you want to load the font.
if (pdfFontName.startsWith(font.getFamilyName()) {
System.out.printf("%s - %s / %s%n", ttfFile.getName(), font.getFamilyName(),
font.getName());
}

iTextPDF BaseFont reading font from database

Does someone do loading font file in iTextPDF from database? When loading font, for example:
BaseFont.createFont("res/mycustomfont.ttf", BaseFont.CP1250, BaseFont.EMBEDDED)
it's loaded from local filesystem.
I have read iText in action 2E, (especialy I have repeated chapter 11 now) but none about loading fonts from database, just how to load them from file system.
Going through source code, looks that for true type font (example above) the real data read is done in TrueTypeFont#process(byte ttfAfm[], boolean preload) and it reads from local filesystem only.
Curious if someone have override it for reading font data from DB?

Embedding font in a Java program using iText

I want to embed font into my Java program and I know how to do it using standard Java libraries. However, how do I do it using iText?
I don't want anything outside of my project (or later jar file) to be tampered with if it's not absolutely necessary (I want my program to work on all Java supporting platforms).
I'm importing font using *.ttf file.
Define a font
public static String[] FONT =
{"path/to/fonts/xxx.ttf", BaseFont.WINANSI};
then use
BaseFont bf = BaseFont.createFont(FONT[0], FONT[1], BaseFont.EMBEDDED);
Font font = new Font(bf, 24);
with the document.

Categories