We're sorry Aspose doesn't work properply without JavaScript enabled.

Free Support Forum - aspose.com

Insert and format horizontal rule in document using .NET


I am using Aspose words for java.How can i insert a Horizontal Rule in a document?


Thanks for your inquiry. There are at least two ways to insert horizontal rule (horizontal line) in the document.

1. If you would like to insert Horizontal Rule as a shape you can use code like you provided. But in this case, you need to know width of container, where you insert a Horizontal Rule.

Document doc = new Document();

DocumentBuilder builder = new DocumentBuilder(doc);

//Create shape

Shape line = new Shape(doc, ShapeType.LINE);

// Specify width of the shape.


// Set line width.


//Insert shape into the document


// Save output document.


2. If you just need to insert horizontal line, you can try inserting it as Paragraph border:

Document doc = new Document();

DocumentBuilder builder = new DocumentBuilder(doc);





This code does the same thing as if you type three consecutive hyphens, underscores, or equal signs and hit Enter in MS Word.

In this case, you do not need to be aware where you insert such Horizontal Rule. It will always occupy all available space.

3. You can also use InsertHtml to insert horizontal Rule. However, this approach more looks like a workaround. But anyways here is code:

Document doc = new Document();

DocumentBuilder builder = new DocumentBuilder(doc);




Hope this helps. Please let me know if you need more information, I will be glad to help you.

Best regards,

in .NET you do like this (it’s kinda ugly code):

public void HorizontalShapeWidth()
var doc = new Document();
var builder = new DocumentBuilder(doc);

var line = new Shape(doc.Document, ShapeType.Line)
Width = builder.PageSetup.PageWidth - builder.PageSetup.LeftMargin - builder.PageSetup.RightMargin



WRONG ideas::

// Width = PreferredWidth.FromPercent(100).Value,
// Width = 100


You can use DocumentBuilder.InsertHorizontalRule method to insert a horizontal rule shape into the document. This feature was added in Aspose.Words for .NET 18.10.

Following code example shows how to insert horizontal rule shape in a document and customize the formatting.

// Use a document builder to insert a horizontal rule
Document doc = new Document();
DocumentBuilder builder = new DocumentBuilder(doc);
Shape shape = builder.InsertHorizontalRule();

HorizontalRuleFormat horizontalRuleFormat = shape.HorizontalRuleFormat;
horizontalRuleFormat.Alignment = HorizontalRuleAlignment.Center;
horizontalRuleFormat.WidthPercent = 70;
horizontalRuleFormat.Height = 3;
horizontalRuleFormat.Color = Color.Blue;
horizontalRuleFormat.NoShade = true;