LeftMargin

Somebody Knows how I can put an textBox inside of leftMargin?
Thanks in advance

Hi Antonio,

Thanks for your inquiry. The TextBox class defines attributes that specify how a text is displayed inside a shape. You can use the TextBox property to access text properties of a shape. Pleas read the members of TextBox class from here:
https://reference.aspose.com/words/net/aspose.words.drawing/textbox/

Following code example creates a textbox with some text and different formatting options in a new document. Hope this helps you.

// Create a blank document.
Document doc = new Document();
// Create a new shape of type TextBox
Shape textBox = new Shape(doc, ShapeType.TextBox);
// Set some settings of the textbox itself.
// Set the wrap of the textbox to inline
textBox.WrapType = WrapType.None;
// Set the horizontal and vertical alignment of the text inside the shape.
textBox.HorizontalAlignment = Aspose.Words.Drawing.HorizontalAlignment.Center;
textBox.VerticalAlignment = VerticalAlignment.Top;
// Set the textbox height and width.
textBox.Height = 50;
textBox.Width = 200;
textBox.TextBox.InternalMarginLeft = 0.0;
textBox.TextBox.InternalMarginTop = 0.0;
// Set the textbox in front of other shapes with a lower ZOrder
textBox.ZOrder = 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.FirstParagraph;
para.ParagraphFormat.Alignment = ParagraphAlignment.Left;
// Add some text to the paragraph.
Run run = new Run(doc);
run.Text = "Content in textbox";
para.AppendChild(run);
// Append the textbox to the first paragraph in the body.
doc.FirstSection.Body.FirstParagraph.AppendChild(textBox);
// Save the output
doc.Save(MyDir + "Shape.CreateTextBox Out.doc");

If this does not help you, please manually create your expected Word document using Microsoft Word and attach it here for our reference. We will then provide you more information along with code.

Hi,
Thanks for your reply.
But what I want is write inside of margin of document instaed of margin of TextBox.
Your answer was usefull but didn’t helped me.
Here I have my code for Java:
public

void insertInLeftMarginInDocument(File file)
{
    try
    {

        FileInputStream fileInputStream = new FileInputStream(file);
        com.aspose.words.Document doc = new com.aspose.words.Document(fileInputStream);
        // DocumentBuilder builder = new DocumentBuilder(doc);
        // Create a new shape of type TextBox

        Shape textBox = new Shape(doc, ShapeType.TEXT_BOX);
        // 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.LEFT);

        textBox.setAllowOverlap(true);
        textBox.setDistanceLeft(0);
        // Set the textbox height and width.
        textBox.setHeight(100);
        textBox.setWidth(10);
        textBox.getTextBox().setInternalMarginLeft(0);
        textBox.getTextBox().setInternalMarginTop(50);
        // 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.LEFT);

        // 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);
        doc.save(file.getAbsolutePath());
    }
    catch (Exception e)
    {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
}

public void insertInLeftMarginInDocument(File file)
{
    try
    {

        FileInputStream fileInputStream = new FileInputStream(file);
        com.aspose.words.Document doc = new com.aspose.words.Document(fileInputStream);
        // DocumentBuilder builder = new DocumentBuilder(doc);
        // Create a new shape of type TextBox

        Shape textBox = new Shape(doc, ShapeType.TEXT_BOX);
        // 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.LEFT);

        textBox.setAllowOverlap(true);
        textBox.setDistanceLeft(0);
        // Set the textbox height and width.
        textBox.setHeight(100);
        textBox.setWidth(10);
        textBox.getTextBox().setInternalMarginLeft(0);
        textBox.getTextBox().setInternalMarginTop(50);
        // 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.LEFT);

        // 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);
        doc.save(file.getAbsolutePath());
    }
    catch (Exception e)
    {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
}

Hi Antonio,

Thanks for sharing the detail. In your case, please use DocumentBuilder to write the contents into your document. The DocumentBuilder has an internal cursor that you can navigate to a different location in a document using various DocumentBuilder.MoveToXXX methods such as DocumentBuilder.MoveToDocumentStart and DocumentBuilder.MoveToField. Please read following documentation links for your kind reference.
https://docs.aspose.com/words/java/document-builder-overview/
https://docs.aspose.com/words/java/use-documentbuilder-to-insert-document-elements/
https://docs.aspose.com/words/java/programming-with-documents/
https://docs.aspose.com/words/java/navigation-with-cursor/

Following code shows how to use MoveToXXX method.

Document doc = new Document(getMyDir() + "DocumentBuilder.WorkingWithNodes.doc");

DocumentBuilder builder = new DocumentBuilder(doc);
// Move to a bookmark and delete the parent paragraph.
builder.moveToBookmark("ParaToDelete");
builder.getCurrentParagraph().remove();
// Move to a particular paragraph's run and replace all occurrences of "bad" with "good" within this run.
builder.moveTo(doc.getLastSection().getBody().getParagraphs().get(0).getRuns().get(0));
builder.getCurrentNode().getRange().replace("bad", "good", false, true);
// Mark the beginning of the document.
builder.moveToDocumentStart();
builder.writeln("Start of document.");
// Mark the ending of the document.
builder.moveToDocumentEnd();
builder.writeln("End of document.");
doc.save(getMyDir() + "DocumentBuilder.WorkingWithNodes Out.doc");

Please note that Aspose.Words is quite different from the Microsoft Word’s Object Model in that it represents the document as a tree of objects more like an XML DOM tree. When you load a Word document into Aspose.Words, it builds its DOM and all document elements and formatting are simply loaded into memory. Please read the following articles for more information on DOM:
https://docs.aspose.com/words/java/aspose-words-document-object-model/
https://docs.aspose.com/words/java/logical-levels-of-nodes-in-a-document/
Hope this answers your query. If this does not help you, please manually create your expected Word document using Microsoft Word and attach it here for our reference. We will then provide you more information on this along with code.

Hi,
I think you cannot understand well my question.
I want to create TextBox, or write in margins of document, I don’t want start in first paragraph, or the first section, etc. So if it possible with Aspose? I want know if I can create one thing like I show in image attached. If it is not possible tell me please for I find another alternative.
Please consider this email like urgently,
Best regards,
Antonio Nobre

Hi,
I think you cannot understand well my question.
I want to create TextBox, or write in margins of document, I don’t want start in first paragraph, or the first section, etc. So if it possible with Aspose? I want know if I can create one thing like I show in image attached. If it is not possible tell me please for I find another alternative.
Please consider this email like urgently,
Best regards,

Hi,
I think you cannot understand well my question.
I want to create TextBox, or write in margins of document, I don’t want start in first paragraph, or the first section, etc. So if it possible with Aspose? I want know if I can create one thing like I show in image attached. If it is not possible tell me please for I find another alternative.
Please consider this email like urgently,
Best regards,
Eduardo79t

Hi Antonio,

Thanks for your interest in Aspose.Words and I am sorry to hear about the troubles you have encountered so far.

Sure, you can insert a floating TextBox Shape anywhere in the page. The place where you want to insert a TextBox is actually an area between the left edge of the page and the left boundary of the body text. The width of this area can be represented by PageSetup.LeftMargin property. Therefore, you can use the following code snippet to achieve what you’re looking for:

Document doc = new Document("C:\Temp\test.docx");
PageSetup ps = doc.getFirstSection().getPageSetup();
Shape textBox = new Shape(doc, ShapeType.TEXT_BOX);
textBox.setWrapType(WrapType.NONE);
textBox.setLeft(0);
textBox.setHeight(ps.getPageHeight() / 2);
textBox.setWidth(ps.getLeftMargin());
textBox.setRelativeHorizontalPosition(RelativeHorizontalPosition.PAGE);
textBox.setRelativeVerticalPosition(RelativeVerticalPosition.PAGE);
textBox.setVerticalAlignment(VerticalAlignment.CENTER);
textBox.appendChild(new Paragraph(doc));
Paragraph para = textBox.getFirstParagraph();
para.getParagraphFormat().setAlignment(ParagraphAlignment.LEFT);
Run run = new Run(doc);
run.setText("Java");
para.appendChild(run);
doc.getFirstSection().getBody().getFirstParagraph().appendChild(textBox);
doc.save("C:\Temp\out.docx");

If we can help you with anything else, please feel free to ask.

Best regards,