Using Aspose Words for generating Word template with absolute positioning

We need to generate Word templates containing absolutely position boxes containing company addresses etc. (basically letter heads). User will enter their custom data like phone numbers
into a web form and we want to generate a Word template from that. Absolute positioning of boxes
is a must. Is this doable?

Hi Andreas,

Thanks for your query. Yes can create your desired word template by using Aspose.Words. Please read following documentation links for your kind reference. It would be great, If you create a sample word template by using MS word so that we can provide your code to create such template.

https://docs.aspose.com/words/java/programming-with-documents/
https://reference.aspose.com/words/java/com.aspose.words/document/

https://reference.aspose.com/words/java/com.aspose.words/documentbuilder/
https://docs.aspose.com/words/java/introduction-and-creating-tables/

This answer is not very helpful…once again: is there some example code
showing how to create a text box with a given width/height that is positioned at
an arbitrary position given through a top+left parameter?

Hi Andreas,

Thanks for your inquiry.

Please see the code below for how to insert a new textbox into a document. As you can see the code produces a textbox that is position floating (absolutely positioned).

// Create or open document.
Document doc = new Document();

// Create textbox shape.
Shape textbox = new Shape(doc, ShapeType.TextBox);
textbox.Width = 300;
textbox.Height = 300;
textbox.Stroked = false;

// Define the exact location of where the textbox will be located on the page in points.
textbox.Left = 40;
textbox.Top = 100;
textbox.RelativeHorizontalPosition = RelativeHorizontalPosition.Page;
textbox.RelativeVerticalPosition = RelativeVerticalPosition.Page;

// Insert textbox into the document.
builder.InsertNode(textbox);

// Move to the textbox and insert some content.
builder.MoveTo(textbox);
builder.Write("Some text");

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

Thanks,

Firstly, as said: we are using Java. So I modified your code to work with Jython:

from com.aspose.words import * ;
doc = Document();
builder = DocumentBuilder(doc);
textbox = Shape(doc, ShapeType.TEXT_BOX);
textbox.setWidth(400);
textbox.setHeight(200)
textbox.setStroked(False);
textbox.setLeft(40)
textbox.setTop(100);
textbox.setRelativeHorizontalPosition(RelativeHorizontalPosition.PAGE)
textbox.setRelativeVerticalPosition(RelativeVerticalPosition.PAGE);
#textbox.setContent("Some text");
builder.insertNode(textbox);
builder.moveTo(textbox);
doc.save('foo.doc')

This code actually create an empty textbox. Your code adds a text to the body of the document but not to the TextBox. I can not find any example how to insert text into the created text box here.

-aj

Hi,

Thanks for your inquiry.I think, you can achieve what you need by using the following code snippet:
// Create a blank document.

Document doc = new Document();
// Create a new shape of type TextBox
Shape textBox = new Shape(doc, ShapeType.TEXT_BOX);
// Set some settings of the textbox itself.
// Set the wrap of the textbox to inline
textBox.setWrapType(WrapType.NONE);
// Set the horizontal and vertical alignment of the text inside the shape.
textBox.setHorizontalAlignment(HorizontalAlignment.CENTER);
textBox.setVerticalAlignment(VerticalAlignment.TOP);
// Set the textbox height and width.
textBox.setHeight(50);
textBox.setWidth(200);
// Set the textbox in front of other shapes with a lower ZOrder
textBox.setZOrder(2);
// Let’s create a new paragraph for the textbox manually and align it in the center. Make sure we add the new nodes to the textbox as well.
textBox.appendChild(new Paragraph(doc));
Paragraph para = textBox.getFirstParagraph();
para.getParagraphFormat().setAlignment(ParagraphAlignment.CENTER);
// Add some text to the paragraph.
Run run = new Run(doc);
run.setText("Content in textbox");
para.appendChild(run);
// Append the textbox to the first paragraph in the body.
doc.getFirstSection().getBody().getFirstParagraph().appendChild(textBox);
// Save the output
doc.save(getMyDir() + "Shape.CreateTextBox Out.doc");

I hope, this will help.

Best Regards,

Hi Andreas,

Thanks for your inquiry.

The first code snippet should work as expected, the problem occurs because you didn’t translate the code quite right. If you move the line builder.write(“Some text”); to after builder.moveTo(textbox) the content should appear inside the textbox.

Thanks,