Search code examples
c#itextitext7pdfa

When converting HTML to PDF/A, I get "All the fonts must be embedded. This one is not: Times-Bold"


public const String INTENT = "sRGB Color Space Profile.icm";
static void Main(string[] args)
{
    String HTML = "<h1>Test</h1><p>Hello World</p>";            
    PdfWriter writer = new PdfWriter("hello.pdf");
    PdfADocument pdf = new PdfADocument(writer, PdfAConformanceLevel.PDF_A_3A, new PdfOutputIntent("Custom", "", "http://www.color.org", "sRGB IEC61966-2.1", new FileStream(INTENT, FileMode.Open)));
    pdf.SetTagged();
    ConverterProperties properties = new ConverterProperties();
    properties.SetBaseUri("");
    HtmlConverter.ConvertToPdf(HTML, pdf, properties);
}

When I run this code, I get a PdfAConformanceException "All the fonts must be embedded. This one is not: Times-Bold".

I try to get the registered fonts through these lines:

var fonts = "";
foreach (string fontname in iTextSharp.text.FontFactory.RegisteredFonts)
{
    fonts += fontname + " ";
}

and i get: "courier courier-bold courier-oblique courier-boldoblique helvetica helvetica-bold helvetica-oblique helvetica-boldoblique symbol times-roman times-bold times-italic times-bolditalic zapfdingbats", so times-bold is in there.

Am I missing something?


Solution

  • The issue here is that there are 14 default fonts in PDF and the default fonts aren't embedded by pdfHtml/iText by default. You can tell pdfHtml to "skip" registering the default fonts by instantiating and configuring a FontProvider like this:

    DefaultFontProvider fontProvider = new DefaultFontProvider(false, true, true);
    properties.setFontProvider(fontProvider);
    

    This will produce a PDF file with an embedded subset of "Freesans".

    See the following for more information: