Uploaded image for project: 'PDFBox'
  1. PDFBox
  2. PDFBOX-3812

Support auto size font for multiline PDTextField

    XMLWordPrintableJSON

Details

    • Improvement
    • Status: Closed
    • Major
    • Resolution: Fixed
    • 2.0.6
    • 2.0.21, 3.0.0 PDFBox
    • AcroForm
    • None
    • Windows 7 and Mac OSX, Java 1.8.0_121

    Description

      Hello, I ve got a simple pdf file "TestPDF.pdf" with just one text field named "Text1". It is set up as auto size and multiline field.

      My code :

      GeneratePDF.java
      	File fileTemplate = new File("/Users/xxx/Downloads/TestPDF.pdf");
      		PDDocument doc = null;
      		try {
      			doc = PDDocument.load(fileTemplate);
      		} catch (IOException e) {
      			e.printStackTrace();
      		}
      		PDDocumentCatalog catalog = doc.getDocumentCatalog();
      		PDAcroForm form = catalog.getAcroForm();
      		PDTextField field = (PDTextField)form.getField("Text1");
      //		field.setDefaultAppearance("/Helv 0 Tf 1 0 0 rg")
      
      		String longText = "THIS IS A VERY VERY LONG TEXT IN A MULTINLINE TEXT FIELD " 
      				+ "THIS CAN BE REALY LONG TEXT FIELD CONTAINING A FULL ADDRESS OF THE CUSTOMER"
      				+ "WE WANT TO HAVE AN AUTO SIZED FIELD WITH DYNAMICALY CHANGED FONT SIZE. TO TEST IT OUT WE" + "NEED AT THE END 123";
      		try {
      			field.setValue(longText);
      //			form.flatten();
      			doc.save("/Users/xxx/TestPDF_generated.pdf");
      			doc.close();
      		} catch (IOException e) {
      			e.printStackTrace();
      		}
      	

      Please open the attached "TestPDF_gen.pdf" file in the Adobe Acrobat. It shows the text cut off at the end. However if you click inside the field it sudenly renders text properly.

      Other remarks :
      1. If you open the "TestPDF_gen.pdf" file in the Preview (Mac OSX default pdf viewer) then the Text1 shows text properly auto sized.
      2. If you do flat the form by uncommenting form.flatten(). Then the Text1 is cut off at the end of the text and font size is not auto. I ve tried both Adobe Acrobat and Preview. See the "TestPDF_gen_flatten.pdf"
      3. I ve also tried calling setDefaultAppearance method before setting the field's value - field.setDefaultAppearance("/Helv 0 Tf 1 0 0 rg") but it did not help.

      Attachments

        1. Autosizing.pdf
          14 kB
          Maruan Sahyoun
        2. PDFBOX3812-acrobat-multiline-auto.pdf
          29 kB
          Maruan Sahyoun
        3. PDFBOX-3812-break-too-long-words-and-use-default-font-size-as-max-in-multiline-fields.patch
          4 kB
          Sebastian Fieber
        4. Test_TextField_Multiline_Autosize.pdf
          19 kB
          Marek Zajac
        5. TestPDF_gen_flatten.pdf
          10 kB
          Marek Zajac
        6. TestPDF_gen.pdf
          11 kB
          Marek Zajac
        7. TestPDF.pdf
          8 kB
          Marek Zajac

        Issue Links

          Activity

            People

              msahyoun Maruan Sahyoun
              Marek Zajac Marek Zajac
              Votes:
              3 Vote for this issue
              Watchers:
              7 Start watching this issue

              Dates

                Created:
                Updated:
                Resolved: