We're sorry Aspose doesn't work properply without JavaScript enabled.

Free Support Forum - aspose.com

Setting Font Size on TextBox not working

I’m trying to set font size on a TextBox but its not working. The problem is that if a user enters a lot of text I want it to cut off instead of fitting to the size of the textbox. I read in other posts that I need to set the FontSize in order to accomplish this.

Below is my code:

            var propControlNo = new Aspose.Pdf.Forms.TextBoxField(page2, new Rectangle(x.X, pageHeight - x.Y + rowPadding, y.X, pageHeight - y.Y + rowPadding));
            propControlNo.PartialName = "PropertyControlNo" + rowCounter;
            propControlNo.DefaultAppearance.FontSize = 16.0;
            
            propControlNo.Value = "Hello there this is a long text! Lets try and make this thing even wider and see what happens shall we? Great! Thanks!";
            doc.Form.Add(propControlNo);

            x.Y += 20 + rowPadding;
            y.Y += 20 + rowPadding;
            rowCounter++;

I have also attached the generated PDF - the second page contains the TextBoxes.

TestDocument.pdf (83.5 KB)

@dennyngc

Thank you for contacting support.

We would like to request you to share complete code snippet because there are some undeclared variables and we are unable to reproduce the problem. Please also share the source document so that we may proceed further to help you out. Before sharing requested data, please ensure using Aspose.PDF for .NET 18.5 in your environment.

@Farhan.Raza

I’ve included the code below. The only external item is the _file object which is the PDF file that is loaded from our database. I updated my Aspose.PDF to the latest version, 18.5

        // _file is a byte[] that comes from our database (the PDF file)
        using (MemoryStream ms = new MemoryStream(_file))
        {
            using (Document doc = new Document(ms))
            {
                // Add Addendum Page
                doc.Pages.Add();
                var page2 = doc.Pages[2];

                var pageHeight = 11 * 72; // PDF is 72 points per inch
                var pageWidth = 8.5 * 72;
                page2.SetPageSize(pageWidth, pageHeight);

                var rowPadding = 5;
                var x = new Point(20, 60);
                var y = new Point(120, 40);

                // Create 3 TextBoxFields
                for (int rowCounter = 0; rowCounter < 3; rowCounter++)
                {
                    // PDF origin is bottom-left corner
                    var rect = new Rectangle(x.X, pageHeight - x.Y + rowPadding, y.X, pageHeight - y.Y + rowPadding);
                    var propControlNo = new Aspose.Pdf.Forms.TextBoxField(page2, rect);
                    propControlNo.PartialName = "PropertyControlNo" + rowCounter;
                    propControlNo.DefaultAppearance.FontSize = 16.0;

                    propControlNo.Value = "Hello there this is a long text! Lets try and make this thing even wider and see what happens shall we? Great! Thanks!";
                    doc.Form.Add(propControlNo);

                    x.Y += 20 + rowPadding;
                    y.Y += 20 + rowPadding;
                }

                using (MemoryStream ms2 = new MemoryStream())
                {
                    doc.Save(ms2);
                    Response.Clear();
                    Response.ClearHeaders();
                    Response.ClearContent();
                    Response.Charset = "UTF-8";
                    Response.AddHeader("content-length", ms2.Length.ToString());
                    Response.AddHeader("content-disposition", String.Format("inline;filename=TestDocument.pdf", "FileName"));
                    Response.Cache.SetCacheability(System.Web.HttpCacheability.NoCache);
                    Response.Cache.SetNoStore();
                    Response.ContentType = "application/pdf";
                    Response.BinaryWrite(ms2.ToArray());
                    Response.Flush();
                    Response.End();
                }
            }
        }

I am attaching a preview of the file and what I expect to see. The first textbox is what I expect to see when setting the font size to 16. However the second and third textboxes are what is being rendered. However what is interesting is that when I look at the properties of the textbox fields in Adobe Acrobat Pro the font size does say 16. If I manually change the size in Acrobat to 16 then it renders how I expect Aspose to render the field.

expected.png (25.7 KB)

@dennyngc

Thank you for sharing requested data.

We would like to request you to add below line of code in your program and then share your kind feedback with us. Set Multiline property to true in order to achieve your requirements.

propControlNo.Multiline = true;

We hope this will be helpful. Please feel free to contact us if you need any further assistance.

Thank you @Farhan.Raza that did resolve the issue.

Also I found that looking at the PDF in Firefox’s built-in PDF viewer did not display the form fields correctly, however other browsers, as well as Acrobat Reader and Acrobat Pro, displayed the form correctly. I’m fairly certain that is a Firefox issue and not Aspose. I was using Firefox ESR 52.8.0.

Thanks for the help!

@dennyngc

Thank you for your kind feedback.

We are glad to know that your issue has been resolved. Please feel free to contact us if you face nay problem while working with Aspose.PDF for .NET API. We will be more than glad to assist you.