1

I'm trying to flatten this interactive PDF that is based on the XML Forms Architecture (XFA):

http://www.finanse.mf.gov.pl/documents/766655/1481810/PIT-11(23)_v1-0E_2016.pdf

It contains Arial font with polish encoding.

After flattening it with the code below, I get a new PDF, but without Arial font and without polish letters of the original (FreeSans and Helvetica are used instead).

How can I make the flattened PDF include the same fonts as the original one?

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
using iTextSharp;
using iTextSharp.text.pdf;
using iTextSharp.tool.xml.xtra.xfa;
using iTextSharp.text;
using iTextSharp.license;

namespace PdfFiller_bon
{
    public class PdfFiller
    {
        public string LastError
        {
            get;
            private set;
        }

        public PdfFiller()
        {
            LastError = "";
        }

        public bool FillPdfFromXml(string PdfSrc, string PdfDest, string XmlSrc, string licenseFile = "")
        {
            if (!File.Exists(PdfSrc))
            {
                LastError = "PDF Form does not exist";
                return false;
            }
            if (!File.Exists(XmlSrc))
            {
                LastError = "XML file does not exist";
                return false;
            }
            PdfReader reader = null;
            PdfStamper stamper = null;
            MemoryStream ms = null;
            PdfWriter writer = null;
            try
            {
                PdfReader.unethicalreading = true;

                LicenseKey.LoadLicenseFile(licenseFile);

                ms = new MemoryStream();
                reader = new PdfReader(PdfSrc);
                stamper = new PdfStamper(reader, ms);
                stamper.Writer.CloseStream = false;
                AcroFields form = stamper.AcroFields;
                XfaForm xfa = form.Xfa;
                xfa.FillXfaForm(new FileStream(XmlSrc, FileMode.Open));
                stamper.Close();

                Document document = new Document();
                writer = PdfWriter.GetInstance(document, new FileStream(PdfDest, FileMode.Create));
                FontFactory.Register("C:\\Windows\\Fonts\\Arial.ttf", "Arial");
                Font font = FontFactory.GetFont("Arial", BaseFont.CP1250, BaseFont.EMBEDDED);
                //TODO how to feed the font to the flattener?
                XFAFlattener xfaf = new XFAFlattener(document, writer);
                ms.Position = 0;
                xfaf.Flatten(new PdfReader(ms));
                document.Close();
                writer.Close();
                ms.Close();
            }

            return true;
        }
    }
}

I have a trial license code and am trying to work up a solution to decide if we should pay for the license.

Bruno Lowagie
  • 75,994
  • 9
  • 109
  • 165
AStraks
  • 11
  • 1
  • This is not true: *It contains Arial font with Polish encoding.* It contains Helvetica (not embedded), Courier, MyriadPro Regular, Bold, and BoldItalic, but not Arial. I will post the question on the internal ticketing system and ask someone to look at it. In the meantime, please contact someone at iText Software to do the same. – Bruno Lowagie Dec 09 '16 at 11:01
  • Thank you, I have sent an e-mail to iText as you advised. Regarding the fonts, I got Arial from document properties of the XFA PDF interactive form (File -> Properties -> Fonts tab): it lists Arial-BoldItalicMT, Arial-BoldMT, ArialMT and MyriadPro-Regular. How else can one check what fonts are used in a PDF document? – AStraks Dec 09 '16 at 12:16
  • I usually look inside the file to discover which fonts are used. You looked at the Fonts tab, but you overlooked the fact that none of the Arial fonts is embedded. If a font is embedded, there is no line that says "Actual Font" nor "Actual Font Type." Instead it says "(Embedded Subset)" (see [screenshot](http://developers.itextpdf.com/sites/default/files/VVPAc.png)) or "(Embedded)". In other words: the fonts you mention aren't embedded. – Bruno Lowagie Dec 09 '16 at 12:50

0 Answers0