以编程方式保存 Visio 文档
Visio 图纸保存概述
使用Diagram.Save保存Microsoft Visio图纸的方法。有允许将绘图保存到文件的重载。绘图可以保存为 Aspose.Diagram 支持的任何保存格式。有关所有支持的保存格式的列表,请参阅保存文件格式枚举。
储蓄 Visio Diagram
Aspose.Diagram API 的 Diagram 类表示一个 Visio 绘图,开发人员可以将其 Visio diagram 对象保存为任何支持的文件格式。要保存 Microsoft Visio 文件,只需使用Diagram.Save方法,它接受具有完整路径的文件名或文件流对象。 Aspose.Diagram API 从文件扩展名推断保存格式,还提供了一个额外的 SaveFileFormat 参数来指定输出文件格式。
以任何支持的文件格式保存 Visio Diagram
使用 Aspose.Diagram API,开发人员可以将 Visio diagram 保存为任何支持的文件格式,如下所列: VSDX, VSDM, VSSX, VSSM, VSTX, VSTM, VDX, VSX, VTX, TIFF, PNG, BMP, EMF, JPEG, PDF, XPS, GIF, HTML, SVG, SWF and XAML
保存 Diagram 编程示例
下面的示例将文档保存到文件中。
// Save a Visio diagram
diagram.Save(GetMyDir() + "MyOutput.vsdx", SaveFileFormat.VSDX);
指定 Visio 保存选项
有几个Diagram.Save method overloads that accept a SaveOptions object. This should be an object of a class derived from the SaveOptions class. Each save format has a corresponding class that holds save options for that save format. For example, there is PdfSaveOptions for the SaveFileFormat.PDF save format.
Visio Diagram 保存选项
这些示例展示了如何:
使用 Diagram 保存选项
下面的代码显示了如何在将文档保存为 Visio 格式之前设置保存选项。
// For complete examples and data files, please go to https://github.com/aspose-diagram/Aspose.Diagram-for-.NET | |
// The path to the documents directory. | |
string dataDir = RunExamples.GetDataDir_LoadSaveConvert(); | |
// Call the diagram constructor to a VSDX diagram | |
Diagram diagram = new Diagram(dataDir + "Drawing1.vsdx"); | |
// Options when saving a diagram into Visio format | |
DiagramSaveOptions options = new DiagramSaveOptions(SaveFileFormat.VSDX); | |
// Summary: | |
// When characters in the diagram are unicode and not be set with correct font | |
// value or the font is not installed locally, they may appear as block, | |
// image or XPS. Set the DefaultFont such as MingLiu or MS Gothic to show these | |
// characters. | |
options.DefaultFont = "MS Gothic"; | |
// Summary: | |
// Defines whether need enlarge page to fit drawing content or not. | |
// Remarks: | |
// Default value is false. | |
options.AutoFitPageToDrawingContent = true; | |
diagram.Save(dataDir + "UseDiagramSaveOptions_out.vsdx", options); |
使用 PDF 保存选项
The code below shows how to set save options before saving a document to a PDF format.
// For complete examples and data files, please go to https://github.com/aspose-diagram/Aspose.Diagram-for-.NET | |
// The path to the documents directory. | |
string dataDir = RunExamples.GetDataDir_LoadSaveConvert(); | |
// Call the diagram constructor to load diagram from a VSDX file | |
Diagram diagram = new Diagram(dataDir + "Drawing1.vsdx"); | |
// Options when saving a diagram into the PDF format | |
PdfSaveOptions options = new PdfSaveOptions(); | |
// Discard saving background pages of the Visio diagram | |
options.SaveForegroundPagesOnly = true; | |
// Specify the quality of JPEG compression for images (if JPEG compression is used). Default is 95. | |
options.JpegQuality = 100; | |
// Specify default font name | |
options.DefaultFont = "Arial"; | |
// Conformance level for generated PDF document. | |
options.Compliance = PdfCompliance.Pdf15; | |
// Load the certificate from disk. | |
// The other constructor overloads can be used to load certificates from different locations. | |
X509Certificate2 cert = new X509Certificate2(dataDir + "certificate.pfx", "feyb4lgcfbme"); | |
// Sets a digital signature details. If not set, then no signing will be performed. | |
options.DigitalSignatureDetails = new PdfDigitalSignatureDetails(cert, "Test Signing", "Aspose Office", DateTime.Now, PdfDigitalSignatureHashAlgorithm.Sha512); | |
// Set encription details | |
PdfEncryptionDetails encriptionDetails = new PdfEncryptionDetails("user password", "Owner Password", PdfEncryptionAlgorithm.RC4_128); | |
options.EncryptionDetails = encriptionDetails; | |
// Sets the number of pages to render in PDF. | |
options.PageCount = 2; | |
// Sets the 0-based index of the first page to render. Default is 0. | |
options.PageIndex = 0; | |
// Set page size | |
PageSize pgSize = new PageSize(PaperSizeFormat.A1); | |
options.PageSize = pgSize; | |
// Save in any supported file format | |
diagram.Save(dataDir + "UsePDFSaveOptions_out.pdf", options); |
使用 HTML 保存选项
The code below shows how to set save options before saving a document to HTML file format.
// For complete examples and data files, please go to https://github.com/aspose-diagram/Aspose.Diagram-for-.NET | |
// The path to the documents directory. | |
string dataDir = RunExamples.GetDataDir_LoadSaveConvert(); | |
// Call the diagram constructor to a VSDX diagram | |
Diagram diagram = new Diagram(dataDir + "Drawing1.vsdx"); | |
// Options when saving a diagram into the HTML format | |
HTMLSaveOptions options = new HTMLSaveOptions(); | |
// Summary: | |
// value or the font is not installed locally, they may appear as a block, | |
// set the DefaultFont such as MingLiu or MS Gothic to show these | |
// characters. | |
options.DefaultFont = "MS Gothic"; | |
// Sets the number of pages to render in HTML. | |
options.PageCount = 2; | |
// Sets the 0-based index of the first page to render. Default is 0. | |
options.PageIndex = 0; | |
// Set page size | |
PageSize pgSize = new PageSize(PaperSizeFormat.A1); | |
options.PageSize = pgSize; | |
// Discard saving background pages of the Visio diagram | |
options.SaveForegroundPagesOnly = true; | |
// Specify whether to save html as a single file, Default value is false. | |
options.SaveAsSingleFile = true; | |
// Specify whether to include the toolbar or not. Default value is true. | |
options.SaveToolBar = false; | |
// Set title of the HTML document | |
options.Title = "Title goes here"; | |
// Save in any supported file format | |
diagram.Save(dataDir + "UseHTMLSaveOptions_out.html", options); | |
// Save resultant HTML directly to a stream | |
MemoryStream stream = new MemoryStream(); | |
diagram.Save(stream, SaveFileFormat.HTML); |
使用图像保存选项
下面的代码显示了如何在将文档保存为图像文件格式之前设置保存选项。
// For complete examples and data files, please go to https://github.com/aspose-diagram/Aspose.Diagram-for-.NET | |
// The path to the documents directory. | |
string dataDir = RunExamples.GetDataDir_LoadSaveConvert(); | |
// Call the diagram constructor to a VSDX diagram | |
Diagram diagram = new Diagram(dataDir + "Drawing1.vsdx"); | |
ImageSaveOptions options = new ImageSaveOptions(SaveFileFormat.JPEG); | |
// Specify the quality level to use during compositing. | |
options.CompositingQuality = Aspose.Diagram.Saving.CompositingQuality.HighQuality; | |
// Sets the brightness for the the generated images. | |
// This property has effect only when saving to raster image formats. | |
// The default value is 0.5. The value must be in the range between 0 and 1. | |
options.ImageBrightness = 1f; | |
// Summary: | |
// value or the font is not installed locally, they may appear as a block, | |
// set the DefaultFont such as MingLiu or MS Gothic to show these | |
// characters. | |
options.DefaultFont = "MS Gothic"; | |
// Sets the number of pages to render in image. | |
options.PageCount = 2; | |
// Sets the 0-based index of the first page to render. Default is 0. | |
options.PageIndex = 0; | |
// Set page size | |
PageSize pgSize = new PageSize(PaperSizeFormat.A1); | |
options.PageSize = pgSize; | |
// Discard saving background pages of the Visio diagram | |
options.SaveForegroundPagesOnly = true; | |
// Sets the color mode for the generated images. | |
options.ImageColorMode = ImageColorMode.BlackAndWhite; | |
// Sets the contrast for the generated images. | |
// This property has effect only when saving to raster image formats. | |
// The default value is 0.5. The value must be in the range between 0 and 1. | |
options.ImageContrast = 1f; | |
// Specify the algorithm that is used when images are scaled or rotated. | |
// This property has effect only when saving to raster image formats. | |
options.InterpolationMode = Aspose.Diagram.Saving.InterpolationMode.NearestNeighbor; | |
// The value may vary from 0 to 100 where 0 means worst quality, | |
// But maximum compression and 100 means best quality but minimum compression. | |
// The default value is 95. | |
options.JpegQuality = 100; | |
// Set a value specifying how pixels are offset during rendering. | |
options.PixelOffsetMode = Aspose.Diagram.Saving.PixelOffsetMode.HighSpeed; | |
// Sets the resolution for the generated images, in dots per inch. The default value is 96. | |
options.Resolution = 2f; | |
// Sets the zoom factor for the generated images. | |
// The default value is 1.0. The value must be greater than 0. | |
options.Scale = 1f; | |
// Specify whether smoothing (antialiasing) is applied to lines | |
// And curves and the edges of filled areas. | |
options.SmoothingMode = Aspose.Diagram.Saving.SmoothingMode.HighQuality; | |
// Sets the type of compression to apply when saving generated images to the TIFF format. | |
options.TiffCompression = TiffCompression.Ccitt3; | |
// Save in any supported file format | |
diagram.Save(dataDir + "UseImageSaveOptions_out.jpeg", options); |
使用 SVG 保存选项
下面的代码显示了如何在将文档保存为 SVG 格式之前设置保存选项。
// For complete examples and data files, please go to https://github.com/aspose-diagram/Aspose.Diagram-for-.NET | |
// The path to the documents directory. | |
string dataDir = RunExamples.GetDataDir_LoadSaveConvert(); | |
// Call the diagram constructor to load diagram from a VSD file | |
Diagram diagram = new Diagram(dataDir + "Drawing1.vsdx"); | |
SVGSaveOptions options = new SVGSaveOptions(); | |
// Summary: | |
// value or the font is not installed locally, they may appear as a block, | |
// set the DefaultFont such as MingLiu or MS Gothic to show these | |
// characters. | |
options.DefaultFont = "MS Gothic"; | |
// Sets the 0-based index of the first page to render. Default is 0. | |
options.PageIndex = 0; | |
// Set page size | |
PageSize pgSize = new PageSize(PaperSizeFormat.A1); | |
options.PageSize = pgSize; | |
diagram.Save(dataDir + "UseSVGSaveOptions_out.svg", options); |
使用 SWF 保存选项
下面的代码显示了如何在将文档保存为 SWF 格式之前设置保存选项。
// For complete examples and data files, please go to https://github.com/aspose-diagram/Aspose.Diagram-for-.NET | |
// The path to the documents directory. | |
string dataDir = RunExamples.GetDataDir_LoadSaveConvert(); | |
// Call the diagram constructor to load diagram from a VSD file | |
Diagram diagram = new Diagram(dataDir + "Drawing1.vsdx"); | |
SWFSaveOptions options = new SWFSaveOptions(); | |
// Summary: | |
// value or the font is not installed locally, they may appear as a block, | |
// set the DefaultFont such as MingLiu or MS Gothic to show these | |
// characters. | |
options.DefaultFont = "MS Gothic"; | |
// Sets the number of pages to render in SWF. | |
options.PageCount = 2; | |
// Sets the 0-based index of the first page to render. Default is 0. | |
options.PageIndex = 0; | |
// Discard saving background pages of the Visio diagram | |
options.SaveForegroundPagesOnly = true; | |
// Specify whether the generated SWF document should include the integrated document viewer or not. | |
options.ViewerIncluded = true; | |
diagram.Save(dataDir + "UseSWFSaveOptions_out.swf", options); |
Sometimes, developers need to save or export Visio diagrams to different file formats programmatically (like VDX, PDF, JPEG and so on).
Save VSD file to different file formats (VDX, PDF and JPEG)
本文提供了一个代码示例,说明如何使用VSTO和Aspose.Diagram for .NET to save a Microsoft Visio VSD file to a VDX file, PDF file or a JPEG file programmatically. Below are parallel code snippets for VSTO and Aspose.Diagram for .NET that explains how to save a VSD file into different file formats. You’ll notice that the Aspose.Diagram code is shorter. Feel free to use the code and change it to meet your specific needs.
使用 VSTO 将 VSD 文件保存为其他格式
VSTO 允许您使用 Microsoft Visio 文件进行编程。要将文件保存为其他格式:
- 创建一个 Visio 应用程序对象。
- 使应用程序对象不可见。
- 加载 diagram。
- Save to VDX, PDF and JPEG.
- 退出 Visio 应用程序对象。
使用 VSTO 编程示例保存 VSD 文件
例子:
// For complete examples and data files, please go to https://github.com/aspose-diagram/Aspose.Diagram-for-.NET | |
// The path to the documents directory. | |
string dataDir = RunExamples.GetDataDir_KnowledgeBase(); | |
// Create Visio Application Object | |
Visio.Application vsdApp = new Visio.Application(); | |
// Make Visio Application Invisible | |
vsdApp.Visible = false; | |
// Create a document object and load a diagram | |
Visio.Document vsdDoc = vsdApp.Documents.Open(dataDir + "Drawing1.vsd"); | |
// Save the VDX diagram | |
vsdDoc.SaveAs(dataDir + "SaveDiagramToVDXwithVSTO_out.vdx"); | |
// Save as PDF file | |
vsdDoc.ExportAsFixedFormat(Visio.VisFixedFormatTypes.visFixedFormatPDF, | |
dataDir + "SaveDiagramToPDFwithVSTO_out.pdf", Visio.VisDocExIntent.visDocExIntentScreen, | |
Visio.VisPrintOutRange.visPrintAll, 1, vsdDoc.Pages.Count, false, true, | |
true, true, true, System.Reflection.Missing.Value); | |
Visio.Page vsdPage = vsdDoc.Pages[1]; | |
// Save as JPEG Image | |
vsdPage.Export(dataDir + "SaveDiagramToJPGwithVSTO_out.jpg"); | |
// Quit Visio Object | |
vsdApp.Quit(); |
用Aspose.Diagram将VSD文件保存为其他格式 for .NET
使用Aspose.Diagram,开发者不需要在机器上使用Microsoft Office Visio,可以独立于Microsoft Office自动化工作。
下面的代码片段展示了如何:
- 加载一个 diagram。
- Save the diagram to VSX, PDF and JPEG.
用 Aspose.Diagram 保存 VSD 文件 for .NET 编程示例
例子:
// For complete examples and data files, please go to https://github.com/aspose-diagram/Aspose.Diagram-for-.NET | |
// The path to the documents directory. | |
string dataDir = RunExamples.GetDataDir_KnowledgeBase(); | |
// Load an exiting Visio diagram | |
Diagram vsdDiagram = new Diagram(dataDir + "Drawing1.vsd"); | |
// Save the diagram as VDX | |
vsdDiagram.Save(dataDir + "SaveDiagramToVDXwithAspose_out.vdx", SaveFileFormat.VDX); | |
// Save as PDF | |
vsdDiagram.Save(dataDir + "SaveDiagramToPDFwithAspose_out.pdf", SaveFileFormat.PDF); | |
// Save as JPEG | |
vsdDiagram.Save(dataDir + "SaveDiagramToJPGwithAspose_out.jpg", SaveFileFormat.JPEG); |