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

Hi.

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

Hello

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.

line.setWidth(100);

// Set line width.

line.getStroke().setWeight(5);

//Insert shape into the document

builder.insertNode(line);

// Save output document.

doc.save("C:\\Temp\\out.doc");

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);

builder.getParagraphFormat().getBorders().getBottom().setLineStyle(LineStyle.SINGLE);

builder.getParagraphFormat().getBorders().getBottom().setLineWidth(1.5);

builder.getParagraphFormat().getBorders().getBottom().setColor(Color.GRAY);

doc.save("C:\\Temp\\out.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);

builder.insertHtml("


");

doc.save("C:\\Temp\\out.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):


[Test]
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
};

builder.InsertNode(line);

doc.Save(@“C:\HorizontalShapeWidth.docx”);
}


WRONG ideas::

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

@vignesh.sr

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;