public void appendToDoc(
      InputStream input,
      OutputStream output,
      Map keyMap,
      Map signMap,
      String html,
      String frmt,
      byte[] empSign,
      byte[] rimDirSign,
      byte[] sjrlogo,
      byte[] srvcCntr,
      SearchAndReplaceCallBack callback)
      throws Exception {
    Document doc = null;

    if (input == null) {
      doc = convertHtml(html, "potriat", null, "normal", "doc", null, srvcCntr, null, true);
    } else if (srvcCntr != null) {
      doc = AsposeLic.createDocument(new ByteArrayInputStream(srvcCntr));
      Document srcDoc = AsposeLic.createDocument(input);
      srcDoc.getFirstSection().getPageSetup().setSectionStart(SectionStart.CONTINUOUS);
      doc.appendDocument(srcDoc, ImportFormatMode.USE_DESTINATION_STYLES);
    } else {
      doc = AsposeLic.createDocument(input);
    }

    Map<String, Object> map = AsposeUtility.getKeyTemplates();
    AsposeUtility.InsertDocumentAtReplaceHandler handler =
        new AsposeUtility.InsertDocumentAtReplaceHandler();

    for (Map.Entry<String, Object> entry : map.entrySet()) {
      if (keyMap.containsKey(entry.getKey())) {

        String key = entry.getKey();
        Object val = keyMap.get(key);
        if (val instanceof InputStream) {
          AsposeUtility.replaceDoc(
              doc,
              "\\[\\[\\[" + key + "\\]\\]\\]",
              (InputStream) val,
              (key.contains("DRAFT") ? true : false),
              handler);
        } else if (val instanceof String) {
          AsposeUtility.replaceHtmlToDoc(
              (String) val,
              doc,
              "\\[\\[\\[" + key + "\\]\\]\\]",
              (key.contains("DRAFT") ? true : false),
              handler);
        }
      }
    }
    replaceSignatures(doc, empSign, rimDirSign, sjrlogo, callback);

    AsposeUtility.replaceHtmlVariables(doc, keyMap);

    AsposeUtility.replaceVariables(doc, signMap);

    AsposeUtility.cleanupDoc(doc);

    if (frmt != null && frmt.equals("doc")) {
      doc.save(output, SaveFormat.DOC);
    } else {
      doc.save(output, SaveFormat.PDF);
    }
  }
  public ByteArrayOutputStream createPdf(
      String lttrTxt,
      Map signMap,
      byte[] lttrHd,
      byte[] empSign,
      byte[] rimDirSign,
      byte[] sjrlogo,
      boolean lttr,
      SearchAndReplaceCallBack callback,
      String frmt)
      throws Exception {
    Document doc = null;
    if (lttrHd == null) {
      doc = AsposeLic.createDocument();
    } else {
      InputStream docStream = new ByteArrayInputStream(lttrHd);

      doc = AsposeLic.createDocument(docStream);
    }

    if (lttr) {
      DocumentBuilder builder = new DocumentBuilder(doc);
      builder.insertHtml(lttrTxt);
    } else {
      doc.getMailMerge().setFieldMergingCallback(new FieldMergingCallBack());

      // Execute simple mail merge.
      doc.getMailMerge().execute(new String[] {"PostCardText1"}, new Object[] {lttrTxt});
      doc.getMailMerge().deleteFields();
    }

    AsposeUtility.replaceVariables(doc, signMap);

    replaceSignatures(doc, empSign, rimDirSign, sjrlogo, callback);

    ByteArrayOutputStream bas = new ByteArrayOutputStream();
    if (frmt != null && frmt.equals("doc")) {
      doc.save(bas, SaveFormat.DOC);
    } else {
      doc.save(bas, SaveFormat.PDF);
    }
    return bas;
  }
  public static Document convertHtml(
      String input,
      String orien,
      String wmark,
      String margins,
      String format,
      Map map,
      byte[] srvcCntr,
      String header,
      boolean footer)
      throws Exception {
    Document doc = null;
    if (srvcCntr != null) {
      doc = AsposeLic.createDocument(new ByteArrayInputStream(srvcCntr));
    } else {
      doc = AsposeLic.createDocument();
    }
    FontSettings.setDefaultFontName("Arial");
    if (orien.equals("potriat")) {
      doc.getSections().get(0).getPageSetup().setOrientation(Orientation.PORTRAIT);
    } else {
      doc.getSections()
          .get(0)
          .getPageSetup()
          .setOrientation(com.aspose.words.Orientation.LANDSCAPE);
    }
    for (Section section : doc.getSections()) {
      if (margins != null && margins.equals("narrow")) {
        section.getPageSetup().setLeftMargin(ConvertUtil.inchToPoint(0.5));
        section.getPageSetup().setRightMargin(ConvertUtil.inchToPoint(0.5));
        section.getPageSetup().setTopMargin(ConvertUtil.inchToPoint(0.5));
        section.getPageSetup().setBottomMargin(ConvertUtil.inchToPoint(0.5));
      } else if (margins != null && margins.equals("moderate")) {
        section.getPageSetup().setLeftMargin(ConvertUtil.inchToPoint(0.75));
        section.getPageSetup().setRightMargin(ConvertUtil.inchToPoint(0.75));
        section.getPageSetup().setTopMargin(ConvertUtil.inchToPoint(1));
        section.getPageSetup().setBottomMargin(ConvertUtil.inchToPoint(1));
      }
    }
    if (wmark != null) {
      AsposeUtility.insertWatermarkText(doc, "DRAFT");
    }
    DocumentBuilder builder = new DocumentBuilder(doc);
    builder.getFont().setName("Arial");
    builder.getFont().setSize(11);
    DocumentHelper helper = new DocumentHelper(builder);
    helper.insertHtmlWithBuilderFormatting(input);
    // builder.insertHtml(input);
    builder.getParagraphFormat().setSpaceAfterAuto(false);
    builder.getParagraphFormat().setSpaceAfter(0);
    //        NodeImporter importer = new NodeImporter(builder.getDocument(), doc,
    // ImportFormatMode.KEEP_SOURCE_FORMATTING) ;
    if (header != null) {
      builder.moveToHeaderFooter(HeaderFooterType.HEADER_PRIMARY);
      builder.getFont().setSize(9);
      helper.insertHtmlWithBuilderFormatting(header);
      // builder.insertHtml(header);
    }

    if (footer) {
      builder.moveToHeaderFooter(HeaderFooterType.FOOTER_PRIMARY);
      builder.getFont().setName("Arial");
      builder.getFont().setSize(10);
      // Insert page numbering text here.
      // It uses PAGE and NUMPAGES fields to auto calculate current page number and total number of
      // pages.
      builder.write("Page ");
      builder.insertField("PAGE", "");
      builder.write(" of ");
      builder.insertField("NUMPAGES", "");
      // Align this text to the left.
      builder.getCurrentParagraph().getParagraphFormat().setAlignment(ParagraphAlignment.CENTER);
      //         builder.moveToDocumentEnd();
    }
    // Loop through all paragraphs and reset auto spacing.
    NodeCollection<Paragraph> paragraphs = doc.getChildNodes(NodeType.PARAGRAPH, true);
    for (Paragraph paragraph : paragraphs) {
      paragraph.getParagraphFormat().setSpaceAfterAuto(false);
      paragraph.getParagraphFormat().setSpaceAfter(0);
    }

    return doc;
  }