How can I force PDFsharp to embed a subset of a font only?

680 views Asked by At

I am able to sucessfully create PDF files using PDFsharp and MigraDoc. Two private fonts (OTF format) are used for the creation of a single page PDF. The created PDF contains both fonts fully embedded. Unfortunatly each font contains Chinese letters too and therefor measures about 4 MB in size each, resulting in a PDF file size about 9 MB (containing one page with a bit of text only!). :shock:

Is it possible to use a subset of those fonts to save valuable space. The thing is I need to create a few thousand PDF files and therefor file size is crucial.

Is there a special setting i can use? Can anyone point me into the right direction?

Update: I used fontforge to extract the embedded font subgroub and found out that the fonts derived from the pdf match the full font files exactly. So no font subsetting is indeed used at all. :(

Taking a look into the PDFsharp sources I found the function

public OpenTypeFontface CreateFontSubSet(Dictionary<int, object> glyphs, bool cidFont)

which is commented as follows: Creates a new font image that is a subset of this font image containing only the specified glyphs. Which is exactly what I want to be used here.

The thing I do not understand here is why this function seems not to get used when creating my PDF. What criteria needs to be met in order to make it work?

2

There are 2 answers

0
I liked the old Stack Overflow On

Some fonts have a "loca table", some do not. The loca table stores the offsets to the locations of the glyphs in the font.

CreateFontSubSet is and can only be called for fonts with a loca table that provides the information needed to create subsets.

0
Thariama On

Just found a solution to my problem that requires no extensive fiddling with additional pdf frameworks. I am able to create font subsets using ghostscript (commandline).

In fact ghostscript takes the (pdfsharp-) generated file and rewrites it (while optimizing the fonts). Here the commandline solution:

gswin64 -dCompatibilityLevel=1.4 -dPDFSETTINGS=/printer -dCompressFonts=true -dSubsetFonts=true -dNOPAUSE -dBATCH -sDEVICE=pdfwrite -sOutputFile=optimized.pdf -c ".setpdfwrite <</NeverEmbed [ ]>> setdistillerparams" -f my_pdfsharp.pdf

My file size of about 9 MB is now down to 51 KB. Yihaa!!!