/* Unlike the shared code version, this expects a base file name -
   * not a full path name.
   * The font configuration file has base file names and the FontConfiguration
   * class reports these back to the GraphicsEnvironment, so these
   * are the componentFileNames of CompositeFonts.
   */
  protected void registerFontFile(
      String fontFileName, String[] nativeNames, int fontRank, boolean defer) {

    // REMIND: case compare depends on platform
    if (registeredFontFiles.contains(fontFileName)) {
      return;
    }
    registeredFontFiles.add(fontFileName);

    int fontFormat;
    if (ttFilter.accept(null, fontFileName)) {
      fontFormat = FontManager.FONTFORMAT_TRUETYPE;
    } else if (t1Filter.accept(null, fontFileName)) {
      fontFormat = FontManager.FONTFORMAT_TYPE1;
    } else {
      /* on windows we don't use/register native fonts */
      return;
    }

    if (fontPath == null) {
      fontPath = getPlatformFontPath(noType1Font);
    }

    /* Look in the JRE font directory first.
     * This is playing it safe as we would want to find fonts in the
     * JRE font directory ahead of those in the system directory
     */
    String tmpFontPath = jreFontDirName + File.pathSeparator + fontPath;
    StringTokenizer parser = new StringTokenizer(tmpFontPath, File.pathSeparator);

    boolean found = false;
    try {
      while (!found && parser.hasMoreTokens()) {
        String newPath = parser.nextToken();
        File theFile = new File(newPath, fontFileName);
        if (theFile.canRead()) {
          found = true;
          String path = theFile.getAbsolutePath();
          if (defer) {
            FontManager.registerDeferredFont(
                fontFileName, path, nativeNames, fontFormat, true, fontRank);
          } else {
            FontManager.registerFontFile(path, nativeNames, fontFormat, true, fontRank);
          }
          break;
        }
      }
    } catch (NoSuchElementException e) {
      System.err.println(e);
    }
    if (!found) {
      addToMissingFontFileList(fontFileName);
    }
  }
 /**
  * Indicates a preference for proportional over non-proportional (e.g. dual-spaced CJK fonts)
  * fonts in the mapping of logical fonts to physical fonts. If the default mapping contains fonts
  * for which proportional and non-proportional variants exist, then calling this method indicates
  * the mapping should use a proportional variant.
  *
  * <p>The actual change in font rendering behavior resulting from a call to this method is
  * implementation dependent; it may have no effect at all. The behavior may differ between font
  * rendering in lightweight and peered components. Since calling this method requests a different
  * font, clients should expect different metrics, and may need to recalculate window sizes and
  * layout. Therefore this method should be called before user interface initialisation.
  *
  * @since 1.5
  */
 public void preferProportionalFonts() {
   sun.font.FontManager.preferProportionalFonts();
 }
 /**
  * Indicates a preference for locale-specific fonts in the mapping of logical fonts to physical
  * fonts. Calling this method indicates that font rendering should primarily use fonts specific to
  * the primary writing system (the one indicated by the default encoding and the initial default
  * locale). For example, if the primary writing system is Japanese, then characters should be
  * rendered using a Japanese font if possible, and other fonts should only be used for characters
  * for which the Japanese font doesn't have glyphs.
  *
  * <p>The actual change in font rendering behavior resulting from a call to this method is
  * implementation dependent; it may have no effect at all, or the requested behavior may already
  * match the default behavior. The behavior may differ between font rendering in lightweight and
  * peered components. Since calling this method requests a different font, clients should expect
  * different metrics, and may need to recalculate window sizes and layout. Therefore this method
  * should be called before user interface initialisation.
  *
  * @since 1.5
  */
 public void preferLocaleFonts() {
   sun.font.FontManager.preferLocaleFonts();
 }
  public static InterviewCandidateMaterial pdf(
      HttpServletRequest request, AddressBook addressbook) {
    // TODO Auto-generated method stub
    log.log(Level.INFO, "ApplicationFormPDFMaintenance --> pdf ");
    Document document = new Document(PageSize.A4, 50, 50, 50, 50);
    // Font myAdobeTypekit =
    // FontFactory.getFont(request.getRealPath(File.separator)+"resources"+File.separator+"HDZB_35.TTF", BaseFont.IDENTITY_H, BaseFont.NOT_EMBEDDED);

    try {
      // String fontPath = "C:/Windows/Fonts";

      String fontPath = request.getRealPath("/") + "resources" + File.separator + "hxb-meixinti";
      File f2 = new File(fontPath + "/hxb-meixinti.ttf");
      BaseFont bf1 = null;
      if (f2.exists()) {
        bf1 =
            BaseFont.createFont(
                fontPath + "/hxb-meixinti.ttf", BaseFont.IDENTITY_H, BaseFont.EMBEDDED);
      } else {
        f2 = new File(fontPath + "/hxb-meixinti.ttf");

        if (f2.exists()) {
          System.out.println("File existed");
          bf1 =
              BaseFont.createFont(
                  fontPath + "hxb-meixinti.ttf", BaseFont.IDENTITY_V, BaseFont.EMBEDDED);
        } else {
          bf1 =
              BaseFont.createFont(
                  FontManager.getFontPath(true) + "/hxb-meixinti.ttf",
                  BaseFont.IDENTITY_H,
                  BaseFont.EMBEDDED);
        }
      }

      Font normalFontCH = new Font(bf1, 15);

      String fileName =
          request.getRealPath("/") + "resources" + File.separator + "upload" + File.separator;
      DateFormat df = new SimpleDateFormat("yyyy-MM-dd");
      Date d1 = new Date();
      String time = (d1.getTime() + "").trim();
      String str = df.format(new Date()) + time;

      PdfWriter writer =
          PdfWriter.getInstance(
              document, new FileOutputStream(fileName + "ApplicationForm_" + str + ".pdf"));
      InterviewCandidateMaterial material = new InterviewCandidateMaterial();
      material.setMaterialFileName("ApplicationForm_" + str + ".pdf");
      material.setCandidateCode(Integer.parseInt(request.getParameter("candidateCode")));
      material.setInterviewCode(Integer.parseInt(request.getParameter("interviewCode")));

      // PdfWriter writer = PdfWriter.getInstance(document, new
      // FileOutputStream("E://applicatonForm2.pdf"));
      document.open();

      addTitle(document, writer, "formHeaderTitle", "Application Form");
      document = personalInformation(document, writer, addressbook, normalFontCH);
      document = familyInformation(document, writer, addressbook);
      document = workExperience(document, writer, addressbook);
      document = Education(document, writer, addressbook);
      document = personalCertification(document, writer, addressbook);
      document = ESingnature(document, writer, addressbook);

      document.close();

      ByteArrayOutputStream bos = new ByteArrayOutputStream();
      File file = new File(fileName + "ApplicationForm_" + str + ".pdf");
      FileInputStream fis = new FileInputStream(file);
      byte[] buf = new byte[1024];
      for (int readNum; (readNum = fis.read(buf)) != -1; ) {
        bos.write(buf, 0, readNum); // no doubt here is 0
      }
      material.setFormContent(bos.toByteArray());
      material.setCreatedDate(new Date());
      material.setMaterialDescrption("AppicationForm");

      //  applicationForm.setFormContent(bos.toByteArray());

      return material;
    } catch (Exception e) {
      log.log(Level.INFO, "ApplicationFormPDFMaintenance --> pdf --> Exception  " + e.getMessage());
      e.printStackTrace();
      e.printStackTrace();
      LogsMaintenance logsMain = new LogsMaintenance();
      StringWriter errors = new StringWriter();
      e.printStackTrace(new PrintWriter(errors));
      logsMain.insertLogs("ApplicationFormPDFMaintenance", Level.SEVERE + "", errors.toString());
    }

    return null;
  }