Java Font 怎么让生成的文字和WPS文档里的一样?

img

img

img

前面两个demo是通过java的Font生成的字体(new Font("黑体", Font.PLAIN, 21) / new Font("黑体", Font.BOLD, 21))。

但是很明显,跟第三个在文档上写出来的字不一样。

请问有没有办法让java生成的问题变得跟里的一样?

++++++
第一次更新
设置抗锯齿以后的效果:

img


文字的长、高比例 和 文字之间的间距仍然不一样(感觉文档那里展示的文字,长、高比例是1:1?),请问还有什么地方需要设置呢?
ps:文字之间的间距目前使用了TextAttribute相关来做了设置

这是没开启抗锯齿的功能:增加一下设置,就可以了

Graphics2D g2d = (Graphics2D) graphics;
        g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
        g2d.setRenderingHint(RenderingHints.KEY_TEXT_ANTIALIASING, RenderingHints.VALUE_TEXT_ANTIALIAS_ON);
        g2d.setRenderingHint(RenderingHints.KEY_RENDERING, RenderingHints.VALUE_RENDER_QUALITY);
g2d.drawString(。。。。。。)

用java实现将html保存为word时,生成的word字体显示样式和html中的相同。请问,怎样改变字体样式
java源代码如下:

package format.conversion;

import java.io.BufferedReader;
import java.io.ByteArrayInputStream;
import java.io.File;
import java.io.FileOutputStream;
import java.io.FileReader;
import java.io.IOException;

import javax.swing.JFileChooser;
import javax.swing.filechooser.FileNameExtensionFilter;

import org.apache.poi.poifs.filesystem.DirectoryEntry;
import org.apache.poi.poifs.filesystem.DocumentEntry;
import org.apache.poi.poifs.filesystem.POIFSFileSystem;

public class HtmlToDoc {

public static void main(String[] args) throws Exception {
HtmlToDoc test=new HtmlToDoc();
test.go();
}

public void go(){
try{
String FilePath="C:/Tomcat/apache-tomcat-7.0.42/webapps/Export-Report/Report.html";
String url = new File(FilePath).toURI().toURL().toString();
System.out.println(url);
JFileChooser fileSave=new JFileChooser(".");

FileNameExtensionFilter extension=new FileNameExtensionFilter("doc files(.doc)","doc");
fileSave.setFileFilter(extension);

fileSave.showSaveDialog(null);
File file=fileSave.getSelectedFile();
if(!file.getPath().endsWith(".doc")){
file=new File(file.getPath()+".doc");
}
String outputFile =file.toString();
writeWordFile(FilePath,outputFile);
System.out.println("word文件保存成功!");
System.out.println("文件保存路径为:"+new File(outputFile).toURI().toURL());
}catch(Exception ex){
System.out.println("word文件保存失败!");
}

}

public boolean writeWordFile(String filepath,String outputFile) throws Exception {
boolean flag = false;
ByteArrayInputStream bais = null;
FileOutputStream fos = null;
try {
if (!"".equals(outputFile)) {

String content = readFile(filepath);
byte b[] = content.getBytes();
bais = new ByteArrayInputStream(b);
POIFSFileSystem poifs = new POIFSFileSystem();
DirectoryEntry directory = poifs.getRoot();
DocumentEntry documentEntry = directory.createDocument("WordDocument", bais);
fos = new FileOutputStream(outputFile);
poifs.writeFilesystem(fos);
bais.close();
fos.close();

}

} catch (IOException e) {
e.printStackTrace();
} finally {
if(fos != null) fos.close();
if(bais != null) bais.close();
}
return flag;
}

public String readFile(String filename) throws Exception {
StringBuffer buffer = new StringBuffer("");
BufferedReader br = null;
try {
br = new BufferedReader(new FileReader(filename));
buffer = new StringBuffer();
while (br.ready())
buffer.append((char) br.read());
} catch (Exception e) {
e.printStackTrace();
} finally {
if(br!=null) br.close();
}
return buffer.toString();
}
}
// 全局字体抗锯齿,必须在初始化 JFrame 之前调用!
static void enableAntiAliasing() {
    System.setProperty("awt.useSystemAAFontSettings", "on");
    System.setProperty("swing.aatext", "true");
}