I am trying to embed fonts into an existing PDF with Aspose.PDF for .NET. I don’t seem to be able to embed any Type 1 fonts. I am setting the EmbedStandardFonts property to true at the document level. I am then looping through the page and form fonts using a code sample I found on your site, and setting IsEmbedded to true if the fonts and not already embedded. I have additionally set the FontOptions.NotifyAboutFontEmbeddingError to true, but am having no errors reported when I save the document. I have enclosed the code that attempts to embed the fonts below:
public static int embedFonts(string dataDir)
{
if (!Directory.Exists(dataDir))
{
Console.WriteLine(string.Format("Input directory {0} does not exists", dataDir));
return 1;
}
else
{
string[] pdfs = Directory.GetFiles(dataDir);
foreach (string pdf in pdfs)
{
FileInfo fi = new FileInfo(pdf);
if (fi.Extension.ToUpper().Equals(".PDF"))
{
Document doc = new Document(pdf);
doc.EmbedStandardFonts = true;
// Iterate through all the pages
foreach (Page page in doc.Pages)
{
if (page.Resources.Fonts != null)
{
foreach (Aspose.Pdf.Text.Font pageFont in page.Resources.Fonts)
{
// Check if font is already embedded
if (!pageFont.IsEmbedded)
{
pageFont.FontOptions.NotifyAboutFontEmbeddingError = true;
pageFont.IsEmbedded = true;
//pageFont.IsSubset = true;
}
}
}
// Check for the Form objects
foreach (XForm form in page.Resources.Forms)
{
if (form.Resources.Fonts != null)
{
foreach (Aspose.Pdf.Text.Font formFont in form.Resources.Fonts)
{
// Check if the font is embedded
if (!formFont.IsEmbedded)
{
formFont.FontOptions.NotifyAboutFontEmbeddingError = true;
formFont.IsEmbedded = true;
//formFont.IsSubset = true;
}
}
}
}
}
String outputPDF = pdf + ".out";
try
{
doc.Save(outputPDF);
}
catch (FontEmbeddingException e)
{
Console.WriteLine("{0}", e.Message);
}
catch (FontNotFoundException e)
{
Console.WriteLine("{0}", e.Message);
}
catch (Exception e)
{
Console.WriteLine("{0}", e.Message);
}
}
}
}
return 0;
}