java实现dwg转pdf,svg,tif

依赖

        <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();
        }
    }

评论 4
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值