Superscript + Underline inline in a RichTextBox in WPF - c#

I have a set of text that I'd like to put in a RichTextBox which goes like so:
So I used a RichTextBox since it allows me to do the following.
var zipCodeParagraph = new Paragraph();
string zipCodes = String.Empty;
var dateRun = new Underline(new Run(DateTime.Today.DayOfWeek + ", " + CultureInfo.CurrentCulture.DateTimeFormat.GetMonthName(DateTime.Today.Month) + ' ' + DateTime.Today.Day));
Underline dateSuperscript;
switch (DateTime.Today.Day % 10)
{
case 1:
dateSuperscript = new Underline(new Run("st"));
break;
case 2:
dateSuperscript = new Underline(new Run("nd"));
break;
case 3:
dateSuperscript = new Underline(new Run("rd"));
break;
default:
dateSuperscript = new Underline(new Run("th"));
break;
}
dateSuperscript.BaselineAlignment = BaselineAlignment.Superscript;
if (ZipCodes.Any())
{
zipCodeParagraph.Inlines.Add(new Run("The following zip codes are facing a "));
zipCodeParagraph.Inlines.Add(new Underline(new Run("Severe Weather Threat")));
zipCodeParagraph.Inlines.Add(new Run(" on "));
zipCodeParagraph.Inlines.Add(dateRun);
zipCodeParagraph.Inlines.Add(dateSuperscript);
zipCodes = String.Join(", ", ZipCodes.ToArray());
}
The outcome however is like so:
The problem is that when changing the baseline of a text to be superscript/subscript then the underline changes to that height as well. I'd like the underline to stay where it is and for the super-scripting to happen as well.
I have found only one close solution which does not do it programmatically here.

I have tried to convert the same code which is mentioned in the link here.
Refer the below code.
FlowDocument mcFlowDoc = new FlowDocument();
Hyperlink hyp = new Hyperlink();
hyp.Foreground = Brushes.Black;
TextBlock txt = new TextBlock();
txt.Foreground = Brushes.Black;
txt.Text = "Friday,April 10";
Run rn = new Run("th");
rn.BaselineAlignment = BaselineAlignment.Superscript;
txt.Inlines.Add(rn);
hyp.Inlines.Add(txt);
Paragraph para = new Paragraph();
para.Inlines.Add(new Run("The following zip codes are facing a "));
para.Inlines.Add(new Underline(new Run("Severe Weather Threat")));
para.Inlines.Add(new Run(" on "));
para.Inlines.Add(hyp);
mcFlowDoc.Blocks.Add(para);
RichTextBox mcRTB = new RichTextBox();
mcRTB.Width = 560;
mcRTB.Height = 100;
mcRTB.Document = mcFlowDoc;

As this seems a limitation of the RichTextBox, the best solution would be the one proposed in the second answer of the question you linked, namely instead of using the normal letters, to use their Unicode superscript variants:
"st" becomes "ˢᵗ"
"nd" becomes "ⁿᵈ"
etc.
You should also remove the baseline setting:
//dateSuperscript.BaselineAlignment = BaselineAlignment.Superscript;

Related

How to add page no. and print date ms interop word DLL

I want to add following text into MS-Word footer using MS-Interop Word DLL.
Required Footer Text:
"Page 1 of 10 and date = {Current Date}" something like this.
I have added below code which add page no. and current date but its not allowing me add any custom text like "Page 1 of 10".
Here is my code
foreach (Microsoft.Office.Interop.Word.Section wordSection in document.Sections)
{
Microsoft.Office.Interop.Word.Range footerRange = wordSection.Footers[Microsoft.Office.Interop.Word.WdHeaderFooterIndex.wdHeaderFooterPrimary].Range;
footerRange.Collapse(Microsoft.Office.Interop.Word.WdCollapseDirection.wdCollapseEnd);
footerRange.Fields.Add(footerRange, Microsoft.Office.Interop.Word.WdFieldType.wdFieldDate,"Date = ");
footerRange.Fields.UpdateSource();
footerRange.Fields.Add(footerRange, Microsoft.Office.Interop.Word.WdFieldType.wdFieldPage, "Page No = ");
footerRange.Fields.UpdateSource();
footerRange.ParagraphFormat.Alignment = Microsoft.Office.Interop.Word.WdParagraphAlignment.wdAlignParagraphRight;
}
An idea how to add such functionality?
Here is the solution which I have found.
Microsoft.Office.Interop.Word.Application app = new Microsoft.Office.Interop.Word.Application();
foreach (Microsoft.Office.Interop.Word.Section wordSection in document.Sections)
{
Microsoft.Office.Interop.Word.Range footerRange = wordSection.Footers[Microsoft.Office.Interop.Word.WdHeaderFooterIndex.wdHeaderFooterPrimary].Range;
footerRange.Collapse(Microsoft.Office.Interop.Word.WdCollapseDirection.wdCollapseEnd);
footerRange.Fields.Add(footerRange, Microsoft.Office.Interop.Word.WdFieldType.wdFieldNumPages);
Microsoft.Office.Interop.Word.Paragraph p4 = footerRange.Paragraphs.Add();
p4.Range.Text = " of ";
footerRange.ParagraphFormat.Alignment = Microsoft.Office.Interop.Word.WdParagraphAlignment.wdAlignParagraphRight;
footerRange.Fields.Add(footerRange, Microsoft.Office.Interop.Word.WdFieldType.wdFieldPage);
Microsoft.Office.Interop.Word.Paragraph p1 = footerRange.Paragraphs.Add();
p1.Range.Text = "Page: ";
footerRange.ParagraphFormat.Alignment = Microsoft.Office.Interop.Word.WdParagraphAlignment.wdAlignParagraphRight;
Microsoft.Office.Interop.Word.Paragraph p3 = footerRange.Paragraphs.Add();
p3.Range.Text = " " + Environment.NewLine;
footerRange.Fields.Add(footerRange, Microsoft.Office.Interop.Word.WdFieldType.wdFieldDate);
Microsoft.Office.Interop.Word.Paragraph p2 = footerRange.Paragraphs.Add();
p2.Range.Text = "Print date: ";
footerRange.ParagraphFormat.Alignment = Microsoft.Office.Interop.Word.WdParagraphAlignment.wdAlignParagraphRight;
}

Defining paragraph with RTL direction in word file by OpenXML in C#

How to set the right-to-left direction for a paragraph in word with OpenXML in C#? I use codes below to define it but they won't make any change:
RunProperties rPr = new RunProperties();
Style style = new Style();
style.StyleId = "my_style";
style.Append(new Justification() { Val = JustificationValues.Right });
style.Append(new TextDirection() { Val = TextDirectionValues.TopToBottomRightToLeft });
style.Append(rPr);
and at the end I will set this style for my paragraph:
...
heading_pPr.ParagraphStyleId = new ParagraphStyleId() { Val = "my_style" };
But is see no changes in the output file.
I have found some post but they didn't help me at all,like:
Changing text direction in word file
How can solve this problem?
Thanks in advance.
Use the BiDi class to set the text direction to RTL for a paragraph.
The following code sample searches for the first paragraph in a word document
and sets the text direction to RTL using the BiDi class:
using (WordprocessingDocument doc =
WordprocessingDocument.Open(#"test.docx", true))
{
Paragraph p = doc.MainDocumentPart.Document.Body.ChildElements.First<Paragraph>();
if(p == null)
{
Console.Out.WriteLine("Paragraph not found.");
return;
}
ParagraphProperties pp = p.ChildElements.First<ParagraphProperties>();
if (pp == null)
{
pp = new ParagraphProperties();
p.InsertBefore(pp, p.First());
}
BiDi bidi = new BiDi();
pp.Append(bidi);
}
There are a few more aspects to bi-directional text in Microsoft Word.
SanjayKumarM wrote an article about how right-to-left text content
is handled in Microsoft Word. See this link for more information.
This code worked for me to set the direction Right to Left
var run = new Run(new Text("Some text"));
var paragraph = new DocumentFormat.OpenXml.Wordprocessing.Paragraph(run);
paragraph.ParagraphProperties = new ParagraphProperties()
{
BiDi = new BiDi(),
TextDirection = new TextDirection()
{
Val = TextDirectionValues.TopToBottomRightToLeft
}
};

How do I create a check box in C# using Open XML SDK

I would like to create a certain number of checkbox using Open XML SDK in C#. How would I do that?
Example:
(Checkbox) - Shoes
(Checkbox) - Shirt
The checkbox count also varies. I am reading a template document, then make edits to return. I have something like this so far:
string documentText;
using (StreamReader reader ...)
{
documentText = reader.ReadToEnd();
}
string addClothes = "";
Run newCheckBox = GenerateRun();
foreach(var item in ClothesList)
{
addClothes = item.clothing;
//MY DILEMMA
documentText = documentText.Replace("##clothing##", newCheckBox + addClothes + "NewLine");
}
public Run GenerateRun()
{
Run run1 = new Run() { RsidRunProperties = "004C0D9A", RsidRunAddition = "00850FA5" };
FieldCode fieldCode1 = new FieldCode() { Space = SpaceProcessingModeValues.Preserve };
fieldCode1.Text = " FORMCHECKBOX ";
run1.Append(fieldCode1);
return run1;
}
using the OpenXML SDK i think it goes something like this:
(raw copy/paste - the value of -1636166143 might be way off)
using w14 = DocumentFormat.OpenXml.Office2010.Word
SdtRun sdtRun1 = new SdtRun();
SdtProperties sdtProperties1 = new SdtProperties();
SdtId sdtId1 = new SdtId(){ Val = -1636166143 };
W14.SdtContentCheckBox sdtContentCheckBox1 = new W14.SdtContentCheckBox();
W14.Checked checked1 = new W14.Checked(){ Val = W14.OnOffValues.Zero };
W14.CheckedState checkedState1 = new W14.CheckedState(){ Font = "MS Gothic", Val = "2612" };
W14.UncheckedState uncheckedState1 = new W14.UncheckedState(){ Font = "MS Gothic", Val = "2610" };
sdtContentCheckBox1.Append(checked1);
sdtContentCheckBox1.Append(checkedState1);
sdtContentCheckBox1.Append(uncheckedState1);
sdtProperties1.Append(sdtId1);
sdtProperties1.Append(sdtContentCheckBox1);
SdtContentRun sdtContentRun1 = new SdtContentRun();
Run run1 = new Run();
RunProperties runProperties1 = new RunProperties();
RunFonts runFonts1 = new RunFonts(){ Hint = FontTypeHintValues.EastAsia, Ascii = "MS Gothic", HighAnsi = "MS Gothic", EastAsia = "MS Gothic" };
runProperties1.Append(runFonts1);
Text text1 = new Text();
text1.Text = "☐";
run1.Append(runProperties1);
run1.Append(text1);
sdtContentRun1.Append(run1);
Any text you want after the checkbox, comes after the above code

Underlining and bolding text

how do I get the text to be underlined and bold? My text gets bold but does not get underlined.
Here is some of my code, which is pretty straight forward uses run properties to bold and underline the given text.
Run run_header = para_main.AppendChild(new Run());
RunProperties runProps = new RunProperties();
Bold bold = new Bold();
Underline ul = new Underline();
runProps.Append(bold);
runProps.Append(ul);
run_header.AppendChild(new RunProperties(runProps));
//run_header.AppendChild(new RunProperties(new Bold(), new Underline()));
string username = form.Username;
string proces_header = form.HeaderTitle;
run_header.AppendChild(new Text(proces_header + " | " + username));
run_header.AppendChild(new Break());
Try to set Underline Val property to Single like this:
new Underline() { Val = DocumentFormat.OpenXml.Wordprocessing.UnderlineValues.Single }

Two CellValues with Different Styles in a cell in OPEN XML SDK 2.0

Im tasked to use OPEN XML SDK 2.0 and had encountered this problem. Is it possible to have different styles for a single CellValue inside a cell something like the picture below:
A: The plain text
B: Bold and Underlined
NOTE: I need both in a single cell only thanks :)
Yes that is possible. One way is to format the value that will be inserted into the SharedStringTable. This snippet will create your example above:
// Creates an SharedStringItem instance and adds its children.
public SharedStringItem GenerateSharedStringItem()
{
SharedStringItem sharedStringItem1 = new SharedStringItem();
Run run1 = new Run();
RunProperties runProperties1 = new RunProperties();
Bold bold1 = new Bold();
Underline underline1 = new Underline();
FontSize fontSize1 = new FontSize(){ Val = 11D };
Color color1 = new Color(){ Theme = (UInt32Value)1U };
RunFont runFont1 = new RunFont(){ Val = "Calibri" };
FontFamily fontFamily1 = new FontFamily(){ Val = 2 };
FontScheme fontScheme1 = new FontScheme(){ Val = FontSchemeValues.Minor };
runProperties1.Append(bold1);
runProperties1.Append(underline1);
runProperties1.Append(fontSize1);
runProperties1.Append(color1);
runProperties1.Append(runFont1);
runProperties1.Append(fontFamily1);
runProperties1.Append(fontScheme1);
Text text1 = new Text();
text1.Text = "Project Name:";
run1.Append(runProperties1);
run1.Append(text1);
Run run2 = new Run();
RunProperties runProperties2 = new RunProperties();
FontSize fontSize2 = new FontSize(){ Val = 11D };
Color color2 = new Color(){ Theme = (UInt32Value)1U };
RunFont runFont2 = new RunFont(){ Val = "Calibri" };
FontFamily fontFamily2 = new FontFamily(){ Val = 2 };
FontScheme fontScheme2 = new FontScheme(){ Val = FontSchemeValues.Minor };
runProperties2.Append(fontSize2);
runProperties2.Append(color2);
runProperties2.Append(runFont2);
runProperties2.Append(fontFamily2);
runProperties2.Append(fontScheme2);
Text text2 = new Text(){ Space = SpaceProcessingModeValues.Preserve };
text2.Text = " ALLAN";
run2.Append(runProperties2);
run2.Append(text2);
sharedStringItem1.Append(run1);
sharedStringItem1.Append(run2);
return sharedStringItem1;
}
You can insert that into the SharedStringTable and then set the cell value to be the index in the SharedStringTable where this was inserted.
There might be some other references that I forgot to include that might be defined in the StylesPart. I recommend creating this example in a blank Excel document and then using the Open XML Productivity Tool to look at the XML. The tool will also supply you with the code I provided you above. It should give you a general direction on where to go next.

Categories