title: PDFBox does not correctly render Simsun (chinese) font
body: <h2>Context</h2>
  
  <p>I am writing a Java code which fill PDF Forms using PDFBox with some user inputs.
  Some of the inputs are in Chinese.</p>
  
  <p>When I generated the PDF, I don't have any errors in the logs but the rendered text is absolutely not the same.</p>
  
  <h2>What I currently have</h2>
  
  <p>Here is what I do:</p>
  
  <ul>
  <li><p>In the PDF file, I specified the SimSun font for the field using Adobe Pro.
  This font handle Simplified Chinese characters.</p></li>
  <li><p>I have the font SimSun installed on my server.</p></li>
  <li><p>PDFBox doesn't display any error (if I remove the SimSun font from my server then PDFBox fallback on another font that is not able to render the characters). So i guess it is able to find the font and use it.</p></li>
  </ul>
  
  <h2>What I tried</h2>
  
  <p>I was able to make this work but I had to manually load the font in the code and add it to the PDF (see examples below).
  But that is not a solution as it means that I would have to load the font every time and add it the the PDF. I would also have to do the same for many other languages.</p>
  
  <p>As far as I understood, PDFBox should be able to use any fonts installed on the server.</p>
  
  <p>Below is a test class that tries 3 different approaches. Only the last one works so far:</p>
  
  <h3>Classic generation</h3>
  
  <p>Simply put Chinese characters inside the text field without changing anything.
  The characters are not rendered correctly (some of them are missing and the ones displayed does not match the input).</p>
  
  <p><a href="https://i.stack.imgur.com/EhAgs.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/EhAgs.png" alt="rendering-issue"></a></p>
  
  <h3>Generation with embedded font</h3>
  
  <p>Try to embed the SimSun font inside the PDF with the <code>PDResource.add(font)</code> method.
  The result is the same as the first method.</p>
  
  <h3>Embed the font and use it</h3>
  
  <p>I embed the SimSun font and <strong>I also override the font used in the TextField</strong> to use the SimSun font I just added.
  This approach works.</p>
  
  <p><a href="https://i.stack.imgur.com/J2kgc.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/J2kgc.png" alt="correct-render"></a></p>
  
  <p>After quite a few readings, I found out that the issue might come from the version of the font I am using.
  Windows 8 (which I use to create the form) <a href="https://docs.microsoft.com/en-us/typography/font-list/simsun" rel="nofollow noreferrer">uses v5.04 of Simsun font</a>.</p>
  
  <p>I use <strong>v2.10</strong> on my laptop and my servers, both being Linux based (I can not find the v5.04).</p>
  
  <p>However, I don't know:</p>
  
  <ul>
  <li>If the issue is really coming from this.</li>
  <li>If I have the right to use this font, as it is developed by Microsoft (and Apple).</li>
  <li>Where to find the latest version of it.</li>
  </ul>
  
  <p>I tried using another font but:</p>
  
  <ul>
  <li>I only find OTF fonts (and not TTF) that support Chinese characters.</li>
  <li>PDFBox does not support OTF (yet). <a href="https://issues.apache.org/jira/browse/PDFBOX-2482" rel="nofollow noreferrer">It is planed for v3.0.0</a>.</li>
  </ul>
  
  <p>So if someone has an idea on how to make this work without having to embed and change the font's name in the code, that would be great!</p>
  
  <p><a href="https://drive.google.com/open?id=176I16S06mXOEcIJxfaEuxUyK9d4cbB6K" rel="nofollow noreferrer">Here are the PDF I used</a> and the code that tests the 3 methods I talked about.
  The TextField in the pdf is named <code>comment</code>.</p>
  
  <pre class="lang-java prettyprint-override"><code>package org.test;
  
  import org.apache.pdfbox.cos.COSDictionary;
  import org.apache.pdfbox.cos.COSName;
  import org.apache.pdfbox.cos.COSString;
  import org.apache.pdfbox.pdmodel.PDDocument;
  import org.apache.pdfbox.pdmodel.PDResources;
  import org.apache.pdfbox.pdmodel.font.PDFont;
  import org.apache.pdfbox.pdmodel.font.PDType0Font;
  import org.apache.pdfbox.pdmodel.interactive.form.PDAcroForm;
  import org.apache.pdfbox.pdmodel.interactive.form.PDField;
  
  import java.io.File;
  import java.io.IOException;
  import java.io.InputStream;
  import java.util.regex.Matcher;
  import java.util.regex.Pattern;
  
  /**
   * Hello world!
   */
  public class App {
      private static final String SIMPLIFIED_CHINESE_STRING = "我不明白为什么它不起作用。";
  
      public static void main(String[] args) throws IOException {
          System.out.println("Hello World!");
          // Test 1
          classicGeneration();
  
          // Test 2
          generationWithEmbededFont();
  
          Test 3
          generationWithFontOverride();
          System.out.println("Bye!");
      }
  
      /**
       * Classic PDF generation without any changes to the PDF.
       */
      private static void classicGeneration() throws IOException {
          PDDocument document = loadPdf();
          PDAcroForm acroForm = document.getDocumentCatalog().getAcroForm();
          PDField commentField = acroForm.getField("comment");
          commentField.setValue(SIMPLIFIED_CHINESE_STRING);
          document.save(new File("result-classic-generation.pdf"));
      }
  
      /**
       * Trying to embed the font in the PDF. It doesn't seem to work.
       * The result is the same as classicGeneration method.
       */
      private static void generationWithEmbededFont() throws IOException {
          PDDocument document = loadPdf();
          PDAcroForm acroForm = document.getDocumentCatalog().getAcroForm();
  
          PDFont font = PDType0Font.load(document, new File("/usr/share/fonts/SimSun.ttf"));
          PDResources res = acroForm.getDefaultResources();
          if (res == null) {
              res = new PDResources();
          }
          COSName fontName = res.add(font);
          acroForm.setDefaultResources(res);
  
          PDField commentField = acroForm.getField("comment");
          commentField.setValue(SIMPLIFIED_CHINESE_STRING);
          document.save(new File("result-with-embeded-font.pdf"));
      }
  
      /**
       * Embed the font in the PDF and change the font used in the TextField to use this one.
       * Here the PDF is correctly rendered and all the characters are displayed.
       * @throws IOException
       */
      private static void generationWithFontOverride() throws IOException {
          PDDocument document = loadPdf();
          PDAcroForm acroForm = document.getDocumentCatalog().getAcroForm();
  
          PDField commentField = acroForm.getField("comment");
  
          // Load the font
          InputStream resourceAsStream = Thread.currentThread().getContextClassLoader().getResourceAsStream("SimSun.ttf");
          PDFont font = PDType0Font.load(document, resourceAsStream);
          PDResources res = acroForm.getDefaultResources();
          if (res == null) {
              res = new PDResources();
          }
          COSName fontName = res.add(font);
          acroForm.setDefaultResources(res);
  
          // Change the font used by the TextField
          COSDictionary dict = commentField.getCOSObject();
          COSString defaultAppearance = (COSString) dict.getDictionaryObject(COSName.DA);
          if (defaultAppearance != null) {
              String currentFont = dict.getString(COSName.DA);
  
              // Retrieve the current font size and color used for the field in order to use the same but with the new font.
              String regex = "[\\w]* ([\\w\\s]*)";
              Pattern pattern = Pattern.compile(regex);
              Matcher matcher = pattern.matcher(currentFont);
  
              // Default font size if we fail to extract the current one
              String fontSize = " 11 Tf";
  
              if (matcher.find()) {
                  fontSize = " " + matcher.group(1);
              }
  
              // Change the font of the TextField.
              dict.setString(COSName.DA, "/" + fontName.getName() + fontSize);
          }
  
          commentField.getCOSObject().addAll(dict);
  
          commentField.setValue(SIMPLIFIED_CHINESE_STRING);
          document.save(new File("result-with-font-override.pdf"));
      }
  
      // HELPER
  
      private static PDDocument loadPdf() throws IOException {
          InputStream stream = Thread.currentThread().getContextClassLoader().getResourceAsStream("sample.pdf");
          return PDDocument.load(stream);
      }
  
  }
  </code></pre>