使用 iTextSharp 将表单域的不同部分设置为具有不同的字体 [英] Set different parts of a form field to have different fonts using iTextSharp

查看:23
本文介绍了使用 iTextSharp 将表单域的不同部分设置为具有不同的字体的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我不确定这是否可行,但我认为这值得一问.我已经弄清楚如何使用 pdfstamper 和 acrofields 方法设置表单域的字体,但我真的希望能够在同一字段中设置文本不同部分的字体.这是我当前设置表单字段字体的方法:

I'm not sure that this is possible but I figured it would be worth asking. I have figured out how to set the font of a formfield using the pdfstamper and acrofields methods but I would really like to be able to set the font of different parts of the text in the same field. Here's how I'm setting the font of the form fields currently:

            // Use iTextSharp PDF Reader, to get the fields and send to the 
            //Stamper to set the fields in the document
            PdfReader pdfReader = new PdfReader(fileName);

            // Initialize Stamper (ms is a MemoryStream object)
            PdfStamper pdfStamper = new PdfStamper(pdfReader, ms);

            // Get Reference to PDF Document Fields
            AcroFields pdfFormFields = pdfStamper.AcroFields;

            //create a bold font
            iTextSharp.text.Font bold = FontFactory.GetFont(FontFactory.COURIER, 8f, iTextSharp.text.Font.BOLD);

            //set the field to bold
            pdfFormFields.SetFieldProperty(nameOfField, "textfont", bold.BaseFont, null);

            //set the text of the form field
            pdfFormFields.SetField(nameOfField, "This:  Will Be Displayed In The Field");

           // Set the flattening flag to false, so the document can continue to be edited
            pdfStamper.FormFlattening = true;

            // close the pdf stamper
            pdfStamper.Close();

我希望能够在上面设置文本的地方将This:"设置为粗体,并将将在字段中显示"保留为非粗体.我不确定这是否真的可行,但我认为值得一问,因为这对我目前的工作真的很有帮助.

What I'd like to be able to do where I set the text above is set the "This: " to bold and leave the "Will Be Displayed In The Field" non-bolded. I'm not sure this is actually possible but I figured it was worth asking because it would really be helpful in what I'm currently working on.

提前致谢!

推荐答案

是的,有点.PDF 字段可以具有富文本值(自 acrobat 6/pdf1.5 起)以及常规值.

Yes, kinda. PDF fields can have a rich text value (since acrobat 6/pdf1.5) along with a regular value.

常规值使用默认外观中定义的字体......单个字体.

The regular value uses the font defined in the default appearances... a single font.

富值格式(iText 不直接支持,至少目前不支持),在 PDF 参考.<b>、<i>、<p>,以及相当多的css2文本属性.它需要一个具有各种属性的.

The rich value format (which iText doesn't support directly, at least not yet), is described in chapter 12.7.3.4 of the PDF Reference. <b>, <i>, <p>, and quite a few css2 text attributes. It requires a with various attributes.

要启用丰富的值,您必须为文本字段设置字段标志 (PdfName.FF) 的第 26 位.PdfFormField 没有setRichValue",但它们是字典,所以你可以:

To enable rich values, you have to set bit 26 of the field flags (PdfName.FF) for a text field. PdfFormField doesn't have a "setRichValue", but they're dictionaries, so you can just:

myPdfFormField.put(PdfName.RV, new PdfString( richTextValue ) );

如果您尝试将富文本添加到尚不支持的现有字段:

If you're trying to add rich text to an existing field that doesn't already support it:

AcroFields fields = stamper.getAcroFields();

AcroFields.Item fldItem = fields.getFieldItem(fldName);
PdfDictionary mergedDict = item.getMerged(0);
int flagVal = mergedDict.getAsNumber(PdfName.FF).intValue();
flagVal |= (1 << 26);

int writeFlags = AcroFields.Item.WRITE_MERGED | AcroFields.Item.WRITE_VALUE;
fldItem.writeToAll(PdfName.FF, new PdfNumber(flagVal), writeFlags);
fldItem.writeToAll(PdfName.RV, new PdfString(richTextValue), writeFlags);

我实际上是在输入此消息时为 iText 添加了富文本支持(不清晰).为 SO 的贡献者欢呼吧.Paulo 最近很擅长保持 iTextSharp 的同步,所以这应该不是问题.下一个主干版本应该有这个功能......所以你可以写:

I'm actually adding rich text support to iText (not sharp) as I type this message. Hurray for contributors on SO. Paulo's been good about keeping iTextSharp in synch lately, so that shouldn't be an issue. The next trunk release should have this feature... so you'd be able to write:

myPdfFormField.setFieldFlags( PdfFormField.FF_RICHTEXT );
myPdfFormField.setRichValue( richTextValue );

// note that this will fail unless the rich flag is set
acroFields.setFieldRichValue( richTextValue );

注意:iText 的外观生成尚未更新,只是事物的价值方面.这需要做更多的工作.因此,您需要 acroFields.setGenerateAppearances(false) 或让 JS 在打开表单时重置字段值以强制 Acrobat/Reader 为您构建外观[s].

NOTE: iText's appearance generation hasn't been updated, just the value side of things. That would take considerably more work. So you'll want to acroFields.setGenerateAppearances(false) or have JS that resets the field value when the form its opened to force Acrobat/Reader to build the appearance[s] for you.

这篇关于使用 iTextSharp 将表单域的不同部分设置为具有不同的字体的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆