我想使用c#从Enterprise Architect导入一个图像到ms Word。现在我通过将图表/图像保存为。pdf文件,然后使用iTextSharp再次读取它来解决这个问题。在我看来,这是解决问题的困难方法,因此我认为必须有一种更简单的方法来使用c#将图像从Enterprise Architect获取到ms Word。
为什么不直接将图像插入到文档中呢?
//create a document generator
EA.DocumentGenerator generator;
//initialize the document generator to create empty document (with no EA template)
generator = Repository.CreateDocumentGenerator();
generator.NewDocument("");
//insert image to the document
generator.DocumentDiagram(diagram.DiagramID, 0, "Diagram Image Template");
//save the documrnt
generator.SaveDocument(@"path/of/word/document/with/extension", 0);
"Diagram Image Template"
是一个模板,您必须按照以下简单步骤在EA中定义自己:
1. 按F8
2.转到模板选项卡
3.点击底部的new
按钮
4.给新模板命名为"Diagram Image Template"
,然后单击OK。将打开新模板的文档。
5.在文档的左侧面板中,选中Diagram
复选框。一些文本将被添加到右边的文档中。
6.在模板文档中,右键单击文本[right-click-to-insert-Diagram-field(s)]
-> Insert Field -> Diagram Image的位置。
7.保存模板
还可以使用剪贴板功能。这个特定的代码打开一个新的word文档,从EA中的图表复制图像并将其粘贴到段落中。
public void getPicture(Repository repository)
{
Object item;
ObjectType ot = repository.GetTreeSelectedItem(out item);
Word.Application wapp = GetWordApp();
var document = wapp.Documents.Add();
var paragraph = document.Paragraphs.Add();
Project project = repository.GetProjectInterface();
if (ot == ObjectType.otDiagram)
{
Diagram d = (Diagram)item;
project.PutDiagramImageOnClipboard(d.DiagramGUID, 0);
paragraph.Range.Paste();
}
}
private Word.Application GetWordApp()
{
Word.Application wapp = new Word.Application();
wapp.Visible = true;
return wapp;
}