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

Free Support Forum - aspose.com

Insert horizontal rule using Java | Line shape

Hello



i’m quite new to Aspose Words and still figuring out different issues. I’m using the builder to create a Word Document.



I already searched the forum here to get some information. Most of the found information are about older versions i guess. My vague Idea about building a line is the following:



//Create shape

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



//Insert shape into the document

builder.insertNode(line);



…but unfortunately it doesn’t work…



How can I insert a horizontal line into my document and how can I specify the format (like width, length, startpoint, endpoint,…)



Thanks a lot in advance



Regards,

christian

Hi Christian,

<?xml:namespace prefix = o ns = "urn:schemas-microsoft-com:office:office" />

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.

Hi Alexey,

thanks a lot for your help so far! i guess i’ll go for the second or third opportunity!



I’ll get back to you as soon as I have further questions.



Best Regards,

christian

Hi Alexey,



now i ran into another problem: I need three lines spread over the page at certain positions (two inside the header area and one inside the footer area). I was trying different ways to position the shape but it seems as i didn’t get the right way to manage this.

i tried the following:



[…]

//insert Lines

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

line1.setBehindText(true);

line1.setRelativeHorizontalPosition(RelativeHorizontalPosition.PAGE);

line1.setHorizontalAlignment(HorizontalAlignment.CENTER);

line1.setVerticalAlignment(VerticalAlignment.TOP);

line1.setDistanceTop(ConvertUtil.millimeterToPoint(10));

// Specify width of the shape.

line1.setWidth(ConvertUtil.millimeterToPoint(200));

// Set line width.

line1.getStroke().setWeight(1);



[…]



Is there a way to define a certain “origin-point” for my shape ?!? independend of pages, paragraphs,… ?





thanks a lot



best regards,

christian

Hi Christian,

<?xml:namespace prefix = o ns = "urn:schemas-microsoft-com:office:office" />

Thanks for your inquiry. Please try using the following code:

//Create shape

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

// Set text wrapping of the shape to none.

line.setWrapType(WrapType.NONE);

line.setRelativeHorizontalPosition(RelativeHorizontalPosition.PAGE);

line.setRelativeVerticalPosition(RelativeVerticalPosition.PAGE);

line.setTop(ConvertUtil.millimeterToPoint(10));

// Specify width of the shape.

line.setWidth(ConvertUtil.millimeterToPoint(200));

// Set line width.

line.getStroke().setWeight(1);

Hope this helps.

Best regards.

Hi Alexey,



well this helps for one line - but how do i manage to get the other two andmake them appear on every page ?



Here’s the code I use while my “initial setup” for the Document:



[…]

//Linie einfügen

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

line1.setBehindText(true);

line1.setWrapType(WrapType.NONE);

line1.setHorizontalAlignment(HorizontalAlignment.CENTER);

// Set text wrapping of the shape to none.

line1.setRelativeHorizontalPosition(RelativeHorizontalPosition.PAGE);

line1.setRelativeVerticalPosition(RelativeVerticalPosition.PAGE);

line1.setTop(ConvertUtil.millimeterToPoint(10));

// Specify width of the shape.

line1.setWidth(ConvertUtil.millimeterToPoint(200));

// Set line width.

line1.getStroke().setWeight(1);



//Linie2 einfügen

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

line2.setBehindText(true);

line2.setWrapType(WrapType.NONE);

line2.setRelativeHorizontalPosition(RelativeHorizontalPosition.PAGE);

line2.setHorizontalAlignment(HorizontalAlignment.CENTER);

// Set text wrapping of the shape to none.

line2.setRelativeHorizontalPosition(RelativeHorizontalPosition.PAGE);

line2.setRelativeVerticalPosition(RelativeVerticalPosition.PAGE);

line2.setTop(ConvertUtil.millimeterToPoint(20));

// Specify width of the shape.

line2.setWidth(ConvertUtil.millimeterToPoint(200));

// Set line width.

line2.getStroke().setWeight(1);







// Header



builder.moveToHeaderFooter(HeaderFooterType.HEADER_EVEN);

builder.insertNode(line1);

builder.insertNode(line2);

builder.getParagraphFormat().setStyle(styleP);

builder.getParagraphFormat().setAlignment(ParagraphAlignment.RIGHT);

builder.insertField(“PAGE”, “”);

builder.moveToDocumentEnd();



builder.moveToHeaderFooter(HeaderFooterType.HEADER_PRIMARY);

builder.insertNode(line1);

builder.insertNode(line2);

builder.getParagraphFormat().setStyle(styleP);

builder.getParagraphFormat().setAlignment(ParagraphAlignment.LEFT);

builder.insertField(“PAGE”, “”);

builder.moveToDocumentEnd();



[…]





where’s my fault here ? I have different shapes and call them one by another in the header dialogue as i thought - they should appear on every page…



Thanks a lot n best regards



christian

Hi

<?xml:namespace prefix = o ns = "urn:schemas-microsoft-com:office:office" />

Thanks for your inquiry. I think in your case you do not need to use Even header at all. This header is displayed only if you specify the following option:

http://www.aspose.com/documentation/.net-components/aspose.words-for-.net-and-java/com/aspose/words/pagesetup.html#OddAndEvenPagesHeaderFooter

If this option is not specified, only Primary header will be displayed (or Primary and FirstPage header if DifferentFirstPageHeaderFooter option is specified.).

Also in your case you insert the same nodes into the Primary and Into the Even headers. In this case, when you insert nodes second time, they are moved to the new location. If you need to insert nodes into both of headers, you need to use code like the following:

builder.moveToHeaderFooter(HeaderFooterType.HEADER_EVEN);

builder.insertNode(line1.deepClone(true));

builder.insertNode(line2.deepClone(true));

builder.getParagraphFormat().setStyle(styleP);

builder.getParagraphFormat().setAlignment(ParagraphAlignment.RIGHT);

builder.insertField("PAGE", "");

builder.moveToDocumentEnd();

builder.moveToHeaderFooter(HeaderFooterType.HEADER_PRIMARY);

builder.insertNode(line1.deepClone(true));

builder.insertNode(line2.deepClone(true));

builder.getParagraphFormat().setStyle(styleP);

builder.getParagraphFormat().setAlignment(ParagraphAlignment.LEFT);

builder.insertField("PAGE", "");

builder.moveToDocumentEnd();

Hope this helps.

Best regards.

Hi Alexey,

that’s it ! Thanks a lot!



Best regards,

christian

@chawo

Starting from Aspose.Words for Java 18.10, you can add horizontal rule shape into the 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.getHorizontalRuleFormat();
horizontalRuleFormat.setAlignment(HorizontalRuleAlignment.CENTER);
horizontalRuleFormat.setWidthPercent(70);
horizontalRuleFormat.setHeight(3);
horizontalRuleFormat.setColor(Color.BLUE);
horizontalRuleFormat.setNoShade(true);
doc.save(MyDir + "output.docx");