C# 中望CAD(AutoCAD 非Com方式)打印PDF

通过C#实现中望CAD的运行中静默打印PDF功能。可以自定义具体打印设备名称、纸张类型、打印样式表、打印范围、着色打印类型等;将using中的“ZwSoft.ZwCAD”改为“Autodesk.AutoCAD”可以应用在AutoCAD开发中。

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using ZwSoft.ZwCAD.ApplicationServices;
using ZwSoft.ZwCAD.DatabaseServices;
using ZwSoft.ZwCAD.PlottingServices;
using ZwSoft.ZwCAD.Runtime;

namespace ZWCADTest_48
{
    public class PrintTest
    {
        [CommandMethod("PrintToPDF")]
        public void PrintToPDF()
        {
            Document acDoc = Application.DocumentManager.MdiActiveDocument;
            Database acDb = acDoc.Database;

            // 打印结果文件的输出路径
            string outputName = @"D:\CAD\PrintPDF\MyFile.pdf";

            // 打印机名称:ZWCAD PDF(High Quality Print).pc5
            string deviceName = "ZWCAD PDF(High Quality Print).pc5";
            // 纸张:"ISO_full_bleed_A0_(841.00_x_1189.00_MM)"
            string canonicalMediaName = "ISO_full_bleed_A0_(841.00_x_1189.00_MM)";
            // 打印样式表:zwcad.ctb
            string styleSheetName = "zwcad.ctb";
            // PlotSettings.PlotType 打印范围:范围 Extents
            // PlotSettings.ShadePlot 着色打印:按显示 AsDisplayed  

            using (Transaction tr = acDb.TransactionManager.StartTransaction())
            {
                // 获取打印机配置
                PlotConfig plotConfig = PlotConfigManager.SetCurrentConfig(deviceName);
                plotConfig.RefreshMediaNameList();
                // 将打印机的PlotToFile属性设置为true
                plotConfig.IsPlotToFile = true;

                PlotSettings plotSettings = new PlotSettings(true);
                PlotSettingsValidator plotSettingsValidator = PlotSettingsValidator.Current;

                // 配置打印设置和打印信息
                plotSettingsValidator.SetUseStandardScale(plotSettings, true);
                plotSettingsValidator.SetStdScaleType(plotSettings, StdScaleType.ScaleToFit);
                plotSettingsValidator.SetPlotCentered(plotSettings, true);
                plotSettingsValidator.SetPlotRotation(plotSettings, PlotRotation.Degrees000);

                // 设置设备名称和图纸名称
                plotSettingsValidator.SetPlotConfigurationName(plotSettings, deviceName, canonicalMediaName);
                // 设置打印样式表
                plotSettingsValidator.SetCurrentStyleSheet(plotSettings, styleSheetName);
                // 设置打印范围
                plotSettingsValidator.SetPlotType(plotSettings, ZwSoft.ZwCAD.DatabaseServices.PlotType.Extents);
                // 设置着色打印选项
                plotSettings.ShadePlot = PlotSettingsShadePlotType.AsDisplayed;

                //验证打印信息PlotInfo
                PlotInfoValidator plotInfoValidator = new PlotInfoValidator();
                PlotInfo plotInfo = new PlotInfo();

                LayoutManager manager = LayoutManager.Current;
                Layout layout = tr.GetObject(manager.GetLayoutId(manager.CurrentLayout), OpenMode.ForRead) as Layout;
                plotInfo.Layout = layout.ObjectId;

                plotInfo.OverrideSettings = plotSettings;
                plotInfo.DeviceOverride = plotConfig;
                plotInfo.ValidatedConfig = plotConfig;

                plotInfoValidator.MediaMatchingPolicy = MatchingPolicy.MatchEnabled;
                plotInfoValidator.Validate(plotInfo);

                // 使用PlotEngine对象开始打印
                using (PlotEngine plotEngine = PlotFactory.CreatePublishEngine())
                {
                    // 执行打印操作
                    plotEngine.BeginPlot(null, null);
                    plotEngine.BeginDocument(plotInfo, acDoc.Name, null, 1, true, outputName);

                    PlotPageInfo pageInfo = new PlotPageInfo();
                    plotEngine.BeginPage(pageInfo, plotInfo, true, null);
                    plotEngine.BeginGenerateGraphics(null);
                    plotEngine.EndGenerateGraphics(null);
                    plotEngine.EndPage(null);
                    plotEngine.EndDocument(null);
                    plotEngine.EndPlot(null);
                }
            }

        }
    }
}

  • 0
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值