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 for your inquiry. There are at least two ways to insert horizontal rule (horizontal line) in the document.
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");
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.
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("<hr />");
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.
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 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);
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 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: https://reference.aspose.com/words/java/com.aspose.words/pagesetup/#getOddAndEvenPagesHeaderFooter
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:
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");