EvotecIT / OfficeIMO

Fast and easy to use cross-platform .NET library that creates or modifies Microsoft Word (DocX) and later also Excel (XLSX) files without installing any software. Library is based on Open XML SDK
MIT License
263 stars 47 forks source link

Place Text at specific location #179

Closed TopperDEL closed 6 months ago

TopperDEL commented 6 months ago

Thank you for this awesome project!

I have a question: can I place Text or a "TextBlock" at a specific location e.g. "2cm from the right border and 3cm from the top border"? I need to place Adress data and such at exact locations.

PrzemyslawKlys commented 6 months ago

Well, how would you do it manually in Word to achieve what you want?

Both of that you can do. But maybe you are thinking on something else?

TopperDEL commented 6 months ago

In Word I would create a TextField: image

And then I would place it at a specific location: image (Sorry, my Word is german, but I hope you recognize it).

Could this be added?

I'm not sure if a Table would do it, as I need multiple TextFields in e.g. the top region of a page - think of Sender-Address, Receiver-Address, Communication Info etc.

The basic problem is: those text fields need to be separate from the "normal" text run. They have to be at a specific location and should not interfere with the rest of the text.

PrzemyslawKlys commented 6 months ago

Looks like TextBox is drawing with anchor

image

Relative to a page

image

It's doable, but we need to add new type and all the properties to change different things. Alternate Content Fallback may be harder to do. But we will see.

TopperDEL commented 6 months ago

Is there anything where I can help? Unfortunately I have no clue what needs to be done - but this feature is critical for my app-idea, so I would be very happy to have this included.

Let me know if/how I may help!

PrzemyslawKlys commented 6 months ago

This looks similar to how you manage Images. So I will use WordImage type as a starting point and will have to create new type WordTextbox type or something similar. Hopefully it will be enough.

We will see from there.

TopperDEL commented 6 months ago

Awesome!

The project I'm working on should get to the market next year. As soon as I earn something with it I will give back to the projects I'm stacking on top like yours is!

PrzemyslawKlys commented 6 months ago

Cool :-) Lets see how that WordTextBox goes first

PrzemyslawKlys commented 6 months ago

I got this thing working

internal static void Example_AddingTextbox2(string folderPath, bool openWord) {
    Console.WriteLine("[*] Creating standard document with some textbox");

    var filePath = System.IO.Path.Combine(folderPath, "BasicDocumentWithTextBox3.docx");

    using (WordDocument document = WordDocument.Create(filePath)) {
        var paragraph = document.AddParagraph("Adding paragraph with some text");

        var textBox = document.AddTextBox("My textbox on the left");

        textBox.HorizontalPositionRelativeFrom = HorizontalRelativePositionValues.Page;
        // horizontal alignment overwrites the horizontal position offset so only one will work
        textBox.HorizontalAlignment = HorizontalAlignmentValues.Left;
        textBox.VerticalPositionOffsetCentimeters = 3;

        var textBox2 = document.AddTextBox("My textbox on the right");
        textBox2.HorizontalPositionRelativeFrom = HorizontalRelativePositionValues.Page;
        textBox2.WordParagraph.ParagraphAlignment = JustificationValues.Right;
        // horizontal alignment overwrites the horizontal position offset so only one will work
        textBox2.HorizontalAlignment = HorizontalAlignmentValues.Right;
        textBox2.VerticalPositionOffsetCentimeters = 3;

        Console.WriteLine(textBox.VerticalPositionOffsetCentimeters);

        Console.WriteLine(document.TextBoxes[0].VerticalPositionOffsetCentimeters);

        Console.WriteLine(document.TextBoxes[1].VerticalPositionOffsetCentimeters);

        document.Save(openWord);
    }
}

image

But need to add some tests and probably there will be things that are going to need improvements. Maybe add some styles that are available in Word. Also the object is very complicated so I am not sure if it will survive testing on real world documents created by other people.

TopperDEL commented 6 months ago

Looks awesome! Exactly what I need! Would love to test this out!

PrzemyslawKlys commented 6 months ago

There is PR linked to this issue. You can play with it

TopperDEL commented 6 months ago

Oh right! Was on my mobile phone and did not realize it. Will try asap!

TopperDEL commented 6 months ago

As mentioned in the PR: it works and is exactly what I need! Would love to see it merged. :) Thank you!

PrzemyslawKlys commented 6 months ago

Well that was an easy part. Now the "tests" needs to be done which is less than exciting ;p