Search code examples
c#pdfitext7

Using iText.Forms.Fields.PdfFormField.SetRichText ()


I'm using Itext7 for filling PDF form; i need at least an implementation of iText.Forms.Fields.PdfFormField.SetRichText() i found nothing on Internet when i try to use a PdfString it show nothing at end. Am i missing something? thanks in advance

using StreamReader reader = File.OpenText("XmlTemplate/AdresseRTFTemplate.xml");
                        string template = reader.ReadToEnd();                     
                        PdfString b = new PdfString(template);
                        field.Value.SetRichText(b);

Here is the xml content

<?xml version="1.0"?>
<body xfa:APIVersion="Acroform:2.7.0.0" xfa:spec="2.1" xmlns="http://www.w3.org/1999/xhtml" xmlns:xfa="http://www.xfa.org/schema/xfa-data/1.0/">
    <p dir="ltr" style="margin-top:0pt;margin-bottom:0pt;font-family:Arial;font-size:8pt">
        This is a<span style="xfa-spacerun:yes">&#160;</span><span style="font-style:italic">rich</span><span style="font-style:normal">
            <span style="xfa-spacerun:yes">&#160;</span>
        </span><span style="font-weight:bold;font-style:normal">Text i modified</span>
    </p>
</body>

Solution

  • iText doesn't support rich text property. iText can set it to the form field element, but to make it work appearance shall be generated based on it and this is something iText is not able to do. However there is workaround which will allow us to explicitly generate appearance and set it directly to the form field. Here is code sample which shows how this can be achieved

    try (PdfDocument pdfDocument = new PdfDocument(new PdfWriter("out PDF.pdf"))) {
    
            PdfAcroForm acroForm = PdfAcroForm.getAcroForm(pdfDocument, true);
    
            Rectangle rectangle = new Rectangle(100, 700, 100, 100);
            PdfTextFormField textFormField = PdfFormField.createText(pdfDocument, rectangle, "text_form");
            textFormField.setValue("This is a rich Text");
            textFormField.setDefaultStyle(new PdfString("font: 18pt Arial"));
            String richText = "<?xml version=\"1.0\"?>" +
                    "<body xfa:APIVersion=\"Acroform:2.7.0.0\" xfa:spec=\"2.1\" xmlns=\"http://www.w3.org/1999/xhtml\" xmlns:xfa=\"http://www.xfa.org/schema/xfa-data/1.0/\">" +
                    "<p style=\"margin-top:0pt;margin-bottom:0pt;text-valign:middle;font-family:Arial;font-size:18pt\">This is" +
                    "<span style=\"xfa-spacerun:yes\">&#160;</span><span style=\"font-weight:bold\">a ric</span>" +
                    "<span style=\"font-weight:normal\">h T</span><span style=\"color:#ff00ff;font-weight:normal\">ex</span>" +
                    "<span style=\"color:#000000;font-weight:normal\">t</span></p></body>";
            textFormField.setRichText(new PdfString(richText));
            textFormField.setRichText(true);
    
            Document document = Jsoup.parse(richText);
            List<IElement> elements = HtmlConverter.convertToElements(document.body().outerHtml());
            PdfFormXObject appearance = new PdfFormXObject(rectangle);
            Canvas canvas = new Canvas(new PdfCanvas(appearance, pdfDocument), rectangle);
            canvas.setProperty(Property.RENDERING_MODE, RenderingMode.HTML_MODE);
            for (IElement ele : elements) {
                if (ele instanceof IBlockElement) {
                    canvas.add((IBlockElement) ele);
                }
            }
            textFormField.setAppearance(PdfName.N, "state", appearance.getPdfObject());
    
            acroForm.addField(textFormField);
        }
    

    There are several major things which are important during work with rich text in form fields:

    • Text value which is set with PdfFormField#setValue method must lexically be equal to the one in rich text string.
    • Acrobat does not support rich text property perfectly. For example I tried to use your xml line and iText generates appearance without any problems, but if you try to change text inside Acrobat, styling will be removed and only plain text will remain. It seems that not every style attribute is supported