我想要完成的事情:
我想将Visio应用程序中的活动页面复制到一个新文档中,并将其保存(并使它成为数据库的byte[] ),我已经在这样做了,但是由于与Visio应用程序的交互太多,所以我已经在这样做了。
将页面复制到字节数组的方法:
private static byte[] VisioPageToBytes()
{
//Make a new invisible app to dump the shapes in
var app = new InvisibleApp();
Page page = MainForm.IVisioApplication.ActivePage;
app.AlertResponse = 2;
//Selact all shapes and copy, then deselect
MainForm.IVisioApplication.ActiveWindow.SelectAll();
MainForm.IVisioApplication.ActiveWindow.Selection.Copy();
MainForm.IVisioApplication.ActiveWindow.DeselectAll();
//Add empty document to invisible app and dump shapes
app.Documents.Add( string.Empty );
app.ActivePage.Paste();
//Save document and convert to byte[]
app.ActiveDocument.SaveAs( Application.UserAppDataPath + @"/LastStored.vsd" );
app.ActiveDocument.Close();
app.Quit();
app.AlertResponse = 0;
var bytes = File.ReadAllBytes( Application.UserAppDataPath + @"/LastStored.vsd" );
Clipboard.Clear();
return bytes;
}为什么是错的:
此代码在visio页面中进行选择,并必须打开一个不可见窗口来存储该页。我正在寻找一种减少与Visio应用程序交互的方法(因为它不稳定)。第二个(不可见的) Visio应用程序的打开偶尔会使我的主要Visio应用程序崩溃。
我想做这样的事情:
Page page = MainForm.IVisioApplication.ActivePage;
Document doc;
doc.Pages.Add( page ); //Pages.Add has no parameters so this doesn't work
doc.SaveAs(Application.UserAppDataPath + @"/LastStored.vsd");如果这是不可能的方式与较少的互动(通过“建立”的文件),请评论,让我知道。
TL;DR;
如果不打开Visio并将(内容)1页复制到Visio文档,我就不会创建新的Visio文档。
发布于 2017-03-10 13:06:21
如果您想要创建一个复制页面,那么您可能会发现page上的复制方法很方便,但是通过它的声音,只需保存现有的文档就可以了:
void Main()
{
var vApp = MyExtensions.GetRunningVisio();
var sourcePage = vApp.ActivePage;
var sourcePageNameU = sourcePage.NameU;
var vDoc = sourcePage.Document;
vDoc.Save(); //to retain original
var origFileName = vDoc.FullName;
var newFileName = Path.Combine(vDoc.Path, $"LastStored{Path.GetExtension(origFileName)}");
vDoc.SaveAs(newFileName);
//Remove all other pages
for (short i = vDoc.Pages.Count; i > 0; i--)
{
if (vDoc.Pages[i].NameU != sourcePageNameU)
{
vDoc.Pages[i].Delete(0);
}
}
//Save single page state
vDoc.Save();
//Close copy and reopen original
vDoc.Close();
vDoc = vApp.Documents.Open(origFileName);
}GetRunningVisio是我用于LinqPad的扩展方法:
http://visualsignals.typepad.co.uk/vislog/2015/12/getting-started-with-c-in-linqpad-with-visio.html
...but你已经有了一个你的应用程序的参考,所以你可以用它代替。
基于注释的更新:
好的,那么修改您的原始代码如何?请注意,我正在从页面中创建一个新的选择对象,而不是更改窗口对象,因此这不应该干扰用户所看到的内容或更改源文档。
void Main()
{
var vApp = MyExtensions.GetRunningVisio();
var sourcePage = vApp.ActivePage;
var sourceDoc = sourcePage.Document;
var vSel = sourcePage.CreateSelection(Visio.VisSelectionTypes.visSelTypeAll);
vSel.Copy(Visio.VisCutCopyPasteCodes.visCopyPasteNoTranslate);
var copyDoc = vApp.Documents.AddEx(string.Empty,
Visio.VisMeasurementSystem.visMSDefault,
(int)Visio.VisOpenSaveArgs.visAddHidden);
copyDoc.Pages[1].Paste(Visio.VisCutCopyPasteCodes.visCopyPasteNoTranslate);
var origFileName = sourceDoc.FullName;
var newFileName = Path.Combine(sourceDoc.Path, $"LastStored{Path.GetExtension(origFileName)}");
copyDoc.SaveAs(newFileName);
copyDoc.Close();
}请注意,这只会创建一个默认页面,因此您可能希望在粘贴之前包括对页面单元格(如PageWidth、PageHeight、PageScale和DrawingScale等)进行复制。
https://stackoverflow.com/questions/42716891
复制相似问题