PDFsharp & MigraDoc Foundation

PDFsharp - A .NET library for processing PDF & MigraDoc Foundation - Creating documents on the fly
It is currently Tue Mar 19, 2024 7:05 am

All times are UTC


Forum rules


Please read this before posting on this forum: Forum Rules



Post new topic Reply to topic  [ 12 posts ] 
Author Message
PostPosted: Mon Jul 30, 2018 1:59 pm 
Offline

Joined: Mon Jul 30, 2018 1:40 pm
Posts: 15
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.


Top
 Profile  
Reply with quote  
PostPosted: Mon Jul 30, 2018 2:08 pm 
Offline
PDFsharp Guru
User avatar

Joined: Mon Oct 16, 2006 8:16 am
Posts: 3092
Location: Cologne, Germany
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.

_________________
Regards
Thomas Hoevel
PDFsharp Team


Top
 Profile  
Reply with quote  
PostPosted: Mon Jul 30, 2018 3:22 pm 
Offline

Joined: Mon Jul 30, 2018 1:40 pm
Posts: 15
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?


Top
 Profile  
Reply with quote  
PostPosted: Mon Jul 30, 2018 4:02 pm 
Offline
PDFsharp Guru
User avatar

Joined: Mon Oct 16, 2006 8:16 am
Posts: 3092
Location: Cologne, Germany
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.

_________________
Regards
Thomas Hoevel
PDFsharp Team


Top
 Profile  
Reply with quote  
PostPosted: Tue Jul 31, 2018 6:01 am 
Offline

Joined: Mon Jul 30, 2018 1:40 pm
Posts: 15
I will give it a try.


Top
 Profile  
Reply with quote  
PostPosted: Tue Jul 31, 2018 6:35 am 
Offline

Joined: Mon Jul 30, 2018 1:40 pm
Posts: 15
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?


Top
 Profile  
Reply with quote  
PostPosted: Tue Jul 31, 2018 8:13 am 
Offline
PDFsharp Guru
User avatar

Joined: Mon Oct 16, 2006 8:16 am
Posts: 3092
Location: Cologne, Germany
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.

_________________
Regards
Thomas Hoevel
PDFsharp Team


Top
 Profile  
Reply with quote  
PostPosted: Tue Jul 31, 2018 10:32 am 
Offline

Joined: Mon Jul 30, 2018 1:40 pm
Posts: 15
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.


Top
 Profile  
Reply with quote  
PostPosted: Tue Jul 31, 2018 11:11 am 
Offline

Joined: Mon Jul 30, 2018 1:40 pm
Posts: 15
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?


Top
 Profile  
Reply with quote  
PostPosted: Tue Jul 31, 2018 3:16 pm 
Offline
PDFsharp Guru
User avatar

Joined: Mon Oct 16, 2006 8:16 am
Posts: 3092
Location: Cologne, Germany
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.

_________________
Regards
Thomas Hoevel
PDFsharp Team


Top
 Profile  
Reply with quote  
PostPosted: Tue Jul 31, 2018 3:45 pm 
Offline

Joined: Mon Jul 30, 2018 1:40 pm
Posts: 15
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!!!


Top
 Profile  
Reply with quote  
PostPosted: Tue Jul 31, 2018 3:52 pm 
Offline
PDFsharp Guru
User avatar

Joined: Mon Oct 16, 2006 8:16 am
Posts: 3092
Location: Cologne, Germany
Thanks for the feedback.

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

_________________
Regards
Thomas Hoevel
PDFsharp Team


Top
 Profile  
Reply with quote  
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 12 posts ] 

All times are UTC


Who is online

Users browsing this forum: Google [Bot] and 46 guests


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Search for:
Jump to:  
Privacy Policy, Data Protection Declaration, Impressum
Powered by phpBB® Forum Software © phpBB Group