Embedding font using itext 5 for PDF/UA compliance

坚强是说给别人听的谎言 提交于 2019-12-12 03:29:31

问题


We are currently building a proof of concept to generate PDF/UA compliant PDF from from a CSS and html (xhtml) file using xslt. We are able tag the PDF and add the appropriate metadata information.

The last major issue we are unable to solve is embedding a standard PDF font zapfdinbats, which our accessibility assessment tool complains about - using PAC 2.0 along with adobe DC built in checker.

As you can see from the image below the other fonts we are using seems automatically get embedded using the xmlworker from our CSS.

I have also tried finding the font as indicated and found one, however, it doesn't seem to be the correct one.

Here is a sample of our code

private static ReturnValue CreateFromHtml(string html)
    {
        ReturnValue Result = new ReturnValue();
        var stream = new MemoryStream();


        using (var doc = new Document(PageSize.LETTER))
        {
            using (var ms = new MemoryStream())
            {
                using (var writer = PdfWriter.GetInstance(doc, ms))
                {
                    writer.CloseStream = false;
                    writer.SetPdfVersion(PdfWriter.PDF_VERSION_1_7);

                    //TAGGED PDFVERSION_1_7
                    //Make document tagged
                    writer.SetTagged();
                    //===============
                    //PDF/UA
                    //Set document metadata
                    writer.ViewerPreferences = PdfWriter.DisplayDocTitle;

                    doc.AddLanguage("en-US");
                    doc.AddTitle("document title");
                    writer.CreateXmpMetadata();

                    doc.Open();

                    var embedfont = HttpContext.Current.Server.MapPath("~/scripts/ZapfDingbats.ttf");
                    var fontProv = new XMLWorkerFontProvider();
                    fontProv.DefaultEncoding = "UTF-8";
                    fontProv.Register(embedfont);

                    //Testing zapfDingbats font 
                    Font font = FontFactory.GetFont(embedfont, BaseFont.IDENTITY_H, BaseFont.EMBEDDED);
                    Paragraph p1 = new Paragraph("Testing of Fonts", font);
                    doc.Add(p1);
                    //end font processing

                    var tagProcessors = (DefaultTagProcessorFactory)Tags.GetHtmlTagProcessorFactory();
                    tagProcessors.RemoveProcessor(HTML.Tag.IMG);
                    tagProcessors.AddProcessor(HTML.Tag.IMG, new CustomImageTagProcessor());

                    var cssFiles = new CssFilesImpl();
                    cssFiles.Add(XMLWorkerHelper.GetInstance().GetDefaultCSS());
                    var cssResolver = new StyleAttrCSSResolver(cssFiles);

                    var charset = Encoding.UTF8;
                    var context = new HtmlPipelineContext(new CssAppliersImpl(new XMLWorkerFontProvider()));
                    context.SetAcceptUnknown(true).AutoBookmark(true).SetTagFactory(tagProcessors);
                    var htmlPipeline = new HtmlPipeline(context, new PdfWriterPipeline(doc, writer));

                    var cssPipeline = new CssResolverPipeline(cssResolver, htmlPipeline);

                    var worker = new XMLWorker(cssPipeline, true);
                    var xmlParser = new XMLParser(true, worker, charset);

                    using (var sr = new StringReader(html))
                    {
                        xmlParser.Parse(sr);
                        doc.Close();
                        ms.Position = 0;
                        ms.CopyTo(stream);
                        stream.Position = 0;
                    }
                }
            }
        }

        // get bytes from stream  
        Result.Data = stream.ToArray();

        // success  
        Result.Success = true;

        return Result;
    }

Maybe there is something in the CSS we need to do (our CSS is quite large f


回答1:


iText only ships with the Adobe Font Metrics (AFM) file of Zapfdingbats. This means that you can't embed that font unless you provide the corresponding PostScript Font Binary (PFB) file. This PFB file can't be shipped with iText because iText doesn't have a license to do so.

The first step to solve this, is to:

  • purchase a Zapfdingbats license so that you get the PFB (If I recall correctly, it's a font owned by Adobe), or
  • use an alternative font when you want to insert special characters (check boxes, phone symbols,...) into your text (e.g. purchase a license for the AdobePiStd font that was used as a substitution font and use that font instead of Zapfdingbats).

In your case, you provided a font ZapfDingbats.ttf which you register with the XMLWorkerFontProvider. When you register this font, it can be recognized through an alias. If ZapfDingbats.ttf isn't picked up by XML Worker, there is probably a mismatch between the name of the font used in the PDF and the alias that was used when ZapfDingbats.ttf was registered.

What is the font name used for ZapfDingbats in the CSS? You should register ZapfDingbats using that name as alias.



来源:https://stackoverflow.com/questions/37577555/embedding-font-using-itext-5-for-pdf-ua-compliance

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!