import com.aspose.cad.*;
import com.aspose.cad.fileformats.cad.CadDrawTypeMode;
import com.aspose.cad.fileformats.cad.CadImage;
import com.aspose.cad.imageoptions.*;
import com.itextpdf.text.PageSize;
import java.io.IOException ;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.HttpURLConnection;
import java.net.URL;
import java.net.URLConnection;
class CADFileUtil{
public static File change(File dwgFile) throws IOException, IOException {
FileInputStream fileInputStream;
//将pdf文件转换成InputStream输入流
fileInputStream = new FileInputStream(dwgFile);
Image objImage = Image.load(fileInputStream);
CadRasterizationOptions rasterizationOptions = new CadRasterizationOptions();
PngOptions pngOptions=new PngOptions();
//pngOptions.setVectorRasterizationOptions(rasterizationOptions);
//设置属性
//rasterizationOptions.setAutomaticLayoutsScaling(true);
//rasterizationOptions.setContentAsBitmap(true);
//rasterizationOptions.setLayouts(new String[]{"Model"});
rasterizationOptions.setDrawType(1);
//图片宽度
rasterizationOptions.setPageWidth(8000);
//图片高度
rasterizationOptions.setPageHeight(7200);
////背景色
//rasterizationOptions.setBackgroundColor(Color.getWhite());
//////线条颜色
//rasterizationOptions.setDrawColor(Color.getBlack());
//// 全局背景色
//rasterizationOptions.setExportAllLayoutContent(false);
pngOptions.setVectorRasterizationOptions(rasterizationOptions);
//输出文件
File outputFile = new File("C:\\Users\\Administrator\\Desktop\\sdsds\\w2.png");
//存放地址
objImage.save(outputFile.getPath(), pngOptions);
System.out.println(dwgFile.getName()+"转换成功!");
fileInputStream.close();
return outputFile;
}
public static void main(String[] args) {
String srcFile="E:/TEMP/sss.dwg";
//String dataDir="E:/TEMP/";
long a= System.currentTimeMillis();
File cadFile = new File("C:\\Users\\Administrator\\Desktop\\sdsds\\fl1.dxf");
try {
change(cadFile);
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
long b=System.currentTimeMillis();
System.out.println(b-a);
//String dirPath = "D:\\test\\test\\";
//String outDirPath = "C:\\Users\\Administrator\\Desktop\\sdsds\\";
//CADFileToPDF("C:\\Users\\Administrator\\Desktop\\sdsds\\fl1.dxf", outDirPath + "dwg-test.pdf");
//CADFileToSvg();
}
/**
* 将cad文件转换为pdf文件
*
* @param srcFile 原始cad文件路径
* @param dataDir 转换后的pdf文件路径
* @param pageSize 页面大小 A4 or A3
* @param pageOrientation 页面方向 portrait or landscape
* @throws Exception
*/
public static void CADFileToPDF(String srcFile, String dataDir, String pageSize, String pageOrientation) {
Image objImage = Image.load(srcFile);
CadImage cadImage = (CadImage) objImage;
//下面两句是为了解决缺失字体导致部分文字变为方框的问题
cadImage.setDefaultFont("仿宋");
CadRasterizationOptions cadRasterizationOptions = new CadRasterizationOptions();
//导出所有的布局:默认为false,导致只能导出第一个布局
cadRasterizationOptions.setExportAllLayoutContent(true);
// 设置颜色:默认黑白
cadRasterizationOptions.setBackgroundColor(Color.getWhite());
// 这一句是输出线条为彩色的关键
cadRasterizationOptions.setDrawType(CadDrawTypeMode.UseObjectColor);
// 设置纸张大小,默认A4
float width = "A3".equals(pageSize) ? PageSize.A3.getWidth() : PageSize.A4.getWidth();
float height = "A3".equals(pageSize) ? PageSize.A3.getHeight() : PageSize.A4.getHeight();
//设置纸张方向,默认纵向,
SizeF size = "landscape".equals(pageOrientation) ? new SizeF(height, width) : new SizeF(width, height);
cadRasterizationOptions.setPageSize(size);
cadRasterizationOptions.setAutomaticLayoutsScaling(false);
cadRasterizationOptions.setNoScaling(false);
PdfOptions pdfOptions = new PdfOptions();
pdfOptions.setVectorRasterizationOptions(cadRasterizationOptions);
objImage.save(dataDir + System.currentTimeMillis() + ".pdf", pdfOptions);
}
public static void CADFileToPDF(String srcFile, String dataDir, String pageSize) {
CADFileToPDF(srcFile, dataDir, pageSize, "portrait");
}
public static void CADFileToPDF(String srcFile, String dataDir) {
CADFileToPDF(srcFile, dataDir, "A4", "portrait");
}
private static void dddd()
{
String srcFile="C:\\Users\\Administrator\\Desktop\\sdsds\\fl1.dxf";
CadRasterizationOptions cadRasterizationOptions = new CadRasterizationOptions();
//cadRasterizationOptions.setBackgroundColor(Color.getWhite());
cadRasterizationOptions.setPageWidth(9600);
cadRasterizationOptions.setPageHeight(10600);
cadRasterizationOptions.setAutomaticLayoutsScaling(true);
JpegOptions jpegOptions = new JpegOptions();
jpegOptions.setVectorRasterizationOptions(cadRasterizationOptions);
cadRasterizationOptions.setDrawType(CadDrawTypeMode.UseObjectColor);
Image objImage = Image.load(srcFile);
objImage.save("C:\\Users\\Administrator\\Desktop\\sdsds\\w33.jpeg", jpegOptions);
}
public static void CADFileToSvg(){
String srcFile="C:\\Users\\Administrator\\Desktop\\sdsds\\fl1.dxf";
Image image = Image.load(srcFile);
// Create an instance of CadRasterizationOptions
CadRasterizationOptions rasterizationOptions = new CadRasterizationOptions();
// Set page width & height
rasterizationOptions.setPageWidth(7200);
rasterizationOptions.setPageHeight(9200);
// Create an instance of PngOptions for the resultant image
ImageOptionsBase options = new PngOptions();
// Set rasterization options
options.setVectorRasterizationOptions(rasterizationOptions);
// Convert CAD DWG DXF file to PNG image
image.save("C:\\Users\\Administrator\\Desktop\\sdsds\\w33.png" , options);
}
}