pdf links

PDF Rendering
Convert PDF to Image (.NET)
Convert PDF to image on Android (Xamarin)
Convert PDF to image on iOS (Xamarin)
Convert PDF to image in Windows Store apps (.NET)
Convert PDF to image in Windows Phone apps (.NET)
PDF to image in Universal Windows Store apps (.NET)
Free PDF Viewer control for Windows Forms (.NET)
How to integrate PDF Viewer control in WPF app (.NET)
Creating WPF PDF Viewer supporting bookmarks (.NET)
Cross-platform PDF Viewer using GTK# (MONO)
Silverlight PDF viewer control (Silverlight 5)
Multithreaded PDF rendering (.NET)
Convert pdf to image in Silverlight app (C# sample)
How to set fallback fonts for PDF rendering (C#)
Avoiding the out-of-memory exception on rendering (C#)
PDF viewer single page application (WebAPI, AngularJS)
PDF viewer control for Windows 10 universal applications
Use custom ICC profile for CMYK to RGB conversion
PDF layers - separate images, text, annotations, graphics

PDF Forms Creation PDF Security
Conversion to PDF/A
Other topics
PDF Document Manipulation PDF Content Generation
Fixed and Flow layout document API (.NET)
Creation of grids and tables in PDF (C# sample)
How to create interactive documents using Actions (C# sample)
Text flow effects in PDF (C# sample)
How to generate ordered and bulleted lists in PDF (C# sample)
Convert HTML to PDF using flow layout API (C# sample)
How to use custom fonts for PDF generation (.NET)
Create document with differently sized pages (C#)
Create PDF documents using MONO (C#/MONO/Windows/OSX)
How to use background images for content elements (C#/PDF Kit/FlowLayout)
Add transparent images to PDF document (C#)
Draw round rect borders in PDF documents(C#)
ICC color profiles and and ICC based colors in PDF (C#)
How to use bidirectional and right to left text in PDF (C#)
Create PDF documents from XML templates (C# sample)
How to resize PDF pages and use custom stamps (C#)
Add header and footer to PDF page (.NET sample)
How to use clipping mask for drawing on PDF page
Fill graphics path with gradient brushes in PDF (Shadings)
Apitron PDF Kit and Rasterizer engine settings
Add layers to PDF page (optional content, C# sample)
How to create free text annotation with custom appearance
PDF Content Extraction PDF Navigation
PDF to TIFF conversion
Contact us if you have a PDF related question and we'll cover it in our blog.

2015-08-31

How to set fallback fonts for PDF rendering and creation

Introduction


PDF specification allows authors and software writers to create completely self-contained documents which include all necessary information for their rendering. It provides font data embedding – an important mechanism that can be used to get the documents consistently presented on many platforms and devices. While it looks good and natural from the first sight, it makes documents bigger because you have to embed the font’s data used in document.

One of the alternatives to embedding is using standard Adobe 14 fonts (Times-Roman, Helvetica, Courier, Symbol, Times-Bold, Helvetica-Bold, Courier-Bold, ZapfDingbats, Times-Italic, Helvetica-Oblique, Courier-Oblique, Times-BoldItalic, Helvetica-BoldOblique, Courier-BoldOblique) which a conforming reader should understand and display correctly.

Other alternative (and most widely used) is to use external fonts, which are usually provided along with the operating system. These fonts can be referred to by their names, and PDF reader tries to find them inside the well-known system folders when it loads the document.

To examine the fonts used inside the particular PDF document, go to the File-> Properties…-> Fonts (tab) in Adobe PDF Reader. See the sample below:

Pic. 1 Examining fonts used in PDF document

Pic. 1 Examining fonts used in PDF document

You may notice that AdobePiStd is marked as embedded subset, while Arial-based fonts are being loaded externally from default system folder ( C:\Windows\Fonts if you’re running the reader on Windows). Furthermore, you may also note that these Arial-based fonts are getting replaced by the fonts shown as Actual fonts. Thus Arial becomes substituted by ArialMT and so on.

External fonts and font substitution may work well under some conditions, but what if you were to create a PDF reading or processing app using Apitron PDF Rasterizer or Apitron PDF Kit .NET components for a platform where system fonts are completely inaccessible e.g. Silverlight? Or you would like to provide a custom font fallback mechanism for cases where you have a very limited system fonts set, e.g. an Android phone app. 

To help you with this, we have introduced new API for specifying font fallbacks. See the code section below for a real-world example.

Code sample


The following piece of code demonstrates how to set up font fallback in any of our products.

// let's set Arial as fallback for all not found fonts
using (Stream fontStream = File.Open("Arial.ttf", FileMode.Open))        
{
    // register the fallback font, it can be either TTF or TTC stream,
    // font name and other attributes will be inferred automatically
    EngineSettings.RegisterUserFont(fontStream);
    // map the font to all not found fonts using *, 
    // it's possible to map particular fonts using their names
    EngineSettings.UserFontMappings.Add(new KeyValuePair<string, string[]>("Arial"
        new[] {"*"}));
}

So, in order to font fallback to work you have to register the fonts and map them using EngineSettings class static methods.

Conclusion


Apitron PDF Kit and Apitron PDF Rasterizer products provide a great base for any PDF processing application. Understanding their settings doesn’t require much time, but can help you to find the solution for the trickiest cases. Contact us if you need any help or have questions and we’ll be happy to help.

Downloadable version of this article can be found by the following link [PDF].

No comments:

Post a Comment