C#進行Visio二次開發之檔案匯出及另存Web頁面

weixin_33816946發表於2016-03-07

在我前面很多關於Visio的開發過程中,介紹了各種Visio的C#開發應用場景,包括對Visio的文件、模具文件、形狀、屬性資料、各種事件等相關的基礎處理,以及Visio本身的整體專案應用,雖然時間過去很久,不過這些技術依舊還在使用中,最近應客戶培訓的需要,我對所有的內容進行了重新整理,把一些沒有介紹的很詳細或者很少的內容進行了豐富,因此本文介紹的主題-Visio二次開發之檔案匯出及另存Web頁面,介紹一下Visio檔案另存為其他幾種格式的處理,以及另存為Web檔案等相關操作。

1、Visio匯出為PDF格式

在一般情況下,PDF格式是較為常用的內容格式,因此Visio文件(Vsd格式)匯出為PDF也是很常見的一件事情,Office文件本身很好支援PDF格式的輸出,因此對於Visio來說,也不是什麼難事,基本上利用它現有的API就可以匯出為PDF格式了。

在Visio的Document文件物件中,就有ExportAsFixedFormat這個方法,可以匯出為PDF或者XPS的格式的,這個格式有很多引數,用來確定匯出那頁,以及格式等設定。

expression.ExportAsFixedFormat(FixedFormat, OutputFileName, Intent, PrintRange, FromPage, ToPage, ColorAsBlack, IncludeBackground, IncludeDocumentProperties, IncludeStructureTags, UseISO19005_1, FixedFormatExtClass)

同時,這些引數的相關說明如下所示。

NameRequired/OptionalData TypeDescription
FixedFormat Required VisFixedFormatTypes The format type in which to export the document. See Remarks for possible values.
OutputFileName Optional String The name and path of the file to which to output, enclosed in quotation marks.
Intent Required VisDocExIntent The output quality. See Remarks for possible values.
PrintRange Required VisPrintOutRange The range of document pages to be exported. See Remarks for possible values.
FromPage Optional Long If PrintRange is visPrintFromTo , the first page in the range to be exported. The default is 1, which indicates the first page of the drawing.
ToPage Optional Long If PrintRange is visPrintFromTo , the last page in the range to be exported. The default is -1, which indicates the last page of the drawing.
ColorAsBlack Optional Boolean True to render all colors as black to ensure that all shapes are visible in the exported drawing. False to render colors normally. The default is False.
IncludeBackground Optional Boolean Whether to include background pages in the exported file. The default is True.
IncludeDocumentProperties Optional Boolean Whether to include document properties in the exported file. The default is True.
IncludeStructureTags Optional Boolean Whether to include document structure tags to improve document accessibility. The default is True.
UseISO19005_1 Optional Boolean Whether the resulting document is compliant with ISO 19005-1 (PDF/A). The default is False.
FixedFormatExtClass Optional [UNKNOWN] A pointer to a class that implements the IMsoDocExporter interface for purposes of creating custom fixed output. The default is a null pointer.

 我們在程式碼裡面匯出PDF如下所示。

            SaveFileDialog dlg = new SaveFileDialog();
            dlg.FileName = "";
            dlg.Filter = "Pdf檔案 (*.pdf)|*.pdf|AutoCAD 繪圖 (*.dwg)|*.dwg|所有檔案(*.*)|*.*";
            dlg.FilterIndex = 1;
            if (dlg.ShowDialog() == DialogResult.OK)
            {
                if (dlg.FileName.Trim() != string.Empty)
                {
                    VisDocument.ExportAsFixedFormat(Visio.VisFixedFormatTypes.visFixedFormatPDF,
                        dlg.FileName,
                        Visio.VisDocExIntent.visDocExIntentScreen,
                        Visio.VisPrintOutRange.visPrintAll,
                        1, VisDocument.Pages.Count, false, true, true, true, true,
                        System.Reflection.Missing.Value);
                }
            }

這樣,我們通過指定PDF格式,以及匯出檔名,以及起止頁碼等資訊後,就可以順利匯出對應的Visio文件了,這種方式匯出的Visio文件,效果非常好,可以放大到最大清晰都很好的。

 

2、Visio另存為CAD格式

Visio和CAD之間是比較好的相容模式的,Visio和CAD本身都是基於向量圖形的繪製,因此轉換為CAD在繼續進行編輯也是很常見的事情,因此在較早時期,Visio本身就對CAD格式(dwg格式)就提供了很好的支援,它可以通過下面程式碼進行CAD格式的匯出。

            SaveFileDialog dlg = new SaveFileDialog();
            dlg.FileName = "";
            dlg.Filter = "AutoCAD 繪圖 (*.dwg)|*.dwg|所有檔案(*.*)|*.*";
            dlg.FilterIndex = 1;
            if (dlg.ShowDialog() == DialogResult.OK)
            {
                if (dlg.FileName.Trim() != string.Empty)
                {
                    VisApplication.ActivePage.Export(dlg.FileName);
                }
            }

如果CAD檔案順利匯出,那麼會有一個日誌檔案提示使用者操作的結果的,如下所示。

Visio還可以匯出為JPG格式,這個和CAD操作類似,都是通過Page物件的Export方法進行匯出,操作程式碼如下所示。

            SaveFileDialog dlg = new SaveFileDialog();
            dlg.FileName = "";
            dlg.Filter = "JPEG檔案 (*.jpg)|*.jpg|所有檔案(*.*)|*.*";
            dlg.FilterIndex = 1;
            if (dlg.ShowDialog() == DialogResult.OK)
            {
                if (dlg.FileName.Trim() != string.Empty)
                {
                    VisApplication.ActivePage.Export(dlg.FileName);
                }
            }

雖然這個匯出的JPG格式,也是比較不錯的,不過相對PDF的向量效果來說,JPG放大的話,一般來說沒有PDF格式那麼清晰,但總體效果也還是可以。

 

3、Visio文件另存Web頁面

對於Visio文件的另存為Web頁面的操作,就沒有上述幾個方法那麼簡單了,一般需要更加複雜一點的處理方式。

雖然對於Visio文件來說,在IE上可以通過ActiveX的Visio Viewer來進行檢視,不過其他瀏覽器都不支援,因此對於另存為Web頁面的檔案,這種方式顯得比較通用一些,可以在各個瀏覽器上檢視HTML頁面,裡面就包含了對Visio檔案的顯示了。

Visio的文件另存為Web頁面的操作,主要思路是利用Application物件的SaveAsWebObject屬性,並通過VisWebPageSettings物件進行一些匯出屬性的設定,如頁面範圍,文件解析度等屬性設定,以及是否在完成後使用瀏覽器開啟檔案等設定。

如獲得物件的操作如下所示。

                // 獲取文件的Application物件
                targetApplication = targetDocument.Application;

                // 獲取並轉換SaveAsWebObject物件
                saveAsWebAddon = (VisSaveAsWeb)targetApplication.SaveAsWebObject;

                // 獲取儲存Web頁面的引數設定物件
                saveAsWebSetting = (VisWebPageSettings)saveAsWebAddon.WebPageSettings;

通過獲得頁面引數物件,我們可以設定匯出的起始頁面,如下所示。

                    saveAsWebSetting.StartPage = startPage;
                    saveAsWebSetting.EndPage = endPage;

然後在繫結到具體匯出的文件裡面就確定對應匯出文件了。

                //使用AttachToVisioDoc指定那個文件作為儲存頁面的物件
                saveAsWebAddon.AttachToVisioDoc(targetDocument);    

為了提高匯出Web頁面的Visio清晰度,我們需要設定文件的顯示比例,如下所示為使用源格式大小。

                //設定其中的相關引數                
                saveAsWebSetting.DispScreenRes = VISWEB_DISP_RES.resSource;//顯示比例

這個VISWEB_DISP_RES裡面有很多引數可以設定的。

ConstantValueDescription

resSource

0

Use resolution of the source image for output.

res180x260

1

180 x 260 pixels

res544x376

2

544 x 376 pixels

res640x480

3

640 x 480 pixels

res720x512

4

720 x 512 pixels

res768x1024

5

768 x 1024 pixels

res800x600

6

800 x 600 pixels

res1024x768

7

1024 x 768 pixels

res1152x882

8

1152 x 882 pixels

res1152x900

9

1152 x 900 pixels

res1280x1024

10

1280 x 1024 pixels

res1600x1200

11

1600 x 1200 pixels

res1800x1440

12

1800 x 1440 pixels

res1920x1200

13

1920 x 1200 pixels

resINVALID

14

Reserved.

 

另外還有一個引數確定是批處理方式(靜默方式)還是完成後通過瀏覽器開啟檔案的方式,如下所示。

                //判斷是否為批處理模式
                if ((flags & RunInBatchMode) != 0)
                {
                    // 如果為批處理模式,那麼瀏覽器視窗不會自動開啟
                    saveAsWebSetting.OpenBrowser = 0;
                    saveAsWebSetting.SilentMode = 1;
                }
                else
                {
                    // 否則儲存完畢後開啟對應給的瀏覽器顯示檔案
                    saveAsWebSetting.OpenBrowser = 1;
                    saveAsWebSetting.QuietMode = 1;
                }

如果一切順利,那麼通過方法直接建立頁面就可以了,如下所示。

saveAsWebAddon.CreatePages();// 建立頁面

以上的方法處理,我們一般封裝在一個類裡面,方便呼叫處理,那麼在介面上,我們處理的方法就可以簡單化一些。

            var fileName = System.IO.Path.Combine(System.Environment.CurrentDirectory, "test.html");

            var success = SaveAsWebApi.SaveDocAsWebPage(this.axDrawingControl1.Document, -1, -1, fileName,
                SaveAsWebApi.ShowPropertiesWindow | SaveAsWebApi.ShowNavigationBar |
                SaveAsWebApi.ShowSearchTool | SaveAsWebApi.ShowPanAndZoom);

            MessageBox.Show(success ? "成功生成Web檔案" : "生成Web檔案操作失敗");

 最後,我們就可以在各個瀏覽器裡面檢視相關的Visio檔案了,這種方式比Visio Viewer的處理更通用,效果也很不錯哦。

 

相關文章