コード例 #1
0
  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;
  }