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

Free Support Forum - aspose.com

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.

http://docs.aspose.com/display/wordsjava/Programming+with+Documents
http://docs.aspose.com/display/wordsjava/Document

http://docs.aspose.com/display/wordsjava/DocumentBuilder
http://docs.aspose.com/display/wordsjava/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.<o:p></o:p>

Document doc = new Document();<o:p></o:p>

<o:p></o:p>

// Create textbox shape.<o:p></o:p>

Shape textbox = new Shape(doc, ShapeType.TextBox);<o:p></o:p>

textbox.Width = 300;<o:p></o:p>

textbox.Height = 300;<o:p></o:p>

textbox.Stroked = false;<o:p></o:p>

<o:p> </o:p>

// Define the exact location of where the textbox will be located on the page in points.<o:p></o:p>

textbox.Left = 40;<o:p></o:p>

textbox.Top = 100;<o:p></o:p>

textbox.RelativeHorizontalPosition = RelativeHorizontalPosition.Page;<o:p></o:p>

textbox.RelativeVerticalPosition = RelativeVerticalPosition.Page;<o:p></o:p>

<o:p></o:p>

// Insert textbox into the document.<o:p></o:p>

builder.InsertNode(textbox);<o:p></o:p>

<o:p> </o:p>

// Move to the textbox and insert some content.<o:p></o:p>

builder.MoveTo(textbox);<o:p></o:p>

builder.Write(“Some text”);<o:p></o:p>

<o:p></o:p>

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,