private float AddText(string[] chunks, iTextSharp.text.Font font, float spacingBefore, float spacingAfter, int alignment, float lineHightMultiplier, Document doc) { int count = 0; float height = 0; var paragraph = new Paragraph(); float fontSize = font.Size * lineHightMultiplier; var phrase = new Phrase(); foreach(string item in chunks) { if (string.IsNullOrEmpty(item)) continue; var chunk = new Chunk(item + Environment.NewLine, font); chunk.setLineHeight(fontSize); phrase.Add(chunk); count++; } if (count > 0) { paragraph.Add(phrase); paragraph.Alignment = alignment; paragraph.SpacingBefore = spacingBefore; paragraph.SpacingAfter = spacingAfter; doc.Add(paragraph); height = spacingBefore + spacingAfter + count * fontSize; } return height; }
/** * * @param c the Chunk to apply CSS to. * @param t the tag containing the chunk data * @return the styled chunk */ virtual public Chunk Apply(Chunk c, Tag t) { Font f = ApplyFontStyles(t); float size = f.Size; String value = null; IDictionary<String, String> rules = t.CSS; foreach (KeyValuePair<String, String> entry in rules) { String key = entry.Key; value = entry.Value; if (Util.EqualsIgnoreCase(CSS.Property.FONT_STYLE, key)) { if (Util.EqualsIgnoreCase(CSS.Value.OBLIQUE, value)) { c.SetSkew(0, 12); } } else if (Util.EqualsIgnoreCase(CSS.Property.LETTER_SPACING, key)) { String letterSpacing = entry.Value; float letterSpacingValue = 0f; if (utils.IsRelativeValue(value)) { letterSpacingValue = utils.ParseRelativeValue(letterSpacing, f.Size); } else if (utils.IsMetricValue(value)) { letterSpacingValue = utils.ParsePxInCmMmPcToPt(letterSpacing); } c.SetCharacterSpacing(letterSpacingValue); } else if (Util.EqualsIgnoreCase(CSS.Property.XFA_FONT_HORIZONTAL_SCALE, key)) { // only % allowed; need a catch block NumberFormatExc? c.SetHorizontalScaling( float.Parse(value.Replace("%", ""))/100); } } // following styles are separate from the for each loop, because they are based on font settings like size. if (rules.TryGetValue(CSS.Property.VERTICAL_ALIGN, out value)) { if (Util.EqualsIgnoreCase(CSS.Value.SUPER, value) || Util.EqualsIgnoreCase(CSS.Value.TOP, value) || Util.EqualsIgnoreCase(CSS.Value.TEXT_TOP, value)) { c.SetTextRise((float) (size/2 + 0.5)); } else if (Util.EqualsIgnoreCase(CSS.Value.SUB, value) || Util.EqualsIgnoreCase(CSS.Value.BOTTOM, value) || Util.EqualsIgnoreCase(CSS.Value.TEXT_BOTTOM, value)) { c.SetTextRise(-size/2); } else { c.SetTextRise(utils.ParsePxInCmMmPcToPt(value)); } } String xfaVertScale; if (rules.TryGetValue(CSS.Property.XFA_FONT_VERTICAL_SCALE, out xfaVertScale)) { if (xfaVertScale.Contains("%")) { size *= float.Parse(xfaVertScale.Replace("%", ""))/100; c.SetHorizontalScaling(100/float.Parse(xfaVertScale.Replace("%", ""))); } } if (rules.TryGetValue(CSS.Property.TEXT_DECORATION, out value)) { String[] splitValues = new Regex(@"\s+").Split(value); foreach (String curValue in splitValues) { if (Util.EqualsIgnoreCase(CSS.Value.UNDERLINE, curValue)) { c.SetUnderline(0.75f, -size/8f); } if (Util.EqualsIgnoreCase(CSS.Value.LINE_THROUGH, curValue)) { c.SetUnderline(0.75f, size/4f); } } } if (rules.TryGetValue(CSS.Property.BACKGROUND_COLOR, out value)) { c.SetBackground(HtmlUtilities.DecodeColor(value)); } f.Size = size; c.Font = f; float? leading = null; value = null; if (rules.TryGetValue(CSS.Property.LINE_HEIGHT, out value)) { if (utils.IsNumericValue(value)) { leading = float.Parse(value) * c.Font.Size; } else if (utils.IsRelativeValue(value)) { leading = utils.ParseRelativeValue(value, c.Font.Size); } else if (utils.IsMetricValue(value)) { leading = utils.ParsePxInCmMmPcToPt(value); } } if (leading != null) { c.setLineHeight((float)leading); } return c; }
private float AddList(string[] chunks, iTextSharp.text.Font font, float spacingBefore, float spacingAfter, float spacingBetween, int alignment, float lineHightMultiplier, string listSymbol, int padding, float indentationLeft, Document doc) { int count = 0; float height = 0; float fontSize = font.Size * lineHightMultiplier; var paragraph = new Paragraph(); var list = new List(List.UNORDERED); list.SetListSymbol(listSymbol.PadRight(padding)); list.IndentationLeft = indentationLeft; foreach (string item in chunks) { if (string.IsNullOrEmpty(item)) continue; var chunk = new Chunk(item, font); chunk.setLineHeight(font.Size * lineHightMultiplier); var listItem = new ListItem(chunk); listItem.SpacingBefore = spacingBetween; list.Add(listItem); count++; } if (count > 0) { paragraph.Add(list); paragraph.Alignment = alignment; paragraph.SpacingBefore = spacingBefore; paragraph.SpacingAfter = spacingAfter; doc.Add(paragraph); height = spacingBefore + spacingAfter + count * (fontSize + spacingBetween) - spacingBetween; } return height; }