java使用jacob调用actocad的打印功能将cad文件转成pdf

实现过程,使用jacob调用autocad软件的打印功能将cad文件打印成pdf文件

1:去官网下载jacob包,将两个dll文件放到JRE的bin目录下,例如:C:\Program Files\Java\jre1.8.0_171\bin

2:设置autocad的打印样式表,可以设置线条粗细等等

3:完整代码如下:

package xxxxx;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
import java.util.Map;
import org.apache.commons.lang.ArrayUtils;
import org.apache.commons.lang.StringUtils;
import com.jacob.activeX.ActiveXComponent;
import com.jacob.com.ComThread;
import com.jacob.com.Dispatch;
import com.jacob.com.SafeArray;
import com.jacob.com.Variant;

import javafx.util.Pair;

public class CadToPdf {
  private ActiveXComponent acad;
  private double[] pageLength = {1, 1};

  public CadToPdf() {
  }

  public Dispatch getDoc(String path) {
    if (acad == null) {
      System.out.println(System.getProperty("java.library.path"));
      ComThread.InitMTA(true);
      String appProgramId = "AutoCAD.Application";
      System.out.println("appProgramId: " + appProgramId);

      while (acad == null) {
        acad = ActiveXComponent.createNewInstance(appProgramId);
      }
      acad.setProperty("Visible", new Variant(false));
    }
    Dispatch document = acad.getProperty("Documents").toDispatch();
    return Dispatch.call(document, "Open", new Variant(path)).toDispatch();
  }


  public void covertToPdf(Dispatch doc, String pageSize, String pdfPath) {
    Dispatch.call(doc, "SetVariable", "BACKGROUNDPLOT", 0);
    Dispatch plot = Dispatch.get(doc, "Plot").getDispatch();
    Dispatch layout = Dispatch.get(doc, "ActiveLayout").getDispatch();

    // 打印样式表PCCAD.ctb
    String StyleSheet ="cadtopdf.ctb";

    System.out.println("打印样式表 StyleSheet: " + StyleSheet);
    Dispatch.put(layout, "StyleSheet", StyleSheet);

    // 打印机或绘图仪
    String ConfigName = "DWG To PDF.pc3";
    System.out.println("打印机或绘图仪  PlotConfig: " + ConfigName);
    Dispatch.put(layout, "ConfigName", ConfigName);

    SafeArray safeArray = Dispatch.call(layout, "GetCanonicalMediaNames").toSafeArray();

    String CanonicalMediaName = null;
    double deviation = 999;
    for (int i = safeArray.getLBound(); i <= safeArray.getUBound(); i++) {
      String mediaName = safeArray.getString(i);
      if (StringUtils.containsIgnoreCase(mediaName, "毫米")) {
        String leftStr = StringUtils.substringBetween(mediaName, " (", " x ");
        String rigthStr = StringUtils.substringBetween(mediaName, " x ", "毫米");
        double left = Double.parseDouble(leftStr);
        double right = Double.parseDouble(rigthStr);
        double abs = Math.abs(Math.min(left, right) - pageLength[0])
            + Math.abs(Math.max(left, right) - pageLength[1]);
        if (abs < deviation) {
          CanonicalMediaName = mediaName;
          deviation = abs;
          if (abs == 0) {
            break;
          }
        }

      } else if (StringUtils.containsIgnoreCase(mediaName, "MM")) {
        String leftStr = StringUtils.substringBetween(mediaName, "(", "_x_");
        String rigthStr = StringUtils.substringBetween(mediaName, "_x_", "_MM");
        double left = Double.parseDouble(leftStr);
        double right = Double.parseDouble(rigthStr);
        double abs = Math.abs(Math.min(left, right) - pageLength[0])
            + Math.abs(Math.max(left, right) - pageLength[1]);
        if (abs < deviation) {
          CanonicalMediaName = mediaName;
          deviation = abs;
          if (abs == 0) {
            break;
          }
        }

      } else if (StringUtils.containsIgnoreCase(mediaName, "Inches")) {
        String leftStr = StringUtils.substringBetween(mediaName, "(", "_x_");
        String rigthStr = StringUtils.substringBetween(mediaName, "_x_", "_Inches");
        double left = Double.parseDouble(leftStr) * 25.4;
        double right = Double.parseDouble(rigthStr) * 25.4;
        double abs = Math.abs(Math.min(left, right) - pageLength[0])
            + Math.abs(Math.max(left, right) - pageLength[1]);
        if (abs < deviation) {
          CanonicalMediaName = mediaName;
          deviation = abs;
          if (abs == 0) {
            break;
          }
        }
      }
    }

    // 图纸尺寸
    // if (CanonicalMediaName==null) {
    // CanonicalMediaName = registry.getString(pageSize + ".CanonicalMediaName",
    // registry.getString("D.CanonicalMediaName", "ISO_A4_(210.00_x_297.00_MM)"));
    // }
    System.out.println("图纸尺寸CanonicalMediaName: " + CanonicalMediaName);
    Dispatch.put(layout, "CanonicalMediaName", CanonicalMediaName);

    // 打印范围
    String PlotType ="1";
    Dispatch.put(layout, "PlotType", PlotType);

    // 打印比例
    String UseStandardScale = "True";
    System.out.println("UseStandardScale: " + UseStandardScale);
    Dispatch.put(layout, "UseStandardScale", UseStandardScale);
    String StandardScale ="0";
        
    System.out.println("打印比例 StandardScale: " + StandardScale);
    Dispatch.put(layout, "StandardScale", StandardScale);

    // 居中打印
    String CenterPlot ="True";
    System.out.println("居中打印CenterPlot: " + CenterPlot);
    Dispatch.put(layout, "CenterPlot", CenterPlot);

    // 旋转
    String PlotRotation ="0";
    System.out.println("旋转 PlotRotation: " + PlotRotation);
    Dispatch.put(layout, "PlotRotation", PlotRotation);

    // 错误继续
    Dispatch.put(plot, "QuietErrorMode", "True");
    boolean printOk = Dispatch.call(plot, "PlotToFile", pdfPath, ConfigName).getBoolean();
    if (printOk) {
      System.out.println("to PDF successful " + pdfPath);
    }
    return;
  }

  public void closedoc(Dispatch doc) {
    if (doc != null) {
      Dispatch.call(doc, "Close", "False");
    }
  }

  public void close() {
    if (acad != null) {
      Dispatch.call(acad, "Quit");
      acad.safeRelease();
      acad = null;
      ComThread.Release();
      // System.gc();
    }
  }
  
}

2:使用方式:

 public static void main(String[] args) {
   CadToPdf cad = new CadToPdf ();
   Dispatch doc =cad.getDoc("C:\\Users\\Administrator\\Desktop\\xxx.dwg");
   
   cad.covertToPdf(doc,"A4","C:\\Users\\Administrator\\Desktop\\xxx.pdf");
   cad.closedoc(doc);
   cad.close();
   }

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

叮当LIU

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值