Horizontal Line in Docx

Jun 26, 2014 at 12:16 AM
How can I add a horizontal line after any line in doc file?
Jun 30, 2014 at 10:33 AM
It's not possible yet, as far as I know.

If you're using the latest source code, you could add the following method to the Paragraph class:
        public void InsertHorizontalLine(string lineType= "single", int size = 6, int space = 1, string color = "auto")
            var pPr = this.GetOrCreate_pPr();
            var border = pPr.Element(XName.Get("pBdr", DocX.w.NamespaceName));
            if (border == null)
                pPr.Add(new XElement(XName.Get("pBdr", DocX.w.NamespaceName)));
                border = pPr.Element(XName.Get("pBdr", DocX.w.NamespaceName));
                border.Add(new XElement(XName.Get("bottom", DocX.w.NamespaceName)));
                var bottom = border.Element(XName.Get("bottom", DocX.w.NamespaceName));
                bottom.SetAttributeValue(XName.Get("val", DocX.w.NamespaceName), lineType);
                bottom.SetAttributeValue(XName.Get("sz", DocX.w.NamespaceName), size.ToString());
                bottom.SetAttributeValue(XName.Get("space", DocX.w.NamespaceName), space.ToString());
                bottom.SetAttributeValue(XName.Get("color", DocX.w.NamespaceName), color);

I put that together real quick, so it might need a bit of work. For now, it can be used as:
Paragraph p = someDoc.InsertParagraph();
Paragraph p = someDoc.InsertParagraph();
p.InsertHorizontalLine("single", 12, 1, "#FF0000");
The first will insert a simple black line. The second offers the option of changing the color and size (and type and space) and inserts a thicker, red line.
Jul 1, 2014 at 12:18 AM
Edited Jul 1, 2014 at 12:21 AM
Thanks. I have taken another simpler approach. I just kept the text within a 1x1 table and displayed only bottom border.

You can use table.AutoFit = AutoFit.Window if required.
Marked as answer by SudiptaB on 6/30/2014 at 5:23 PM