首页 > 解决方案 > 使用 iText.Forms.Fields.PdfFormField.SetRichText()

问题描述

我正在使用 Itext7 填写 PDF 表单;我至少需要一个 iText.Forms.Fields.PdfFormField.SetRichText() 的实现,当我尝试使用 PdfString 时,我在 Internet 上一无所获。我错过了什么吗?提前致谢

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

这是xml内容

<?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>

标签: c#pdfitext7

解决方案


iText 不支持富文本属性。iText 可以将其设置为表单字段元素,但要使其工作外观应基于它生成,这是 iText 无法做到的。但是,有一种解决方法可以让我们显式生成外观并将其直接设置到表单字段。这是代码示例,它显示了如何实现这一点

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);
    }

在表单字段中使用富文本时,有几件重要的事情很重要:

  • 使用方法设置的文本值在PdfFormField#setValue词法上必须等于富文本字符串中的值。
  • Acrobat 不完全支持富文本属性。例如,我尝试使用您的 xml 行并且 iText 生成外观没有任何问题,但是如果您尝试在 Acrobat 中更改文本,样式将被删除,并且只保留纯文本。似乎并非所有样式属性都受支持

推荐阅读