如何批量打印图纸?已解决!如何不打开图形打印?
本帖最后由 作者 于 2009-10-9 14:07:23 编辑 <br /><br /> <p>有一个文件夹下1000张图,需要把他们用adobe pdf打印机打印到另外一个文件夹中,</p><p>打印范围是(0,0)(420,297),A3纸,横向打印,打印后pdf名字取dwg的名字,去掉dwg,改成pdf,如1.dwg,打印完了以后就是1.pdf。</p><p>主要是如何打印一张图纸,那么1000张也就好办了。最后能够不用打开图纸,用database读取dwg或dxf,然后打印。</p><p>PlottingServices里面的东西太多了,不知道怎么用。</p><p>谢谢!</p><p>下面的是VBA代码,不知道C#怎么用</p><p>Public Sub PlotWindow()<br/> ' 确保当前布局是模型空间<br/> ThisDrawing.ActiveLayout = ThisDrawing.Layouts.Item("Model")<br/> <br/> ' 设置打印设备<br/> ThisDrawing.ActiveLayout.ConfigName = "DWF6 ePlot.pc3"<br/> <br/> ' 设置打印比例为"布满图纸"<br/> ThisDrawing.ActiveLayout.StandardScale = acScaleToFit<br/> <br/> ' 设置图纸类型<br/> ThisDrawing.ActiveLayout.CanonicalMediaName = "ISO_A4_(210.00_x_297.00_MM)"<br/> <br/> ' 让AutoCAD在前台进行打印<br/> ThisDrawing.SetVariable "BACKGROUNDPLOT", 0<br/> <br/> Dim objPlot As AcadPlot<br/> Set objPlot = ThisDrawing.Plot<br/> <br/> ' 设置打印窗口<br/> Dim minPoint(0 To 1) As Double, maxPoint(0 To 1) As Double<br/> SetPoint2d minPoint, 0, 0<br/> SetPoint2d maxPoint, 800, 600<br/> ThisDrawing.ActiveLayout.SetWindowToPlot minPoint, maxPoint<br/> <br/> ' 设置打印类型<br/> ThisDrawing.ActiveLayout.PlotType = acWindow<br/> <br/> objPlot.PlotToFile "C:\test.dwf"<br/> <br/> ' 恢复系统变量的值<br/> ThisDrawing.SetVariable "BACKGROUNDPLOT", 2<br/>End Sub</p><p></p><p><font color="#f73809">以下是解决的代码,速度不算慢,100张图7分钟,每张图都是先打开,再打印完,再关闭的。</font></p><p><font color="#f73809" size="3"><strong>能否这样,不打开图形,采用database.readdwg或者database.dxfin的方法进行打印?怎么实现?</strong></font></p><p><font color="#f73809"></font></p><p>using System;<br/>using System.Collections.Generic;<br/>using System.ComponentModel;<br/>using System.Data;<br/>using System.Drawing;<br/>using System.Linq;<br/>using System.Text;<br/>using System.Windows.Forms;<br/>using Autodesk.AutoCAD.Runtime;<br/>using Autodesk.AutoCAD.ApplicationServices;<br/>using Autodesk.AutoCAD.DatabaseServices;<br/>using Autodesk.AutoCAD.EditorInput;<br/>using Autodesk.AutoCAD.Geometry;<br/>using System.IO;<br/>using Excel;// =Microsoft.Office.Interop.Excel ;<br/>using VB = Microsoft.VisualBasic;//用于激活窗口<br/>using System.Runtime.InteropServices;//使用Marshal获得进程<br/>using System.Reflection;//使用Missing.value<br/>using FS = Scripting;//文件系统<br/>using Autodesk.AutoCAD.PlottingServices;<br/>using System.Collections.Specialized;</p><p><br/>namespace GWDwgFrame<br/>{<br/> public partial class DwgFrame : Form<br/> {<br/> </p><p> private Excel.Application ExcelApp;//excel工程<br/> string excelPath;//Excel路径<br/> Excel.Workbook operBook;//操作book<br/> Excel.Worksheet operSheet;//操作sheet<br/> int iRow;//Excel循环数</p><p> int finish;//完成数1-100<br/> string folderPath="";//文件夹路径<br/> string framePath="";//图框路径<br/> string operFilePath;//当前操作的文件路径<br/> string frameBlkRecName = "LoopNo";<br/> int iDwg;//操作文件的循环计数<br/> FS.FileSystemObject fso=new FS.FileSystemObject();<br/> FS.Folder dwgFolder;<br/> FS.Files dwgFileGroup;<br/> FS.File dwgFile;<br/> public System.Collections.ArrayList dwgFilesRoute=new System.Collections.ArrayList();//所有文件的路径<br/> public int dwgCount;//文件数计算<br/> string fileType="DXF";//文件类型,DXF或者DWG<br/> ObjectIdCollection frameIDCol;//复制块定义所需ID集合<br/> IdMapping acIdMap;// //复制数据库函数成员<br/> Autodesk.AutoCAD.DatabaseServices.Database operDb;//操作数据库<br/> Autodesk.AutoCAD.DatabaseServices.Database frameDb;//图框数据库<br/> //Document operDwgDoc;//操作图形文档<br/> string frameName;//图框名字,带后缀.dwg<br/> string dwgRefName;//外部参照名字<br/> string[] loopHead = new string[] { "文件名","回路名", "EPC图号", "业主图号" };<br/> string[] loopInfor = new string;//分别是回路名,回路名,EPC图号,业主图号<br/> Autodesk.AutoCAD.DatabaseServices.AttributeCollection attCollec;//快参照属性集合<br/> Autodesk.AutoCAD.DatabaseServices.AttributeReference attOper;//属性参照<br/> DwgVersion dwgSaveVer=DwgVersion.AC1015;//保存的版本2000<br/> public string devName;//打印设备<br/> public string devPaper;//打印图纸<br/> public bool plotToFile = false;//是否打印到图纸<br/> public PlotRotation rotAng;//旋转角度</p><p>//获得可用的打印机</p><p>private void DwgFrameLoad(object sender, EventArgs e)<br/> {<br/> //comboBoxFileType.SelectedIndex = 0;<br/> //comboBoxFileType.Text = "DXF";<br/> //comboBoxDwgVer.SelectedIndex = 1;//默认版本2000<br/> Document doc =Autodesk.AutoCAD.ApplicationServices.Application.DocumentManager.MdiActiveDocument;<br/> Editor ed = doc.Editor;<br/> PlotSettingsValidator psv =PlotSettingsValidator.Current;<br/> StringCollection devlist =psv.GetPlotDeviceList();<br/> for (int i = 0; i < devlist.Count; i++)<br/> {<br/> comboBoxPrintDev.Items.Add(devlist);<br/> }</p><p></p><p> }</p><p>//获得该打印机图纸<br/> private void getPaperSize(object sender, EventArgs e)<br/> {<br/> comboBoxPaperSize.Items.Clear();<br/> devName = comboBoxPrintDev.Text;<br/> Document doc =Autodesk.AutoCAD.ApplicationServices.Application.DocumentManager.MdiActiveDocument;<br/> Editor ed = doc.Editor;<br/> PlotSettingsValidator psv =PlotSettingsValidator.Current;</p><p> PlotSettings ps = new PlotSettings(true);<br/> psv.SetPlotConfigurationName(ps, devName, null);<br/> psv.RefreshLists(ps);<br/> StringCollection medlist =psv.GetCanonicalMediaNameList(ps);<br/> for (int i = 0; i < medlist.Count; i++)<br/> {<br/> comboBoxPaperSize.Items.Add(medlist);<br/> }</p><p><br/> }</p><p> //打印图纸大小<br/> private void setPaperSize(object sender, EventArgs e)<br/> {<br/> devPaper = comboBoxPaperSize.Text;<br/> }</p><p> //打印角度<br/> private void rotateAngle(object sender, EventArgs e)<br/> {<br/> string rot=comboBoxRotate.Text;<br/> if (rot=="0" )<br/> {<br/> rotAng = PlotRotation.Degrees000;<br/> }<br/> if (rot=="90" )<br/> {<br/> rotAng = PlotRotation.Degrees090;<br/> }<br/> if (rot=="180" )<br/> {<br/> rotAng = PlotRotation.Degrees180;<br/> }<br/> if (rot=="270" )<br/> {<br/> rotAng = PlotRotation.Degrees270;<br/> }<br/> }<br/> //是否打印到文件<br/> private void plottoFile(object sender, EventArgs e)<br/> {<br/> plotToFile=checkBoxPrintFile.Checked;</p><p> if (plotToFile == true)<br/> {<br/> folderBrowserDialog1.SelectedPath = "";<br/> folderBrowserDialog1.Description = "请选择打印文件的存放文件夹";<br/> folderBrowserDialog1.ShowDialog();<br/> <br/> if (fso.FolderExists(folderBrowserDialog1.SelectedPath) == false)<br/> {<br/> return;<br/> }<br/> folderPath = folderBrowserDialog1.SelectedPath;<br/> textBoxPintFolder.Text = "文件夹路径:" + folderPath;<br/> textBoxPintFolder.Visible = true;<br/> //dwgFolder = fso.GetFolder(folderPath);</p><p><br/> if (folderPath.Substring(folderPath.Length - 1, 1) == "\\")<br/> {<br/> folderPath = folderPath ;<br/> }<br/> else<br/> {<br/> folderPath = folderPath + "\\";<br/> } <br/> }<br/> }<br/> //开始打印<br/> private void buttonPrint_Click(object sender, EventArgs e)<br/> {<br/> string printFile;<br/> //PlotProgressDialog ppd=new PlotProgressDialog(false, 1, true);<br/> //PlotEngine pe = PlotFactory.CreatePublishEngine();<br/> for (iDwg = 0; iDwg < dwgCount; iDwg++)<br/> {<br/> //打印结束才可打开新文档<br/> for (; ; )<br/> {<br/> if (PlotFactory.ProcessPlotState == ProcessPlotState.NotPlotting)<br/> //if (ppd.IsSheetCancelled)<br/> {<br/> break;<br/> }<br/> }<br/> operFilePath = dwgFilesRoute.ToString();//获得操作图形路径<br/> printFile = System.IO.Path.GetFileNameWithoutExtension(operFilePath);<br/> printFile = folderPath + printFile;<br/> //Autodesk.AutoCAD.ApplicationServices.Application.SetSystemVariable("REGENMODE", 0);<br/> Autodesk.AutoCAD.ApplicationServices.Application.DocumentManager.Open(operFilePath, false);<br/> Document doc = Autodesk.AutoCAD.ApplicationServices.Application.DocumentManager.MdiActiveDocument;<br/> <br/> <font color="#ff6600">doc.LockDocument();<br/></font> <font color="#ff0000">Autodesk.AutoCAD.ApplicationServices.Application.SetSystemVariable("BACKGROUNDPLOT", 0);<br/></font> <br/> Editor ed = doc.Editor;<br/> Database db = doc.Database;</p><p> Transaction tr =<br/> db.TransactionManager.StartTransaction();<br/> using (tr)<br/> {<br/> // We'll be plotting the current layout</p><p> BlockTableRecord btr =<br/> (BlockTableRecord)tr.GetObject(<br/> db.CurrentSpaceId,<br/> OpenMode.ForRead<br/> );<br/> Layout lo =<br/> (Layout)tr.GetObject(<br/> btr.LayoutId,<br/> OpenMode.ForRead<br/> );</p><p> // We need a PlotInfo object<br/> // linked to the layout</p><p> PlotInfo pi = new PlotInfo();<br/> pi.Layout = btr.LayoutId;</p><p> // We need a PlotSettings object<br/> // based on the layout settings<br/> // which we then customize</p><p> PlotSettings ps =<br/> new PlotSettings(lo.ModelType);<br/> ps.CopyFrom(lo);</p><p> // The PlotSettingsValidator helps<br/> // create a valid PlotSettings object</p><p> PlotSettingsValidator psv =<br/> PlotSettingsValidator.Current;</p><p> // We'll plot the extents, centered and <br/> // scaled to fit<br/> psv.SetPlotRotation(ps, rotAng);<br/> psv.SetPlotType(<br/> ps,<br/> Autodesk.AutoCAD.DatabaseServices.PlotType.Extents<br/> );<br/> psv.SetUseStandardScale(ps, true);<br/> psv.SetStdScaleType(ps, StdScaleType.ScaleToFit);<br/> psv.SetPlotCentered(ps, true);</p><p> // We'll use the standard DWF PC3, as<br/> // for today we're just plotting to file</p><p> psv.SetPlotConfigurationName(<br/> ps,<br/> devName,//"Adobe PDF",<br/> devPaper//"A3"<br/> );</p><p> // We need to link the PlotInfo to the<br/> // PlotSettings and then validate it</p><p> pi.OverrideSettings = ps;<br/> PlotInfoValidator piv =<br/> new PlotInfoValidator();<br/> piv.MediaMatchingPolicy =<br/> MatchingPolicy.MatchEnabled;<br/> piv.Validate(pi);</p><p> // A PlotEngine does the actual plotting<br/> // (can also create one for Preview)</p><p> //if (PlotFactory.ProcessPlotState ==<br/> // ProcessPlotState.NotPlotting)</p><p> PlotEngine pe =<br/> PlotFactory.CreatePublishEngine();<br/> using (pe)<br/> {<br/> // Create a Progress Dialog to provide info<br/> // and allow thej user to cancel</p><p> PlotProgressDialog ppd = new PlotProgressDialog(false, 1, true);<br/> using (ppd)<br/> {<br/> ppd.set_PlotMsgString(<br/> PlotMessageIndex.DialogTitle,<br/> "正在打印"+operFilePath//"Custom Plot Progress"<br/> );<br/> ppd.set_PlotMsgString(<br/> PlotMessageIndex.CancelJobButtonMessage,<br/> "取消当前打印"//"Cancel Job"<br/> );<br/> ppd.set_PlotMsgString(<br/> PlotMessageIndex.CancelSheetButtonMessage,<br/> "取消本页打印"//"Cancel Sheet"<br/> );<br/> ppd.set_PlotMsgString(<br/> PlotMessageIndex.SheetSetProgressCaption,<br/> "打印集合进度"//"Sheet Set Progress"<br/> );<br/> ppd.set_PlotMsgString(<br/> PlotMessageIndex.SheetProgressCaption,<br/> "打印页进度"//"Sheet Progress"<br/> );<br/> ppd.LowerPlotProgressRange = 0;<br/> ppd.UpperPlotProgressRange = 100;<br/> ppd.PlotProgressPos = 0;</p><p> // Let's start the plot, at last</p><p> ppd.OnBeginPlot();<br/> ppd.IsVisible = true;<br/> pe.BeginPlot(ppd, null);</p><p> // We'll be plotting a single document</p><p> pe.BeginDocument(<br/> pi,<br/> doc.Name,<br/> null,<br/> 1,<br/> plotToFile,//true, // Let's plot to file<br/> printFile//"c:\\test-output"<br/> );</p><p> // Which contains a single sheet</p><p> ppd.OnBeginSheet();</p><p> ppd.LowerSheetProgressRange = 0;<br/> ppd.UpperSheetProgressRange = 100;<br/> ppd.SheetProgressPos = 0;</p><p> PlotPageInfo ppi = new PlotPageInfo();<br/> pe.BeginPage(<br/> ppi,<br/> pi,<br/> true,<br/> null<br/> );<br/> pe.BeginGenerateGraphics(null);<br/> pe.EndGenerateGraphics(null);</p><p> // Finish the sheet<br/> pe.EndPage(null);<br/> ppd.SheetProgressPos = 100;<br/> ppd.OnEndSheet();</p><p> // Finish the document</p><p> pe.EndDocument(null);</p><p> // And finish the plot</p><p> ppd.PlotProgressPos = 100;<br/> ppd.OnEndPlot();<br/> pe.EndPlot(null);<br/> </p><p> }<br/> }//using pe<br/> <br/> <br/> }<br/> for (; ; )<br/> {<br/> this.Update();<br/> //if (ppd.IsDisposed)<br/> if (PlotFactory.ProcessPlotState == ProcessPlotState.NotPlotting)<br/> {<br/> break;<br/> }<br/> }<br/> //for (int i = 0; i < 10000000; i++)<br/> //{<br/> // if (ppd.IsPlotCancelled)<br/> // if (PlotFactory.ProcessPlotState == ProcessPlotState.NotPlotting)<br/> // {<br/> // break;<br/> // }<br/> //}<br/> doc.CloseAndDiscard();<br/> labelExecuteCondition.Text = "正在打印,请等待……";<br/> finish = (int)((Double)(iDwg + 1) / (Double)(dwgCount) * 100);<br/> progressBar1.Value = finish;<br/> progressBar1.Visible = true;<br/> labelExecuteCondition.Visible = true;<br/> this.Update();<br/> <br/> }<br/> Autodesk.AutoCAD.ApplicationServices.Application.ShowAlertDialog("打印结束");<br/> progressBar1.Visible = false;<br/> labelExecuteCondition.Visible = false;<br/> Autodesk.AutoCAD.ApplicationServices.Application.DocumentManager.Add("");</p><p><br/> <br/> }</p><p></p><p></p> <p>先看看这里吧:</p><p><a href="http://bbs.mjtd.com/forum.php?mod=viewthread&tid=76505">http://bbs.mjtd.com/forum.php?mod=viewthread&tid=76505</a></p> 我看了那些kean的文章,实在是太复杂了,就这么一个打印功能,搞的如此复杂,真实有点怕了! 我也想搞一个.以前用VBA搞了一个.想改成C#的.可是就是不知怎么改. 有办法把所有PDF合并成一个吗 menethil 发表于 2017-12-5 14:03有办法把所有PDF合并成一个吗
两个方法:
1、pdfFactory pro
2、用第三方pdf操作库
页:
[1]