依赖
<dependency>
<groupId>com.aspose</groupId>
<artifactId>aspose-cad</artifactId>
<version>23.9</version>
</dependency>
实现代码
/**
* cad文件转pdf
*
* @param inputFilePath cad文件路径
* @param outputFilePath pdf输出文件路径
* @return 转换是否成功
*/
public Boolean cadToPdf(String inputFilePath, String outputFilePath) {
// 创建CAD延时处理器
final InterruptionTokenSource source = new InterruptionTokenSource();
// 创建SVG、PDF和TIFF选项
final SvgOptions svgOptions = new SvgOptions();
final PdfOptions pdfOptions = new PdfOptions();
final TiffOptions tiffOptions = new TiffOptions(TiffExpectedFormat.TiffJpegRgb);
//获取输出文件格式
String cadPreviewType = outputFilePath.substring(outputFilePath.lastIndexOf(".") + 1);
// 获取输出文件路径中的目录
int index = outputFilePath.lastIndexOf("/");
String folder = outputFilePath.substring(0, index);
File path = new File(folder);
// 如果目录不存在,则创建新的目录
if (!path.exists()) {
path.mkdirs();
}
// 创建输出文件
File outputFile = new File(outputFilePath);
try {
// 加载CAD文件并设置指定的编码
LoadOptions opts = new LoadOptions();
opts.setSpecifiedEncoding(CodePages.SimpChinese);
final Image cadImage = Image.load(inputFilePath, opts);
try {
// 设置矢量化质量
RasterizationQuality rasterizationQuality = new RasterizationQuality();
rasterizationQuality.setArc(RasterizationQualityValue.High);
rasterizationQuality.setHatch(RasterizationQualityValue.High);
rasterizationQuality.setText(RasterizationQualityValue.High);
rasterizationQuality.setOle(RasterizationQualityValue.High);
rasterizationQuality.setObjectsPrecision(RasterizationQualityValue.High);
rasterizationQuality.setTextThicknessNormalization(true);
// 设置CAD矢量化选项
CadRasterizationOptions cadRasterizationOptions = new CadRasterizationOptions();
cadRasterizationOptions.setBackgroundColor(Color.getWhite());
cadRasterizationOptions.setPageWidth(cadImage.getWidth());
cadRasterizationOptions.setPageHeight(cadImage.getHeight());
cadRasterizationOptions.setUnitType(cadImage.getUnitType());
cadRasterizationOptions.setAutomaticLayoutsScaling(false);
cadRasterizationOptions.setNoScaling(false);
cadRasterizationOptions.setQuality(rasterizationQuality);
cadRasterizationOptions.setDrawType(CadDrawTypeMode.UseObjectColor);
cadRasterizationOptions.setExportAllLayoutContent(true);
cadRasterizationOptions.setVisibilityMode(VisibilityMode.AsScreen);
// 根据cadPreviewType选择不同的输出格式选项
switch (cadPreviewType) {
case "svg":
svgOptions.setVectorRasterizationOptions(cadRasterizationOptions);
svgOptions.setInterruptionToken(source.getToken());
break;
case "pdf":
pdfOptions.setVectorRasterizationOptions(cadRasterizationOptions);
pdfOptions.setInterruptionToken(source.getToken());
break;
case "tif":
tiffOptions.setVectorRasterizationOptions(cadRasterizationOptions);
tiffOptions.setInterruptionToken(source.getToken());
break;
}
// 保存CAD文件为指定格式
try (OutputStream stream = new FileOutputStream(outputFile)) {
switch (cadPreviewType) {
case "svg":
cadImage.save(stream, svgOptions);
break;
case "pdf":
cadImage.save(stream, pdfOptions);
break;
case "tif":
cadImage.save(stream, tiffOptions);
break;
}
} catch (IOException e) {
log.error("CADFileNotFoundException,inputFilePath:{}", inputFilePath, e);
return false;
} finally {
// 释放资源并中断任务
cadImage.dispose();
source.interrupt();
source.dispose();
}
return true;
} finally {
// 释放资源
source.dispose();
cadImage.dispose();
}
}catch (Exception e){
e.printStackTrace();
return false;
} finally {
// 释放资源
source.dispose();
}
}