ListFonts.cs
// 完毕:
using System;
using System.IO;
using System.Drawing;
using System.Collections.Generic;
using System.Linq;
using GrapeCity.Documents.Pdf;
using GrapeCity.Documents.Text;

namespace DsPdfWeb.Demos.Basics
{
    // 此示例列出了在加载的 PDF 中找到的所有字体,
    // 打印每种字体的一些信息,并指示 Font 对象是否
    // 可以从 PDF 中的字体创建。
    public class ListFonts
    {
        public int CreatePDF(Stream stream)
        {
            var sourcePDF = "CompleteJavaScriptBook.pdf";

            var doc = new GcPdfDocument();
            var page = doc.NewPage();

            var rc = Common.Util.AddNote(
                "此示例将任意 PDF 加载到临时 GcPdfDocument 中," +
                "",
                page);

            // 渲染文本的文本布局:
            var tab = 24;
            var tl = page.Graphics.CreateTextLayout();
            tl.DefaultFormat.Font = StandardFonts.Times;
            tl.DefaultFormat.FontSize = 12;
            tl.MaxWidth = doc.PageSize.Width;
            tl.MaxHeight = doc.PageSize.Height;
            tl.MarginAll = rc.Left;
            tl.MarginTop = rc.Bottom + 36;
            tl.TabStops = new List<TabStop>()
            {
                new TabStop(tab)
            };
            tl.FirstLineIndent = -tab;
            tl.MarginRight = 144;

            // 寡妇/孤儿控制的文本分割选项:
            var to = new TextSplitOptions(tl)
            {
                KeepParagraphLinesTogether = true,
                MinLinesInFirstParagraph = 2,
                MinLinesInLastParagraph = 2,
                RestMarginTop = rc.Left,
            };

            // 打开任意 PDF,将其加载到临时文档中并获取所有字体:
            using var fs = File.OpenRead(Path.Combine("Resources", "PDFs", sourcePDF));
            var doc1 = new GcPdfDocument();
            doc1.Load(fs);

            var fonts = doc1.GetFonts();

            tl.AppendLine($"Total of {fonts.Count} fonts found in {sourcePDF}:");
            tl.AppendLine();
            int i = 0;
            foreach (var font in fonts)
            {
                var nativeFont = font.CreateNativeFont();
                tl.Append($"{i}:\tBaseFont: {font.BaseFont}; IsEmbedded: {font.IsEmbedded}.");
                tl.AppendParagraphBreak();
                if (nativeFont != null)
                    tl.AppendLine($"\tCreateNativeFont succeeded, family: {nativeFont.FontFamilyName}; bold: {nativeFont.FontBold}; italic: {nativeFont.FontItalic}.");
                else
                    tl.AppendLine("\tCreateNativeFont failed");
                tl.AppendLine();
                ++i;
            }
            tl.PerformLayout(true);
            while (true)
            {
                // 'rest' 将接受不适合的文本:
                var splitResult = tl.Split(to, out TextLayout rest);
                doc.Pages.Last.Graphics.DrawTextLayout(tl, PointF.Empty);
                if (splitResult != SplitResult.Split)
                    break;
                tl = rest;
                doc.NewPage();
            }
            // 完毕:
            doc.Save(stream);
            return doc.Pages.Count;
        }
    }
}