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

Free Support Forum - aspose.com

How to insert Textbox into document using C#

Could someone plse post an example of how I would add a Textbox to a word ducument in a position of my choice containinf text of my choice. Also how I would add a border to the textbox.

Thanks

@terryaitch

Thanks for your query. Please use the following code snippet to add text box in your document.

// 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.Left = 100;

textbox.Top = 100;


Paragraph paragraph = new Paragraph(doc);

paragraph.AppendChild(new Run(doc, "Test"));


// Insert paragraph into the textbox.

textbox.AppendChild(paragraph);


// Insert textbox into the document.

doc.FirstSection.Body.FirstParagraph.AppendChild(textbox);


doc.Save(MyDir + "AsposeOut.docx");

Hope this helps you. Please let us know if you have any more queries.

Thanks for response.

What I need is to add a peice of text (this will be converted to barcode by using a39r Normal font) onto the same position on each page of a document. This text will be made up as follows

*{PersonID - 8 digit with leading zeros}{LetterID - 8 digit with leading zeros}{PageNumber - 2 digit with leading zero)*

ie *000012340000567801* on page one *000012340000567802* on page two etc

The code I went with for my test is pasted below

What I find is that when I print directly from aspose using doc.Print command I get the following

*00001234000056781* on page one and *00001234000056782* on page two. Note that the leading zeros are missing from PageNumber part of text

If instead of printing directly I save the file using doc.Save command, I end up with a document that has *000012340000567801* on page one and *000012340000567802* on page two; note the leading zeros on the PageNumber part.

I need to know how to ensure that when I print directly using doc.print, that the leading zeros remain on the PageNumber part of my text

===========================================================================
Sub TestAspose()

'Position of top left corner of barcode relative to top left of page
'This values will come from config
Dim x As Double = 50
Dim y As Double = 780
Dim h As Double = 50 'Height of textbox
Dim w As Double = 300 'width of textbox


'Barcodes will go into footer

'// Create or open document.
Dim doc As Document = New Document("C:\temp\TZ\Template.doc")
'create a builder object
Dim db As New DocumentBuilder(doc)

'Create textbox shape.
Dim textbox As Shape = New Shape(doc, ShapeType.TextBox)
textbox.Left = x
textbox.Top = y
textbox.Width = w
textbox.Height = h

'absolutely position textbox relative to top left corner of page
textbox.RelativeHorizontalPosition = RelativeHorizontalPosition.Page
textbox.RelativeVerticalPosition = RelativeVerticalPosition.Page
textbox.WrapType = WrapType.Square
textbox.Stroked = False

'add textbox to document header so that data appears on all pages
db.MoveToHeaderFooter(HeaderFooterType.HeaderPrimary)
db.InsertNode(textbox)

'create paragraph for data to be written in
Dim paragraph As Paragraph = New Paragraph(doc)
textbox.AppendChild(paragraph)
db.MoveTo(paragraph)

'get these values from config
db.Font.Name = "Arial"
db.Font.Size = 10

'write data into paragraph
db.Write("*")
db.Write(Format(1234, "00000000")) 'PersonID formatted to 8 digits with leading zeros
db.Write(Format(5678, "00000000")) 'LetterID formatted to 8 digits with leading zeros
db.InsertField("PAGE \# ""00"" ", "") 'PageNumber formatted to 2 digits with leading zeros
db.Write("*")

'print directly from Aspose
doc.Print("\\svr-prt-que01\Xerox Mono")

'save document so that it can be opened and the printed
doc.Save("C:\Temp\TZ\out.doc")


End Sub

=============================================================================

Hi,

Thanks for your query. It would be great if you please share your document for investigation purposes.

I have attached a zip file containing two files.

Template.doc is is the document that I open and add text box to. This is a blank word document created from word 2003.

Out.doc is the file created on line doc.Save("C:\Temp\TZ\out.doc"). In this document you will see that there is a leading zero before the PageNumber part of the data in the textbox at the bottom of the page. When this document is printed on the line before doc.Print("\\svr-prt-que01\Xerox Mono"), this leading zero is missing. If out.doc is opened and printed using MS Word 2003 then the leading zero prints

Hi,

Thanks for sharing the details. I have tested the scenario and have not found any issue while using latest version of Aspose.Words for .NET. Please use the latest version of Aspose.Words for .NET.

Please let us know if you have any more queries.

A post was split to a new topic: How to insert textbox using .NET