How to increase the Font Size of a PDF Field in ItextSharp(PdfStamper)?

I am using PdfStamper to create PDF at run time. My problem is that I am not able to increase the font size of a PDF field. I have tried this but no luck,

stamper.AcroFields.SetFieldProperty("names", "textsize", 4f, null);
Font font = FontFactory.GetFont(FontFactory.COURIER, 2f, iTextSharp.text.Font.BOLD);    
stamper.AcroFields.AddSubstitutionFont(font.BaseFont);

Upvotes: 3

Views: 11482

Answers (4)

Ogglas
Ogglas

Reputation: 69958

To change font size for every form field you could do it like this:

using (PdfReader pdfReader = new PdfReader(fileInfo.FullName))
{
    using (var ms = new MemoryStream())
    {
        using (var pdfStamper = new PdfStamper(pdfReader, ms))
        {
            SetAcroFields(pdfStamper, myModel);

            // flatten the form to remove editting options, set it to false
            // to leave the form open to subsequent manual edits
            pdfStamper.FormFlattening = true;
            var pdfFormFields = pdfStamper.AcroFields;

            foreach (var f in pdfReader.AcroFields.Fields)
            {
                //Change font size here if auto should not be used
                pdfFormFields.SetFieldProperty(f.Key.ToString(), "textsize", (float)8.0, null);
            }
        }
        return ms.ToArray();
    }
}

Upvotes: 0

tvdias
tvdias

Reputation: 840

I got it working using

stamper.AcroFields.SetFieldProperty("names", "textsize", 4f, null);

but it has to be set before the field is filled

Upvotes: 14

Chris
Chris

Reputation: 73

Which version of iTextSharp are you using? I have 5.0.6.0 and the following line of code works for me:

stamper.AcroFields.SetFieldProperty("SomeDateField", "textsize", 8f, null);

However, I encountered an oddity... the above line only works for me if that field's font size is set to Auto. When it is set to a fixed font-size, I can't seem to change it through code (I tried several different ways that I had come across).

I'd be curious if you experience the same if you set that field to Auto font-size in Acrobat.

Upvotes: 6

Mike Varosky
Mike Varosky

Reputation: 390

From what I've been able to ascertain, it looks like the font size is completely relative to the horizontal and vertical width of the text field. I have played around with a few processes to try to "re-size" the text at run-time, but none have yielded any results. The only "false-positive" I was able to produce was when I re-sized the text field manually. Sorry this wasn't more helpful to solving your problem, I just figured I would share my experience with this same problem. I'll keep an eye out for any solutions though, and if you manage to come up with a solution for this, please post it, because it would be very valuable knowledge.

Upvotes: 2

Related Questions