程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> .NET網頁編程 >> C# >> C#入門知識 >> C#調用AutoCAD組件將DWG轉成PDF的方法

C#調用AutoCAD組件將DWG轉成PDF的方法

編輯:C#入門知識

下面就是個演示例子(目前我在HIGER所以版權是它的,呵呵),原理就是把DWG打印到一個打印機上,要想實現自動打印機要支持自動保存,下面的PDFCreator就支持。

想要調試就自己建一個項目加個FORM就好了~~~~~~~~~~~

\

 

using System; 
using System.Collections.Generic; 
using System.ComponentModel; 
using System.Data; 
using System.Drawing; 
using System.Text; 
using System.Windows.Forms; 
using Autodesk.AutoCAD.Interop; 
using System.IO; 
 
namespace WindowsFormsApplication1 

    public partial class Form1 : Form 
    { 
        AcadApplicationClass app; //常駐應用程序  
 
        public Form1() 
        { 
            InitializeComponent(); 
 
            try 
            { 
                app = new AcadApplicationClass(); 
            } 
            catch 
            { 
                MessageBox.Show("Start AutoCAD object failed! Installed?"); 
                button1.Enabled = false; 
                return; 
            } 
        } 
 
        private void button1_Click(object sender, EventArgs e) 
        { 
            if (!File.Exists(textBox1.Text)) 
            { 
                MessageBox.Show("File is not exists"); 
                return; 
            } 
 
            AcadDocument doc; 
            try 
            { 
                doc = app.Documents.Open(textBox1.Text, null, null); //打開文件  
            } 
            catch (Exception error) 
            { 
                MessageBox.Show("Can't open the file. " + error.Message); 
                return; 
            } 
             
            doc.ModelSpace.Layout.PlotType = Autodesk.AutoCAD.Interop.Common.AcPlotType.acExtents; //定義打印范圍為自動延伸全圖紙  
            doc.ModelSpace.Layout.ConfigName = textBox3.Text; //選定打印機  
            doc.ModelSpace.Layout.CanonicalMediaName = comboBox1.Text; //設置圖幅  
 
            doc.Plot.NumberOfCopies = 1; //打印份數  
            try 
            { 
                doc.Plot.PlotToDevice(null); 
            } 
            catch (Exception error) 
            { 
                MessageBox.Show("Send to printer failed. " + error.Message); 
                return; 
            } 
            finally 
            { 
                doc.Close(false, null); 
            } 
        } 
 
        private void Form1_FormClosing(object sender, FormClosingEventArgs e) 
        { 
            if (app != null) 
                try 
                { 
                    app.Quit(); 
                } 
                catch { } 
        }   www.2cto.com
 
        private void button2_Click_1(object sender, EventArgs e) 
        { 
            OpenFileDialog d = new OpenFileDialog(); 
            d.Filter = "AutoCAD圖紙(*.dwg)|*.dwg"; 
            if (d.ShowDialog() == DialogResult.OK) 
            { 
                textBox1.Text = d.FileName; 
            } 
        } 
    } 

  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved