Place text on document

Trying to place text on given dimensions inside a page. Trying to use the textbox function as a floating element but having trouble styling it for my needs. Anyone that can point me in the right direction?
I need to remove the border, padding / margin, set font size, font name. Also need to remove the edit mode that seams to be active when saving to pdf?

Its the text 1:100 that I need to style some way, the layout behind is an svg placed on the document with floating layout.

Thanks
Anders

Hi Anders,

Try this:

var textbox = new TextBox(document, new FloatingLayout(
    new HorizontalPosition(450, LengthUnit.Point, HorizontalPositionAnchor.Page),
    new VerticalPosition(750, LengthUnit.Point, VerticalPositionAnchor.Page),
    new Size(60, 30)));

// Remove borders and margins.
textbox.Outline.Fill.SetEmpty();
textbox.TextBoxFormat.InternalMargin = new Padding(0);

// Set vertical alignment.
textbox.TextBoxFormat.VerticalAlignment = VerticalAlignment.Center;

var paragraph = new Paragraph(document);
textbox.Blocks.Add(paragraph);

// Set horizontal alignment.
paragraph.ParagraphFormat.Alignment = HorizontalAlignment.Center;

var run = new Run(document, "1:100");
paragraph.Inlines.Add(run);

// Set font size and font name.
run.CharacterFormat.Size = 18;
run.CharacterFormat.FontName = "Arial";

Unfortunately, I’m not sure what you mean by that.
Can you try explaining exactly what you’re referring to?

Regards,
Mario

The edit mode is when Adobe PDF is viewing the document. Can this be disabled or is it default for Adobe PDF?

Adjusted the code a bit, could perhaps font size and font name be set using a style that is reusable?

public void AddText(string text, double x, double y, double width, double height, Paragraph paragraph)
        {
            var textBox = new TextBox(Document, new FloatingLayout(
                new HorizontalPosition(x, LengthUnit.Millimeter, HorizontalPositionAnchor.Page),
                new VerticalPosition(y, LengthUnit.Millimeter, VerticalPositionAnchor.Page),
                new Size(width, height, LengthUnit.Millimeter)));

            // Remove borders and margins.
            textBox.Outline.Fill.SetEmpty();
            textBox.TextBoxFormat.InternalMargin = new Padding(0);

            // Set vertical alignment.
            textBox.TextBoxFormat.VerticalAlignment = VerticalAlignment.Center;

            //Add new paragraph to textBox
            var paragraphInsideTextBox = new Paragraph(Document);
            textBox.Blocks.Add(paragraphInsideTextBox);

            // Set horizontal alignment.
            paragraphInsideTextBox.ParagraphFormat.Alignment = HorizontalAlignment.Left;
            
            var run = new Run(Document, text);
            paragraphInsideTextBox.Inlines.Add(run);

            // Set font size and font name.
            run.CharacterFormat.Size = 10;
            run.CharacterFormat.FontName = "Arial";
            
            paragraph.Inlines.Add(textBox);
        }

The passed in paragraph is defined in a ctor for the “report”. What’s the best practice here, should I reuse the paragraph defined in the ctor of the report class for many floating layout or should one add new paragraph at the “main” level of a section for each floating layout?

            Document.Sections.Add(section);
            var paragraph = new Paragraph(Document);
            _paragraph = paragraph;
            section.Blocks.Add(_paragraph);

Thanks!

The size of a textbox, knowing the exact size that a text needs on creation, know of any way to determine this or ?

Hi Anders, I still don’t know what you mean by edit mode, but it seems to be something related to Adobe Acrobat.

Regarding the styles, please check this example:
https://www.gemboxsoftware.com/document/examples/word-styles/604

And this documentation page:
https://www.gemboxsoftware.com/document/docs/formattings-and-styles.html

Regarding the paragraphs, you cannot reuse them.
Please check the content model diagram of the DocumentModel object:
https://www.gemboxsoftware.com/document/docs/content-model.html

Regarding the textbox size, you can use auto-fit properties of the TextBoxFormat, see its members:
https://www.gemboxsoftware.com/document/docs/GemBox.Document.Drawing.TextBoxFormat.html

Regards,
Mario