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

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

问题描述

我不知道这是可能的,但我想这将是值得一问。我已经找到了如何设置使用pdfstamper和acrofields方法formfield的字体,但我真的很希望能够设置在同一领域文本的不同部分的字体。以下是我设置当前表单域的字体:

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

我希望能够做到,我设置上面的文字设定了本,以大胆而离开将显示在该领域的非粗体。我不知道这其实是可能的,但我想这是值得一问,因为这真的是什么,我目前工作的帮助。

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字段可以有丰富的文本值(因为杂技演员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&GT中,< I&GT中,< 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.

要支持丰富的价值,你必须设置26位的字段标志( PdfName.FF )的文本字段。 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(不清晰),在我键入此消息格式文本支持。华友世纪的投稿者。保罗一直良好保持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(假)或有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天全站免登陆