public virtual object Clone() { var tm = new TextMeasurement(); tm.CopyFrom(this); return(tm); }
private bool TooWide(string word, Unit width) { var tm = new TextMeasurement(_document.Styles["Table"].Font.Clone()); float f = tm.MeasureString(word, UnitType.Point).Width; return(f > width.Point); }
private void Remeasure() { if (_text != null) { _measurement = new TextMeasurement (_text, _textWrapping == TextWrapping.Wrap ? ActualWidth : int.MaxValue); } }
static TextMeasurement charRectangle(this IntPtr hdc, string text, Rectangle container, string wholeText = null, Point?point = null, bool adjustByPoint = false, DrawTextFlags?flags = null) { if (string.IsNullOrEmpty(text)) { return(TextMeasurement.Default); } TextMeasurement measurement = new TextMeasurement(); Rectangle textBound; wholeText = (wholeText ?? text); var location = container.Location; var measureWholeText = point == null; measurement.UserPoint = point ?? Point.Empty; textBound = hdc.textBound(wholeText, container, flags: flags); var rect = textBound; var p = point ?? new Point(container.Right, container.Y); if (!measureWholeText) { if (p.X > textBound.Right) { p.X = textBound.Right; } else if (p.X < textBound.Left) { p.X = textBound.X; } } var charIndex = 0; var result = hdc.charRectangle(text, ref p, rect, flags, measureWholeText); charIndex = Math.Max(0, result.Item2); var rectangles = result.Item1; measurement.Bounds = rectangles[0]; measurement.TextBounds = (measureWholeText) ? rectangles[1] : textBound; rectangles[1] = measurement.TextBounds; if (!measureWholeText && adjustByPoint && charIndex > 0) { float middle = (float)measurement.Bounds.Left + measurement.Bounds.Width / 2; if (p.X > middle - 1) { Rectangle r; Dimension r1 = measurement.TextBounds; var newresult = hdc.charBound(text, charIndex + 2, ref r1, (int)flags.getMeasureFlag(false), out r); if (!newresult.Equals(measurement.Bounds) && newresult.X > measurement.Bounds.X) { charIndex++; measurement.Bounds = newresult; } } } if (measurement.Bounds.Size.Width <= 0) { measurement.Bounds = new Rectangle(measurement.Bounds.Location, new Size(2, 2)); } measurement.CharIndex = charIndex; measurement.Char = '\0'; measurement.Char = text[Math.Min(charIndex, text.Length - 1)]; return(measurement); }
internal static double[] CalculateColumnWidths( IPdfStyling pdfStyling, Document document, IEnumerable <IPdfTableHeaderCell> tHead, IEnumerable <PdfTableRow> tBody, bool fitToDocument, Font font = null) { // Only calculate when not all widths are set! if (tHead.Count() == tHead.Count(o => o.WidthInCentimeter > 0)) { return(tHead.Select(o => Unit.FromCentimeter(o.WidthInCentimeter).Point).ToArray()); } if (font == null) { font = document.Styles[StyleNames.Normal].Font.Clone(); } // Start calculating the widths TextMeasurement tm = new TextMeasurement(font); double maxWidth = pdfStyling.MaxWidthInPoints; double maxWidthPerColumn = maxWidth / tHead.Count(); //TODO: Do we need that? It will even work if it's commented out... if (tHead.Any(o => o.WidthInCentimeter > 0)) { double widths = tHead.Sum(o => Unit.FromCentimeter(o.WidthInCentimeter).Point); maxWidthPerColumn = (maxWidth - widths) / tHead.Count(); } List <PdfTableColumn> columns = new List <PdfTableColumn>(); PdfTableColumn column; IPdfTableCell cell; double width, colWidth; for (int i = 0; i < tHead.Count(); i++) { column = new PdfTableColumn(); if (tHead.ElementAt(i).WidthInCentimeter > 0) { column.Width = Unit.FromCentimeter(tHead.ElementAt(i).WidthInCentimeter).Point; } else { // Measure header text to get the width of the column // http://forum.pdfsharp.net/viewtopic.php?f=2&t=747 width = tHead.ElementAt(i).Text == null ? 0 : tm.MeasureString(tHead.ElementAt(i).Text.Trim()).Width; // but we also have to check the body text width foreach (PdfTableRow row in tBody) { cell = i < row.Cells.Count ? row.Cells[i] : null; if (cell == null) { continue; } //TODO: This is a QUICK HACK! We should calculate the real width of the colspan! if (cell.Colspan > 1) { continue; } colWidth = cell.Text == null ? 0 : tm.MeasureString(cell.Text.Trim()).Width; if (colWidth > width) { width = colWidth; } } if (width > maxWidthPerColumn) { // Add the maximum width per column column.Width = maxWidthPerColumn; column.Fit = false; // But if we have more space left we will add it later... } else { // We add 10 to be sure we have enough column.Width = width + 10; } } columns.Add(column); } double currentWidth = columns.Sum(o => o.Width); if (fitToDocument && currentWidth < maxWidth) { double available = 0; // If there is a column that doesn't fit - make it fit (first priority) // otherwise we will fit the full table (second priority) if (columns.Any(o => o.Fit == false)) { available = (maxWidth - currentWidth) / columns.Count(o => o.Fit == false); foreach (PdfTableColumn col in columns.Where(o => o.Fit == false)) { col.Width += available; } } else { available = (maxWidth - currentWidth) / columns.Count(); foreach (PdfTableColumn col in columns) { col.Width += available; } } } return(columns.Select(o => o.Width).ToArray()); }
private static void formPI_DoWork_GenerateMoviesCatalog(FrmProgressIndicator sender, DoWorkEventArgs e) { var catalogGenDet = (KeyValuePair <Document, PdfGenParams>)e.Argument; var document = catalogGenDet.Key; var pdfGenParams = catalogGenDet.Value; var movies = Desene.DAL.GetMoviesForPDF(pdfGenParams); //var style = document.Styles["Normal"]; //style.Font.Name = "Arial Narrow"; //style.Font.Size = 10; var section = document.AddSection(); section.PageSetup.PageFormat = PageFormat.A4; section.PageSetup.MirrorMargins = true; section.PageSetup.TopMargin = Unit.FromCentimeter(0.5); section.PageSetup.BottomMargin = Unit.FromCentimeter(0.5); section.PageSetup.LeftMargin = Unit.FromCentimeter(2); section.PageSetup.RightMargin = Unit.FromCentimeter(1); var table = section.AddTable(); var sectionWidth = (int)document.DefaultPageSetup.PageWidth - (int)section.PageSetup.LeftMargin - (int)section.PageSetup.RightMargin; var columnWidth = sectionWidth / 4; var column = table.AddColumn(); column.Width = columnWidth; var column2 = table.AddColumn(); column2.Width = columnWidth; var column3 = table.AddColumn(); column3.Width = columnWidth; var column4 = table.AddColumn(); column4.Width = columnWidth; Row row = null; var indexPos = 0; for (var i = 0; i < movies.Count; i += 4) { if (sender.CancellationPending) { e.Cancel = true; return; } row = table.AddRow(); for (var j = 0; j < 4; j++) { var k = i + j; if (k >= movies.Count) { break; } var movieObj = movies[k]; indexPos++; sender.SetProgress(indexPos, movieObj.FN); var imgOgj = GraphicsHelpers.CreatePosterThumbnailForPDF(150, 232, movieObj.Cover, movieObj.R, movieObj.A, pdfGenParams.PDFGenType == PDFGenType.All ? movieObj.T == "Craciun" ? Properties.Resources.Christmas_Tree_icon : movieObj.T == "Helloween" ? Properties.Resources.Pumpkin_icon : null : null); row.Cells[j].Format.Alignment = ParagraphAlignment.Center; row.Cells[j].VerticalAlignment = VerticalAlignment.Center; row.Cells[j].AddParagraph().AddImage(MigraDocFilenameFromByteArray(imgOgj)); } row = table.AddRow(); for (var j = 0; j < 4; j++) { var k = i + j; if (k >= movies.Count) { break; } var movieObj = movies[k]; var dataTable = new Table(); var columnD1 = dataTable.AddColumn(); columnD1.Width = columnWidth; var rowD1 = dataTable.AddRow(); rowD1.Cells[0].Format.Alignment = ParagraphAlignment.Center; rowD1.Cells[0].Format.Font.Name = "Arial Narrow"; rowD1.Cells[0].Format.Font.Size = 10; var tm = new TextMeasurement(rowD1.Cells[0].Format.Font); //var strWidth = tm.MeasureString(movieObj.FN).Width; var movieTitle = movieObj.FN; var lineCount = tm.GetSplittedLineCount(movieTitle, columnWidth, null); if (lineCount > 2) { movieTitle = tm.GetStringWithEllipsis(movieObj.FN, columnWidth, 2); } rowD1.Cells[0].AddParagraph(movieTitle); row.Cells[j].Elements.Add(dataTable); } } //e.Result = document; }