none
Word Footer alignment RRS feed

  • Question

  • Hi Friends,

    I have a function which dynamically adds footer during word file creation,its working fine. After word file is displayed when I add AIP footer (Microsoft Azure Information Protection) Plugin Confidentiality Footer its getting added for newly created document, both footer are added.

    Now the problem is both are not in the same line, Confidential (C3) is slightly above the other footer User Added Footer Protection.

    Below is the Create method:

    private bool addText(object fileName, string language)
            {
                object missing = System.Reflection.Missing.Value;
                Microsoft.Office.Interop.Word.Document aDoc = null;
                var wordApp = new Microsoft.Office.Interop.Word.Application();
    
                if (File.Exists((string)fileName))
                {
                    object readOnly = false;
                    object isVisible = true;
    
                    wordApp.Visible = false;
    
                    aDoc = wordApp.Documents.Open(ref fileName, ref missing,
                            ref readOnly, ref missing, ref missing, ref missing,
                            ref missing, ref missing, ref missing, ref missing,
                            ref missing, ref isVisible, ref missing, ref missing,
                            ref missing, ref missing);
    
                    aDoc.Activate();
                   
                    foreach (Microsoft.Office.Interop.Word.Section wordSection in aDoc.Application.ActiveDocument.Sections)
                    {
    
                        Microsoft.Office.Interop.Word.Range footerRange = wordSection.Footers[Microsoft.Office.Interop.Word.WdHeaderFooterIndex.wdHeaderFooterFirstPage].Range;
                    
                             footerRange.InsertAfter("   User Added Footer Protection");                    
                                           
    
                    }
                 
                    aDoc.Save();
                    wordApp.Quit();
                }
                return true;
            }
    

    Monday, December 23, 2019 11:42 AM

All replies

  • I'm not really familiar with the AIP, but I had a look and...

    When AIP inserts a footer it inserts the text using a textbox, which is a "floating" object.

    That's why you're having difficulty aligning the text of an existing footer with the AIP footer.

    If you need to align it with an existing footer, it looks to me as if the simplest thing to do might be to reposition the textbox.

    However, you might have to do that *every time* the AIP inserted its footer, because it appears to put its textbook in a very specific place. I would have to look further to see what aspects of the document layout it looks at. But it would be difficult to make progress on this problem without feedback from you - e.g. at what point does your code run (is it before or after AIP adds its footer? Can you put the text where you like?

    (It's also not completely obvious how AIP identifies that a textbox "belongs" to AIP. As far as I can tell, it relies on the Name of the Shape in Word, and that's it. When it removes its footer, it doesn't even bother to remove the textbox, just the content. Or rather, the content of any shape with a "matching" name.)

    Peter Jamieson

    Friday, December 27, 2019 6:40 PM
  • Hi Peter,

    Thanks for the response.

    Initially word document is created with footer text which we have  passed, later we add the AIP Confidentiality footer. 

    by default Footer added by us is displayed from bottom 0.26'' ,if we can change the Text to be displayed From Bottom 0.15'' it will be in the same line of AIP Footer.


    

       Microsoft.Office.Interop.Word.Range do we have any properties using which we can set the Text From Bottom 0.15'' ? I also tried using footerRange.SetRange(900,500); but no use. 

    Please suggest me if there any other properties which i can try. 

    Regards,

    Praveen Kumar 

    Sunday, December 29, 2019 6:46 PM
  • It may be simpler to modify the ActiveDocument.PageSetup.FooterDistance.

    The trouble with trying to adjust the range is that you really need to change the vertical position of the paragraph, and that's not so simple because adjusting the "space above" doesn't really help very much.

    Another possibility might be to try to get the position and height information of the AIP textbox and insert your own textbox using those measurements. Here, the AIP adding appears to insert its footer in a textbox that is at an absolute position from the top of the page (26.67cm on an A4 page with 2.54cm margins).


    Peter Jamieson

    Monday, December 30, 2019 12:26 PM
  • Hi Peter,

    Thanks for the response.

    Can you please guide me how to implement Textbox in footer, I am not able to find any reference for the same.

    Rerards,

    Praveen Kumar

    Monday, January 6, 2020 7:27 PM
  • Hi Praveen,

    In the general case it's not at all easy. The code below should give you a general idea, and point out some of the things you might need to deal with. It assumes you want a right-aligned text box with text vertically aligned with he MSI box, on a fairly simple page layout. Not at all well-tested.

    Sub insertMatchingFooterBox()
    Const MSIBoxPrefix As String = "MSI"
    Const ourBoxPrefix As String = "OURBOX"
    ' I use a fixed width that I guessed.
    ' You might be able to make the text box fit the text width instead.
    Const ourBoxWidth As Single = 100#
    Dim doc As Word.Document
    Dim docSection As Word.Section
    Dim docFooter As Word.HeaderFooter
    Dim footerShape As Word.Shape
    Dim MSIBox As Word.Shape
    Dim ourBox As Word.Shape
    
    ' This example assumes a fairly simple left-right-layout, with no
    ' need to check mirror margins and so on.
    
    Set doc = ActiveDocument
    ' potentially multiple sections per document
    For Each docSection In doc.Sections
      ' potentially multiple footer types per section
      For Each docFooter In docSection.Footers
        ' may need to check for existence as well
        If Not docFooter.IsEmpty Then
          Set MSIBox = Nothing
          Set ourBox = Nothing
          For Each footerShape In docFooter.Shapes
            With footerShape
              If .Type = msoTextBox Then
                ' try to find *one* existing MSI text box
                ' NB, MSIBox and ourBox could be set more than once
                ' and *one* existing "our box"
                If UCase(Left(.Name, Len(MSIBoxPrefix))) = MSIBoxPrefix Then
                  Set MSIBox = footerShape
                ElseIf UCase(Left(.Name, Len(ourBoxPrefix))) = ourBoxPrefix Then
                  Set ourBox = footerShape
                End If
                If Not (MSIBox Is Nothing) And Not (ourBox Is Nothing) Then
                  Exit For
                End If
              End If
            End With
          Next
          If MSIBox Is Nothing Then ' no MSIBox - don't know what you want to do here!
            '
          Else 'MSI Box exists
            If ourBox Is Nothing Then
              Dim p As PageSetup
              Set p = docSection.PageSetup
    '          Set p = Nothing
              Set ourBox = docFooter.Shapes.AddTextbox( _
                Orientation:=msoTextOrientationHorizontal, _
                Left:=docSection.PageSetup.PageWidth - docSection.PageSetup.RightMargin - ourBoxWidth, _
                Top:=MSIBox.Top, _
                Width:=ourBoxWidth, _
                Height:=MSIBox.Height, _
                Anchor:=MSIBox.Anchor)
              Set p = Nothing
              With ourBox
                .Name = ourBoxPrefix ' do these have to be unique?
                ' Try to match the parts of the formatting we want to match.
                ' Unfortunately it is almost impossible to predict what you
                ' might need to achieve alignment
                With .TextFrame
                  With .TextRange
                    .Text = "Your footer text"
                    With .Paragraphs(1)
                      .Alignment = wdAlignParagraphRight
                      .Range.Font = MSIBox.TextFrame.TextRange.Paragraphs(1).Range.Font
                      .Range.Font.Size = MSIBox.TextFrame.TextRange.Paragraphs(1).Range.Font.Size
                      ' Not obvious ehat the UI does when you modify the vertical alignment
                      ' in a textbox so this is a kludge
                      .SpaceBefore = (MSIBox.Height - .LineSpacing) / 2
                    End With
                  End With
                  .VerticalAnchor = MSIBox.TextFrame.VerticalAnchor
                End With
                .ZOrder msoBringToFront
              End With
            Else
              Debug.Print "our box already exists"
              ' existing box - you might need to reposition it, or change the text in it.
              ' perhaps simpler to delete and use the same insertion code as above.
            End If
          End If
          Set ourBox = Nothing
          Set MSIBox = Nothing
                
        End If
      Next
    Next
    
    
    Set doc = Nothing
    End Sub
    


    Peter Jamieson

    Tuesday, January 7, 2020 12:15 PM