Skip to main content
Skip table of contents

pdfXFA: Add support for rich text values in form state

In previous versions of pdfXFA, form flattening lacked support for rich text values. This resulted in the loss of rich text content when flattening XFA form fields. 

However, in pdfXFA version 3.0.1 rich text data in form fields is now supported. 

The following example will replicate the issue in previous versions, as well as demonstrating the fix in the current version. 

JAVA

JAVA
import com.itextpdf.licensing.base.LicenseKey; 

import com.itextpdf.tool.xml.xtra.xfa.MetaData; 
import com.itextpdf.tool.xml.xtra.xfa.XFAFlattener; 
import com.itextpdf.tool.xml.xtra.xfa.XFAFlattenerProperties; 

import java.io.FileInputStream; 
import java.io.FileOutputStream; 

public class RichTextXFATest { 

    private static String LICENSE = "license.json"; 

    public static void main(String[]args){
        manipulatePdf("richTextFieldNoDataBinding.pdf" ,"result.pdf");
    } 

    public static void manipulatePdf(String src, String dest) throws Exception { 
        LicenseKey.loadLicenseFile(new FileInputStream(LICENSE)); 

       XFAFlattenerProperties flattenerProperties = new XFAFlattenerProperties()
                .setPdfVersion(XFAFlattenerProperties.PDF_1_7)
                .createXmpMetaData()
                .setTagged()
                .setMetaData(
                        new MetaData()
                            .setAuthor("iText Samples")
                            .setLanguage("EN")
                            .setSubject("Showing off our flattening skills")
                            .setTitle("Flattened XFA"));
 
        XFAFlattener xfaf = new XFAFlattener()
                .setFlattenerProperties(flattenerProperties);

        xfaf.flatten(new FileInputStream(src), new FileOutputStream(dest)); 
    } 
} 

C#

C#
using iText.Licensing.Base;
using iTextSharp.tool.xml.xtra.xfa;

namespace XFATests;
{
  public class RichTextXFATest
  {
    private static string LICENSE = "license.json";
    
    static void Main(string[] args)
    {
      ManipulatePdf("richTextFieldNoDataBinding.pdf", "result.pdf")
    }
    
    public static void ManipulatePdf(string src, string dest)
    {
      LicenseKey.LoadLicenseFile(new FileStream(LICENSE, FileMode.Open));

      XFAFlattenerProperties flattenerProperties = new XFAFlattenerProperties()
        .SetPdfVersion(XFAFlattenerProperties.PDF_1_7)
        .CreateXmpMetaData()
        .SetTagged()
        .SetMetaData(
          new MetaData()
            .SetAuthor("iText Samples")
            .SetLanguage("EN")
            .SetSubject("Showing off our flattening skills")
            .SetTitle("Flattened XFA"));

      XFAFlattener xfaf = new XFAFlattener()
        .SetFlattenerProperties(flattenerProperties);

      xfaf.Flatten(new FileStream(src, FileMode.Open, FileAccess.Read),
        new FileStream(dest, FileMode.Create, FileAccess.Write));
    }
  }
}

Resources:

richTextFieldNoDataBinding.pdf
result.pdf

JavaScript errors detected

Please note, these errors can depend on your browser setup.

If this problem persists, please contact our support.