public static Table CreateMergedDownTable(string[] strArrayHeader, string[] strArrayHeaderSize, string[][] strMatrixContent, int[][] matrixMergeDown, string fontName, Color fontColor, Color shadeColor, Unit fontSize, double borderSize, double edgeSize, bool isHeaderBold, string imageWidth) { Table table = new Table(); table.Borders.Width = borderSize; for (int i = 0; i < strArrayHeader.GetLength(0); i++) { table.AddColumn(strArrayHeaderSize[i]); } Row row = table.AddRow(); row.Format.Alignment = ParagraphAlignment.Center; row.Format.Font.Bold = isHeaderBold; row.Format.Font.Name = fontName; row.Format.Font.Color = fontColor; row.Format.Font.Size = fontSize; row.Shading.Color = shadeColor; row.VerticalAlignment = VerticalAlignment.Center; Cell cell; for (int i = 0; i < strArrayHeader.GetLength(0); i++) { cell = row.Cells[i]; cell.AddParagraph(strArrayHeader[i]); } for (int i = 0; i < strMatrixContent.GetLength(0); i++) { row = table.AddRow(); row.Format.Alignment = ParagraphAlignment.Left; row.Format.Font.Bold = false; row.Format.Font.Name = fontName; row.Format.Font.Color = fontColor; row.Format.Font.Size = fontSize; string[] strArrayContent = strMatrixContent[i]; for (int j = 0; j < strArrayContent.GetLength(0); j++) { cell = row.Cells[j]; cell.MergeDown = matrixMergeDown[i][j]; if (strArrayContent[j].Contains(".jpg") || strArrayContent[j].Contains(".png")) { Image image = new Image(strArrayContent[j]); image.Width = imageWidth; cell.Add(image); } else { cell.AddParagraph(strArrayContent[j]); } } } table.SetEdge(0, 0, strArrayHeader.GetLength(0), strMatrixContent.GetLength(0) + 1, Edge.Box, BorderStyle.Single, edgeSize, Colors.Black); return table; }
public static Column AddColumn(this Table table, ParagraphAlignment align, Unit unit = new Unit()) { Column column = table.AddColumn(); column.Width = unit; column.Format.Alignment = align; return column; }
private void SetMargin(HtmlNode node, ExCSS.Stylesheet sheet, out Unit margin, string cssMargin) { var @class = node.Attributes["class"]; margin = Unit.FromCentimeter(0.2); if (@class != null) { var css = (from t in sheet.RuleSets where t.Selectors.Any(x => x.SimpleSelectors.Any(z => z.Class == @class.Value)) select t).FirstOrDefault(); if (css == null) { } if (css != null) { var mar = (from top in css.Declarations where top.Name == cssMargin select top).FirstOrDefault(); if (mar != null) { var tm = mar.Expression.Terms.Where(x => x.Value != string.Empty).FirstOrDefault(); if (tm != null) { margin = Unit.FromCentimeter(double.Parse(tm.Value)); } } } } }
/// <summary> /// Renders <paramref name="control"/> to the <paramref name="document"/>. /// </summary> /// <param name="document">The PDF document.</param> /// <param name="control">The control to be rendered.</param> /// <param name="level">The depth down the control tree being rendered (affects indenting).</param> /// <param name="controlList">The complete control list.</param> protected override void DoRender(Document document, Control control, int level, ControlList controlList) { TextFrame frameTemplate = this.CreateCharacterInputBox(document.Styles[StyleNames.Normal]); Paragraph paragraphTemplate = new Paragraph { Style = StyleNames.Normal, Format = { Alignment = ParagraphAlignment.Justify, Font = { Color = Colors.LightGray } } }; Unit spacer = new Unit((frameTemplate.Width.Point - paragraphTemplate.Format.Font.Size.Point) / 4, UnitType.Point); paragraphTemplate.Format.SpaceBefore = spacer; paragraphTemplate.Format.LeftIndent = spacer; frameTemplate.MarginBottom = frameTemplate.Height; for (int i = 0; i < 8; i++) { TextFrame frame = frameTemplate.Clone(); frame.WrapFormat.DistanceLeft = (frame.Width * i) + (new Unit(PdfConstants.IndentMultiplier * level, UnitType.Millimeter)); string watermark = i < 2 ? "D" : i < 4 ? "M" : "Y"; Paragraph paragraph = paragraphTemplate.Clone(); paragraph.AddText(watermark); frame.Add(paragraph); document.LastSection.Add(frame); } TextFrame clearFrame = frameTemplate.Clone(); clearFrame.WrapFormat.Style = WrapStyle.TopBottom; clearFrame.LineFormat.Width = 0; document.LastSection.Add(clearFrame); }
/// <summary> /// Appends vertical space. /// </summary> /// <param name="document">The <see cref="MigraDoc.DocumentObjectModel.Document"/> to append to.</param> /// <param name="spaceSize">The amount of vertical space to append. Defaults to 10pt.</param> internal static void AppendVerticalSpace(this Document document, Unit spaceSize) { Paragraph paragraph = document.LastSection.AddParagraph(); paragraph.Format.LineSpacingRule = LineSpacingRule.Exactly; paragraph.Format.LineSpacing = Unit.FromMillimeter(0.0); paragraph.Format.SpaceAfter = spaceSize; }
/// <summary> /// Renders <paramref name="control"/> to the <paramref name="document"/>. /// </summary> /// <param name="document">The PDF document.</param> /// <param name="control">The control to be rendered.</param> /// <param name="level">The depth down the control tree being rendered (affects indenting).</param> /// <param name="controlList">The complete control list.</param> protected override void DoRender(Document document, Control control, int level, ControlList controlList) { var indent = new Unit(PdfConstants.IndentMultiplier * level, UnitType.Millimeter); Shape shape = this.AddDefaultShape(document); shape.Width = shape.Width - indent; shape.Left = indent; shape.Height = shape.Height * 4; }
/// <summary> /// Appends a new <see cref="MigraDoc.DocumentObjectModel.Tables.Row"/> to <paramref name="table"/>. /// The new row will contain a single cell spanning the entire table. /// </summary> /// <param name="table">The <see cref="MigraDoc.DocumentObjectModel.Tables.Table"/> to append to.</param> /// <param name="height">The cell height.</param> /// <param name="header">The cell value.</param> /// <param name="style">The cell style name.</param> /// <param name="borderColor">The border colour.</param> /// <returns>The appended row.</returns> internal static Row AppendHeaderRow(this Table table, Unit height, string header, string style, Color borderColor) { Row row = table.AppendRow(height, header); row.Style = style; row.Shading = new Shading { Color = borderColor }; row.Cells[0].MergeRight = table.Columns.Count - 1; return row; }
/// <summary> /// Gets a TabStop by its position. /// Note that also Removed TabStops are taken into account. /// </summary> public TabStop GetTabStopAt(Unit position) { int count = Count; for (int index = 0; index < count; index++) { TabStop tabStop = (TabStop)this[index]; if (Math.Abs(tabStop.Position.Point - position.Point) < TabStopPrecision) return tabStop; } return null; }
/// <summary> /// Appends a new <see cref="MigraDoc.DocumentObjectModel.Tables.Row"/> to <paramref name="table"/>. /// The new row will contain one cell for each item in <paramref name="values"/>. /// </summary> /// <param name="table">The <see cref="MigraDoc.DocumentObjectModel.Tables.Table"/> to append to.</param> /// <param name="height">The cell height.</param> /// <param name="values">The cell values.</param> /// <returns>The appended row.</returns> internal static Row AppendRow(this Table table, Unit height, params string[] values) { Row row = table.AddRow(); row.BottomPadding = 2; row.TopPadding = 2; row.Height = height; for (int i = 0; i < values.Length; i++) { row.Cells[i].AddParagraph(values[i] ?? string.Empty); } return row; }
/// <summary> /// Adds a TabStop object to the collection and sets its leader. /// </summary> public TabStop AddTabStop(Unit position, TabLeader leader) { TabStop tab = AddTabStop(position); tab.Leader = leader; return tab; }
/// <summary> /// Adds a TabStop object to the collection and sets its alignment and leader. /// </summary> public TabStop AddTabStop(Unit position, TabAlignment alignment, TabLeader leader) { TabStop tab = AddTabStop(position); tab.Alignment = alignment; tab.Leader = leader; return tab; }
/// <summary> /// Adds a TabStop object at the specified position to the collection. If a TabStop with the /// same position already exists, it is replaced by the new TabStop. /// </summary> public TabStop AddTabStop(Unit position) { if (TabStopExists(position)) return GetTabStopAt(position); TabStop tab = new TabStop(position); return AddTabStop(tab); }
/// <summary> /// Adds a TabStop object to the collection marked to remove the tab stop at /// the given position. /// </summary> public void RemoveTabStop(Unit position) { this.TabStops.RemoveTabStop(position); }
/// <summary> /// Translates a value named 'valueName' to a Rtf Control word that specifies a unit, enum, bool, int or color. /// </summary> protected void Translate(string valueName, string rtfCtrl, RtfUnit unit, Unit val, bool withStar) { Translate(valueName, rtfCtrl, unit, ToRtfUnit(val, RtfUnit.Twips).ToString(CultureInfo.InvariantCulture), withStar); }
/// <summary> /// Initializes a new instance of the TabStop class with the specified position. /// </summary> public TabStop(Unit position) : this() { this.position = position; }
/// <summary> /// Gets the page's size and height for the given PageFormat. /// </summary> public static void GetPageSize(PageFormat pageFormat, out Unit pageWidth, out Unit pageHeight) { //Sizes in mm: pageWidth = 0; pageHeight = 0; int A0Height = 1189; int A0Width = 841; int height = 0; int width = 0; switch (pageFormat) { case PageFormat.A0: height = A0Height; width = A0Width; break; case PageFormat.A1: height = A0Width; width = A0Height / 2; break; case PageFormat.A2: height = A0Height / 2; width = A0Width / 2; break; case PageFormat.A3: height = A0Width / 2; width = A0Height / 4; break; case PageFormat.A4: height = A0Height / 4; width = A0Width / 4; break; case PageFormat.A5: height = A0Width / 4; width = A0Height / 8; break; case PageFormat.A6: height = A0Height / 8; width = A0Width / 8; break; case PageFormat.B5: height = 257; width = 182; break; case PageFormat.Letter: pageWidth = Unit.FromPoint(612); pageHeight = Unit.FromPoint(792); break; case PageFormat.Legal: pageWidth = Unit.FromPoint(612); pageHeight = Unit.FromPoint(1008); break; case PageFormat.Ledger: pageWidth = Unit.FromPoint(1224); pageHeight = Unit.FromPoint(792); break; case PageFormat.P11x17: pageWidth = Unit.FromPoint(792); pageHeight = Unit.FromPoint(1224); break; } if (height > 0) pageHeight = Unit.FromMillimeter(height); if (width > 0) pageWidth = Unit.FromMillimeter(width); }
/// <summary> /// Adds a TabStop object to the collection and sets its alignment. /// </summary> public TabStop AddTabStop(Unit position, TabAlignment alignment) { TabStop tab = AddTabStop(position); tab.Alignment = alignment; return tab; }
/// <summary> /// Initializes a new instance of the Font class with the specified name and size. /// </summary> public Font(string name, Unit size) { this.name.Value = name; this.size.Value = size; }
private static bool TooWide(string word, Unit width, TextMeasurement tm) { float f = tm.MeasureString(word, UnitType.Point).Width; return f > width.Point; }
private static string MakeFit(string word, Unit width, TextMeasurement tm) { var adjustedWord = new StringBuilder(); var current = string.Empty; foreach (char c in word) { if (TooWide(current + c, width, tm)) { adjustedWord.Append(current); adjustedWord.Append(Chars.CR); current = c.ToString(); } else { current += c; } } adjustedWord.Append(current); return adjustedWord.ToString(); }
/// <summary> /// Adds a TabStop object to the collection marked to remove the tab stop at /// the given position. /// </summary> public void RemoveTabStop(Unit position) { TabStop tab = AddTabStop(position); tab.AddTab = false; }
/// <summary> /// Aligns the given top and bottom position so that ShapePosition.Top results in top position = 0. /// </summary> private void AlignVertically(ShapePosition shpPos, Unit distanceTopBottom, out Unit topValue, out Unit bottomValue) { double height = GetShapeHeight().Point; topValue = 0; bottomValue = height; Unit topWrap = (Unit)GetValueOrDefault("WrapFormat.DistanceTop", (Unit)0); Unit bottomWrap = (Unit)GetValueOrDefault("WrapFormat.DistanceBottom", (Unit)0); switch (shpPos) { case ShapePosition.Bottom: topValue = distanceTopBottom - height - bottomWrap; bottomValue = distanceTopBottom - bottomWrap; break; case ShapePosition.Center: { Unit centerPos = distanceTopBottom / 2.0; topValue = centerPos - height / 2.0; bottomValue = centerPos + height / 2.0; } break; case ShapePosition.Top: topValue = topWrap; bottomValue = topWrap + height; break; } }
/// <summary> /// Returns whether a TabStop exists at the given position. /// Note that also Removed TabStops are taken into account /// </summary> public bool TabStopExists(Unit position) { return GetTabStopAt(position) != null; }
/// <summary> /// Adds a TabStop object to the collection. /// </summary> public TabStop AddTabStop(Unit position) { return this.TabStops.AddTabStop(position); }
/// <summary> /// Aligns the given left and right position so that ShapePosition.Left results in left position = 0. /// </summary> private void AlignHorizontally(ShapePosition shpPos, Unit distanceLeftRight, out Unit leftValue, out Unit rightValue) { double width = GetShapeWidth().Point; leftValue = 0; rightValue = width; Unit leftWrap = (Unit)GetValueOrDefault("WrapFormat.DistanceLeft", (Unit)0); Unit rightWrap = (Unit)GetValueOrDefault("WrapFormat.DistanceRight", (Unit)0); switch (shpPos) { case ShapePosition.Right: //Positioning the shape Outside seems impossible=>Do the best that's possible. case ShapePosition.Outside: leftValue = distanceLeftRight.Point - width - rightWrap; rightValue = distanceLeftRight - rightWrap; break; case ShapePosition.Center: { double centerPos = distanceLeftRight.Point / 2; leftValue = centerPos - width / 2.0; rightValue = centerPos + width / 2.0; } break; case ShapePosition.Left: //Positioning the shape inside seems impossible=>Do the best that's possible. case ShapePosition.Inside: leftValue = leftWrap; rightValue = leftWrap + width; break; } }
/// <summary> /// Adds a TabStop object to the collection and sets its alignment and leader. /// </summary> public TabStop AddTabStop(Unit position, TabAlignment alignment, TabLeader leader) { return this.TabStops.AddTabStop(position, alignment, leader); }
private void CalculateImageDimensions() { try { this.imageFile = File.OpenRead(this.filePath); //System.Drawing.Bitmap bip2 = new System.Drawing.Bitmap(imageFile); XImage bip = XImage.FromFile(this.filePath); float horzResolution; float vertResolution; string ext = Path.GetExtension(this.filePath).ToLower(); float origHorzRes = (float)bip.HorizontalResolution; float origVertRes = (float)bip.VerticalResolution; this.originalHeight = bip.PixelHeight * 72 / origVertRes; this.originalWidth = bip.PixelWidth * 72 / origHorzRes; if (this.image.IsNull("Resolution")) { horzResolution = (ext == ".gif") ? 72 : (float)bip.HorizontalResolution; vertResolution = (ext == ".gif") ? 72 : (float)bip.VerticalResolution; } else { horzResolution = (float)GetValueAsIntended("Resolution"); vertResolution = horzResolution; } Unit origHeight = bip.Size.Height * 72 / vertResolution; Unit origWidth = bip.Size.Width * 72 / horzResolution; this.imageHeight = origHeight; this.imageWidth = origWidth; bool scaleWidthIsNull = this.image.IsNull("ScaleWidth"); bool scaleHeightIsNull = this.image.IsNull("ScaleHeight"); float sclHeight = scaleHeightIsNull ? 1 : (float)GetValueAsIntended("ScaleHeight"); this.scaleHeight = sclHeight; float sclWidth = scaleWidthIsNull ? 1 : (float)GetValueAsIntended("ScaleWidth"); this.scaleWidth = sclWidth; bool doLockAspectRatio = this.image.IsNull("LockAspectRatio") || this.image.LockAspectRatio; if (doLockAspectRatio && (scaleHeightIsNull || scaleWidthIsNull)) { if (!this.image.IsNull("Width") && this.image.IsNull("Height")) { imageWidth = this.image.Width; imageHeight = origHeight * imageWidth / origWidth; } else if (!this.image.IsNull("Height") && this.image.IsNull("Width")) { imageHeight = this.image.Height; imageWidth = origWidth * imageHeight / origHeight; } else if (!this.image.IsNull("Height") && !this.image.IsNull("Width")) { imageWidth = this.image.Width; imageHeight = this.image.Height; } if (scaleWidthIsNull && !scaleHeightIsNull) scaleWidth = scaleHeight; else if (scaleHeightIsNull && !scaleWidthIsNull) scaleHeight = scaleWidth; } else { if (!this.image.IsNull("Width")) imageWidth = this.image.Width; if (!this.image.IsNull("Height")) imageHeight = this.image.Height; } return; } catch (FileNotFoundException) { Trace.WriteLine(Messages.ImageNotFound(this.image.Name), "warning"); } catch (Exception exc) { Trace.WriteLine(Messages.ImageNotReadable(this.image.Name, exc.Message), "warning"); } //Setting defaults in case an error occured. this.imageFile = null; this.imageHeight = (Unit)GetValueOrDefault("Height", Unit.FromInch(1)); this.imageWidth = (Unit)GetValueOrDefault("Width", Unit.FromInch(1)); this.scaleHeight = (double)GetValueOrDefault("ScaleHeight", 1.0); this.scaleWidth = (double)GetValueOrDefault("ScaleWidth", 1.0); }
/// <summary> /// Adds a TabStop object to the collection and sets its leader. /// </summary> public TabStop AddTabStop(Unit position, TabLeader leader) { return this.TabStops.AddTabStop(position, leader); }
/// <summary> /// Creates a new table with the given number of column + column width /// </summary> /// <param name="columnNames"></param> /// <param name="columnWidth"></param> public void AddTable(string[] columnNames, int[] columnWidth) { table = section.AddTable(); table.Style = "Table"; table.Borders.Color = Colors.Black; table.Borders.Width = 0.25; table.Borders.Left.Width = 0.5; table.Borders.Right.Width = 0.5; table.Rows.LeftIndent = 0; Unit totalWidth = 0; for (int i = 0; i <= columnWidth.Length - 2; i++) { Unit colWidth = new Unit(columnWidth[i], UnitType.Millimeter); table.AddColumn(colWidth); totalWidth += colWidth; } table.AddColumn(TABLE_WIDTH - totalWidth); AddTableHeader(columnNames); }
/// <summary> /// Adds a TabStop object to the collection and sets its alignment. /// </summary> public TabStop AddTabStop(Unit position, TabAlignment alignment) { return this.TabStops.AddTabStop(position, alignment); }