3

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);
2
  • Perhaps you can find some help in this post witch contains a simular problem.
    – Jonas W
    Aug 2, 2012 at 5:45
  • @user960567 can you please add the solution for it or accept one of the answers?
    – tvdias
    Nov 30, 2019 at 11:01

4 Answers 4

13

I got it working using

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

but it has to be set before the field is filled

1
  • 3
    The critical part for me was the IT HAS TO BE SET BEFORE the field is filled. Thanks! Jun 22, 2016 at 20:36
6

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.

2

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.

1
  • 1
    I was able to solve this by creating multiple pdf forms which different font. Aug 4, 2012 at 9:20
0

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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.