private PdfObject GetParentAttribute(IPdfStructureElement parent, PdfName name) { if (parent == null) { return(null); } return(parent.GetAttribute(name)); }
private void WriteAttributes(Paragraph paragraph) { if (paragraph != null) { // Setting non-inheritable attributes if (paragraph.SpacingBefore.CompareTo(0f) != 0) { this.SetAttribute(PdfName.SPACEBEFORE, new PdfNumber(paragraph.SpacingBefore)); } if (paragraph.SpacingAfter.CompareTo(0f) != 0) { this.SetAttribute(PdfName.SPACEAFTER, new PdfNumber(paragraph.SpacingAfter)); } // Setting inheritable attributes IPdfStructureElement parent = (IPdfStructureElement)this.GetParent(true); PdfObject obj = parent.GetAttribute(PdfName.COLOR); if ((paragraph.Font != null) && (paragraph.Font.Color != null)) { BaseColor c = paragraph.Font.Color; SetColorAttribute(c, obj, PdfName.COLOR); } obj = parent.GetAttribute(PdfName.TEXTINDENT); if (paragraph.FirstLineIndent.CompareTo(0f) != 0) { bool writeIndent = true; if (obj is PdfNumber) { if (((PdfNumber)obj).FloatValue.CompareTo(paragraph.FirstLineIndent) == 0) { writeIndent = false; } } if (writeIndent) { this.SetAttribute(PdfName.TEXTINDENT, new PdfNumber(paragraph.FirstLineIndent)); } } obj = parent.GetAttribute(PdfName.STARTINDENT); if (obj is PdfNumber) { float startIndent = ((PdfNumber)obj).FloatValue; if (startIndent.CompareTo(paragraph.IndentationLeft) != 0) { this.SetAttribute(PdfName.STARTINDENT, new PdfNumber(paragraph.IndentationLeft)); } } else { if (Math.Abs(paragraph.IndentationLeft) > float.Epsilon) { this.SetAttribute(PdfName.STARTINDENT, new PdfNumber(paragraph.IndentationLeft)); } } obj = parent.GetAttribute(PdfName.ENDINDENT); if (obj is PdfNumber) { float endIndent = ((PdfNumber)obj).FloatValue; if (endIndent.CompareTo(paragraph.IndentationRight) != 0) { this.SetAttribute(PdfName.ENDINDENT, new PdfNumber(paragraph.IndentationRight)); } } else { if (paragraph.IndentationRight.CompareTo(0) != 0) { this.SetAttribute(PdfName.ENDINDENT, new PdfNumber(paragraph.IndentationRight)); } } SetTextAlignAttribute(paragraph.Alignment); } }
private void WriteAttributes(Chunk chunk) { if (chunk != null) { if (chunk.GetImage() != null) { WriteAttributes(chunk.GetImage()); } else { Dictionary <String, Object> attr = chunk.Attributes; if (attr != null) { // Setting non-inheritable attributes if (attr.ContainsKey(Chunk.UNDERLINE)) { this.SetAttribute(PdfName.TEXTDECORATIONTYPE, PdfName.UNDERLINE); } if (attr.ContainsKey(Chunk.BACKGROUND)) { Object[] back = (Object[])attr[Chunk.BACKGROUND]; BaseColor color = (BaseColor)back[0]; this.SetAttribute(PdfName.BACKGROUNDCOLOR, new PdfArray(new float[] { color.R / 255f, color.G / 255f, color.B / 255f })); } // Setting inheritable attributes IPdfStructureElement parent = (IPdfStructureElement)this.GetParent(true); PdfObject obj = parent.GetAttribute(PdfName.COLOR); if ((chunk.Font != null) && (chunk.Font.Color != null)) { BaseColor c = chunk.Font.Color; SetColorAttribute(c, obj, PdfName.COLOR); } PdfObject decorThickness = parent.GetAttribute(PdfName.TEXTDECORATIONTHICKNESS); PdfObject decorColor = parent.GetAttribute(PdfName.TEXTDECORATIONCOLOR); if (attr.ContainsKey(Chunk.UNDERLINE)) { Object[][] unders = (Object[][])attr[Chunk.UNDERLINE]; Object[] arr = unders[unders.Length - 1]; BaseColor color = (BaseColor)arr[0]; float[] floats = (float[])arr[1]; float thickness = floats[0]; // Setting thickness if (decorThickness is PdfNumber) { float t = ((PdfNumber)decorThickness).FloatValue; if (thickness.CompareTo(t) != 0) { this.SetAttribute(PdfName.TEXTDECORATIONTHICKNESS, new PdfNumber(thickness)); } } else { this.SetAttribute(PdfName.TEXTDECORATIONTHICKNESS, new PdfNumber(thickness)); } // Setting decoration color if (color != null) { SetColorAttribute(color, decorColor, PdfName.TEXTDECORATIONCOLOR); } } if (attr.ContainsKey(Chunk.LINEHEIGHT)) { float height = (float)attr[Chunk.LINEHEIGHT]; PdfObject parentLH = parent.GetAttribute(PdfName.LINEHEIGHT); if (parentLH is PdfNumber) { float pLH = ((PdfNumber)parentLH).FloatValue; if (pLH.CompareTo(height) != 0) { this.SetAttribute(PdfName.LINEHEIGHT, new PdfNumber(height)); } } else { this.SetAttribute(PdfName.LINEHEIGHT, new PdfNumber(height)); } } } } } }
private PdfObject GetParentAttribute(IPdfStructureElement parent, PdfName name) { if (parent == null) { return null; } return parent.GetAttribute(name); }
private void WriteAttributes(Paragraph paragraph) { if (paragraph != null) { // Setting non-inheritable attributes if (paragraph.SpacingBefore.CompareTo(0f) != 0) { this.SetAttribute(PdfName.SPACEBEFORE, new PdfNumber(paragraph.SpacingBefore)); } if (paragraph.SpacingAfter.CompareTo(0f) != 0) { this.SetAttribute(PdfName.SPACEAFTER, new PdfNumber(paragraph.SpacingAfter)); } // Setting inheritable attributes IPdfStructureElement parent = (IPdfStructureElement)this.GetParent(true); PdfObject obj = parent.GetAttribute(PdfName.COLOR); if ((paragraph.Font != null) && (paragraph.Font.Color != null)) { BaseColor c = paragraph.Font.Color; SetColorAttribute(c, obj, PdfName.COLOR); } obj = parent.GetAttribute(PdfName.TEXTINDENT); if (paragraph.FirstLineIndent.CompareTo(0f) != 0) { bool writeIndent = true; if (obj is PdfNumber) { if (((PdfNumber)obj).FloatValue.CompareTo(paragraph.FirstLineIndent) == 0) { writeIndent = false; } } if (writeIndent) { this.SetAttribute(PdfName.TEXTINDENT, new PdfNumber(paragraph.FirstLineIndent)); } } obj = parent.GetAttribute(PdfName.STARTINDENT); if (obj is PdfNumber) { float startIndent = ((PdfNumber)obj).FloatValue; if (startIndent.CompareTo(paragraph.IndentationLeft) != 0) { this.SetAttribute(PdfName.STARTINDENT, new PdfNumber(paragraph.IndentationLeft)); } } else { if (Math.Abs(paragraph.IndentationLeft) > float.MinValue) { this.SetAttribute(PdfName.STARTINDENT, new PdfNumber(paragraph.IndentationLeft)); } } obj = parent.GetAttribute(PdfName.ENDINDENT); if (obj is PdfNumber) { float endIndent = ((PdfNumber)obj).FloatValue; if (endIndent.CompareTo(paragraph.IndentationRight) != 0) { this.SetAttribute(PdfName.ENDINDENT, new PdfNumber(paragraph.IndentationRight)); } } else { if (paragraph.IndentationRight.CompareTo(0) != 0) { this.SetAttribute(PdfName.ENDINDENT, new PdfNumber(paragraph.IndentationRight)); } } PdfName align = null; switch (paragraph.Alignment) { case Element.ALIGN_LEFT: align = PdfName.START; break; case Element.ALIGN_CENTER: align = PdfName.CENTER; break; case Element.ALIGN_RIGHT: align = PdfName.END; break; case Element.ALIGN_JUSTIFIED: align = PdfName.JUSTIFY; break; } obj = parent.GetAttribute(PdfName.TEXTALIGN); if (obj is PdfName) { PdfName textAlign = ((PdfName)obj); if (align != null && !textAlign.Equals(align)) { this.SetAttribute(PdfName.TEXTALIGN, align); } } else { if (align != null && !PdfName.START.Equals(align)) { this.SetAttribute(PdfName.TEXTALIGN, align); } } } }