c# a with umlaut wrong font openxml - c#

I have a problem with umlauts on characters when using openxml. Every char in the string is in Arial, but the ä is in Calibri. I really dont know why.
Can someone help me?
this is my code:
DocumentFormat.OpenXml.Wordprocessing.Run run = new DocumentFormat.OpenXml.Wordprocessing.Run();
RunProperties runProp = new RunProperties(); // Create run properties.
RunFonts runFont = new RunFonts(); // Create font
runFont.Ascii = "Arial"; // Specify font family
runProp.Append(runFont);
run.Append(runProp);
run.Append(new Text("Kapazität"));

You need to specify the HighAnsi property of the RunFonts object.
runFont.HighAnsi = "Arial";
As you would expect, Ascii font designation only accounts for ASCII characters (and a very short range of Unicode U+0000-U+007F). The umlaut characters are in the "extended" unicode range, and HighAnsi is responsible for most of that character set.

Related

C# change the color of the character on MS word document(.docx)

I am going to change the font color of the specific character instead of the whole paragraph on the MS document.
I'd like to do this in C#.
Now I can change the font color of the whole paragraph using DOCX library, but not able to change the character.
Looking to hearing any help.
In the examples of this Library you have this code:
// Insert a Paragraph into this document.
var p = document.InsertParagraph();
// Append some text and add formatting.
p.Append( "This is an " ).Font( new Font( "Arial" )).Color(Color.Black)
.Append( " E" ).Font( new Font( "Arial" )).Color( Color.Blue )
.Append( " xample." ).Font( new Font( "Arial" )).Color( Color.Black);
In a paragraph you are able to add and format any kind of text. If you want to change the format of a letter, you need to add a letter + the format. This code will be displaying "This is an Example" and the letter "E" will be blue.

How to display chinese characters in PDF using ITextSharp in c#?

I want to display one word of chinese characters in PDF along with english characters. Currently chinese characters are not getting displayed in PDF.I have used itextsharp to generate pdf with contents.I have tried some chinese fonts but not working. How to show chinese characters along with english characters in PDF using itextsharp?
var docIndex = new Document(new Rectangle(792, 612));
var writer = PdfWriter.GetInstance(docIndex, new FileStream(Server.MapPath("~") + "/pdf/Project-" + report.pulledId + currentTime + ".pdf", FileMode.Create));
docIndex.Open();
FontFactory.Register("c:/windows/fonts/msmincho.ttc");
StyleSheet style = new StyleSheet();
style.LoadTagStyle("body", "face", "songti");
style.LoadTagStyle("body", "encoding", BaseFont.IDENTITY_H);
foreach (var elementIndex in HTMLWorker.ParseToList(
new StringReader(sbsample.ToString()), style))
{
docIndex.Add(elementIndex);
}
docIndex.Close();

add character spacing for range of character in open xml c#

how to add character spacing for range of characters
like i want to give character spacing In word toggling for 2 characters
gg of spacing="Expanded" with By value of 4pt
Open XML has not only SDK, but also a tool for converting any document to C# code.
Best way to find out how to use some word feature is to make 2 short documnets - one with this feature used and the other - without. Then convert both documnets into C# code and compare generated code (you can use WinMerge, for example).
As you wish to apply a style to the middle of a paragraph you will need to have (at least) 3 separate Run elements; the first for the start of the unstyled text, the second for the text that has the spacing and the third for the rest of the unstyled text.
To add the character spacing you need to add a Spacing element to the Run. The Spacing element has a Value property that sets the spacing you want in twentieths of a point (so to get 4pt you need to set the Value to 80).
The following code will create a document with spacing on the gg in the word toggling
public static void CreateDoc(string fileName)
{
// Create a Wordprocessing document.
using (WordprocessingDocument package =
WordprocessingDocument.Create(fileName, WordprocessingDocumentType.Document))
{
// Add a new main document part.
package.AddMainDocumentPart();
//create a body and a paragraph
Body body = new Body();
Paragraph paragraph = new Paragraph();
//add the first part of the text to the paragraph in a Run
paragraph.AppendChild(new Run(new Text("This sentence has spacing between the gg in to")));
//create another run to hold the text with spacing
Run secondRun = new Run();
//create a RunProperties with a Spacing child.
RunProperties runProps = new RunProperties();
runProps.AppendChild(new Spacing() { Val = 80 });
//append the run properties to the Run we wish to assign spacing to
secondRun.AppendChild(runProps);
//add the text to the Run
secondRun.AppendChild(new Text("gg"));
//add the spaced Run to the paragraph
paragraph.AppendChild(secondRun);
//add the final text as a third Run
paragraph.AppendChild(new Run(new Text("ling")));
//add the paragraph to the body
body.AppendChild(paragraph);
package.MainDocumentPart.Document = new Document(body);
// Save changes to the main document part.
package.MainDocumentPart.Document.Save();
}
}
The above produces the following:
Note that you can set the Value of the Spacing to a negative number and the text will be condensed rather than expanded.

using C# with the Open XML SDK 2.5 how can you set a cell's value individual characters to specific individual font colours

I want to colourise my amino acids 1 letter codes automatically from the c# from which they are currently output.
The spreadsheet is output correctly and I have a column such as this example:
A B
1 KJAGLKJGKASLJG
2 FLFSFHDSHDSHHF
3 KJSDALKDJKLFJK
4 KLFDJSHASDFFSD
Every distinct character will be assigned its own colour. For example, K would be Red, L would be Yellow, and D would be Orange. This must be done with the Open XML SDK and not Excel COM programming or a VBA script.
At the moment, this is how the cells are output, but they use the default style sheet.
string columnName = AlphabetLetterRollOver((int) columnIndex);
string cellRef = columnName + (rowIndex + 1);
Cell cell1 = new Cell {CellReference = cellRef, StyleIndex = 1U};
cell1.DataType = CellValues.String;
CellValue cellValue1 = new CellValue();
cellValue1.Text = columnValue;
cell1.Append(cellValue1);
row1.Append(cell1);
I tried the following code as a test case to achieve what I want, but it did not work; it throws an exception:
RunProperties runProperties = new RunProperties();
FontSize fontSize = new FontSize() {Val = 10D};
Color color = new Color() {Rgb = "FF000000"};
RunFont runFont = new RunFont() {Val = "Consolas"};
FontFamily fontFamily = new FontFamily() {Val = 3};
runProperties.Append(fontSize);
runProperties.Append(color);
runProperties.Append(runFont);
runProperties.Append(fontFamily);
Run run = new Run();
Text text = new Text();
text.Text = "A";
run.Append(runProperties);
run.Append(text);
cell1.Append(run);
row1.Append(cell1);
This exception thrown is as follows:
Exception: System.InvalidOperatioonException
Message: Cannot insert the OpenXmlElement "newChild" because it is part of a tree.
Source: DocumentFormat.OpenXml
StrackTrace: at DocumentFormat.OpenXml.OpenXmlCompositeElement.AppendChild[T](T newChild)
at DocumentFormat.OpenXml.OpenXmlElement.Append(OpenXmlElement[] newChildren)
Any ideas will be appreciated, thanks!
What you need to do is create shared strings with each letter set to color you need. Then append as many of them to cells you want.
EDIT -
Download OpenXMLSDKToolV25.msi
Now here is the trick, OpenXML productivity tool is the one you need here. It allows you to brows an existing Excel file and break it down to CODES : watch
Now what you need to do is create an Excel sheet manually with what you want [In your case having multi colur text] Then open this Excel file with productivity tool and understand the CODE . Note that you will need to understand Spreadsheet file structure to understand this CODE [ Refer this] .. Now write your codes to meet your requirement using codes of Productivity tool
NOTE - Once you analyse the dummy Spreadsheet with Productivity tool you will understand why giving or guiding with CODE examples as an answer is not practical. Just try with A cell with multicolor texts

FormattedText.BuildGeometry dropping characters

I am trying to use FormattedText.BuildGeometry to determine how the characters are laid out, in order to determine the logical position of the mouse. In my context, the FormattedText can be assumed to be a single line - I should get a geometry group with 1 child, which has a child geometry for each character. This is true except that for the characters "f" and "t", repeating the character in the text of the FormattedText will cause the number of geometries on the line to be one less than the number of characters in the text.
Example code:
var tf = new Typeface(new FontFamily("Calibri"), FontStyles.Normal, FontWeights.Normal, FontStretches.Normal);
var ft = new FormattedText("ff", CultureInfo.CurrentCulture, FlowDirection.LeftToRight, tf, 14, Brushes.Black);
var g = ft.BuildGeometry(new Point(0, 0));
var gc = (GeometryGroup)((GeometryGroup)g).Children[0];
Debug.Assert(gc.Children.Count == ft.Text.Length, "Expected length of " + ft.Text.Length + " but found " + gc.Children.Count);
You can just attach this to a button in an empty WPF application.
This fails for anything that contains "ff" or "tt". Changing the font changes the behaviour - some fonts I haven't found characters that cause this.
The short answer is that BuildGeometry thinks in glyphs, not characters. From the documentation (emphasis mine):
Returns a Geometry object that represents the formatted text, including all glyphs and text decorations.
In several fonts, "ff" and "tt" are ligatures, so they are represented by a single glyph.
See this question for an explanation of how to do what you wish. Basically, BuildHighlightGeometry(Point, int, int) can return the bounding box for individual characters. You can iterate over the bounding boxes and perform hit tests until you find the character that matches.

Categories