【问题标题】:Import image from Enterprise Architect to ms Word从 Enterprise Architect 导入图像到 ms Word
【发布时间】:2014-08-19 17:58:16
【问题描述】:

使用 Enterprise Architect 加载项我想使用 c# 将图像从 Enterprise Architect 导入到 ms Word。现在我已经通过将图表/图像保存为 .pdf 文件然后使用 iTextSharp 再次读取它来解决这个问题。 在我看来,这是解决问题的困难方法,因此我认为必须有一种更简单的方法来使用 c# 将图像从 Enterprise Architect 获取到 ms Word。

【问题讨论】:

  • 那么,你有没有尝试过?
  • 我尝试查看 EA.diagram 界面,看看里面是否有我可以使用的东西。我发现那里唯一的东西是“SaveAsPDF”命令。

标签: c# .net import enterprise-architect


【解决方案1】:

为什么不直接将图像插入文档中?

//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",然后单击确定。将打开一个新模板的文档。
5.在文档的左侧面板上,选中Diagram 复选框。一些文本将添加到右侧的文档中。
6.在模板文档中,在您看到文本[right-click-to-insert-Diagram-field(s)]的地方单击鼠标右键-> 插入字段-> 图表图像。
7.保存模板。

【讨论】:

  • 这是解决问题的一种更简单的方法。但知道我在注册时遇到问题:检索具有 CLSID {ED685310-1B2F-486F-8BD0-8A753DD6054D} 的组件的 COM 类工厂失败,原因是以下错误:80040154 未注册类(HRESULT 异常:0x80040154( REGDB_E_CLASSNOTREG))。我尝试将平台目标从“Any CPU”转换为“x86”和“x64”,但这些都没有帮助我。
  • 我不明白这个问题。你试过我建议的方法了吗?
  • 是的,我尝试了你的建议,但我必须分配变量,所以我这样做了:EA.DocumentGenerator generator = new EA.DocumentGenerator();然后我得到了我之前发布的错误。
  • 你说得对,我忘了发布如何初始化生成器。查看修改后的代码!
  • 现在差不多了!代码运行没有任何错误,但是在生成的文档中找不到图像。这可能是因为图像格式错误或图像 ID 错误?
【解决方案2】:

您还可以使用剪贴板功能。此特定代码打开一个新的 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;
    }

【讨论】:

  • 通过进一步调查,让您的代码“窃取”剪贴板似乎是一种不好的做法,因为剪贴板只能在“用户空间”中使用。无论如何,似乎最好的解决方法是将图片写入一个可以从中读取的临时文件。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-08-02
  • 2013-05-17
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多