aspose-cad dwg转pdf dwg转png dwg转svg
- 1、首先引入aspose-cad,我的maven中央仓库配置的阿里云,下载不下来,手动到https://mvnrepository.com/放到本地maven仓库的
<dependency>
<groupId>com.aspose</groupId>
<artifactId>aspose-cad</artifactId>
<version>版本</version>
</dependency>`
2.1、dwg转pdf
public String dwgToPdf(FileConvertInPutVO inPutVO) {
Image image = Image.load(inPutVO.getSourcePath());
CadRasterizationOptions cadRasterizationOptions = new CadRasterizationOptions();
//设置背景颜色
cadRasterizationOptions.setBackgroundColor(Color.getBlack());
cadRasterizationOptions.setPageWidth(1400);
cadRasterizationOptions.setPageHeight(650);
cadRasterizationOptions.setAutomaticLayoutsScaling(true);
cadRasterizationOptions.setNoScaling(false);
cadRasterizationOptions.setDrawType(1);
//AutoCAD图形可能包含多个图层,其中每个图层可能具有不同的尺寸。如果希望转换后的PDF文件具有统一的缩放比例,可以配置CadRasterizationOptions.AutomaticLayoutsScaling设置为true,API根据PDF文档中的页面大小自动缩放图层
cadRasterizationOptions.setAutomaticLayoutsScaling(true);
PdfOptions options = new PdfOptions();
options.setVectorRasterizationOptions(cadRasterizationOptions);
//输出文件
image.save(inPutVO.getTargetPath(), options);
return inPutVO.getTargetPath();
}
2.2、dwgToPng
public String dwgToPng(FileConvertInPutVO inPutVO) {
//1、加载CAD文件
Image image = Image.load(inPutVO.getSourcePath());
//2、设置导出格式
//ImageOptionsBase子类:PdfOptions , BmpOptions , GifOptions , JpegOptions , PngOptions , PsdOptions , TiffOptions和一些更 。 每个都提供相应输出文件格式的设置。 然后通过设置其属性进行设置,然后将其传递到第三步。
PngOptions pngOptions = new PngOptions();
// 设置png的压缩等级 0 最低 9 最高
pngOptions.setCompressionLevel(0);
// 原图尺寸类型
int width = image.getWidth();
int height = image.getHeight();
// 100万像素等比倍数
int zoom = 1008000 / width / height;
//此类指定应如何渲染CAD图像-宽度,高度(以像素为单位),CAD内容应居中,对象的背景颜色和替代颜色等。
CadRasterizationOptions cadRasterizationOptions = new CadRasterizationOptions();
//设置背景颜色
cadRasterizationOptions.setBackgroundColor(Color.getBlack());//Color.Beige
//设置绘图颜色
cadRasterizationOptions.setDrawColor(Color.getBlue());
cadRasterizationOptions.setPageHeight(width * zoom);
cadRasterizationOptions.setPageWidth(height * zoom);
cadRasterizationOptions.setDrawType(CadDrawTypeMode.UseObjectColor);
//如果AutoCAD工程图中有多个图层,则可以指定要在转换的PDF文档中具有的图层
cadRasterizationOptions.setLayers(Lists.newArrayList("LayerA"));
cadRasterizationOptions.setUnitType(UnitType.Unitless);
pngOptions.setVectorRasterizationOptions(cadRasterizationOptions);
//3、保存图像
image.save(inPutVO.getTargetPath(), pngOptions);
return inPutVO.getTargetPath();
}
2.3、dwgToSvg
public String dwgToSvg(FileConvertInPutVO inPutVO) {
SvgOptions options = new SvgOptions();
Image image = Image.load(inPutVO.getSourcePath());
int width = image.getWidth();
int height = image.getHeight();
//100万像素等比倍数
int zoom = 1008000 / width / height;
CadRasterizationOptions cadRasterizationOptions = new CadRasterizationOptions();
cadRasterizationOptions.setPageHeight(height * zoom);
cadRasterizationOptions.setPageWidth(width * zoom);
cadRasterizationOptions.setDrawType(CadDrawTypeMode.UseObjectColor);
options.setVectorRasterizationOptions(cadRasterizationOptions);
image.save(inPutVO.getTargetPath(), options);
return inPutVO.getTargetPath();
}