Pack and Optimize Fonts in PDF Documents with GdPicture.NET
Hi Everyone,
Speaking about PDF format, one of its robust features is to accurately display your document regardless of software, hardware, or the operating system.
Therefore fonts used to render the document content become an essential part of the document structure. And because the size matters, let’s try to reduce the file size by packing the fonts.
Characters, glyphs, and fonts
When you talk, you use characters. They are abstract.
When you write, you use graphical symbols to render these abstract characters. They are called glyphs.
Glyphs are grouped into a set of glyphs called font.
Each font contains the glyph descriptions composed as a program designed for direct use. The result is a font file with a specific extension characterising the font format.
Such a font file precisely describes how to draw characters and symbols used in content.
The next step is to keep the same document layout for other users, so they get the same file.
A challenging factor here is the file size of the resulting PDF document.
Fonts in PDF
As said above, mentioned font programs are a necessary part of the PDF document structure. The reason is apparent – to preserve the visual document layout same on all devices.
Generally, including font files in PDF documents happens in two ways. Fonts can be embedded or non-embedded in the document structure.
Non-embedded fonts only specify some of the required font parameters, like font family, font style, or encoding to tell the viewer which font should it use to render the text. PDF viewers try to substitute absenting glyphs with some predefined ones. This way some symbols can have different visual appearances as when they were created or even they are impossible to draw.
This approach has the lowest impact on the resulting document size but often leads to insufficiency in rendering.
Embedded fonts are stored as complete font programs, means a full set of characters, in a PDF document itself. The document content renders in the precisely same layout as when designing it, for every user and on every device.
The only consequence here is the highest impact on the document size as the whole font file is inserted inside the document structure.
Subsetting
The file size is always essential when you want to archive your documents.
Embedding only a font subset looks like the best solution to keep the file size as low as possible with preserving the unaltered visual appearance of the document content.
The process of subsetting a font means to store only those glyphs of the font file which are actually used in the document layout.
So in the final stage of the document, for example before archiving, you can optimize font files by restructuring the used font programs to only include used glyphs and throw out unused ones.
This can significantly reduce the file size of the resulting PDF document with all the benefits of fully embedded font files.
Packing fonts
Starting with version 14.0.61, the font packing feature offers font optimization primarily on those PDF documents, that are ready for storing or archiving.
Many times, PDF documents reach the final stage with weak or even none font optimization. The resulting documents are large, and they can generate storage problems. Our handy PackFonts() method solves these issues in one step.
To quickly show you the process behind the scene, here are some secrets:
- 1. The toolkit associates all texts in the PDF document with the included embedded font files.
- 2. If it detects the fully embedded font, it creates the corresponding subset of this font file and subsequently replaces the initially embedded font with the subsetted one.
- 3. If only font subset is embedded, the toolkit ensures that the included subset is correct and shaves off any unnecessary leftover information. This can happen for example, when you split the final document into smaller ones, but each of the newly created ones will include the fully embedded fonts from the source, although it is not necessary.
- 4. Finally, the packing feature removes all other unnecessary data in the stored font files. The file size of such optimized PDF document reduces, and the document is ready for archiving to save the space.
Eager to try it? Download our new release!
Sample Code (C#)
string caption = "Example: PackFonts"; using (GdPicturePDF oGdPicturePDF = new GdPicturePDF()) { if (oGdPicturePDF.LoadFromFile("test.pdf", false) == GdPictureStatus.OK) { if (oGdPicturePDF.PackFonts() == GdPictureStatus.OK) { if (oGdPicturePDF.SaveToFile("test_optimized.pdf") == GdPictureStatus.OK) { MessageBox.Show("The example has been followed successfully.", caption); } else { MessageBox.Show("The file can't be saved. Status: " + oGdPicturePDF.GetStat().ToString(), caption); } } else { MessageBox.Show("The error occurs when optimizing fonts. Status: " + oGdPicturePDF.GetStat().ToString(), caption); } } else { MessageBox.Show("The file can't be opened. Status: " + oGdPicturePDF.GetStat().ToString(), caption); } }
See you next time!
Gabriela
Tags: