title: Java PDFBox setting custom font for a few fields in PDF Form
body: <p>I am using Apache PDFBox to read a fillable PDF form and fill the fields based on some data. I am using the below code (as per suggestions from other SO answers) to get the default Appearance String and changing it (as you can see below, I am changing the font size from 10 to 12 if the field name is "Field1". </p>
  
  <ol>
  <li>How do I bold the field? Any documentation on what order the /Helv 10 Tf 0 g are arranged? What I need to set to bold the field?</li>
  <li>If I understand right, there are 14 basic fonts that I can use in PDFBox out of the box (pun unintended). I would like to use one or more fonts that look like Signatures (cursive). Any out of the box fonts that do that? If not, if I have my own font, how do I set in the method to be written to the PDF?</li>
  </ol>
  
  <p>Please note, the below code works fine by filling the specific 'value' passed in the method parameter in the specific 'name' field of the method parameter.</p>
  
  <p>Thank you !</p>
  
  <pre><code>public static void setField(String name, String value ) throws     IOException {
      PDDocumentCatalog docCatalog = _pdfDocument.getDocumentCatalog();
      PDAcroForm acroForm = docCatalog.getAcroForm();
      PDField field = acroForm.getField( name );
  
      COSDictionary dict = ((PDField)field).getDictionary();
      COSString defaultAppearance = (COSString) dict.getDictionaryObject(COSName.DA);
      if (defaultAppearance != null)
      {
          dict.setString(COSName.DA, "/Helv 10 Tf 0 g");
          if(name.equalsIgnoreCase("Field1"))
          {
              dict.setString(COSName.DA, "/Helv 12 Tf 0 g");
          }
      }
      if(field instanceof PDTextbox)
      {
          field= new PDTextbox(acroForm, dict);
          ((PDField)field).setValue(value);
      }
  </code></pre>
  
  <p>As per mkl's answer, to use two fonts in the same PDF, I used the following method: I could not get the default font and a custom font working together, so I added two fonts to the resources and used them.</p>
  
  <pre><code>public List&lt;String&gt; prepareFont(PDDocument _pdfDocument) throws IOException
  {
      PDDocumentCatalog docCatalog = _pdfDocument.getDocumentCatalog();
      PDAcroForm acroForm = docCatalog.getAcroForm();
  
      PDResources res = acroForm.getDefaultResources();
      if (res == null)
          res = new PDResources();
  
      InputStream fontStream = getClass().getResourceAsStream("LiberationSans-Regular.ttf");
  InputStream fontStream2 = getClass().getResourceAsStream("Font2.ttf");
      PDTrueTypeFont font = PDTrueTypeFont.loadTTF(_pdfDocument, fontStream);
  PDTrueTypeFont font2 = PDTrueTypeFont.loadTTF(_pdfDocument, fontStream2);
      String fontName = res.addFont(font); 
  String fontName2 = res.addFont(font2);
      acroForm.setDefaultResources(res);
      List&lt;String&gt; fontList = new ArrayList&lt;String&gt;();    fontList.add(font1);fontList.add(font2);
      return fontList;
  }
  </code></pre>