Question:
How do I quickly detect if a PDF contains Type 3 fonts?
Answer:
The following code will check for the presence of any Type 3 fonts in the PDF.
PDFDoc doc = new PDFDoc(filepath);
SDFDoc cos_doc = doc.GetSDFDoc();
int num_objs = cos_doc.XRefSize();
for (int i = 1; i < num_objs; ++i)
{
Obj obj = cos_doc.GetObj(i);
if (obj == null || obj.IsFree() || !obj.IsDict()) continue;
Obj typeFont = obj.FindObj("Type");
if (typeFont == null || !typeFont.IsName()) continue;
if (typeFont.GetName().CompareTo("Font") != 0) continue;
var font = new pdftron.PDF.Font(obj);
if (font.GetType() == pdftron.PDF.Font.Type.e_Type3)
{
Console.WriteLine("Type 3 font found");
}
}
Hi,
Thanks for this. I tried writing it in Python but getting some errors.
doc = PDFDoc(filepath)
cos_doc = doc.GetSDFDoc()
num_objs = cos_doc.XRefSize()
for idx in range(num_objs):
obj = cos_doc.GetObj(idx)
if obj is None or obj.IsFree() or not obj.IsDict():
continue
typeFont = obj.FindObj("Type")
if typeFont is None or not typeFont.IsName() or typeFont.GetName() != "Font":
continue
font = Font(typeFont)
if font.GetType() == Font.e_Type3:
return True
return False
but getting an error of
font = Font(typeFont)
File "../lib/python3.9/site-packages/PDFNetPython3/PDFNetPython.py", line 7929, in __init__
_PDFNetPython.Font_swiginit(self, _PDFNetPython.new_Font(*args))
Exception: Get() can't be invoked on Obj of this type.
My gut feel is something missing in the Python bindings. Any suggestions?
Thanks
Hi Kenneth,
Your error is in the following line:
font = Font(typeFont)
It should instead be:
font = Font(obj)
Matching what was initially written in the forum post.
My bad, must be my eyes then.
Amazing, it works now. Thanks for quick response.