本文整理汇总了C#中Dictionary.AddIfMissing方法的典型用法代码示例。如果您正苦于以下问题:C# Dictionary.AddIfMissing方法的具体用法?C# Dictionary.AddIfMissing怎么用?C# Dictionary.AddIfMissing使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类Dictionary
的用法示例。
在下文中一共展示了Dictionary.AddIfMissing方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: CreateStyleFromShd
private static void CreateStyleFromShd(Dictionary<string, string> style, XElement shd)
{
if (shd == null)
return;
var shadeType = (string)shd.Attribute(W.val);
var color = (string)shd.Attribute(W.color);
var fill = (string)shd.Attribute(W.fill);
if (ShadeMapper.ContainsKey(shadeType))
{
color = ShadeMapper[shadeType](color, fill);
}
if (color != null)
{
var cvtColor = ConvertColor(color);
if (cvtColor != null && cvtColor != "")
style.AddIfMissing("background", cvtColor);
}
}
示例2: ConvertRun
/*
* Handle:
* - b
* - bdr
* - caps
* - color
* - dstrike
* - highlight
* - i
* - position
* - rFonts
* - shd
* - smallCaps
* - spacing
* - strike
* - sz
* - u
* - vanish
* - vertAlign
*
* Don't handle:
* - em
* - emboss
* - fitText
* - imprint
* - kern
* - outline
* - shadow
* - w
*
*/
private static object ConvertRun(WordprocessingDocument wordDoc, HtmlConverterSettings settings, XElement run)
{
var style = new Dictionary<string, string>();
var sn = (string)run.Attribute(PtOpenXml.StyleName);
if (sn != null)
style.Add("PtStyleName", sn);
var rPr = run.Element(W.rPr);
if (rPr == null)
{
object content2 = run.Elements().Select(e => ConvertToHtmlTransform(wordDoc, settings, e, false, 0m));
return content2;
}
// hide all content that contains the w:rPr/w:webHidden element
if (rPr.Element(W.webHidden) != null)
return null;
// W.bdr
if (rPr.Element(W.bdr) != null && (string)rPr.Elements(W.bdr).Attributes(W.val).FirstOrDefault() != "none")
{
style.AddIfMissing("border", "solid windowtext 1.0pt");
style.AddIfMissing("padding", "0in");
}
// W.color
string color = (string)rPr.Elements(W.color).Attributes(W.val).FirstOrDefault();
if (color != null)
CreateColorProperty("color", color, style);
// W.highlight
string highlight = (string)rPr.Elements(W.highlight).Attributes(W.val).FirstOrDefault();
if (highlight != null)
CreateColorProperty("background", highlight, style);
// W.shd
string shade = (string)rPr.Elements(W.shd).Attributes(W.fill).FirstOrDefault();
if (shade != null)
CreateColorProperty("background", shade, style);
// Pt.FontName
string font = null;
if (run.Element(W.sym) != null)
font = (string)run.Elements(W.sym).Attributes(W.font).FirstOrDefault();
else
font = (string)run.Attributes(PtOpenXml.FontName).FirstOrDefault();
if (font != null)
CreateFontCssProperty(font, style);
// W.sz
var languageType = (string)run.Attribute(PtOpenXml.LanguageType);
decimal? sz = null;
if (languageType == "bidi")
sz = (decimal?)rPr.Elements(W.szCs).Attributes(W.val).FirstOrDefault();
else
sz = (decimal?)rPr.Elements(W.sz).Attributes(W.val).FirstOrDefault();
if (sz != null)
style.AddIfMissing("font-size", string.Format("{0}pt", sz / 2.0m));
// W.caps
if (getBoolProp(rPr, W.caps))
style.AddIfMissing("text-transform", "uppercase");
// W.smallCaps
if (getBoolProp(rPr, W.smallCaps))
style.AddIfMissing("font-variant", "small-caps");
//.........这里部分代码省略.........
示例3: CreateBorderDivs
private static object CreateBorderDivs(WordprocessingDocument wordDoc, HtmlConverterSettings settings, IEnumerable<XElement> elements)
{
return elements.GroupAdjacent(e =>
{
if (e.Elements(W.pPr).Elements(W.pBdr).Any())
{
var pBdr = e.Element(W.pPr).Element(W.pBdr);
var indStr = "";
var ind = e.Element(W.pPr).Element(W.ind);
if (ind != null)
indStr = ind.ToString(SaveOptions.DisableFormatting);
return pBdr.ToString(SaveOptions.DisableFormatting) + indStr;
}
else if (e.Name == W.tbl)
{
return "table";
}
else
{
return ""; // empty string means no pBdr
}
})
.Select(g =>
{
if (g.Key == "")
{
var o = GroupAndVerticallySpaceNumberedParagraphs(wordDoc, settings, g, 0m);
return (object)o;
}
if (g.Key == "table")
{
var o = g.Select(gc => ConvertToHtmlTransform(wordDoc,
settings, gc, false, 0));
return o;
}
var pPr = g.First().Element(W.pPr);
var pBdr = pPr.Element(W.pBdr);
Dictionary<string, string> style = new Dictionary<string, string>();
GenerateBorderStyle(pBdr, W.top, style, BorderType.Paragraph);
GenerateBorderStyle(pBdr, W.right, style, BorderType.Paragraph);
GenerateBorderStyle(pBdr, W.bottom, style, BorderType.Paragraph);
GenerateBorderStyle(pBdr, W.left, style, BorderType.Paragraph);
var ind = pPr.Element(W.ind);
decimal currentMarginLeft = 0m;
if (ind != null)
{
decimal? left = (decimal?)ind.Attribute(W.left);
decimal leftInInches = 0;
if (left != null)
leftInInches = (decimal)left / 1440;
decimal? hanging = (decimal?)ind.Attribute(W.hanging);
decimal hangingInInches = 0;
if (hanging != null)
hangingInInches = -(decimal)hanging / 1440;
currentMarginLeft = leftInInches + hangingInInches;
style.AddIfMissing("margin-left", string.Format("{0:0.00}in", currentMarginLeft));
}
var div = new XElement(Xhtml.div,
GroupAndVerticallySpaceNumberedParagraphs(wordDoc, settings, g, currentMarginLeft));
div.AddAnnotation(style);
return div;
})
.ToList();
}
示例4: ProcessTableCell
private static object ProcessTableCell(WordprocessingDocument wordDoc, HtmlConverterSettings settings, XElement element)
{
var style = new Dictionary<string, string>();
XAttribute colSpan = null;
XAttribute rowSpan = null;
var tcPr = element.Element(W.tcPr);
if (tcPr != null)
{
if ((string) tcPr.Elements(W.vMerge).Attributes(W.val).FirstOrDefault() == "restart")
{
var currentRow = element.Parent.ElementsBeforeSelf(W.tr).Count();
var currentCell = element.ElementsBeforeSelf(W.tc).Count();
var tbl = element.Parent.Parent;
int rowSpanCount = 1;
currentRow += 1;
while (true)
{
var row = tbl.Elements(W.tr).Skip(currentRow).FirstOrDefault();
if (row == null)
break;
var cell2 = row.Elements(W.tc).Skip(currentCell).FirstOrDefault();
if (cell2 == null)
break;
if (cell2.Elements(W.tcPr).Elements(W.vMerge).FirstOrDefault() == null)
break;
if ((string) cell2.Elements(W.tcPr).Elements(W.vMerge).Attributes(W.val).FirstOrDefault() == "restart")
break;
currentRow += 1;
rowSpanCount += 1;
}
rowSpan = new XAttribute("rowspan", rowSpanCount);
}
if (tcPr.Element(W.vMerge) != null &&
(string) tcPr.Elements(W.vMerge).Attributes(W.val).FirstOrDefault() != "restart")
return null;
if (tcPr.Element(W.vAlign) != null)
{
var vAlignVal = (string) tcPr.Elements(W.vAlign).Attributes(W.val).FirstOrDefault();
if (vAlignVal == "top")
style.AddIfMissing("vertical-align", "top");
else if (vAlignVal == "center")
style.AddIfMissing("vertical-align", "middle");
else if (vAlignVal == "bottom")
style.AddIfMissing("vertical-align", "bottom");
else
style.AddIfMissing("vertical-align", "middle");
}
style.AddIfMissing("vertical-align", "top");
if ((string) tcPr.Elements(W.tcW).Attributes(W.type).FirstOrDefault() == "dxa")
{
decimal width = (int) tcPr.Elements(W.tcW).Attributes(W._w).FirstOrDefault();
style.AddIfMissing("width", string.Format(NumberFormatInfo.InvariantInfo, "{0}pt", width/20m));
}
if ((string) tcPr.Elements(W.tcW).Attributes(W.type).FirstOrDefault() == "pct")
{
decimal width = (int) tcPr.Elements(W.tcW).Attributes(W._w).FirstOrDefault();
style.AddIfMissing("width", string.Format(NumberFormatInfo.InvariantInfo, "{0:0.0}%", width/50m));
}
var tcBorders = tcPr.Element(W.tcBorders);
GenerateBorderStyle(tcBorders, W.top, style, BorderType.Cell);
GenerateBorderStyle(tcBorders, W.right, style, BorderType.Cell);
GenerateBorderStyle(tcBorders, W.bottom, style, BorderType.Cell);
GenerateBorderStyle(tcBorders, W.left, style, BorderType.Cell);
CreateStyleFromShd(style, tcPr.Element(W.shd));
var gridSpan = tcPr.Elements(W.gridSpan).Attributes(W.val).Select(a => (int?) a).FirstOrDefault();
if (gridSpan != null)
colSpan = new XAttribute("colspan", (int) gridSpan);
}
style.AddIfMissing("padding-top", "0");
style.AddIfMissing("padding-bottom", "0");
var cell = new XElement(Xhtml.td,
rowSpan,
colSpan,
CreateBorderDivs(wordDoc, settings, element.Elements()));
cell.AddAnnotation(style);
return cell;
}
示例5: ProcessTableRow
private static object ProcessTableRow(WordprocessingDocument wordDoc, HtmlConverterSettings settings, XElement element,
decimal currentMarginLeft)
{
var style = new Dictionary<string, string>();
int? trHeight = (int?) element.Elements(W.trPr).Elements(W.trHeight).Attributes(W.val).FirstOrDefault();
if (trHeight != null)
style.AddIfMissing("height",
string.Format(NumberFormatInfo.InvariantInfo, "{0:0.00}in", (decimal) trHeight/1440m));
var htmlRow = new XElement(Xhtml.tr,
element.Elements().Select(e => ConvertToHtmlTransform(wordDoc, settings, e, false, currentMarginLeft)));
if (style.Any())
htmlRow.AddAnnotation(style);
return htmlRow;
}
示例6: ConvertToHtmlTransform
//.........这里部分代码省略.........
}
var paragraph = element.Ancestors(W.p).FirstOrDefault();
bool isBidi = false;
if (paragraph != null)
{
var bidi = paragraph
.Elements(W.pPr)
.Elements(W.bidi)
.Where(b => b.Attribute(W.val) == null || b.Attribute(W.val).ToBoolean() == true)
.FirstOrDefault();
isBidi = bidi != null;
}
var br = new XElement(Xhtml.br);
XEntity zeroWidthChar = null;
if (isBidi)
zeroWidthChar = new XEntity("#x200f"); // RLM
else
zeroWidthChar = new XEntity("#x200e"); // LRM
return new object[] {
br,
zeroWidthChar,
span,
};
}
// Transform w:noBreakHyphen to '-'
if (element.Name == W.noBreakHyphen)
return new XText("-");
// Transform w:tbl to h:tbl.
if (element.Name == W.tbl)
{
var style = new Dictionary<string, string>();
style.AddIfMissing("border-collapse", "collapse");
style.AddIfMissing("border", "none");
var bidiVisual = element.Elements(W.tblPr).Elements(W.bidiVisual).FirstOrDefault();
var tblW = element.Elements(W.tblPr).Elements(W.tblW).FirstOrDefault();
if (tblW != null)
{
var type = (string)tblW.Attribute(W.type);
if (type != null && type == "pct")
{
var w = (int)tblW.Attribute(W._w);
style.AddIfMissing("width", (w / 50).ToString() + "%");
}
}
var tblInd = element.Elements(W.tblPr).Elements(W.tblInd).FirstOrDefault();
if (tblInd != null)
{
var tblIndType = (string)tblInd.Attribute(W.type);
if (tblIndType != null)
{
if (tblIndType == "dxa")
{
var width = (decimal?)tblInd.Attribute(W._w);
if (width != null)
{
style.AddIfMissing("margin-left", string.Format("{0}pt", width / 20m));
}
}
}
}
XAttribute tableDirection = null;
if (bidiVisual != null)
{
tableDirection = new XAttribute("dir", "rtl");
示例7: CreateStyleFromInd
private static void CreateStyleFromInd(Dictionary<string, string> style, XElement ind, XName elementName,
decimal currentMarginLeft, bool isBidi)
{
if (ind == null) return;
var left = (decimal?) ind.Attribute(W.left);
if (left != null && elementName != Xhtml.span)
{
var leftInInches = (decimal) left/1440 - currentMarginLeft;
style.AddIfMissing(isBidi ? "margin-right" : "margin-left",
leftInInches > 0m
? string.Format(NumberFormatInfo.InvariantInfo, "{0:0.00}in", leftInInches)
: "0");
}
var right = (decimal?) ind.Attribute(W.right);
if (right != null)
{
var rightInInches = (decimal) right/1440;
style.AddIfMissing(isBidi ? "margin-left" : "margin-right",
rightInInches > 0m
? string.Format(NumberFormatInfo.InvariantInfo, "{0:0.00}in", rightInInches)
: "0");
}
var firstLine = (decimal?) ind.Attribute(W.firstLine);
if (firstLine != null && elementName != Xhtml.span)
{
var firstLineInInches = (decimal) firstLine/1440m;
style.AddIfMissing("text-indent",
string.Format(NumberFormatInfo.InvariantInfo, "{0:0.00}in", firstLineInInches));
}
var hanging = (decimal?) ind.Attribute(W.hanging);
if (hanging != null && elementName != Xhtml.span)
{
var hangingInInches = (decimal) -hanging/1440m;
style.AddIfMissing("text-indent",
string.Format(NumberFormatInfo.InvariantInfo, "{0:0.00}in", hangingInInches));
}
}
示例8: DefineRunStyle
private static Dictionary<string, string> DefineRunStyle(XElement run)
{
var style = new Dictionary<string, string>();
var rPr = run.Elements(W.rPr).First();
var styleName = (string) run.Attribute(PtOpenXml.StyleName);
if (styleName != null)
style.Add("PtStyleName", styleName);
// W.bdr
if (rPr.Element(W.bdr) != null && (string) rPr.Elements(W.bdr).Attributes(W.val).FirstOrDefault() != "none")
{
style.AddIfMissing("border", "solid windowtext 1.0pt");
style.AddIfMissing("padding", "0");
}
// W.color
var color = (string) rPr.Elements(W.color).Attributes(W.val).FirstOrDefault();
if (color != null)
CreateColorProperty("color", color, style);
// W.highlight
var highlight = (string) rPr.Elements(W.highlight).Attributes(W.val).FirstOrDefault();
if (highlight != null)
CreateColorProperty("background", highlight, style);
// W.shd
var shade = (string) rPr.Elements(W.shd).Attributes(W.fill).FirstOrDefault();
if (shade != null)
CreateColorProperty("background", shade, style);
// Pt.FontName
var sym = run.Element(W.sym);
var font = sym != null
? (string) sym.Attributes(W.font).FirstOrDefault()
: (string) run.Attributes(PtOpenXml.FontName).FirstOrDefault();
if (font != null)
CreateFontCssProperty(font, style);
// W.sz
var languageType = (string)run.Attribute(PtOpenXml.LanguageType);
var sz = GetFontSize(languageType, rPr);
if (sz != null)
style.AddIfMissing("font-size", string.Format(NumberFormatInfo.InvariantInfo, "{0}pt", sz/2.0m));
// W.caps
if (GetBoolProp(rPr, W.caps))
style.AddIfMissing("text-transform", "uppercase");
// W.smallCaps
if (GetBoolProp(rPr, W.smallCaps))
style.AddIfMissing("font-variant", "small-caps");
// W.spacing
var spacingInTwips = (decimal?) rPr.Elements(W.spacing).Attributes(W.val).FirstOrDefault();
if (spacingInTwips != null)
style.AddIfMissing("letter-spacing",
spacingInTwips > 0m
? string.Format(NumberFormatInfo.InvariantInfo, "{0}pt", spacingInTwips/20)
: "0");
// W.position
var position = (decimal?) rPr.Elements(W.position).Attributes(W.val).FirstOrDefault();
if (position != null)
{
style.AddIfMissing("position", "relative");
style.AddIfMissing("top", string.Format(NumberFormatInfo.InvariantInfo, "{0}pt", -(position/2)));
}
// W.vanish
if (GetBoolProp(rPr, W.vanish) && !GetBoolProp(rPr, W.specVanish))
style.AddIfMissing("display", "none");
// W.u
if (rPr.Element(W.u) != null && (string) rPr.Elements(W.u).Attributes(W.val).FirstOrDefault() != "none")
style.AddIfMissing("text-decoration", "underline");
// W.i
style.AddIfMissing("font-style", GetBoolProp(rPr, W.i) ? "italic" : "normal");
// W.b
style.AddIfMissing("font-weight", GetBoolProp(rPr, W.b) ? "bold" : "normal");
// W.strike
if (GetBoolProp(rPr, W.strike) || GetBoolProp(rPr, W.dstrike))
style.AddIfMissing("text-decoration", "line-through");
return style;
}
示例9: CreateBorderDivs
private static object CreateBorderDivs(WordprocessingDocument wordDoc, HtmlConverterSettings settings, IEnumerable<XElement> elements)
{
return elements.GroupAdjacent(e =>
{
var pBdr = e.Elements(W.pPr).Elements(W.pBdr).FirstOrDefault();
if (pBdr != null)
{
var indStr = string.Empty;
var ind = e.Elements(W.pPr).Elements(W.ind).FirstOrDefault();
if (ind != null)
indStr = ind.ToString(SaveOptions.DisableFormatting);
return pBdr.ToString(SaveOptions.DisableFormatting) + indStr;
}
return e.Name == W.tbl ? "table" : string.Empty;
})
.Select(g =>
{
if (g.Key == string.Empty)
{
return (object) GroupAndVerticallySpaceNumberedParagraphs(wordDoc, settings, g, 0m);
}
if (g.Key == "table")
{
return g.Select(gc => ConvertToHtmlTransform(wordDoc, settings, gc, false, 0));
}
var pPr = g.First().Elements(W.pPr).First();
var pBdr = pPr.Element(W.pBdr);
var style = new Dictionary<string, string>();
GenerateBorderStyle(pBdr, W.top, style, BorderType.Paragraph);
GenerateBorderStyle(pBdr, W.right, style, BorderType.Paragraph);
GenerateBorderStyle(pBdr, W.bottom, style, BorderType.Paragraph);
GenerateBorderStyle(pBdr, W.left, style, BorderType.Paragraph);
var currentMarginLeft = 0m;
var ind = pPr.Element(W.ind);
if (ind != null)
{
var leftInInches = (decimal?) ind.Attribute(W.left)/1440m ?? 0;
var hangingInInches = -(decimal?) ind.Attribute(W.hanging)/1440m ?? 0;
currentMarginLeft = leftInInches + hangingInInches;
style.AddIfMissing("margin-left",
currentMarginLeft > 0m
? string.Format(NumberFormatInfo.InvariantInfo, "{0:0.00}in", currentMarginLeft)
: "0");
}
var div = new XElement(Xhtml.div,
GroupAndVerticallySpaceNumberedParagraphs(wordDoc, settings, g, currentMarginLeft));
div.AddAnnotation(style);
return div;
})
.ToList();
}
示例10: DefineFontSize
private static void DefineFontSize(Dictionary<string, string> style, XElement paragraph)
{
var sz = paragraph
.DescendantsTrimmed(W.txbxContent)
.Where(e => e.Name == W.r)
.Select(GetFontSize)
.Max();
if (sz != null)
style.AddIfMissing("font-size", string.Format(NumberFormatInfo.InvariantInfo, "{0}pt", sz / 2.0m));
}
示例11: DefineLineHeight
private static void DefineLineHeight(Dictionary<string, string> style, XElement paragraph)
{
var allRunsAreUniDirectional = paragraph
.DescendantsTrimmed(W.txbxContent)
.Where(e => e.Name == W.r)
.Select(run => (string)run.Attribute(PtOpenXml.LanguageType))
.All(lt => lt != "bidi");
if (allRunsAreUniDirectional)
style.AddIfMissing("line-height", "108%");
}
示例12: CreateStyleFromTextAlignment
private static void CreateStyleFromTextAlignment(Dictionary<string, string> style, XElement textAlignment)
{
if (textAlignment == null) return;
var verticalTextAlignment = (string)textAlignment.Attributes(W.val).FirstOrDefault();
if (verticalTextAlignment == null || verticalTextAlignment == "auto") return;
if (verticalTextAlignment == "top")
style.AddIfMissing("vertical-align", "top");
else if (verticalTextAlignment == "center")
style.AddIfMissing("vertical-align", "middle");
else if (verticalTextAlignment == "baseline")
style.AddIfMissing("vertical-align", "baseline");
else if (verticalTextAlignment == "bottom")
style.AddIfMissing("vertical-align", "bottom");
}
示例13: CreateStyleFromSpacing
private static void CreateStyleFromSpacing(Dictionary<string, string> style, XElement spacing, XName elementName,
bool suppressTrailingWhiteSpace)
{
if (spacing == null) return;
var spacingBefore = (decimal?) spacing.Attribute(W.before);
if (spacingBefore != null && elementName != Xhtml.span)
style.AddIfMissing("margin-top",
spacingBefore > 0m
? string.Format(NumberFormatInfo.InvariantInfo, "{0}pt", spacingBefore/20.0m)
: "0");
var lineRule = (string) spacing.Attribute(W.lineRule);
if (lineRule == "auto")
{
var line = (decimal) spacing.Attribute(W.line);
if (line != 240m)
{
var pct = (line/240m)*100m;
style.Add("line-height", string.Format(NumberFormatInfo.InvariantInfo, "{0:0.0}%", pct));
}
}
if (lineRule == "exact")
{
var line = (decimal) spacing.Attribute(W.line);
var points = line/20m;
style.Add("line-height", string.Format(NumberFormatInfo.InvariantInfo, "{0:0.0}pt", points));
}
if (lineRule == "atLeast")
{
var line = (decimal) spacing.Attribute(W.line);
var points = line/20m;
if (points >= 14m)
style.Add("line-height", string.Format(NumberFormatInfo.InvariantInfo, "{0:0.0}pt", points));
}
var spacingAfter = suppressTrailingWhiteSpace ? 0m : (decimal?) spacing.Attribute(W.after);
if (spacingAfter != null)
style.AddIfMissing("margin-bottom",
spacingAfter > 0m
? string.Format(NumberFormatInfo.InvariantInfo, "{0}pt", spacingAfter/20.0m)
: "0");
}
示例14: CreateStyleFromJc
private static void CreateStyleFromJc(Dictionary<string, string> style, XElement jc, bool isBidi)
{
if (jc != null)
{
var jcVal = (string)jc.Attributes(W.val).FirstOrDefault() ?? "left";
if (jcVal == "left")
style.AddIfMissing("text-align", isBidi ? "right" : "left");
else if (jcVal == "right")
style.AddIfMissing("text-align", isBidi ? "left" : "right");
else if (jcVal == "center")
style.AddIfMissing("text-align", "center");
else if (jcVal == "both")
style.AddIfMissing("text-align", "justify");
}
}
示例15: CreateColorProperty
private static void CreateColorProperty(string propertyName, string color, Dictionary<string, string> style)
{
if (color == null)
return;
if (namedColors.ContainsKey(color))
{
var lc = namedColors[color];
if (lc == "")
return;
style.AddIfMissing(propertyName, lc);
return;
}
style.AddIfMissing(propertyName, "#" + color);
}