package com.whyc.util; import com.aspose.cad.Color; import com.aspose.cad.Image; import com.aspose.cad.fileformats.cad.CadDrawTypeMode; import com.aspose.cad.imageoptions.CadRasterizationOptions; import com.aspose.cad.imageoptions.PngOptions; import com.aspose.cad.imageoptions.UnitType; import lombok.extern.slf4j.Slf4j; import java.io.File; import java.io.FileInputStream; import java.io.FileNotFoundException; import java.io.IOException; @Slf4j public class DwgToPngUtil { public static String dwg2png(File dwgFile){ FileInputStream fileInputStream; //将dwg文件转换成InputStream输入流 try { fileInputStream = new FileInputStream(dwgFile); } catch (FileNotFoundException e) { throw new RuntimeException(e); } Image objImage = Image.load(fileInputStream); CadRasterizationOptions rasterizationOptions = new CadRasterizationOptions(); //设置颜色 rasterizationOptions.setBackgroundColor(Color.getBlack()); int width = objImage.getWidth(); int height = objImage.getHeight(); int zoom = 1008000 / width / height; rasterizationOptions.setPageWidth(width*zoom); rasterizationOptions.setPageHeight(height*zoom); //rasterizationOptions.setPageWidth(1400); //rasterizationOptions.setPageHeight(650); //rasterizationOptions.setAutomaticLayoutsScaling(true); //rasterizationOptions.setNoScaling (false); //rasterizationOptions.setDrawType(1); rasterizationOptions.setDrawType(CadDrawTypeMode.UseObjectColor); rasterizationOptions.setDrawColor(Color.getBlue()); rasterizationOptions.setUnitType(UnitType.Unitless); PngOptions pngOptions = new PngOptions(); pngOptions.setCompressionLevel(0); pngOptions.setVectorRasterizationOptions(rasterizationOptions); //输出文件 File outputFile = new File(dwgFile.getParent()+File.separator+dwgFile.getName().substring(0,dwgFile.getName().lastIndexOf("."))+"-dwg.png"); //存放地址 try { objImage.save(outputFile.getPath(), pngOptions); } catch (Exception e) { e.printStackTrace(); log.error("dwg转pdf失败{}",dwgFile.getName()); } finally { try { fileInputStream.close(); } catch (IOException e) { throw new RuntimeException(e); } } //log.info("文件转换成功{}",dwgFile.getName()); return outputFile.getAbsolutePath(); } public static void main(String[] args) throws IOException { String s = DwgToPngUtil.dwg2png(new File("C:\\Users\\29550\\Desktop\\当前项目\\202207图纸管理\\图纸升级\\fgcd-80030nt-701_a01.dwg")); System.out.println(s); //DwgToPngUtil.dwg2Pdf(new File("C:\\Users\\29550\\Desktop\\当前项目\\202207图纸管理\\图纸升级\\fgcd-80030nt-701_a02.dwg")); //DwgToPngUtil.dwg2Pdf(new File("C:\\Users\\29550\\Desktop\\当前项目\\202207图纸管理\\图纸升级\\fgcd-80030nt-702_a01.dwg")); //DwgToPngUtil.dwg2Pdf(new File("C:\\Users\\29550\\Desktop\\当前项目\\202207图纸管理\\图纸升级\\fgcd-80030nt-702_a02.dwg")); //DwgToPngUtil.dwg2Pdf(new File("C:\\Users\\29550\\Desktop\\当前项目\\202207图纸管理\\图纸升级\\fgcd-80030nt-703_a01.dwg")); //DwgToPngUtil.dwg2Pdf(new File("C:\\Users\\29550\\Desktop\\当前项目\\202207图纸管理\\图纸升级\\fgcd-80030nt-703_a02.dwg")); } }