Free Support Forum - aspose.com

How to format horizontal rule using .NET

Is there a way to insert a horizontal line? There are several InsertXYZ methods on the DocumentBuilder, but I didn't see one I thought would work.

I just want a simple black line ( HTML:


) to be inserted at the position of my choosing. I can do it in MS Word by going to Format | Borders and Shading and click the Horizontal Line button.

Hi,

Thank you for your interest in Aspose.Word.

You can use a workaround for the time being. Create a paragraph and set its bottom border to a line, then clear it in the next paragraph.

But we have logged your request and will implement it, please check back later.

Dmitry's way sounds easier than what we do in our app. But in case it helps ...












//
// Embeds a
by using a standard black line image
//
private void WriteRule(DocumentBuilder wdb) {
 wdb.InsertParagraph();
//wdb.ParagraphFormat.LeftIndent = 0;
Bitmap img = new Bitmap(RuleWidth,1, PixelFormat.Format32bppArgb);
Graphics g = Graphics.FromImage(img);
SolidBrush b = new SolidBrush(Color.FromArgb(102,122,132));
g.FillRectangle(b,0,0,RuleWidth,1);
 wdb.InsertImage(img);
}
//RuleWidth is in pixels, i think.

David

Anyway, we are going to implement


support soon. Then we will probably implement full horizontal line support - just like in Word.

Has this support for horizontal lines been added and how does one access it??

Yes, we now have support for paragraph borders, which are basically used in MS Word to draw horizontal lines in text. Here is a sample code that draws horizontal line above the first paragraph of the document:

Document doc = new Document(filename);

Paragraph paragraph = (Paragraph)doc.GetChild(NodeType.Paragraph, 0, true);

Border topBorder = paragraph.ParagraphFormat.Borders[BorderType.Top];

topBorder.Color = System.Drawing.Color.Black;

topBorder.LineStyle = LineStyle.Single;

topBorder.LineWidth = 1;

@mistercain

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

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;