-
最近项目遇到一个问题,pdf文件需要转换为图片,但是转出来的图片缺少字体导致转出来的图片中的字是方块
-
使用的是aspose.pdf,然后使用其中api把文件转成图片
-
但是由于缺少字体,导致转出来的图片不理想,然后查了官方api,存在设置转出来的图片默认字体
-
代码如下
Document pdfDocument = new Document(_dataDir + "input.pdf");
FileOutputStream imageStream = null;;
try {
imageStream = new FileOutputStream(_dataDir + "SetDefaultFontName.png");
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
//此处为设置清晰度
Resolution resolution = new Resolution(300);
PngDevice pngDevice = new PngDevice(resolution);
RenderingOptions ro = new RenderingOptions();
//此处为设置默认字体
ro.setDefaultFontName ("Arial");
pngDevice.setRenderingOptions(ro);
pngDevice.process(pdfDocument.getPages().get_Item(1), imageStream);
ArrayList<FontSourceBase> fontSources = new ArrayList<>();
String fontPath = new ApplicationHome().getDir().getPath() + File.separator + "template" + File.separator + "font";
FolderFontSource fontSource = new FolderFontSource(fontPath, true);
Workbook workbook=new Workbook();
Style style=workbook.Styles[workbook.Styles.Add()];
WorkSheet worksheet=workbook.WorkSheet[0];
//字体样式
style.Font.Color = Color.Red;//字体颜色
style.Font.Size = 10;//字体大小
style.Font.IsB.
(1)使mkfontscale和mkfontdir命令正常运行
yum install mkfontscale
(2)使fc-cache命令正常运行。如果提示 fc-cache: command not found
yum install fontconfig
liunx下安装字体