//
// This code is part of Document Solutions for PDF demos.
// Copyright (c) MESCIUS inc. All rights reserved.
//
using System;
using System.IO;
using System.Drawing;
using GrapeCity.Documents.Pdf;
using GrapeCity.Documents.Pdf.TextMap;
using GrapeCity.Documents.Pdf.Text;
using GrapeCity.Documents.Text;
using GrapeCity.Documents.Pdf.Spec;
using System.Collections.Immutable;
using System.Collections.Generic;
namespace DsPdfWeb.Demos
{
// This sample loads an existing PDF, enumerates the fonts used in it,
// determines whether each font is embedded, and prints the list of fonts
// along with their embedding status on the first page of the resulting PDF,
// followed by the original document.
//
// Note: The check for whether a font is embedded is reliable.
// However, determining whether an embedded font is a full font or a subset
// is heuristic and may not be accurate for all PDFs.
public class FontEmbeddingStatus
{
public int CreatePDF(Stream stream)
{
// This function checks whether a font is embedded, and if it is,
// also tries to determine whether it is a subset or a complete font.
FontEmbedMode GetEmbedMode(GrapeCity.Documents.Pdf.Text.Font font)
{
if (font.IsEmbedded)
{
int p = font.BaseFont.IndexOf('+');
if (p > 0)
return FontEmbedMode.EmbedSubset;
else
return FontEmbedMode.EmbedFullFont;
}
return FontEmbedMode.NotEmbed;
}
string EmbedModeToString(FontEmbedMode fem)
{
if (fem == FontEmbedMode.EmbedFullFont)
return "Embedded font";
else if (fem == FontEmbedMode.EmbedSubset)
return "Embedded subset";
else
return "Not embedded";
}
const string pdfName = "C1Olap-QuickStart.pdf";
var fs = File.OpenRead(Path.Combine("Resources", "PDFs", pdfName));
var doc = new GcPdfDocument();
doc.Load(fs);
var page = doc.Pages.Insert(0);
var g = page.Graphics;
var tl = g.CreateTextLayout();
tl.AppendLine($"Fonts used in {pdfName}, and their embedding status:\n");
// List fonts and their embedding states:
var fonts = doc.GetFonts().ToImmutableArray().Sort(
new Comparison<GrapeCity.Documents.Pdf.Text.Font>(
(f1_, f2_) => StringComparer.InvariantCulture.Compare(f1_.BaseFont, f2_.BaseFont)));
foreach (var font in fonts)
{
var embedMode = GetEmbedMode(font);
tl.AppendLine($"- \"{font.BaseFont}\": {EmbedModeToString(GetEmbedMode(font))}");
}
tl.MarginAll = g.Resolution;
g.DrawTextLayout(tl, Point.Empty);
doc.Save(stream);
return doc.Pages.Count;
}
}
}