通过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);
}
}
}
}
}