Answered Drawing in Word.Canvas using C#

  • Saturday, June 25, 2011 1:45 PM
     
     
    Hi all. I've some drawing method. This method possible to draw GDI+ object in canvas (User Control) Windows Form. Can I reuse them to draw same object on Word Canvas? I've try before to draw simple shape in Word canvas using Interop.Word, but it's very hard to set the shape position on that canvas. thanks all.

All Replies

  • Monday, June 27, 2011 8:48 AM
    Moderator
     
     

    Hi Lutfi

    No, it's not possible to "draw" directly on an Office document using any tools that aren't part of the Word/Office libraries.

    Which version of Word are you working with? Is there a particular reason you're using a "Canvas"?


    Cindy Meister, VSTO/Word MVP
  • Monday, June 27, 2011 11:32 AM
     
     

    I'm using Office Word 2007 and Microsoft.Office.Interop.Word 12.0 as library to access word component. In Word 2007 we can add Shape like Basic Shape (Square, Circle, Trapezoid, and others), Lines (Arrow, Line, Double Arrow, and others), and in the end of the option we can Add New Drawing Canvas.

    I've try draw a simple square shape in that canvas using C#, but I can't adjust the shape position exactly on that canvas. Can I reposition my shape on that canvas ? Or any other method to draw shapes in the simple way ?  Thanks a lot for your suggestion.

  • Tuesday, June 28, 2011 6:57 AM
    Moderator
     
     Answered

    Hi Lufti

    Thanks for the information about the version of Word you're using, but...

    You still haven't explained to me WHY you're using a Drawing Canvas. I'm familiar with some of the problems with the Drawing Canvas - your issue being one of them - which is why the functionality was "deprecated" going into Office 2007. You need to have a really good reason to use it, otherwise... don't.

    You can insert any Shape using the appropriate Shapes.Add*** methods, for example:

    theDoc.Shapes.AddShape(Office.MsoAutoShapeType.msoShapeType, 20f, 20f, 100f, 100f, ref missing);


    Cindy Meister, VSTO/Word MVP
  • Tuesday, June 28, 2011 9:51 AM
     
     

    Thanks for your advice. I'm using Drawing Canvas to produce editable image in my document (generated by my document generator app). Before that, I've tried produce editable rectangle using GDI+ DrawRectangle in Windows Form, save the rectangle as bitmap file, and insert into document using Clipboard. But when I resize this bitmap, the rectangle image will be blurred.

    Then, I try to draw directly in Word by adding Shape with defined size. I'm very sorry if my question isn't clear enough. This is I have just found the way to reposition my Word Shape using this code:

            private void ThisDocument_Startup(object sender, System.EventArgs e)
            {
                object missing = System.Type.Missing;
                this.Shapes.AddShape(1, 0, 0, 100, 50, ref missing); //original position : X = 0, Y = 0, original size : W = 100px, H = 50px
                object index = 1;
                Shape shape = Shapes.get_Item(ref index);
                shape.Left = 100; //left position = X position
                shape.Top = 100; //top position = Y position
            }

    Thanks a lot for your advice.


  • Tuesday, June 28, 2011 9:53 AM
     
     
    The Drawing Canvas used as grouping Shape object in document. Thanks
  • Tuesday, June 28, 2011 1:18 PM
    Moderator
     
     
    The Drawing Canvas used as grouping Shape object in document.

    There is also a Group (and Ungroup) method, which is what was used in Word before the Drawing Canvas was introduced (and is still used for those times when the Drawing Canvas is too "buggy" to work with.
    Cindy Meister, VSTO/Word MVP