Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add basic method to add textbox to header/footer #187

Merged
merged 3 commits into from
Jan 16, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion OfficeIMO.Examples/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -156,7 +156,7 @@ static void Main(string[] args) {

WordTextBox.Example_AddingTextbox(folderPath, false);
WordTextBox.Example_AddingTextbox2(folderPath, false);

WordTextBox.Example_AddingTextbox3(folderPath, false);
}
}
}
62 changes: 62 additions & 0 deletions OfficeIMO.Examples/Word/WordTextBox/WordTextBox.Sample3.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
using System;
using DocumentFormat.OpenXml.Drawing.Wordprocessing;
using DocumentFormat.OpenXml.Spreadsheet;
using DocumentFormat.OpenXml.Wordprocessing;
using OfficeIMO.Word;
using Color = SixLabors.ImageSharp.Color;
using HorizontalAlignmentValues = DocumentFormat.OpenXml.Drawing.Wordprocessing.HorizontalAlignmentValues;

namespace OfficeIMO.Examples.Word {
internal static partial class WordTextBox {
internal static void Example_AddingTextbox3(string folderPath, bool openWord) {
Console.WriteLine("[*] Creating standard document with some textbox");

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

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

document.AddHeadersAndFooters();

var textBox = document.Header.Default.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);

//var textBox3 = document.AddTextBox("My textbox in the center with borders");
//textBox3.HorizontalPositionRelativeFrom = HorizontalRelativePositionValues.Page;
//textBox3.HorizontalAlignment = HorizontalAlignmentValues.Center;
//textBox3.VerticalPositionOffsetCentimeters = 10;
//textBox3.WordParagraph.Borders.BottomStyle = BorderValues.BasicWideOutline;
//textBox3.WordParagraph.Borders.BottomSize = 10;
//textBox3.WordParagraph.Borders.BottomColor = Color.Red;
//textBox3.WordParagraph.Borders.BottomShadow = false;
//textBox3.WordParagraph.Borders.TopStyle = BorderValues.BasicWideOutline;
//textBox3.WordParagraph.Borders.LeftStyle = BorderValues.BasicWideOutline;
//textBox3.WordParagraph.Borders.RightStyle = BorderValues.BasicWideOutline;

//textBox3.WordParagraph.Borders.SetBorder(WordParagraphBorderType.Left, BorderValues.BasicWideOutline, Color.Red, 10, false);

//// remove the textbox
//textBox2.Remove();

document.Save(openWord);
}
}
}
}
5 changes: 5 additions & 0 deletions OfficeIMO.Word/WordHeader.cs
Original file line number Diff line number Diff line change
Expand Up @@ -83,5 +83,10 @@ public static void RemoveHeaders(WordDocument document) {
public WordWatermark AddWatermark(WordWatermarkStyle watermarkStyle, string text) {
return new WordWatermark(this._document, this._section, this, watermarkStyle, text);
}

public WordTextBox AddTextBox(string text) {
WordTextBox wordTextBox = new WordTextBox(this._document, this, text);
return wordTextBox;
}
}
}
4 changes: 2 additions & 2 deletions OfficeIMO.Word/WordHeaderFooter.Methods.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,8 @@ public WordParagraph AddParagraph(string text) {
return paragraph;
}

public WordParagraph AddParagraph() {
var wordParagraph = new WordParagraph(_document, newParagraph: true, newRun: false);
public WordParagraph AddParagraph(bool newRun = false) {
var wordParagraph = new WordParagraph(_document, newParagraph: true, newRun: newRun);
if (_footer != null) {
_footer.Append(wordParagraph._paragraph);
} else if (_header != null) {
Expand Down
13 changes: 13 additions & 0 deletions OfficeIMO.Word/WordTextBox.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ namespace OfficeIMO.Word {
public class WordTextBox {
private WordDocument _document;
private WordParagraph _wordParagraph;
private readonly WordHeaderFooter _headerFooter;
private Run _run => _wordParagraph._run;

/// <summary>
Expand Down Expand Up @@ -37,6 +38,18 @@ public WordTextBox(WordDocument wordDocument, Paragraph paragraph, Run run) {
_wordParagraph = new WordParagraph(wordDocument, paragraph, run);
}

public WordTextBox(WordDocument wordDocument, WordHeaderFooter wordHeaderFooter, string text) {
_document = wordDocument;
_headerFooter = wordHeaderFooter;

var paragraph = wordHeaderFooter.AddParagraph(newRun: true);
paragraph._run.Append(new RunProperties());
AddAlternateContent(wordDocument, paragraph, text);

_document = wordDocument;
_wordParagraph = paragraph;
}

/// <summary>
/// Allows to set the text of the text box
/// For more advanced text formatting use WordParagraph property
Expand Down