# office_preview **Repository Path**: cnbbx_com/office_preview ## Basic Information - **Project Name**: office_preview - **Description**: java office在线预览 使用 openoffice 基于SpringBoot2.2.2版本 - **Primary Language**: Java - **License**: Apache-2.0 - **Default Branch**: master - **Homepage**: None - **GVP Project**: No ## Statistics - **Stars**: 13 - **Forks**: 4 - **Created**: 2019-12-21 - **Last Updated**: 2024-11-30 ## Categories & Tags **Categories**: doc-tools **Tags**: None ## README # Getting Started ### Reference Documentation For further reference, please consider the following sections: * [Official Apache Maven documentation](https://maven.apache.org/guides/index.html) * [Spring Boot Maven Plugin Reference Guide](https://docs.spring.io/spring-boot/docs/2.2.2.RELEASE/maven-plugin/) ~~~java public class DocumentUtils { /** * 使用 JODConverter 将 Office 文档转换成 PDF 文件 * * @param inFilePath Office 文档路径 * @param outFilePath 生成的 PDF 文件路径 * @throws IOException */ public static void convertOfficeDocumentToPDF(String inFilePath, String outFilePath) /** * 使用 JODConverter 将 Office 文档输入流转换成 PDF 输出流 * * @param inputStream 输入流 * @param inputFormat 输入流的文档格式 * @param outputStream 输出流 * @param outputFormat 输出流的文档格式 * @throws IOException */ public static void convert(InputStream inputStream, DocumentFormat inputFormat, OutputStream outputStream, DocumentFormat outputFormat) } ~~~ ~~~java final DocumentFormat pdf = new DocumentFormat("Portable Document Format", "application/pdf", "pdf"); pdf.setExportFilter(DocumentFamily.DRAWING, "draw_pdf_Export"); pdf.setExportFilter(DocumentFamily.PRESENTATION, "impress_pdf_Export"); pdf.setExportFilter(DocumentFamily.SPREADSHEET, "calc_pdf_Export"); pdf.setExportFilter(DocumentFamily.TEXT, "writer_pdf_Export"); final DocumentFormat xls = new DocumentFormat("Microsoft Excel", DocumentFamily.SPREADSHEET, "application/vnd.ms-excel", "xls"); xls.setExportFilter(DocumentFamily.SPREADSHEET, "MS Excel 97"); final DocumentFormat xlsx = new DocumentFormat("Microsoft Excel 2007 XML", DocumentFamily.SPREADSHEET, "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet", "xlsx"); xlsx.setExportFilter(DocumentFamily.SPREADSHEET, "MS Excel 2007"); final DocumentFormat ppt = new DocumentFormat("Microsoft PowerPoint", DocumentFamily.PRESENTATION, "application/vnd.ms-powerpoint", "ppt"); ppt.setExportFilter(DocumentFamily.PRESENTATION, "MS PowerPoint 97"); final DocumentFormat pptx = new DocumentFormat("Microsoft PowerPoint 2007 XML", DocumentFamily.PRESENTATION, "application/vnd.openxmlformats-officedocument.presentationml.presentation", "pptx"); pptx.setExportFilter(DocumentFamily.PRESENTATION, "MS PowerPoint 2007"); final DocumentFormat doc = new DocumentFormat("Microsoft Word", DocumentFamily.TEXT, "application/msword", "doc"); doc.setExportFilter(DocumentFamily.TEXT, "MS Word 97"); final DocumentFormat docx = new DocumentFormat("Microsoft Word 2007 XML", DocumentFamily.TEXT, "application/vnd.openxmlformats-officedocument.wordprocessingml.document", "docx"); docx.setExportFilter(DocumentFamily.TEXT, "MS Word 2007"); BufferedInputStream bis = null; try { //获取远程服务器端文件输入流; bis = new BufferedInputStream(urlconn.getInputStream()); DocumentFormat autoDoc = xls; System.out.println("file type: " + urlconn.getContentType()); switch (urlconn.getContentType()) { case "application/vnd.ms-excel": autoDoc = xls; break; case "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet": autoDoc = xlsx; break; case "application/vnd.ms-powerpoint": autoDoc = ppt; break; case "application/vnd.openxmlformats-officedocument.presentationml.presentation": autoDoc = pptx; break; case "application/msword": autoDoc = doc; break; case "application/vnd.openxmlformats-officedocument.wordprocessingml.document": autoDoc = docx; break; } OutputStream os = response.getOutputStream(); DocumentUtils.convert(bis, autoDoc, os, pdf); ~~~