FileOutputStream outputStream=null; try{ outputStream=new FileOutputStream(saveFilePath); PdfWriter writer = new PdfWriter(saveFilePath); PdfDocument pdfDoc = new PdfDocument(writer); pdfDoc.setDefaultPageSize(PageSize.A4); ConverterProperties converterProperties = new ConverterProperties(); FontProvider provider = new FontProvider(); provider.addStandardPdfFonts(); provider.addSystemFonts(); byte[] fontBytes = Files.readAllBytes(Paths.get("D:/home/file/static/fonts/SourceHanSansSC-Regular.otf")); PdfFont pdfFont = PdfFontFactory.createFont(fontBytes, PdfEncodings.IDENTITY_H, true); pdfDoc.addFont(pdfFont); converterProperties.setFontProvider(provider); converterProperties.setCharset("UTF-8"); pdfDoc.addEventHandler(PdfDocumentEvent.END_PAGE, new PDFBuilder(pdfFont,title,pdfTime,footLabel)); Document doc = HtmlConverter.convertToDocument(htmlData, pdfDoc, converterProperties); doc.close(); writer.close(); outputStream.close(); }catch(Exception ex){ throw new PDFException("PDF export to File fail",ex); }finally{ IOUtils.closeQuietly(outputStream); }字体文件有中文斜体字形,但是生成pdf时中文斜体样式没有效果,该怎么改
时间: 2024-02-14 22:08:09 浏览: 81
ioliu.rar_IO流 java_Java Reader Writer_java IO_stream
你可以在FontProvider中添加你的字体文件,然后在ConverterProperties中设置字体属性,例如:
```
FontProvider fontProvider = new FontProvider();
fontProvider.addFont("D:/home/file/static/fonts/SourceHanSansSC-Regular.otf");
ConverterProperties converterProperties = new ConverterProperties();
converterProperties.setFontProvider(fontProvider);
```
另外,你可以尝试在创建PdfFont对象时,将isEmbedded参数设置为false,例如:
```
PdfFont pdfFont = PdfFontFactory.createFont(fontBytes, PdfEncodings.IDENTITY_H, false);
```
这样可以让iText不对字体进行嵌入,以避免字体文件中缺少斜体字形的问题。
阅读全文