PDFsharp & MigraDoc Foundation
http://forum.pdfsharp.com/

How can i force PDFSharp to embed a subset of a font only?
http://forum.pdfsharp.com/viewtopic.php?f=2&t=3818
Page 1 of 1

Author:  Thariama [ Mon Jul 30, 2018 1:59 pm ]
Post subject:  How can i force PDFSharp to embed a subset of a font only?

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 contain 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 thousends pdf files and therefor file size is crucial.

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

PS: Attached you will find the pdf file i created.

Author:  Thomas Hoevel [ Mon Jul 30, 2018 2:08 pm ]
Post subject:  Re: How can i force PDFSharp to embed a subset of a font onl

Hi!
Thariama wrote:
Can anyone point me into the right direction?
There are two implementations of PDFsharp: The GDI+ build and the WPF build.

I assume you are using the WPF build. If so, the GDI+ build could give you better results (or vice versa).

There are also are different font formats. This is not my area of expertise, but this could also make a difference. Trying both PDFsharp 1.32 and 1.50 could also make a difference.

Do you need Chinese letters? If not, maybe a Latin only font can be a solution.

P. S.: There is no attached PDF, probably due to filesize restrictions. It probably won't give much insight anyway.

Author:  Thariama [ Mon Jul 30, 2018 3:22 pm ]
Post subject:  Re: How can i force PDFSharp to embed a subset of a font onl

I am using the wpf build.

Does any of those PDFSharp versions use font subsets?
I think the whole problem comes down to this (no font subsets => big pdf files).

Is there no configuration setting?

Author:  Thomas Hoevel [ Mon Jul 30, 2018 4:02 pm ]
Post subject:  Re: How can i force PDFSharp to embed a subset of a font onl

Thariama wrote:
Does any of those PDFSharp versions use font subsets?
I think the GDI+ build does.
That's why I assumed you were using the WPF build and that's why I suggested trying the GDI+ build.

AIUI the GDI+ build uses much own code for font handling while the WPF build relies more on .NET code. The WPF build supports more font formats AFAIK, but has less control about subsetting.

Author:  Thariama [ Tue Jul 31, 2018 6:01 am ]
Post subject:  Re: How can i force PDFSharp to embed a subset of a font onl

I will give it a try.

Author:  Thariama [ Tue Jul 31, 2018 6:35 am ]
Post subject:  Re: How can i force PDFSharp to embed a subset of a font onl

All i am able to find are ways to make it work using the wpf build (http://www.pdfsharp.net/wiki/PrivateFonts-sample.ashx).

I seems like the GDI build does not work with private fonts. Or at least i am too stupid to find it out. There seems to be no FontResolver for GDI but I red something about a XPrivateFontCollection.

If GDI works with private fonts how can i include them and make PDFSharp use them? Any advice?

Author:  Thomas Hoevel [ Tue Jul 31, 2018 8:13 am ]
Post subject:  Re: How can i force PDFSharp to embed a subset of a font onl

The GDI+ build uses XPrivateFontCollection and AFAIK it still works with 1.50.

To add a font, call
Code:
XPrivateFontCollection.Add();
passing a stream or a byte array.


To test the differences of font embedding, you can simply try the Hello World sample using fonts that are installed on the computer.

Author:  Thariama [ Tue Jul 31, 2018 10:32 am ]
Post subject:  Re: How can i force PDFSharp to embed a subset of a font onl

Thanks for the answer.

In the meantime i found out how to add the font myself. But it does not work.
The problem i got now is that GDI does not support open font types.
The fonts i got are in otf format only and due to licence restrictions i am not allowed to modify the fonts in any kind.

I really would prefer to use the WPF PDFSharp build, but there needs to be a way to create minimal font subsets.
I mean adobe offers font subsertting and it is state of the art for commercial pdf frameworks.

Author:  Thariama [ Tue Jul 31, 2018 11:11 am ]
Post subject:  Re: How can i force PDFSharp to embed a subset of a font onl

I used fontforge to extract the embedded font subgroub and found out that the font derived from the pdf matches the full file exactly.
So indeed no font subsetting is used at all. :cry:

Taking a look into the PDFSharp sources i found the function
Code:
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. :o
Which is exactly what i want to be used here.

The thing i do not understand if why this function seems not to get used when creating my pdf. :?:
What criteria needs to be met in order to make it work?

Author:  Thomas Hoevel [ Tue Jul 31, 2018 3:16 pm ]
Post subject:  Re: How can i force PDFSharp to embed a subset of a font onl

Thariama wrote:
The thing i do not understand if why this function seems not to get used when creating my pdf.
I don't know for sure.
My understanding: The point is: OTF work with the WPF build because we do not have to handle the fonts with own code. We do not have to know the internals of those fonts, but at the same time we cannot create subsets.

If you have fonts that work with the WPF build, but do not work with the GDI+ build, then PDFsharp does not (yet) have code to handle those fonts directly and then it cannot create subsets for those fonts.

Update: The function "CreateFontSubSet" is only called for font that have a "loca table", a table giving the index of each glyph. Without loca table, the whole font is a monolithic block and subsets cannot be created.

Author:  Thariama [ Tue Jul 31, 2018 3:45 pm ]
Post subject:  Re: How can i force PDFSharp to embed a subset of a font onl

Thank you for your patience and your explanations.

I found a solution to my problem.
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 is the commandline solution for others as well:

Code:
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. Yes!!!

Author:  Thomas Hoevel [ Tue Jul 31, 2018 3:52 pm ]
Post subject:  Re: How can i force PDFSharp to embed a subset of a font onl

Thanks for the feedback.

So it must be possible to create font subsets even without loca table, but PDFsharp does not (yet) support this.

Page 1 of 1 All times are UTC
Powered by phpBB® Forum Software © phpBB Group
https://www.phpbb.com/