Underlining and bolding text - c#

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 }

Related

Visual studio 2019 OpenXML polish letters

I'm trying to change basic font to "Arial" but if it is any polish symbol its change back to Calibri like on picture. Is is any way to apply it for polish symbols?
Run run = new Run();
RunProperties runProp = new RunProperties();
Text textRun = new Text()
{
Text = text,
Space = SpaceProcessingModeValues.Preserve
};
runProp.Append(new RunFonts() { Ascii = EnumFonts.Arial.ToString() }, new FontSize());
run.Append(runProp);
run.Append(textRun);

Insert text at the start of a document in Word - openXML

I am using OpenXML to manipulate Microsoft Word files (.docx).
I am sending the Word files as memory stream, editing them and then send them back to browser so they open in client office program.
I want to insert text, approximatley 10 lines, at the start of a document which already got content. I am doing it like this;
using (var wordprocessingDocument = WordprocessingDocument.Open(mem, true))
{
Paragraph firstParagraph = wordprocessingDocument.MainDocumentPart.Document.Descendants<Paragraph>().First();
Run run1 = new Run();
Text text1 = new Text();
text1.Text = "Document type: ";
run1.Append(text1);
Run run2 = new Run();
Break break1 = new Break();
run2.Append(break1);
ProofError proofError1 = new ProofError() { Type = ProofingErrorValues.SpellStart };
Run run3 = new Run();
Text text2 = new Text();
text2.Text = "Document ID";
run3.Append(text2);
ProofError proofError2 = new ProofError() { Type = ProofingErrorValues.SpellEnd };
Run run4 = new Run();
Break break2 = new Break();
Text text3 = new Text();
text3.Text = "Document Title";
run4.Append(break2);
run4.Append(text3);
firstParagraph.Append(run1);
firstParagraph.Append(run2);
firstParagraph.Append(proofError1);
firstParagraph.Append(run3);
firstParagraph.Append(proofError2);
firstParagraph.Append(run4);
Paragraph paragraph3 = new Paragraph() { RsidParagraphAddition = "0068718C", RsidParagraphProperties = "0068718C", RsidRunAdditionDefault = "00E1050C" };
Run run5 = new Run();
Text text4 = new Text();
text4.Text = "A";
run5.Append(text4);
Run run6 = new Run() { RsidRunAddition = "00126F2D" };
Text text5 = new Text();
text5.Text = "tlet";
run6.Append(text5);
paragraph3.Append(run5);
paragraph3.Append(run6);
Paragraph paragraph4 = new Paragraph() { RsidParagraphMarkRevision = "0068718C", RsidParagraphAddition = "00E1050C", RsidParagraphProperties = "0068718C", RsidRunAdditionDefault = "00E1050C" };
BookmarkStart bookmarkStart1 = new BookmarkStart() { Name = "_GoBack", Id = "0" };
BookmarkEnd bookmarkEnd1 = new BookmarkEnd() { Id = "0" };
paragraph4.Append(bookmarkStart1);
paragraph4.Append(bookmarkEnd1);
SectionProperties sectionProperties1 = new SectionProperties() { RsidRPr = "0068718C", RsidR = "00E1050C", RsidSect = "000C7A63" };
}
My problem is that if I already got some text from the first line, then my text will be appended after the original text there. How can I push the original content some lines down, and insert my text above?
Use PrependChild instead of Append. Append will always insert at the end of the current element. So if you already have content in the first paragraph your append will put your text at the end of it. You can also insert your text as a new first paragraph by calling Document.PrependChild(firstParagraph)

Superscript + Underline inline in a RichTextBox in WPF

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;

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

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