使用Java格式化String输出的字体

时间:2014-10-08 18:34:25

标签: html email fonts

我无法理解如何将字符串输出格式化为特定字体。我的脚本从摘要数据库表中接收一串文本,并嵌入到电子邮件中以进行传递。我是Java的新手,非常感谢您帮助将EMAIL_TEXT字符串转换为在Lucida Console中打印。我的脚本示例如下。

     if (r == null) {
  setOutputDone();
  return false;
 }  

        String originalFilePath= get(Fields.In, "css_imgAdd").getString(r);
        String cssImagePath= get(Fields.In, "css_imgAdd").getString(r);
        String PREFERRED_EMAIL = get(Fields.In,"EMAIL_TO").getString(r);
        String PREFERRED_EMAIL_CC = get(Fields.In,"EMAIL_CC").getString(r);
        String EMAIL_SUBJECT = get(Fields.In,"EMAIL_SUBJECT").getString(r);
        String EMAIL_TEXT = get(Fields.In,"EMAIL_TEXT").getString(r);
        String host_prop = get(Fields.In, "host").getString(r);
        String cssImgPath=cssImagePath;
        String port_prop = get(Fields.In, "port").getString(r);
        String mail_from_prop = get(Fields.In, "EMAIL_FROM").getString(r);
        String password_prop =get(Fields.In, "password").getString(r);
        String out = password_prop;
        String host = host_prop;
        String port =port_prop;
        String mailFrom =mail_from_prop;
        String password =out;
        String security_prop = get(Fields.In, "security").getString(r);
        String security=security_prop;
        String cssADD;
        String completeCssADD;
        String imgADD="";
        String completeImgADD;
        DateFormat dateFormat = new SimpleDateFormat("yyyy-MMM-dd");
        Date date = new Date();
        String curDate=dateFormat.format(date);
        String mailTo=PREFERRED_EMAIL;
        String[] mailToAdd = mailTo.split(",");
        String mailToCC=PREFERRED_EMAIL_CC;
        String[] mailToAddCC = mailToCC.split(",");
        String emailText=EMAIL_TEXT;
        String[] emailTextAdd = emailText.split("\n");
        String REPORT_NAME = get(Fields.In, "OUTPUT_FILE_PATHS").getString(r);
        String reportNameArray []=REPORT_NAME.split(",");
        String subject="";
        //Jlabel theSubject = new JLabel(email_text)
        //java.awt.Font theFont = new java.awt.Font("Courier", Font.PLAIN, 10);
        //theSubject.setFont(theFont);
        BufferedReader br;
        BufferedReader brCSS;
        FileReader fr;
        FileReader frCSS;
        String content="";
        String CssContent="";
        Document document1=null;
        String FILEPATH = get(Fields.In, "OUTPUT_FILE_PATHS").getString(r);
        String filePathArray []=FILEPATH.split(",");
        String mainContents = "";
        int j;
  try{

        for(j=0;j<filePathArray.length;j++)
    {

        subject="Reports - "+EMAIL_SUBJECT;
        fr=new FileReader(filePathArray[j]);
        br= new BufferedReader(fr);
        String s;
        content = "";
        String c;

    if(mainContents.contains("header-image.jpg")!=true)
         {
            mainContents += "<img src=header-image.jpg>";
            mainContents +="<h3>FROM :</h3>"+mailFrom;
            mainContents +="<h3>Sent:</h3>"+curDate;
            mainContents +="<h3>To:</h3>";
            for(int i=0;i<mailToAdd.length;i++)
                {
                        mainContents +=mailToAdd[i];
                        mainContents +=",";

                }
                    mainContents +="<h3>Cc:</h3>";
                for(int i=0;i<mailToAddCC.length;i++)
            {
                    mainContents +=mailToAddCC[i];
                    mainContents +=",";
            }

            mainContents +="<h3>Subject:</h3>";
            mainContents +="</br>";

//mainContents +="<h3>Email Text:</h3>";
for(int i=0;i<emailTextAdd.length;i++)
{
mainContents +=emailTextAdd[i]+"</br>";
}

         }
logBasic("----66666");
     //content = content + "<h3> Report Name :"+reportNameArray[j]+" </h3></br></br>";
    while(( s=br.readLine())!=null)
    {

     content=content+s;

    } 


    mainContents +=  content.replaceAll("style-","style-"+"test"+j);
logBasic("main content-------------"+mainContents);
    document1 = Jsoup.parse(content);
         Elements elementsStyle = document1.select("link");
         Element elementStyle = null;
        for(int i=0;i<elementsStyle.size();i++)
         {
         elementStyle = elementsStyle.get(i);
           String href = elementStyle.attr("href");
     //logBasic("hres-------"+href);
           String cssPath=cssImgPath+"/"+href;
     //logBasic("cssImgPath----->> "+cssImgPath);
     //logBasic("cssPath----->> "+cssPath);
          changeCss(cssPath,"test"+j);
      }

   }

       Document document = Jsoup.parse(mainContents);
        //logBasic("--------------->>>>>>"+document.html());

      Elements elements = document.select("img");
      Element element = null;
      Map inlineImages = new HashMap();
      for(int i=0;i<elements.size();i++){
       element = elements.get(i);
       String src = element.attr("src");
       element.attr("src", "cid:image"+i);
       element = elements.get(i);
       inlineImages.put("image"+i, cssImgPath+"/"+src);
      }

      Elements elementsStyle = document.select("link");
      Element elementStyle = null;
      for(int i=0;i<elementsStyle.size();i++)
  {
       elementStyle = elementsStyle.get(i);
       String href = elementStyle.attr("href");
       elementStyle.attr("href", "cid:style"+i);
       elementStyle = elementsStyle.get(i);
    inlineImages.put("style"+i, cssImgPath+"/"+href);
       }

      String html = document.html();
      System.out.print(html);

我试图改变字符串的格式,但我不确定这是否是正确的方法。

            //Jlabel theSubject = new JLabel(email_text)
            //java.awt.Font theFont = new java.awt.Font("Courier", Font.PLAIN, 10);
            //theSubject.setFont(theFont);

1 个答案:

答案 0 :(得分:0)

阅读这篇文章,然后我想你会很清楚。

http://www.javaworld.com/article/2076250/core-java/printing-in-java--part-2.html

也许从第1部分开始,特别注意处理文本和字体部分。