StandardFontSample.cs :  » PDF » PDF-Clown » it » stefanochizzolini » clown » samples » C# / CSharp Open Source

Home
C# / CSharp Open Source
1.2.6.4 mono .net core
2.2.6.4 mono core
3.Aspect Oriented Frameworks
4.Bloggers
5.Build Systems
6.Business Application
7.Charting Reporting Tools
8.Chat Servers
9.Code Coverage Tools
10.Content Management Systems CMS
11.CRM ERP
12.Database
13.Development
14.Email
15.Forum
16.Game
17.GIS
18.GUI
19.IDEs
20.Installers Generators
21.Inversion of Control Dependency Injection
22.Issue Tracking
23.Logging Tools
24.Message
25.Mobile
26.Network Clients
27.Network Servers
28.Office
29.PDF
30.Persistence Frameworks
31.Portals
32.Profilers
33.Project Management
34.RSS RDF
35.Rule Engines
36.Script
37.Search Engines
38.Sound Audio
39.Source Control
40.SQL Clients
41.Template Engines
42.Testing
43.UML
44.Web Frameworks
45.Web Service
46.Web Testing
47.Wiki Engines
48.Windows Presentation Foundation
49.Workflows
50.XML Parsers
C# / C Sharp
C# / C Sharp by API
C# / CSharp Tutorial
C# / CSharp Open Source » PDF » PDF Clown 
PDF Clown » it » stefanochizzolini » clown » samples » StandardFontSample.cs
using it.stefanochizzolini.clown.documents;
using it.stefanochizzolini.clown.documents.contents.composition;
using it.stefanochizzolini.clown.documents.contents.fonts;
using it.stefanochizzolini.clown.files;

using System;
using System.Drawing;

namespace it.stefanochizzolini.clown.samples{
  /**
    <summary>This sample demonstrates the use of standard Type 1 fonts, which are the 14 built-in fonts
    prescribed by the PDF specification to be shipped along with any conformant PDF viewer.</summary>
    <remarks>In particular, this sample displays the complete glyphset of each standard font,
    iterating through character codes and glyph styles (regular, italic, bold).</remarks>
  */
  public class StandardFontSample
    : ISample
  {
    #region static
    #region fields
    private static readonly int FontBaseSize = 20;
    private static readonly int Margin = 50;
    #endregion
    #endregion

    #region dynamic
    #region interface
    #region public
    #region ISample
    public void Run(
      SampleLoader loader
      )
    {
      // 1. PDF file instantiation.
      File file = new File();
      Document document = file.Document;

      // 2. Content creation.
      Populate(document);

      // (boilerplate metadata insertion -- ignore it)
      loader.BuildAccessories(document,this.GetType(),"Standard Type 1 fonts","applying standard Type 1 fonts");

      // 3. Serialize the PDF file (again, boilerplate code -- see the SampleLoader class source code)!
      loader.Serialize(file,this.GetType().Name,false);
    }
    #endregion
    #endregion

    #region private
    private void Populate(
      Document document
      )
    {
      Page page = new Page(document);
      document.Pages.Add(page);
      SizeF pageSize = page.Size.Value;

      PrimitiveFilter builder = new PrimitiveFilter(page);

      int x = Margin, y = Margin;
      StandardType1Font titleFont = new StandardType1Font(
        document,
        StandardType1Font.FamilyNameEnum.Times,
        true,
        true
        );
      StandardType1Font font = null;
      // Iterating through the standard Type 1 fonts...
      foreach(StandardType1Font.FamilyNameEnum fontFamilyName
        in (StandardType1Font.FamilyNameEnum[])Enum.GetValues(typeof(StandardType1Font.FamilyNameEnum)))
      {
        // Iterating through the font styles...
        for(int styleIndex = 0; styleIndex < 4; styleIndex++)
        {
          /*
            NOTE: Symbol and Zapf Dingbats are available just as regular fonts (no italic or bold variant).
          */
          if(styleIndex > 0
            && (fontFamilyName == StandardType1Font.FamilyNameEnum.Symbol
              || fontFamilyName == StandardType1Font.FamilyNameEnum.ZapfDingbats))
              break;

          bool bold, italic;
          switch(styleIndex)
          {
            case 0: // Regular.
              bold = false;
              italic = false;
              break;
            case 1: // Bold.
              bold = true;
              italic = false;
              break;
            case 2: // Italic.
              bold = false;
              italic = true;
              break;
            case 3: // Bold italic.
              bold = true;
              italic = true;
              break;
            default:
              throw new Exception("styleIndex " + styleIndex + " not supported.");
          }
          // Define the font used to show its character set!
          font = new StandardType1Font(
            document,
            fontFamilyName,
            bold,
            italic
            );

          if(y > pageSize.Height - Margin)
          {
            builder.Flush();

            page = new Page(document);
            document.Pages.Add(page);
            pageSize = page.Size.Value;
            builder = new PrimitiveFilter(page);
            x = Margin; y = Margin;
          }

          if(styleIndex == 0)
          {
            builder.DrawLine(
              new PointF(x,y),
              new PointF(pageSize.Width - Margin,y)
              );
            builder.Stroke();
            y += 5;
          }

          builder.SetFont(
            titleFont,
            FontBaseSize * (styleIndex == 0 ? 1.5 : 1)
            );
          builder.ShowText(
            fontFamilyName.ToString() + (bold ? " bold" : "") + (italic ? " italic" : ""),
            new PointF(x,y)
            );

          y += 40;
          // Set the font used to show its character set!
          builder.SetFont(font,FontBaseSize);
          // Iterating through the font characters...
          for(int charCode = 32; charCode < 256; charCode++)
          {
            if(y > pageSize.Height - Margin)
            {
              builder.Flush();

              page = new Page(document);
              document.Pages.Add(page);
              pageSize = page.Size.Value;
              builder = new PrimitiveFilter(page);
              x = Margin; y = Margin;

              builder.SetFont(titleFont,FontBaseSize);
              builder.ShowText(
                fontFamilyName.ToString() + " (continued)",
                new PointF(pageSize.Width - Margin, y),
                AlignmentXEnum.Right,
                AlignmentYEnum.Top,
                0
                );
              builder.SetFont(font,FontBaseSize);
              y += FontBaseSize * 2;
            }

            try
            {
              // Show the current character (using the current standard Type 1 font)!
              builder.ShowText(
                new String(new char[]{(char)charCode}),
                new PointF(x,y)
                );
              x += FontBaseSize;
              if(x > pageSize.Width - Margin)
              {x = Margin; y += 30;}
            }
            catch(Exception e)
            { /* NOOP. */ }
          }

          x = Margin; y += Margin;
        }
      }
      builder.Flush();
    }
    #endregion
    #endregion
    #endregion
  }
}
www.java2v.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.