none
Adding WordArt to header when the header contains a table, the WordArt go inside the table RRS feed

  • Question

  • When i add a WordArt to the center of the header and the header contains a table, the WordArt will show up inside the table and not in the center of the page.

    Is there a way to prevent the textbox from going into the table?

    The property: LayoutInCell only works for some of the cases.

    Here is my code:

    var shape = shapes.AddTextEffect(MsoPresetTextEffect.msoTextEffect1, watermarkingData.Text, watermarkingData.FontName, 1, MsoTriState.msoFalse, MsoTriState.msoFalse, 0, 0);
    
    using (var textEffect = ComWrapper.Create(shape.Value.TextEffect))
    				textEffect.Value.NormalizedHeight = MsoTriState.msoFalse;
    
    			//hide the line of the shape and set the fill to solid
    			using (var line = ComWrapper.Create(shape.Value.Line))
    				line.Value.Visible = MsoTriState.msoFalse;
    			using (var fill = ComWrapper.Create(shape.Value.Fill))
    			{
    				fill.Value.Visible = MsoTriState.msoTrue;
    				fill.Value.Solid();
    				fill.Value.ForeColor.RGB = (Int32) GetWordColorFromHtmlColor(watermarkingData.FontColor);
    			}
    
    			using (var wrap = ComWrapper.Create(shape.Value.WrapFormat))
    			{
    				wrap.Value.AllowOverlap = (int) MsoTriState.msoTrue;
    				wrap.Value.Type = WdWrapType.wdWrapBehind;
    			}
    
    
    			shape.Value.Height = stringSize.Height;
    			shape.Value.Width = stringSize.Width;
    
    			shape.Value.RelativeHorizontalPosition = WdRelativeHorizontalPosition.wdRelativeHorizontalPositionMargin;
    			shape.Value.RelativeVerticalPosition = WdRelativeVerticalPosition.wdRelativeVerticalPositionMargin;
    
    			shape.Value.Left =(int)WdShapePosition.wdShapeCenter;
    			shape.Value.Top = (int)WdShapePosition.wdShapeCenter;
    
    			shape.Value.LayoutInCell = 0;


    • Edited by shenhavba Wednesday, September 21, 2016 9:22 AM
    Tuesday, September 20, 2016 7:11 AM

Answers

  • Hi,

    For the issue you mentioned above that you could see the view changing, I wonder if you are adding the watermark in the headerfooter view?

    Anyway, please try the code below, it could succeed in adding the watermark in your document whether in wdSeekMainDocument view or wdSeekCurrentPageHeader view.

    However, if you are adding in wdSeekCurrentPageHeader view, we are unable to avoid that it would change into wdSeekMainDocument view. 

        private void button11_Click(object sender, RibbonControlEventArgs e)
            {
                try
                {
                    Globals.ThisAddIn.Application.ScreenUpdating = false;
                    var acWin = Globals.ThisAddIn.Application.ActiveWindow;
                    //if (acWin.View.SplitSpecial != WdSpecialPane.wdPaneNone)
                    //{
                    //    acWin.Panes[2].Close();
                    //}
                    if (acWin.ActivePane.View.Type != WdViewType.wdPrintView)
                    {
                        acWin.ActivePane.View.Type = WdViewType.wdPrintView;
                        acWin.ActivePane.View.SeekView = WdSeekView.wdSeekMainDocument;  
                    }
                    acWin.ActivePane.View.SeekView = WdSeekView.wdSeekMainDocument;
    
    
                    Range currentSelect = Globals.ThisAddIn.Application.Selection.Range;
                    //Section section = Globals.ThisAddIn.Application.ActiveDocument.Sections[1];
                    foreach (Section section in Globals.ThisAddIn.Application.ActiveDocument.Sections)
                    {
                        // switch to headerfooter view
                    
                        Selection selectRng = Globals.ThisAddIn.Application.Selection;
                        section.Range.Select();
    
                        acWin.ActivePane.View.SeekView = WdSeekView.wdSeekCurrentPageHeader;
                        // table for test
                        //Table t = selectRng.HeaderFooter.Range.Tables.Add(Globals.ThisAddIn.Application.Selection.Range, 1, 1);
                        //t.set_Style(WdBuiltinStyle.wdStyleTableLightGrid);
                        //t.Borders.InsideColor = WdColor.wdColorBlue;
    
                        // Collapse the selection of table, if using wdCollapseStart, LayoutInCell should be false
                        if (selectRng.HeaderFooter.Range.Tables.Count > 0)
                        {
                            selectRng.HeaderFooter.Range.Tables[1].Select();
                            Globals.ThisAddIn.Application.Selection.Collapse(WdCollapseDirection.wdCollapseEnd);
                            Globals.ThisAddIn.Application.Selection.MoveDown();
                        }
    
    
                        // add word art
                        var testShape = selectRng.HeaderFooter.Shapes.AddTextEffect(Microsoft.Office.Core.MsoPresetTextEffect.msoTextEffect29, "test",
                            "Arial Black", 24, Microsoft.Office.Core.MsoTriState.msoFalse,
                            Microsoft.Office.Core.MsoTriState.msoFalse, 0, 0);
                        // set position
                        testShape.Select();
                        testShape.RelativeHorizontalPosition = WdRelativeHorizontalPosition.wdRelativeHorizontalPositionMargin;
                        testShape.RelativeVerticalPosition = WdRelativeVerticalPosition.wdRelativeVerticalPositionMargin;
                        testShape.Left = (int)WdShapePosition.wdShapeCenter;
                        testShape.Top = (int)WdShapePosition.wdShapeCenter;
                        testShape.LayoutInCell = 0;
    
                        // switch to maindocument 
                        acWin.ActivePane.View.SeekView = WdSeekView.wdSeekMainDocument;
    
                        // go back to the original location 
                        currentSelect.Select();
                        //Globals.ThisAddIn.Application.GoBack();
                        Globals.ThisAddIn.Application.Selection.Collapse();
                        Globals.ThisAddIn.Application.ScreenUpdating = true;
                    }
                }
                catch (Exception ex)
                {
                    System.Windows.Forms.MessageBox.Show(ex.ToString());
                }
            }


    Thursday, September 29, 2016 9:28 AM
    Moderator

All replies

  • Based on your code, it seems that you are adding WordArt shape.

    What do you want to add, textbox or WordArt?

    And how do you position the table in the header?

    Wednesday, September 21, 2016 8:03 AM
    Moderator
  • Sorry, i am adding a WordArt not a textbox.

    I am not adding the table to the document.

    I need to add watermarking to documents, sometimes those documents has a table in the header and i can control the position of the table.

    The problems is, that with some of the documents, the watermarking is not centered correctly and i noticed that if i delete the table, the watermarking is also deleted.

    Wednesday, September 21, 2016 9:21 AM
  • The issue causes from the Anchor of the WordArt would be positioned at the table, so the WordArt would be inside of the table.

    To avoid the issue, I think you could see the code below to work around.

    Use

                Selection.Collapse(WdCollapseDirection.wdCollapseEnd);

                Selection.MoveDown();

    To make selection out of the table then add the wordart.

      Globals.ThisAddIn.Application.ScreenUpdating = false;
             
                Selection Selection = Globals.ThisAddIn.Application.Selection;
             
                Globals.ThisAddIn.Application.ActiveDocument.Sections[1].Range.Select();
                Globals.ThisAddIn.Application.ActiveWindow.ActivePane.View.SeekView = WdSeekView.wdSeekCurrentPageHeader;
                // add a table for testing
                Table t = Selection.HeaderFooter.Range.Tables.Add(Selection.HeaderFooter.Range, 1, 1);
                t.Select();
                Selection.Collapse(WdCollapseDirection.wdCollapseEnd);
                Selection.MoveDown();
                // add the WordArt
                float leftPosition = (float)Selection.Information[
                    Microsoft.Office.Interop.Word.WdInformation.wdHorizontalPositionRelativeToPage];
    
                float topPosition = (float)Selection.Information[
                    Microsoft.Office.Interop.Word.WdInformation.wdVerticalPositionRelativeToPage];
    
                Microsoft.Office.Interop.Word.Shape shape = Selection.HeaderFooter.Shapes.AddTextEffect(
                   Microsoft.Office.Core.MsoPresetTextEffect.msoTextEffect29, "test",
                   "Arial Black", 24, Microsoft.Office.Core.MsoTriState.msoFalse,
                   Microsoft.Office.Core.MsoTriState.msoFalse, leftPosition, topPosition);
                // change its position
                shape.Select();
                shape.RelativeHorizontalPosition = WdRelativeHorizontalPosition.wdRelativeHorizontalPositionMargin;
                shape.RelativeVerticalPosition = WdRelativeVerticalPosition.wdRelativeVerticalPositionMargin;            
                shape.Left = (int)WdShapePosition.wdShapeCenter;
                shape.Top = (int)WdShapePosition.wdShapeCenter;
    
                Globals.ThisAddIn.Application.ActiveWindow.ActivePane.View.SeekView = WdSeekView.wdSeekMainDocument;
                Globals.ThisAddIn.Application.ScreenUpdating = true;
    

    Friday, September 23, 2016 9:01 AM
    Moderator
  • Thanks for your help.

    Unfortunately, your sample did not work for me, the WordArt position was in the middle but it deleted every shape from the header and caused the cursor to jump to the last page. 

    Friday, September 23, 2016 10:31 AM
  • Hi,

    I am trying to figure out why adding the new shape would delete the old shapes and try to find if there are any other workarounds to set the anchor avoiding the cursor jumps to the last page.

    Your patience will be greatly appreciated.

    Sorry for any inconvenience and have a nice day! 

    Regards,

    Celeste

    Monday, September 26, 2016 10:57 AM
    Moderator
  • Hi,

    Please have a test on the code below.

                Range currentSelect = Globals.ThisAddIn.Application.Selection.Range ;
                Globals.ThisAddIn.Application.ScreenUpdating = false;
    
                Section section = Globals.ThisAddIn.Application.ActiveDocument.Sections[1];
                // switch to headerfooter view
                Selection selectRng = Globals.ThisAddIn.Application.Selection;
                section.Range.Select();
                Globals.ThisAddIn.Application.ActiveWindow.ActivePane.View.SeekView = WdSeekView.wdSeekCurrentPageHeader;
                // table for test
                Table t = selectRng.HeaderFooter.Range.Tables.Add(Globals.ThisAddIn.Application.Selection.Range, 1, 1);
                t.set_Style(WdBuiltinStyle.wdStyleTableLightGrid);
                t.Borders.InsideColor = WdColor.wdColorBlue;
    
                // Collapse the selection of table, if using wdCollapseStart, LayoutInCell should be false
    
                Globals.ThisAddIn.Application.Selection.Collapse(WdCollapseDirection.wdCollapseStart);
                //Globals.ThisAddIn.Application.Selection.MoveDown(WdUnits.wdLine);
                // add word art
                var testShape = selectRng.HeaderFooter.Shapes.AddTextEffect(Microsoft.Office.Core.MsoPresetTextEffect.msoTextEffect29, "test",
                    "Arial Black", 24, Microsoft.Office.Core.MsoTriState.msoFalse,
                    Microsoft.Office.Core.MsoTriState.msoFalse, 0, 0);
                // set position
                testShape.Select();
                testShape.RelativeHorizontalPosition = WdRelativeHorizontalPosition.wdRelativeHorizontalPositionMargin;
                testShape.RelativeVerticalPosition = WdRelativeVerticalPosition.wdRelativeVerticalPositionMargin;
                testShape.Left = (int)WdShapePosition.wdShapeCenter;
                testShape.Top = (int)WdShapePosition.wdShapeCenter;
                testShape.LayoutInCell = 0;
    
                // switch to maindocument 
                Globals.ThisAddIn.Application.ActiveWindow.ActivePane.View.SeekView = WdSeekView.wdSeekMainDocument;
                // go back to the original location 
                currentSelect.Select();
                Globals.ThisAddIn.Application.GoBack();
                Globals.ThisAddIn.Application.Selection.Collapse();
    
                Globals.ThisAddIn.Application.ScreenUpdating = true;

    Monday, September 26, 2016 4:47 PM
    Moderator
  • Hi,

    Now the WordArt is in the upper left corner inside the table, not in the middle.

    And this line: 

    Globals.ThisAddIn.Application.ActiveWindow.ActivePane.View.SeekView = WdSeekView.wdSeekCurrentPageHeader;

    The change is visible on the screen, i can see the view changing, ScreenUpdating does not work in that case.

    Thanks for trying to help!

    Tuesday, September 27, 2016 5:46 AM
  • Hi,

    Before Collapsing the selection, we need to select the table.

    Since it might be no table in the documents, please try.

                if (selectRng.HeaderFooter.Range.Tables.Count>0)

                {

                    selectRng.HeaderFooter.Range.Tables[1].Select();

                    Globals.ThisAddIn.Application.Selection.Collapse(WdCollapseDirection.wdCollapseEnd);

                    Globals.ThisAddIn.Application.Selection.MoveDown();

                }

     

    And I suggest you add the following to avoid the view switching into Draft

                var acWin = Globals.ThisAddIn.Application.ActiveWindow;

                if (acWin.View.SplitSpecial != WdSpecialPane.wdPaneNone)

                {

                    acWin.Panes[2].Close();

                    if (acWin.ActivePane.View.Type != WdViewType.wdPrintView)

                    {

                        acWin.ActivePane.View.Type = WdViewType.wdPrintView;

                        acWin.ActivePane.View.SeekView = WdSeekView.wdSeekMainDocument;

                    }

                }

    For the seekview, I could not reproduce and I am not sure why your change is visible, could you please share a sample document with us for testing?


    Tuesday, September 27, 2016 6:25 AM
    Moderator
  • I really appreciate your help.

    I managed to crate a problematic document(the original file was rms protected)

    You can download it from:

    https://drive.google.com/open?id=0B3QlDNcSTXqLQTB2VEpiNDR2TjQ

    Thanks

    Wednesday, September 28, 2016 3:11 PM
  • Hi,

    For the issue you mentioned above that you could see the view changing, I wonder if you are adding the watermark in the headerfooter view?

    Anyway, please try the code below, it could succeed in adding the watermark in your document whether in wdSeekMainDocument view or wdSeekCurrentPageHeader view.

    However, if you are adding in wdSeekCurrentPageHeader view, we are unable to avoid that it would change into wdSeekMainDocument view. 

        private void button11_Click(object sender, RibbonControlEventArgs e)
            {
                try
                {
                    Globals.ThisAddIn.Application.ScreenUpdating = false;
                    var acWin = Globals.ThisAddIn.Application.ActiveWindow;
                    //if (acWin.View.SplitSpecial != WdSpecialPane.wdPaneNone)
                    //{
                    //    acWin.Panes[2].Close();
                    //}
                    if (acWin.ActivePane.View.Type != WdViewType.wdPrintView)
                    {
                        acWin.ActivePane.View.Type = WdViewType.wdPrintView;
                        acWin.ActivePane.View.SeekView = WdSeekView.wdSeekMainDocument;  
                    }
                    acWin.ActivePane.View.SeekView = WdSeekView.wdSeekMainDocument;
    
    
                    Range currentSelect = Globals.ThisAddIn.Application.Selection.Range;
                    //Section section = Globals.ThisAddIn.Application.ActiveDocument.Sections[1];
                    foreach (Section section in Globals.ThisAddIn.Application.ActiveDocument.Sections)
                    {
                        // switch to headerfooter view
                    
                        Selection selectRng = Globals.ThisAddIn.Application.Selection;
                        section.Range.Select();
    
                        acWin.ActivePane.View.SeekView = WdSeekView.wdSeekCurrentPageHeader;
                        // table for test
                        //Table t = selectRng.HeaderFooter.Range.Tables.Add(Globals.ThisAddIn.Application.Selection.Range, 1, 1);
                        //t.set_Style(WdBuiltinStyle.wdStyleTableLightGrid);
                        //t.Borders.InsideColor = WdColor.wdColorBlue;
    
                        // Collapse the selection of table, if using wdCollapseStart, LayoutInCell should be false
                        if (selectRng.HeaderFooter.Range.Tables.Count > 0)
                        {
                            selectRng.HeaderFooter.Range.Tables[1].Select();
                            Globals.ThisAddIn.Application.Selection.Collapse(WdCollapseDirection.wdCollapseEnd);
                            Globals.ThisAddIn.Application.Selection.MoveDown();
                        }
    
    
                        // add word art
                        var testShape = selectRng.HeaderFooter.Shapes.AddTextEffect(Microsoft.Office.Core.MsoPresetTextEffect.msoTextEffect29, "test",
                            "Arial Black", 24, Microsoft.Office.Core.MsoTriState.msoFalse,
                            Microsoft.Office.Core.MsoTriState.msoFalse, 0, 0);
                        // set position
                        testShape.Select();
                        testShape.RelativeHorizontalPosition = WdRelativeHorizontalPosition.wdRelativeHorizontalPositionMargin;
                        testShape.RelativeVerticalPosition = WdRelativeVerticalPosition.wdRelativeVerticalPositionMargin;
                        testShape.Left = (int)WdShapePosition.wdShapeCenter;
                        testShape.Top = (int)WdShapePosition.wdShapeCenter;
                        testShape.LayoutInCell = 0;
    
                        // switch to maindocument 
                        acWin.ActivePane.View.SeekView = WdSeekView.wdSeekMainDocument;
    
                        // go back to the original location 
                        currentSelect.Select();
                        //Globals.ThisAddIn.Application.GoBack();
                        Globals.ThisAddIn.Application.Selection.Collapse();
                        Globals.ThisAddIn.Application.ScreenUpdating = true;
                    }
                }
                catch (Exception ex)
                {
                    System.Windows.Forms.MessageBox.Show(ex.ToString());
                }
            }


    Thursday, September 29, 2016 9:28 AM
    Moderator