Search code examples
c#pdfitext7xfa

how to flatten xfa pdf file with russian text, using itext pdfXfa


Im tryng to flatten dynamic xfa file using itext 7 pdfxfa trial version. File contains some numbers, russian and english text.

LicenseKey.LoadLicenseFile("itextkey.xml");
XFAFlattener xfaf = new XFAFlattener();
xfaf.Flatten(new FileStream("xfaform.pdf", FileMode.Open, FileAccess.Read), new FileStream("flat.pdf", FileMode.Create, FileAccess.Write));

It working, but in flat.pdf i see only some of numbers, english text and punctuation marks. Adobe reader allows to copy all the text of the source file. Unfortunately i can't show original pdf. Is there a way correctly flatten xfa form with russian text?

fonts from original xfa file:

fonts from flattened file:


Solution

  • If your XFA form references fonts that can be accessed but don't contain some of the glyphs (like Helvetica does not contain Russian glyphs in your case), you need to either fix the original XFA form by specifying other fonts and embedding them in the original PDF with XFA form, or you can tell XFAFlattener which fonts to use for the glyphs that are missing instead. Here is an example:

    XFAFontSettings fontSettings = new XFAFontSettings()
           .setFontsPath("C:/Windows/Fonts"); // Set path to the folder containing your font files
    HashMap<String, String> mp = new HashMap<String, String>();
    // Tell pdfXFA to use Arial instead of Helvetica for the missing glyphs
    mp.put("Helvetica", "Arial");
    fontSettings.setFontSubstitutionMap(mp);
    
    // Passing your font settings to pdfXFA
    XFAFlattener xfaf = new XFAFlattener()
            .setFontSettings(fontSettings);