Question

I need to add a rectangle into my Visio file and want to set the font and text color, how can I do this?

visio.Application app = new visio.Application();
visio.Document doc;
doc = app.Documents.Open(processPath);

visio.Page page = doc.Pages[1];
CreateVisio vis = new CreateVisio();
visio.Shape edit = page.DrawRectangle(3.2d, 6.9d, 4.9d, 7.9d);
Was it helpful?

Solution

How to Create a New VISIO Document

Microsoft.Office.Interop.Visio.Application application = 
        new Microsoft.Office.Interop.Visio.Application();  
application.Visible = false; 
Microsoft.Office.Interop.Visio.Document doc = application.Documents.Add(templatePath);
Microsoft.Office.Interop.Visio.Page page = application.Documents[1].Pages[1];

How to Get the Width and Height of the Sheet You are Working On

double xPosition = page.PageSheet.get_CellsU("PageWidth").ResultIU;
double yPosition = page.PageSheet.get_CellsU("PageHeight").ResultIU; 

We are using this information about the sheet width and height to know where to place the boxes. We are putting the root boxes in the middle of the sheet by dividing the sheet width by the number of roots. Also we are subtracting from the yPosition the level number so that the boxes with increasing level number will get a lower position on the chart.

How to Create a Shape and Place it on the Chart (Drop it)

//creating the type of shape in the organizational chart it could be: "Position", 
//"Executive", "Manager",  "Assistant" and others according 
//to what you have in your stencil. 
Microsoft.Office.Interop.Visio.Master position = doc.Masters.get_ItemU("Position"); 
//placing the shape in the xPosition and yPosition coordinates
Microsoft.Office.Interop.Visio.Shape shape = page.Drop(position, xPosition, yPosition);  

How to Set the Shapes Properties

//set the text
shape.Text = box.Name;

//set hyperlink
if (!String.IsNullOrEmpty(box.HyperLink.Trim()))
{
     Hyperlink link = shape.Hyperlinks.Add();
     link.Address = box.HyperLink;
}

//set the shape width
shape.get_CellsSRC(
                (short)Microsoft.Office.Interop.Visio.VisSectionIndices.
                visSectionObject,
                (short)Microsoft.Office.Interop.Visio.VisRowIndices.
                visRowXFormIn,
                (short)Microsoft.Office.Interop.Visio.VisCellIndices.
                visXFormWidth).ResultIU = box.Width;

//set the shape height
shape.get_CellsSRC(
               (short)Microsoft.Office.Interop.Visio.VisSectionIndices.
               visSectionObject,
               (short)Microsoft.Office.Interop.Visio.VisRowIndices.
               visRowXFormIn,
               (short)Microsoft.Office.Interop.Visio.VisCellIndices.
               visXFormHeight).ResultIU = box.Height;

//set the shape fore color
shape.Characters.set_CharProps(
                (short)Microsoft.Office.Interop.Visio.
                    VisCellIndices.visCharacterColor,
                (short)Utilities.GetVisioColor(box.ForeColor));

//set the shape back color
shape.get_CellsSRC((short)VisSectionIndices.visSectionObject,
         (short)VisRowIndices.visRowFill, 
    (short)VisCellIndices.visFillForegnd).FormulaU = 
    "RGB(" + box.BackColor.R.ToString() + "," + box.BackColor.G.ToString() + "," 
    + box.BackColor.B.ToString() + ")"; 

Connecting the shapes is done using the method connectWithDynamicGlueAndConnector(). This method accepts two parameters, the parent shape and the childShape and will create the connector between. This method is the exact one found in VISIO SDK.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top