Visual studio 2019 OpenXML polish letters - c#

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);

Related

TuesPechkin adding  in front of Currency £ Symbol

I have been using Tues Pechkin for html to PDF Conversion
But there is one issue
Tues Pechkin is adding by default  this symbol in front of £ Symbol
Example :- £85,000
var document = new HtmlToPdfDocument
{
GlobalSettings =
{
ProduceOutline = true,
DocumentTitle = "TEST",
PaperSize = PaperKind.A4, // Implicit conversion to PechkinPaperSize
Margins =
{
All = 1.375,
Unit = Unit.Centimeters
}
}}
Please help
Thanku.
added a new Websettings
new ObjectSettings { HtmlText = strContent,
WebSettings = new WebSettings
{
DefaultEncoding = "UTF-8",
LoadImages = true,
PrintBackground = true
}
}
So that it can accept UTF-8 character ,And it is resolved. Thanku

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)

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
}
};

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