Unable to change textbox properties when LayoutCollector and LayoutEnumerator are used

Hi,

we are facing an issue with setting textbox properties (clearing text and applying color ) when using LayoutCollector and LayoutEnumerator.
Example code:

var wordDocument = new Document(@“D:\TestSignature\Test1.docx”);
LayoutCollector collector = new LayoutCollector(wordDocument);
LayoutEnumerator enumerator = new LayoutEnumerator(wordDocument);
var textBoxes = new List();

        foreach (Section section in wordDocument.Sections)
        {
            var shapes = section.GetChildNodes(NodeType.Shape, true);
            foreach (Shape textBox in shapes)
            {
                if (textBox.ShapeType != ShapeType.TextBox)
                    continue;
               
                var text = textBox.GetText().ToLower();
                if (text.StartsWith("signature"))
                {
                    enumerator.Current = collector.GetEntity(textBox);
                    textBox.RemoveAllChildren();
                    textBox.FillColor = Color.White;
                    textBox.StrokeColor = Color.White;
                    textBoxes.Add(textBox);
                    Console.WriteLine($"\tTextBox:  \t Width: {textBox.Width} \tHeight: {textBox.Height}");
                    Console.WriteLine(" --> Left : " + enumerator.Rectangle.Left + " Top : " + enumerator.Rectangle.Top + " Right: " + enumerator.Rectangle.Right + " Bottom: " + enumerator.Rectangle.Bottom);
                }
            }
        }

        wordDocument.Save(@"D:\TestSignature\Test1.pdf", new PdfSaveOptions());
The code should get all the textboxes in a Word document that start with the text *signature*, get their location and than clear the text and the background. This only works if:
  • The layout of the textbox is set to: Behind or In front of text

  • LayoutCollector and LayoutEnumerator are not used

Test file:
Test1.docx (20.3 KB)

@ivan.jovanovski,

Please simply call Document.UpdatePageLayout Method before saving to PDF.

            ...
            ...
            Console.WriteLine(" --> Left : " + enumerator.Rectangle.Left + " Top : " + enumerator.Rectangle.Top + " Right: " + enumerator.Rectangle.Right + " Bottom: " + enumerator.Rectangle.Bottom);
        }
    }
}
wordDocument.UpdatePageLayout();
wordDocument.Save(@"D:\TestSignature\Test1.pdf", new PdfSaveOptions());
1 Like

Thank you. That solved the issue.

1 Like