2
votes

Working on an Enterprise Architect add-in I want to import an image from Enterprise Architect to ms Word using c#. For now I have solved this by saving the diagram/image as a .pdf file and then reading it again using the iTextSharp. It seems to me that this is the hard way around the problem and therefore I think there must be a more simple way to get the image from Enterprise Architect to ms Word using c#.

2
So, have you tried anything?john
I tried looking at the interface EA.diagram to see if there was anything in there I could use. The only thing I found there was the "SaveAsPDF" command.n.Stenvang

2 Answers

3
votes

Why don't you insert the image straight into the document?

//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);

The "Diagram Image Template" is a template you have to define yourself in EA by following these easy steps:
1. Click F8
2.Go to the Templates tab
3.Click on the new button on he bottom
4.Give the new template the name "Diagram Image Template", and click OK. A document of new template will be opened.
5.On the left panel of the document, select the Diagram checkbox. Some text will be added to the document on the right.
6.In the template document, right click where you see the text [right-click-to-insert-Diagram-field(s)] -> Insert Field -> Diagram Image.
7.Save the template.

1
votes

You can also use the clipboard function. This particular code opens a new word document, copies the image from a diagram in EA and paste it into a paragraph.

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;
    }