工作中 经常会遇到文件在线预览的问题,如果仅仅是预览的话,可以通过技术手段,将文件统一转化为pdf,然后使用浏览器针对pdf的预览功能即可。
开始看代码
1 、引入坐标依赖
<dependency> <groupId>org.apache.pdfbox</groupId> <artifactId>pdfbox</artifactId> <version>2.0.24</version> </dependency> <dependency> <groupId>org.apache.poi</groupId> <artifactId>poi</artifactId> <version>4.1.2</version> </dependency> <dependency> <groupId>org.apache.poi</groupId> <artifactId>poi-ooxml</artifactId> <version>4.1.2</version> </dependency> <dependency> <groupId>fr.opensagres.xdocreport</groupId> <artifactId>org.apache.poi.xwpf.converter.pdf</artifactId> <version>2.0.2</version> </dependency>
2 、代码实现
import java.io.File; import java.io.FileInputStream; import java.io.FileOutputStream; import org.apache.pdfbox.pdmodel.PDDocument; import org.apache.pdfbox.pdmodel.PDPage; import org.apache.pdfbox.pdmodel.PDPageContentStream; import org.apache.pdfbox.pdmodel.common.PDRectangle; import org.apache.pdfbox.rendering.PDFRenderer; import org.apache.pdfbox.tools.PDFToImage; import org.apache.pdfbox.tools.imageio.ImageIOUtil; import org.apache.poi.hwpf.HWPFDocument; import org.apache.poi.hwpf.converter.PDFConverter; import org.apache.poi.hwpf.usermodel.Range; import org.apache.poi.xwpf.converter.pdf.PdfConverter; import org.apache.poi.xwpf.converter.pdf.PdfOptions; import org.apache.poi.xwpf.usermodel.XWPFDocument; public class FileToPdfConverter { public static void main(String[] args) { try { // 读入需要转换的文件 File inputFile = new File("input.doc"); FileInputStream inStream = new FileInputStream(inputFile); // 创建pdf输出文件 File outputFile = new File("output.pdf"); FileOutputStream outStream = new FileOutputStream(outputFile); // 创建pdf文档 PDDocument document = new PDDocument(); // 根据文件类型选择转换方法 if (inputFile.getName().endsWith(".doc")) { // 将doc文件转换为pdf文件 HWPFDocument doc = new HWPFDocument(inStream); Range range = doc.getRange(); PdfConverter.getInstance().convert(range, document); } else if (inputFile.getName().endsWith(".docx")) { // 将docx文件转换为pdf文件 XWPFDocument doc = new XWPFDocument(inStream); PdfOptions options = PdfOptions.create(); PdfConverter.getInstance().convert(doc, document, options); } else if (inputFile.getName().endsWith(".txt")) { // 将txt文件转换为pdf文件 PDPage page = new PDPage(PDRectangle.A4); document.addPage(page); PDPageContentStream contentStream = new PDPageContentStream(document, page); FileInputStream inputStream = new FileInputStream(inputFile); int data = inputStream.read(); while(data != -1) { contentStream.beginText(); contentStream.showText(String.valueOf((char)data)); contentStream.endText(); contentStream.newLineAtOffset(0, -12); data = inputStream.read(); } inputStream.close(); contentStream.close(); } else if (inputFile.getName().endsWith(".jpg") || inputFile.getName().endsWith(".jpeg") || inputFile.getName().endsWith(".png")) { // 将图片文件转换为pdf文件 PDPage page = new PDPage(); document.addPage(page); PDFRenderer renderer = new PDFRenderer(document); PDFToImage pdfToImage = new PDFToImage(); pdfToImage.setImageType("png"); pdfToImage.setOutputPrefix("tmp"); pdfToImage.setPdfFile(inputFile.getAbsolutePath()); pdfToImage.startConversion(renderer, 1, 1); File tmpFile = new File("tmp-1.png"); ImageIOUtil.writeImage(renderer.renderImage(0), "tmp-1.png", 300); PDPageContentStream contentStream = new PDPageContentStream(document, page); contentStream.drawImage(PDImageXObject.createFromFile(tmpFile.getAbsolutePath(), document), 0, 0, PDRectangle.A4.getWidth(), PDRectangle.A4.getHeight()); contentStream.close(); tmpFile.delete(); } else { System.out.println("不支持的文件类型"); return; } // 保存pdf文件并关闭输入输出流 document.save(outStream); document.close(); inStream.close(); outStream.close(); } catch (Exception e) { e.printStackTrace(); } } }