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.

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.

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



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.setContent("Some text");

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.



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
// Set the horizontal and vertical alignment of the text inside the shape.
// Set the textbox height and width.
// Set the textbox in front of other shapes with a lower ZOrder
// 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();
// Add some text to the paragraph.
Run run = new Run(doc);
run.setText("Content in textbox");
// Append the textbox to the first paragraph in the body.
// Save the output + "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.