private bool GuardarDatos() { object tamano = 0; object familia = null; Style estiloNuevo = new Style(); FontSizeConverter convertidor = new FontSizeConverter(); FontFamilyConverter conFami = new FontFamilyConverter(); try { tamano = txtTamano.Text.ToString(); familia = listFonts.SelectedItem.ToString(); Setter Size = new Setter(FontSizeProperty, convertidor.ConvertFrom(tamano)); Setter Family = new Setter(FontFamilyProperty, conFami.ConvertFrom(familia)); Setter color = new Setter(ForegroundProperty, new SolidColorBrush(Color.FromRgb(byte.Parse(sldRojo.Value.ToString()), byte.Parse(sldVerde.Value.ToString()), byte.Parse(sldAzul.Value.ToString())))); estiloNuevo.Setters.Add(Size); estiloNuevo.Setters.Add(Family); estiloNuevo.Setters.Add(color); App.Current.Resources["txtEnter"] = estiloNuevo; return(true); } catch (Exception e) { MessageBox.Show("ERROR: " + e.Message); return(false); } }
protected override FontFamily Convert(string value, object parameter) { if (string.IsNullOrWhiteSpace(value)) { return(defaultValue); } try { return((FontFamily)ffc.ConvertFrom(value)); } catch (Exception e) { Console.Error.WriteLine(e); return(defaultValue); } }
/// <summary> /// Opens a .docx file through a string path, /// converts the file and its contents to an in-memory /// FlowDocument and returns the FlowDocument. /// </summary> /// <param name="path">Full path of the file.</param> /// <returns>new FlowDocument populated with the contents of the .docx file.</returns> public FlowDocument LoadDocument(string path) { FlowDocument flowDoc = new FlowDocument(); Package package = Package.Open(path); Uri documentUri = new Uri("/word/document.xml", UriKind.Relative); PackagePart documentPart = package.GetPart(documentUri); XElement wordDoc = XElement.Load(new StreamReader(documentPart.GetStream())); XNamespace w = "http://schemas.openxmlformats.org/wordprocessingml/2006/main"; var paragraphs = from p in wordDoc.Descendants(w + "p") select p; foreach (var p in paragraphs) { var style = from s in p.Descendants(w + "pPr") select s; var font = (from f in style.Descendants(w + "rFonts") select f.FirstAttribute).FirstOrDefault(); var size = (from s in style.Descendants(w + "sz") select s.FirstAttribute).FirstOrDefault(); Paragraph par = new Paragraph(); Run r = new Run(p.Value); if (font != null) { FontFamilyConverter converter = new FontFamilyConverter(); r.FontFamily = (FontFamily)converter.ConvertFrom(font.Value); } if (size != null) { r.FontSize = double.Parse(size.Value); } par.Inlines.Add(r); flowDoc.Blocks.Add(par); } return(flowDoc); }
public void OpenDocxFile(string fileName) { XElement wordDoc = null; try { Package package = Package.Open(fileName); Uri documentUri = new Uri("/word/document.xml", UriKind.Relative); PackagePart documentPart = package.GetPart(documentUri); wordDoc = XElement.Load(new StreamReader(documentPart.GetStream())); } catch (Exception) { this.rtbDocument.Document.Blocks.Add(new Paragraph(new Run("Cannot open file!"))); return; } XNamespace w = "http://schemas.openxmlformats.org/wordprocessingml/2006/main"; var paragraphs = from p in wordDoc.Descendants(w + "p") select p; foreach (var p in paragraphs) { var style = from s in p.Descendants(w + "rPr") select s; var parStyle = from s in p.Descendants(w + "pPr") select s; var font = (from f in style.Descendants(w + "rFonts") select f.FirstAttribute).FirstOrDefault(); var size = (from s in style.Descendants(w + "sz") select s.FirstAttribute).FirstOrDefault(); var rgbColor = (from c in style.Descendants(w + "color") select c.FirstAttribute).FirstOrDefault(); var bold = (from b in style.Descendants(w + "b") select b).FirstOrDefault(); var italic = (from i in style.Descendants(w + "i") select i).FirstOrDefault(); var underline = (from u in style.Descendants(w + "u") select u).FirstOrDefault(); var alignment = (from a in parStyle.Descendants(w + "jc") select a).FirstOrDefault(); var textElements = from t in p.Descendants(w + "t") select t; var list = (from l in parStyle.Descendants(w + "numPr") select l).FirstOrDefault(); StringBuilder text = new StringBuilder(); foreach (var element in textElements) { text.Append(element.Value); } Paragraph par = new Paragraph(); Run run = new Run(text.ToString()); List items = new List(); if (list != null) { //List items = new List(); items.ListItems.Add(new ListItem(par)); var markerStyleElement = (from l in list.Descendants(w + "numId") select l).FirstOrDefault(); TextMarkerStyle markerStyle = new TextMarkerStyle(); if (markerStyleElement != null) { int value = int.Parse(markerStyleElement.Attribute(w + "val").Value); switch (value) { case 1: markerStyle = TextMarkerStyle.Disc; break; case 2: markerStyle = TextMarkerStyle.Decimal; break; } } items.MarkerStyle = markerStyle; } if (font != null) { FontFamilyConverter converter = new FontFamilyConverter(); run.FontFamily = (FontFamily)converter.ConvertFrom(font.Value); } if (size != null) { run.FontSize = double.Parse(size.Value); } if (rgbColor != null) { Color color = ConvertRgbToColor(rgbColor.Value); run.Foreground = new SolidColorBrush(color); } if (bold != null) { if (bold.Attribute(w + "val") != null) { if (bold.Attribute(w + "val").Value == "off") { run.FontWeight = FontWeights.Normal; } else { run.FontWeight = FontWeights.Bold; } } else { run.FontWeight = FontWeights.Bold; } } if (italic != null) { if (italic.Attribute(w + "val") != null) { if (italic.Attribute(w + "val").Value == "off") { run.FontStyle = FontStyles.Normal; } else { run.FontStyle = FontStyles.Italic; } } else { run.FontStyle = FontStyles.Italic; } } if (underline != null) { run.TextDecorations.Add(TextDecorations.Underline); } if (alignment != null) { TextAlignment textAlignment = new TextAlignment(); string value = alignment.Attribute(w + "val").Value; switch (value) { case "left": textAlignment = TextAlignment.Left; break; case "center": textAlignment = TextAlignment.Center; break; case "right": textAlignment = TextAlignment.Right; break; case "justify": textAlignment = TextAlignment.Justify; break; } par.TextAlignment = textAlignment; } else { par.TextAlignment = TextAlignment.Left; } par.Inlines.Add(run); if (list != null) { this.rtbDocument.Document.Blocks.Add(items); } else { this.rtbDocument.Document.Blocks.Add(par); } } }
internal static Style ToWPFStyle(this XElement elem) { Style style = new Style(); if (elem != null) { var setters = elem.Descendants().Select(elm => { Setter setter = null; if (elm.Name == w + "left" || elm.Name == w + "right" || elm.Name == w + "top" || elm.Name == w + "bottom") { ThicknessConverter tk = new ThicknessConverter(); Thickness thinkness = (Thickness)tk.ConvertFrom(elm.Attribute(w + "sz").Value); BrushConverter bc = new BrushConverter(); Brush color = (Brush)bc.ConvertFrom(string.Format("#{0}", elm.Attribute(w + "color").Value)); setter = new Setter(Block.BorderThicknessProperty, thinkness); //style.Setters.Add(new Setter(Block.BorderBrushProperty,color)); } else if (elm.Name == w + "rFonts") { FontFamilyConverter ffc = new FontFamilyConverter(); setter = new Setter(TextElement.FontFamilyProperty, ffc.ConvertFrom(elm.Attribute(w + "ascii").Value)); } else if (elm.Name == w + "b") { setter = new Setter(TextElement.FontWeightProperty, FontWeights.Bold); } else if (elm.Name == w + "color") { BrushConverter bc = new BrushConverter(); setter = new Setter(TextElement.ForegroundProperty, bc.ConvertFrom(string.Format("#{0}", elm.Attribute(w_val).Value))); } else if (elm.Name == w + "em" || elm.Name == w + "i") { setter = new Setter(TextElement.FontStyleProperty, FontStyles.Italic); } else if (elm.Name == w + "strike") { setter = new Setter(Inline.TextDecorationsProperty, TextDecorations.Strikethrough); } else if (elm.Name == w + "sz") { FontSizeConverter fsc = new FontSizeConverter(); setter = new Setter(TextElement.FontSizeProperty, fsc.ConvertFrom(elm.Attribute(w_val).Value)); } else if (elm.Name == w + "ilvl") { Console.WriteLine(elm.Attribute(w_val)); } else if (elm.Name == w + "numPr") { Console.WriteLine(elm.Value); } else if (elm.Name == w + "numId") { Console.WriteLine(elm.Attribute(w_val)); int value = int.Parse(elm.Attribute(w + "val").Value); if (value == 2) { setter = new Setter(List.MarkerStyleProperty, TextMarkerStyle.Decimal); } } else if (elm.Name == w + "u") { setter = new Setter(Inline.TextDecorationsProperty, TextDecorations.Underline); } else if (elm.Name == w + "jc") { TextAlignment textAlignment = new TextAlignment(); string value = elm.Attribute(w + "val").Value; switch (value) { case "left": textAlignment = TextAlignment.Left; break; case "center": textAlignment = TextAlignment.Center; break; case "right": textAlignment = TextAlignment.Right; break; case "justify": textAlignment = TextAlignment.Justify; break; } setter = new Setter(Block.TextAlignmentProperty, textAlignment); } else { Console.WriteLine(elm.Name); } return(setter); }); foreach (SetterBase setter in setters) { if (setter != null) { style.Setters.Add(setter); } } } return(style); }