Hi,
In Germany it is becoming a requirement that all fonts are embedded in the PDF before submission in legal matters.
How can we ensure that all fonts are embedded in the PDF? Using .NET Framework version.
Of course here I also think of fonts not availble on the current system.
Please advise
The below snippet seems to somewhat work, but is very inefficient, especially on large documents. Please advise of better
public static bool EmbedUsedFonts(PdfLoadedDocument ldoc)
{
try
{
if (ldoc.UsedFonts == null || ldoc.UsedFonts.Length == 0)
{
return false;
}
for (int i = 0; i < ldoc.UsedFonts.Count(); i++)
{
if (!ldoc.UsedFonts[i].Type.ToString().Contains("Embed"))
{
try
{
ldoc.UsedFonts[i].Replace(
new PdfTrueTypeFont(
new Font(ldoc.UsedFonts[i].Name, ldoc.UsedFonts[i].Size),
GetFontStyle(ldoc.UsedFonts[i].Style),
ldoc.UsedFonts[i].Size,
false, true)
);
}
catch (Exception e)
{
// Font not available?
Console.WriteLine(string.Format("Font: {0} not available"), ldoc.UsedFonts[i].Name);
}
}
}
return true;
}
catch (Exception e)
{
return false;
}
return false;
}
Hi MrHessellund,
We can only identify the embedded font by using Type property from the PdfUsedFont in an existing PDF document. In this API, we can retrieve all fonts from the existing PDF document.
Please find the code snippet below,
loadedDocument.UsedFonts[i].Type |
Using this approach, we can find the existing font is embed or not.
Regards,
Surya V
Hi,
But then how to I ensure that non-embedded font get embedded?
During your conversion to PDF/A format, it shall also be ensured that all fonts are embedded or at least substitution fonts are embedded. How can this be done without conversion to PDF/A ?
Best regards,
Hi,
The IsAllFontsEmbedded bool property would be helpful.
However, we need some efficient way to embed all fonts which are not embedded.
Thus, something like this would be good:
if (PdfLoadedDocument.IsAllFontsEmbedded == false)
{
for each (Font font in PdfLoadedDocument.UsedFonts)
{
if (font.IsEmbedded == false)
{
PdfLoadedDocument.EmbedFont(font);
}
}
}
Best rega
PdfLoadedDocument loadedDocument = new PdfLoadedDocument(@"../../Data/input.pdf");
var fonts = loadedDocument.UsedFonts;
if (loadedDocument.IsAllFontsEmbedded == false)
{
foreach(PdfUsedFont font in fonts)
{
if (font.Type != PdfFontType.TrueTypeEmbedded)
{
loadedDocument.EmbedFont(font);
}
}
}
loadedDocument.Save("Output.pdf"); |
PdfLoadedDocument loadedDocument = new PdfLoadedDocument(@"../../Data/input.pdf");
var fonts = loadedDocument.UsedFonts;
if (loadedDocument.IsAllFontsEmbedded == false)
{
foreach(PdfUsedFont font in fonts)
{
if (font.Type != PdfFontType.TrueTypeEmbedded)
{
loadedDocument.EmbedFont(font);
}
}
}
loadedDocument.Save("Output.pdf"); |