本文共 4193 字,大约阅读时间需要 13 分钟。
2021SC@SDUSC
CAD文件,全程Computer Aided Design,即计算机辅助设计文件。CAD文件专门存储2D或者3D的设计,由专门CAD软件如AutoCAD,SolidWorks等创建而成。
和word文档一样,CAD文件的浏览格式有两种:图片预览和pdf预览。该项目里暂时只实现了pdf预览。
目录
CAD文件预处理
首先来看一下,系统是如何加载CAD文件的,在转换之前要做哪些转换工作:
对cad文件进行预处理的方法filePreviewHandle封装在CadFilePreviewImpl中,继承自FilePreview中。首先浏览文件属性,获取文件类型CAD、url、文件名、转换后的pdf名、输出路径等消息。
之后就是判断之前是否转换过,若转换过直接执行返回,若还没转换,先将文件下载下来,调用fileHandlerService的cadToPdf方法将cad文件转换成pdf。
public String filePreviewHandle(String url, Model model, FileAttribute fileAttribute) { // 预览Type,参数传了就取参数的,没传取系统默认 String officePreviewType = fileAttribute.getOfficePreviewType() == null ? ConfigConstants.getOfficePreviewType() : fileAttribute.getOfficePreviewType(); String baseUrl = BaseUrlFilter.getBaseUrl(); String fileName = fileAttribute.getName(); String pdfName = fileName.substring(0, fileName.lastIndexOf(".") + 1) + "pdf"; String outFilePath = FILE_DIR + pdfName; // 判断之前是否已转换过,如果转换过,直接返回,否则执行转换 if (!fileHandlerService.listConvertedFiles().containsKey(pdfName) || !ConfigConstants.isCacheEnabled()) { String filePath; ReturnResponseresponse = DownloadUtils.downLoad(fileAttribute, null); if (response.isFailure()) { return otherFilePreview.notSupportedFile(model, fileAttribute, response.getMsg()); } filePath = response.getContent(); if (StringUtils.hasText(outFilePath)) { boolean convertResult = fileHandlerService.cadToPdf(filePath, outFilePath); if (!convertResult) { return otherFilePreview.notSupportedFile(model, fileAttribute, "cad文件转换异常,请联系管理员"); } if (ConfigConstants.isCacheEnabled()) { // 加入缓存 fileHandlerService.addConvertedFile(pdfName, fileHandlerService.getRelativePath(outFilePath)); } } } if (baseUrl != null && (OFFICE_PREVIEW_TYPE_IMAGE.equals(officePreviewType) || OFFICE_PREVIEW_TYPE_ALL_IMAGES.equals(officePreviewType))) { return getPreviewType(model, fileAttribute, officePreviewType, baseUrl, pdfName, outFilePath, fileHandlerService, OFFICE_PREVIEW_TYPE_IMAGE,otherFilePreview); } model.addAttribute("pdfUrl", pdfName); return PDF_FILE_PREVIEW_PAGE; }
pdf预览CAD文件:
该过程将包括三个主要步骤:1)加载CAD文件;2)CAD文件导出为pdf;3)保存文件。cadToPdf方法负责实现cad到pdf的文件转换,需要传入的参数为cad文件传输路径(imputFilePath)和pdf输出路径(outputFilePath)。
1、加载CAD文件
aspose.cad是用于将AutoCAD、DWG和DXF文档转换为PDF和Raster图像的原生API,调用Image.load(inputFilePath)方法,Load方法将根据文件扩展名确定文件格式,并创建Image类对象cadImage。
com.aspose.cad.Image cadImage = com.aspose.cad.Image.load(inputFilePath)
2、CAD文件导出为pdf
这里创建了新的CadRasterizationOptions类的实例来设置应如何渲染CAD图像,此类指定应如何渲染CAD图像-宽度,高度(以像素为单位),CAD内容在pdf中的位置,对象的背景颜色和替代颜色等。
CadRasterizationOptions cadRasterizationOptions = new CadRasterizationOptions();cadRasterizationOptions.setLayouts(new String[]{"Model"});//设置布局,使用模型的特定布局名称。模型也是一种布局。cadRasterizationOptions.setNoScaling(true); // 在导出期间设置不缩放。cadRasterizationOptions.setBackgroundColor(Color.getWhite()); // 背景颜色cadRasterizationOptions.setPageWidth(cadImage.getWidth()); // 设置文件宽度cadRasterizationOptions.setPageHeight(cadImage.getHeight()); // 设置文件高度cadRasterizationOptions.setPdfProductLocation("center"); // 设置pdf文件位置cadRasterizationOptions.setAutomaticLayoutsScaling(true);cadRasterizationOptions.setDrawType(CadDrawTypeMode.UseObjectColor);
对文件渲染完成之后,需要通过创建ImageOptionsBase的相应子类的对象来选择输出格式,ImageOptionsBase有:PdfOptions、BmpOptions、PngOptions、TiffOptions等子类,每个都提供相应输出文件格式的设置。
这里创建了一个PdfOptions实例,该实例将输出设置为PDF文件格式,将之前创建的CadRasterizationOptions实例为PdfOptions实例设置VectorRasterizationOptions属性,为其指定输出图像的高度和宽度(默认情况下,CAD图像将被拉伸/缩小以适合指定的输出页面尺寸,并保持宽高比),并使其栅格化使用CAD文件中指定的颜色。
PdfOptions pdfOptions = new PdfOptions();pdfOptions.setVectorRasterizationOptions(cadRasterizationOptions);
3、保存文件
File outputFile = new File(outputFilePath);OutputStream stream;try { stream = new FileOutputStream(outputFile); cadImage.save(stream, pdfOptions); cadImage.close(); return true;} catch (FileNotFoundException e) { logger.error("PDFFileNotFoundException,inputFilePath:{}", inputFilePath, e); return false;}
转载地址:https://blog.csdn.net/m0_55503427/article/details/120958207 如侵犯您的版权,请留言回复原文章的地址,我们会给您删除此文章,给您带来不便请您谅解!