开发者

Set different parts of a form field to have different fonts using iTextSharp

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 actu开发者_Go百科ally possible but I figured it was worth asking because it would really be helpful in what I'm currently working on.

Thanks in advance!


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.

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.

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

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

or

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

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.


It took me some time to figure out after richtextfield did not work the way it was suppose too with acrofields and most of the cases were the pdf was not editable with different fonts at runtime.

I worked out way of setting different fonts in acrofields and passing values and editing at runtime with itextsharp and I thought it will be useful for others.

  1. Create pdf with a text field in PDF1.pdf (I hope you know how to create field in pdf) e.g., txtComments Go to the property section and Set the property to richtext,Mulitiline

  2. Format the text content in word or pdf by adding the fonts and colors. If done in word, copy and paste the content in pdf - txtcomments field.

    Note: If you want to add dynamic content. Set the parameter “{0}” to the txtComment field in the pdf. using string format method you can set values to it.This is shown in the code below.

            e.g., "Set different parts of a form field to have different fonts using {0}"
    
  3. Add the following code in a button (this is not specific) event by reference in the itextsharp.dll 5.4.2


Response.ContentType = "application/pdf";
Response.AddHeader("Content-disposition","attachment; filename=your.pdf");
PdfReader reader = new PdfReader(@"C:\test\pdf1.pdf");
PdfStamper stamp = new PdfStamper(reader, Response.OutputStream);
AcroFields field = pdfStamp.AcroFields;
string comments = field .GetFieldRichValue("txtcomments");
string Name = "Test1";
string value = string.Format(comments,Name);
field.SetField("txtComment", value );
field.GenerateAppearances = false;//Pdf knows what to do;
stamp.FormFlattening = false;//available for edit at run time
stamp.FreeTextFlattening = true;
stamp.Close();
reader.Close()

Hope this helps.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜