星空网 > 软件开发 > ASP.net

C#进行Visio二次开发之文件导出及另存Web页面

在我前面很多关于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
FixedFormatRequiredVisFixedFormatTypesThe format type in which to export the document. See Remarks for possible values.
OutputFileNameOptionalStringThe name and path of the file to which to output, enclosed in quotation marks.
IntentRequiredVisDocExIntentThe output quality. See Remarks for possible values.
PrintRangeRequiredVisPrintOutRangeThe range of document pages to be exported. See Remarks for possible values.
FromPageOptionalLongIf PrintRange is visPrintFromTo , the first page in the range to be exported. The default is 1, which indicates the first page of the drawing.
ToPageOptionalLongIf PrintRange is visPrintFromTo , the last page in the range to be exported. The default is -1, which indicates the last page of the drawing.
ColorAsBlackOptionalBooleanTrue 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.
IncludeBackgroundOptionalBooleanWhether to include background pages in the exported file. The default is True.
IncludeDocumentPropertiesOptionalBooleanWhether to include document properties in the exported file. The default is True.
IncludeStructureTagsOptionalBooleanWhether to include document structure tags to improve document accessibility. The default is True.
UseISO19005_1OptionalBooleanWhether the resulting document is compliant with ISO 19005-1 (PDF/A). The default is False.
FixedFormatExtClassOptional[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文档,效果非常好,可以放大到最大清晰都很好的。

C#进行Visio二次开发之文件导出及另存Web页面

C#进行Visio二次开发之文件导出及另存Web页面

 

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文件顺利导出,那么会有一个日志文件提示用户操作的结果的,如下所示。

C#进行Visio二次开发之文件导出及另存Web页面

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格式那么清晰,但总体效果也还是可以。

C#进行Visio二次开发之文件导出及另存Web页面

 

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的处理更通用,效果也很不错哦。

C#进行Visio二次开发之文件导出及另存Web页面

 




原标题:C#进行Visio二次开发之文件导出及另存Web页面

关键词:C#

C#
*特别声明:以上内容来自于网络收集,著作权属原作者所有,如有侵权,请联系我们: admin#shaoqun.com (#换成@)。

跨境电商周伯通:https://www.goluckyvip.com/tag/36662.html
跨境电商主图:https://www.goluckyvip.com/tag/36663.html
跨境电商主要是什么:https://www.goluckyvip.com/tag/36665.html
跨境电商主要是什么工作:https://www.goluckyvip.com/tag/36666.html
跨境电商主要是做什么的:https://www.goluckyvip.com/tag/36667.html
跨境电商主要指的:https://www.goluckyvip.com/tag/36668.html
从园岭新村到大梅沙海滨总站坐什么车:https://www.vstour.cn/a/363191.html
七月份适合去日本旅游吗 7月份去日本哪里好玩:https://www.vstour.cn/a/363192.html
相关文章
我的浏览记录
最新相关资讯
海外公司注册 | 跨境电商服务平台 | 深圳旅行社 | 东南亚物流