java word转Pdf 指定字体库

 /**
     * 文件类型转换
     *
     * @param srcFile    src文件
     * @param saveFormat {@link SaveFormat} 保存格式
     * @return {@link byte[]} 字节数组
     * @throws WatermarkException 水印异常
     */
    private static byte[] convertWord(File srcFile) throws WatermarkException {
        LicenseUtils.verificationLicense();
        ByteArrayOutputStream os = null;
        try {
            setFonts();
            os = new ByteArrayOutputStream(1024);
            Document doc = new Document(srcFile.getPath());

            // 40 - PDF
            doc.save(os, 40);
            return os.toByteArray();
        } catch (Exception e) {
            logger.error("convertWord {}", e.getMessage());
            throw new WatermarkException(e.getMessage());
        }finally {
            IoUtil.close(os);
        }
    }

 /**

* 自定字体库

*/
 private static void setFonts(){
         String osType=String.valueOf(OSInfo.getOSType);
          String fonts [] = null;
            /**
             * 适配字体文件
             */
            if(osType.equals("Linux")){
                //      /usr/share/fonts/chinese/  存放字体文件夹
                fonts = new String[]{"/usr/share/fonts/chinese/","/usr/share/fonts/simsun/","/usr/share/fonts/black/","/usr/share/fonts/wingdings/"};
            }else if(osType.equals("Windows")){
                fonts = new String[]{"/Users/fonts/chinese/","/Users/fonts/simsun/","/Users/fonts/black/","/Users/fonts/wingdings/"};
            }
            FontSettings.setFontsFolders(fonts, true);
}

/**
 * 许可证工具类
 *
 * @author Rong.Jia
 * @date 2021/08/29
 */
public class LicenseUtils {

    private static final Logger logger = LoggerFactory.getLogger(LicenseUtils.class);
    private static final String LICENSE_XML = "license.xml";
    private static final AtomicBoolean LICENSE = new AtomicBoolean(Boolean.FALSE);

    static {
        try {
            License wordLicense = new License();
            com.aspose.cells.License cellsLicense = new com.aspose.cells.License();
            com.aspose.slides.License slideLicense = new com.aspose.slides.License();
            com.aspose.pdf.License pdLicense = new com.aspose.pdf.License();
            cellsLicense.setLicense(LicenseUtils.class.getClassLoader().getResourceAsStream(LICENSE_XML));
            pdLicense.setLicense(LicenseUtils.class.getClassLoader().getResourceAsStream(LICENSE_XML));
            slideLicense.setLicense(LicenseUtils.class.getClassLoader().getResourceAsStream(LICENSE_XML));
            wordLicense.setLicense(LicenseUtils.class.getClassLoader().getResourceAsStream(LICENSE_XML));
            LICENSE.compareAndSet(Boolean.FALSE, Boolean.TRUE);
        } catch (Exception e) {
            LICENSE.compareAndSet(Boolean.FALSE, Boolean.FALSE);
            logger.error("License验证失败... ", e);
        }
    }

    /**
     * 验证许可证
     */
    public static void verificationLicense() {
        Assert.isTrue(LICENSE.get(), "License验证不通过...");
    }
  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
根据提供的引用内容,可以得知在Linux下进行wordPDF时,可能会出现中文乱码的问题,原因是缺少一些字体。而Java作为一种跨平台的编程语言,同样也可能会出现这个问题。解决方法如下: 1.安装所需字体 在Linux系统中,可以通过以下命令安装中文字体: ```shell sudo apt-get install ttf-wqy-zenhei ``` 2.使用iText库进行换 iText是一个开源的Java PDF库,可以用于创建、修改和维护PDF文档。使用iText库进行wordPDF,可以避免中文乱码的问题。以下是一个简单的示例代码: ```java import com.itextpdf.text.Document; import com.itextpdf.text.Paragraph; import com.itextpdf.text.pdf.PdfWriter; import org.apache.poi.hwpf.HWPFDocument; import org.apache.poi.hwpf.converter.PDFConverter; import org.apache.poi.hwpf.usermodel.Range; import java.io.*; public class WordToPdf { public static void main(String[] args) { String inputFile = "input.doc"; String outputFile = "output.pdf"; try { InputStream input = new FileInputStream(new File(inputFile)); HWPFDocument wordDocument = new HWPFDocument(input); Document document = new Document(); PdfWriter.getInstance(document, new FileOutputStream(outputFile)); document.open(); Range range = wordDocument.getRange(); document.add(new Paragraph(range.text())); document.close(); input.close(); System.out.println("换成功!"); } catch (Exception e) { e.printStackTrace(); } } } ``` 以上代码使用Apache POI库读取word文档,然后使用iText库将其换为PDF文档。 3.使用JODConverter进行换 JODConverter是一个开源的Java库,可以将Office文档换为PDF、HTML、ODF等格式。以下是一个简单的示例代码: ```java import org.artofsolving.jodconverter.OfficeDocumentConverter; import org.artofsolving.jodconverter.office.DefaultOfficeManagerConfiguration; import org.artofsolving.jodconverter.office.OfficeManager; import java.io.File; public class WordToPdf { public static void main(String[] args) { String inputFile = "input.doc"; String outputFile = "output.pdf"; OfficeManager officeManager = null; try { officeManager = new DefaultOfficeManagerConfiguration().buildOfficeManager(); officeManager.start(); OfficeDocumentConverter converter = new OfficeDocumentConverter(officeManager); converter.convert(new File(inputFile), new File(outputFile)); System.out.println("换成功!"); } catch (Exception e) { e.printStackTrace(); } finally { if (officeManager != null) { officeManager.stop(); } } } } ``` 以上代码使用JODConverterword文档换为PDF文档。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值