WinForms: PDF viewer with custom PDF font programs controller.

Code samples for VintaSoft PDF .NET Plug-in. Here you can request a code sample.

Moderator: Alex

Post Reply
Alex
Site Admin
Posts: 1359
Joined: Thu Jul 10, 2008 2:21 pm

WinForms: PDF viewer with custom PDF font programs controller.

Post by Alex » Thu Feb 16, 2017 10:09 am

PDF document can use the external fonts. A font is external if it is stored separately from PDF document.

The VintaSoft Imaging .NET SDK can load an external font only if it is a TrueType, OpenType font or a collection of TrueType fonts. By default, the SDK searches for the font by its name in directory "$ASSEMBLY_DIRECTORY$\Fonts\". If the font is not found, the SDK searches font in fonts directory of operating system. If font is not found again, the SDK tries to get information about font from the PDF font programs controller if controller exists.

In version 8.4 and earlier versions the SDK does not define the default PDF font programs controller and programmer must add the custom PDF font programs controller for correct work with "unknown" external PDF fonts. Since version 8.5 we will add the default controller,
which will made standard actions, for example use Arial font instead of not found font.

This topic contains a WinForms application, which shows how to create simple PDF viewer with PDF custom font programs controller.

Here is source codes of main form (Form1.cs):

Code: Select all

using System;
using System.Windows.Forms;
using Vintasoft.Imaging.Pdf;

namespace WindowsFormsApplication1
{
    public partial class Form1 : Form
    {

        public Form1()
        {
            InitializeComponent();
        }



        private void Form1_Load(object sender, EventArgs e)
        {
            PdfDocumentController.DocumentOpened += PdfDocumentController_DocumentOpened;
        }

        private void PdfDocumentController_DocumentOpened(object sender, PdfDocumentOpenedEventArgs e)
        {
            PdfDocument pdfDocument = e.Document;
            pdfDocument.FontProgramsController = new CustomFontProgramsController();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            if (openFileDialog1.ShowDialog() == DialogResult.OK)
            {
                imageViewer1.Images.Add(openFileDialog1.FileName);
            }
        }

    }
}
Here is source codes of custom PDF font programs controller (CustomFontProgramsController.cs):

Code: Select all

using System.IO;
using Vintasoft.Imaging.Pdf;
using Vintasoft.Imaging.Pdf.Tree.Fonts;

namespace WindowsFormsApplication1
{
    /// <summary>
    /// Provides access to fonts located in the specified directory
    /// and system fonts.
    /// </summary>
    public class CustomFontProgramsController : FileFontProgramsController
    {

        /// <summary>
        /// Initializes a new instance of the CustomFontProgramsController class.
        /// </summary>
        public CustomFontProgramsController()
            : base(true, @"fonts\")
        {
        }



        /// <summary>
        /// Returns a TrueType font program using the specified font and font name.
        /// </summary>
        /// <param name="font">The font.</param>
        /// <param name="fontName">Font name.</param>
        /// <returns>The Stream that contains the font
        /// program in TrueType format if font is found successfully;
        /// otherwise, null.</returns>
        public override Stream GetTrueTypeFontProgram(PdfFont font, string fontName)
        {
            Stream result = base.GetTrueTypeFontProgram(font, fontName);
            // if font program is not found then
            if (result == null)
            {
                // use substitution font

                string substitutionFontName = "Arial";
                string fontNameUpper = fontName.ToUpperInvariant();
                // use Arial Bold Italic font instead of XXX Bold Italic fonts
                if (fontNameUpper.EndsWith("BOLD ITALIC") || fontNameUpper.EndsWith("-BOLDITALICMT"))
                    substitutionFontName += "-BoldItalic";
                // use Arial Bold font instead of XXX Bold fonts
                else if (fontNameUpper.EndsWith("BOLD") || fontNameUpper.EndsWith("-BOLDMT"))
                    substitutionFontName += "-Bold";
                // use Arial Italic font instead of XXX Italic fonts
                else if (fontNameUpper.EndsWith("ITALIC") || fontNameUpper.EndsWith("-ITALICMT"))
                    substitutionFontName += "-Italic";

                result = base.GetTrueTypeFontProgram(font, substitutionFontName);
                if (result != null)
                {
                    string message = string.Format(
                        "Font program '{0}' was not found, '{1}' program is used.",
                        fontName, substitutionFontName);
                    if (font != null)
                        font.Document.RuntimeMessages.Add(
                            new PdfRuntimeWarning("FontProgramsController", message));
                }
            }
            return result;
        }
    }
}

Source codes of WinForms application can be downloaded here.

Post Reply

Who is online

Users browsing this forum: No registered users and 1 guest