iText, What's going on with Font, BaseFont and createFont()? - itext

There is a lot of mystery to me about what is going on with font and basefont. Especially when it comes to the constructor. The iText website gives this line as example code for new fonts
BaseFont unicode = BaseFont.createFont("c:/windows/fonts/arialuni.ttf",
BaseFont.IDENTITY_H,
BaseFont.EMBEDDED);
I can get this call to work:
BaseFont bf = BaseFont.createFont(BaseFont.COURIER, BaseFont.CP1257,
BaseFont.EMBEDDED);
But if I replace BaseFont.CP1257 with say BaseFont.HELVETICA then it doesn't work and I get a page that says "failed to load pdf document."
I tried looking through the class file and I can't seem to figure out what that second parameter is (I'm assuming it is something like a backup font in case the first font doesn't work, like in HTML) and I can't figure out why some fonts would work and not others.

To load it from inside your jar use the leading slash otherwise, just use the absolute path of your font ("C:[...]\fonts\Sansation_Regular.ttf"). For example:
Font font = FontFactory.getFont("/fonts/Sansation_Regular.ttf",
BaseFont.IDENTITY_H, BaseFont.EMBEDDED, 0.8f, Font.NORMAL, BaseColor.BLACK);
BaseFont baseFont = font.getBaseFont();
The relative path of the font is: 'src/main/resources/fonts'
Using Itext 5.4.5
example: https://code.google.com/p/jhocr/source/browse/trunk/src/main/java/com/googlecode/jhocr/converter/HocrPageProcessor.java

The second parameter is the encoding.
Refer to documentation here for more information.

This is what worked for me. Old post, but I couldn't find a simple answer.
//Here you setup the font that you want. I put it under the root/Content/fonts folder in my project
Font font = FontFactory.GetFont("~/Content/fonts/ARIALN.ttf", BaseFont.CP1252,false, 9);
//Here I create the paragraph then assign the font to it at the end
var addressAttn = new Paragraph("Attn: Manager, Billing Services", font);

Related

font registration for flying saucer with ITextRenderer

I am using Flying Saucer to generate pdf from html using iText-2.1.7.
For each document I print, I do:
ITextRenderer renderer = new ITextRenderer();
for each font I use:
renderer.getFontResolver().addFont(font.getPath(),font.getName(), BaseFont.CP1252, BaseFont.EMBEDDED, null);
and then the rest of the code to create the pdf:
renderer.getSharedContext().setReplacedElementFactory(...)
renderer.getSharedContext().setUserAgentCallback(...);
renderer.setDocument(xhtmlDom,null);
renderer.layout();
renderer.createPDF(byteArrayOutputStream);
So, for each document, I register all the fonts again and again...
Is this strictly necessary? Is there any font cache service I can use, or something similar?
Thank you very much!

can not read font in Itext sharp with asp.net core 2.0

I am Working with iTextSharp! I'm trying to use some type of fonts from wwwroot file
But can not read the font
the problem with this line of code
var fontPath = FontFactory.GetFont(Path.Combine(_environment.WebRootPath, "fonts//Times New Roman Bold.ttf"), BaseFont.IDENTITY_H, 12);
you need to register the path to the fonts folder

iText 7: How to build a paragraph mixing different fonts?

I've been using iText 7 for a few days to build pdf files, unfortunately, iText 7 is very different from iText 5 and the documentation is still very incomplete.
I'm trying to build a paragraph that mixes two fonts or two fonts style (example: have a bold text in the middle of a paragraph)
Using iText 5 this would be done using Chunks:
Font regular = new Font(FontFamily.HELVETICA, 12);
Font bold = Font font = new Font(FontFamily.HELVETICA, 12, Font.BOLD);
Phrase p = new Phrase("NAME: ", bold);
p.add(new Chunk(cc_cust_dob, regular));
PdfPCell cell = new PdfPCell(p);
Using iText 7, I still haven't found way to do this.
Has anyone tried to do this using the last version of iText?
Note: I'm using the csharp but java is also useful
Please read the documentation, more specifically iText 7: building blocks "Chapter 1: Introducing the PdfFont class"
In that chapter, you'll discover that it's much easier to switch fonts when using iText 7, because you can work with default fonts and font sizes, you can define and reuse Style objects, and so on.
An example:
Style normal = new Style();
PdfFont font = PdfFontFactory.createFont(FontConstants.TIMES_ROMAN);
normal.setFont(font).setFontSize(14);
Style code = new Style();
PdfFont monospace = PdfFontFactory.createFont(FontConstants.COURIER);
code.setFont(monospace).setFontColor(Color.RED)
.setBackgroundColor(Color.LIGHT_GRAY);
Paragraph p = new Paragraph();
p.add(new Text("The Strange Case of ").addStyle(normal));
p.add(new Text("Dr. Jekyll").addStyle(code));
p.add(new Text(" and ").addStyle(normal));
p.add(new Text("Mr. Hyde").addStyle(code));
p.add(new Text(".").addStyle(normal));
document.add(p);
First we define a Style that we call normal and that uses 14 pt Times-Roman. Then we define a Style that we call code and that uses 12 pt Courier in Red with a gray background.
Then we compose a Paragraph using Text objects that use these styles.
Note that you can chain add() comments, as is done in this example:
Text title1 = new Text("The Strange Case of ").setFontSize(12);
Text title2 = new Text("Dr. Jekyll and Mr. Hyde").setFontSize(16);
Text author = new Text("Robert Louis Stevenson");
Paragraph p = new Paragraph().setFontSize(8)
.add(title1).add(title2).add(" by ").add(author);
document.add(p);
We set the font size of the newly created Paragraph to 8 pt. This font size will be inherited by all the objects that are added to the Paragraph, unless the objects override that default size. This is the case for title1 for which we defined a font size of 12 pt and for title2 for which we defined a font size of 16 pt. The content added as a String (" by ") and the content added as a Text object for which no font size was defined inherit the font size 8 pt from the Paragraph to which they are added.
This is a copy/paste from the official tutorial. I hope this is sufficient for StackOverflow where "link-only" answers aren't allowed. This "no link-only answers rule" shouldn't lead to copy/pasting a full chapter of a manual...

iTextSharp embedded cyrillic font in Adobe Reader

My PDF is not displayed properly with Adobe Reader. It is fine with other PDF readers so this must be a syntax issue, as I've heard that Adobe Reader is more strict with PDF syntax. The fonts seem to be twice as big as they should be but the horizontal spacing is correct, this makes the fonts overlap with each other.
This is my C# code (font creation code is at the end of this post).
Font officialUseFont = EmbeddedResources.CreateDesignFont(webform);
PdfContentByte officialUseCanvas = _stamper.GetOverContent(3);
ColumnText.ShowTextAligned(officialUseCanvas, Element.ALIGN_CENTER, new Phrase(webform.Text, officialUseFont), posX, posY, 0);
I'm using iTextSharp 5.4.2.0 with runtime v2.0.50727.
I must have embedded some fonts because the Cyrillic alphabet and Chinese alphabets were not working before but they work now. The form fields which exist in the PDF are populated with Cyrillic characters without any problems, it's only the canvas which causes the issue.
public Font CreateDesignFont(IForm webform)
{
var baseFont = GetBaseFont(fontNamespace.Length, selectedFontName);
return new Font(baseFont, webform.FontSize);
}
private static BaseFont GetBaseFont(int fontNamespaceLength, string selectedFontName)
{
byte[] fontBuffer;
using (var stream = (Assembly.GetExecutingAssembly().GetManifestResourceStream(selectedFontName)))
{
fontBuffer = new byte[stream.Length];
stream.Read(fontBuffer, 0, fontBuffer.Length);
}
var fontfile = selectedFontName.Substring(fontNamespaceLength);
var customFont = BaseFont.CreateFont(fontfile, BaseFont.IDENTITY_H, BaseFont.EMBEDDED, BaseFont.CACHED, fontBuffer, null);
return customFont;
}
The code snippet you're providing isn't sufficient. The problem originates in whatever happens when you do EmbeddedResources.CreateDesignFont(webform);
If the font isn't shown in Adobe Reader, you didn't embed the font. Maybe you think you do, but judging by the behavior of the PDF viewers, you didn't.
Can you provide a screen shot of the Document Properties, more specifically the "Fonts" tab?
UPDATE
I tried writing my own code snippet, and I wasn't able to reproduce the problem. So I took another look at your code, and I saw that you're caching the font, but you've already used ArialMT using the WINANSI encoding to fill out the fields on page 1. IMO (I don't have the time to check) that's incompatible with using the same font from cache using IDENTITY_H. If you don't cache the font (why would you? you're passing the fontBuffer! no need to store the font in a cache if you're already caching the font bytes yourself), your problem will probably be solved.
The problem stemmed from the fact I was using .otf fonts. When I changed to .ttf the problem disappeared.

iTextSharp equivalent of XPdfFontOptions

I'm converting a C# app page which uses PdfSharp to iTextSharp & have found a line of code I can't see an obvious replacement for.
The existing code is
PdfSharp.Drawing.XPdfFontOptions options = new PdfSharp.Drawing.XPdfFontOptions(PdfFontEncoding.Unicode,
PdfFontEmbedding.Always);
Also, what if I want to use other, non-base fonts? I can see from the docs how to create one of the 16 types, however what if I want "Frutiger LT 45 Light"?
Thanks in advance.
Have a look at the examples from iText in Action — 2nd Edition Chapter 11: Choosing the right font; the .Net versions are available here.
You'll see that fonts can be selected, configured, and used like this:
public const string FONT = "c:/windows/fonts/arialbd.ttf";
BaseFont bf = BaseFont.CreateFont(FONT, BaseFont.IDENTITY_H, BaseFont.EMBEDDED);
Font f = new Font(bf, 12);
document.Add(new Paragraph("Text", f));
Font garamondItalic = FontFactory.GetFont(
"Garamond", BaseFont.WINANSI, BaseFont.EMBEDDED, 12, Font.ITALIC
);
document.Add(new Paragraph("Garamond-Italic", garamondItalic));
Thus, you explicitly enter the encoding and embedding options in the font creation instead of via some font option object.
BTW, here BaseFont does not refer to the standard 14 fonts which shall be available to the conforming reader according to ISO 32000-1:2008 (I assume that you mean those fonts when you talk about the 16 types) but instead is a base object from which fonts at given sizes are created.