示例#1
0
        private static string WideWordsAdjust(Cell cell, string text, TextMeasurement tm, string formatColumn = null)
        {
            if (string.IsNullOrWhiteSpace(text))
            {
                return("");
            }

            Column column      = cell.Column;
            Unit   columnWidth = column.Width - (column.Table.Borders.Width * 2);

            var tooWideWords = text.Split(" ".ToCharArray()).Distinct().Where(s => TooWide(s, columnWidth, tm));
            var adjusted     = new StringBuilder(text);

            foreach (string word in tooWideWords)
            {
                var adjustedWord = new StringBuilder();
                var current      = string.Empty;
                foreach (char c in word)
                {
                    if (TooWide(current + c, columnWidth, tm))
                    {
                        adjustedWord.Append(current);
                        adjustedWord.Append(Chars.CR);//TODO: ERROR!! here there is Bug in case we have a very wide word
                        current = c.ToString();
                    }
                    else
                    {
                        current += c;
                    }
                }
                adjusted.Replace(word, adjustedWord.Append(current).ToString());
            }

            return(adjusted.ToString());
        }
示例#2
0
        private static void AutoFitColumn(Cell cell, string cols, TextMeasurement tm)
        {
            Column column = cell.Column;

            if (column.Width < Unit.FromMillimeter(cols.Length * 4))
            {
                column.Width = Unit.FromMillimeter(cols.Length * 4);
            }
        }
示例#3
0
        private static bool TooWide(string word, Unit width, TextMeasurement tm)
        {
            float f = tm.MeasureString(word, UnitType.Millimeter).Width;

            return(f > width.Millimeter);
        }
示例#4
0
        //PM> Install-Package PdfSharp
        //license: http://www.pdfsharp.net/PDFsharp_License.ashx
        public static string ExportListToPDF(ExportOptions opt, List <ExportRecordDTO> result, int totalRows,
                                             Func <Document, Table, object> _pdfOvveride)
        {
            var firstResult = result.First();

            firstResult.MarkVisibleColumns(opt);

            var columns        = result.First().Columns;
            var visibleColumns = columns.Where(c => c.IsVisible);

            var document = InitializePDFDocument(visibleColumns.Count(), out Unit availableWidth, out Unit availableColumnWidth, opt.PortraitOrientation);

            //create section wrapper of table
            Section section = new Section();

            document.Add(section);

            //create table
            Table table = new Table();

            table.AddColumn();//counterColumn

            Cell   cell;
            Column column;


            List <ExportColumnDTO> columnCaptions = new List <ExportColumnDTO>();

            //create the columns based on headers
            foreach (var cols in visibleColumns)
            {
                table.AddColumn();
                columnCaptions.Add(cols);
            }


            //create one row for the headers, the style of headers and the row contents
            var row = table.AddRow();
            GroupStyleColorParse mycolor = new GroupStyleColorParse(opt.HeaderColor);

            row.Shading.Color = Color.FromRgbColor(255, new Color((byte)mycolor.Red, (byte)mycolor.Green, (byte)mycolor.Blue));

            var styleHeader = document.AddStyle("Headers", "Normal");

            styleHeader.Font.Name = "Headers";
            styleHeader.Font.Bold = true;
            styleHeader.Font.Size = 14;
            TextMeasurement tmHeader = new TextMeasurement(document.Styles["Headers"].Font.Clone());


            var currentItemIndex = 0;

            foreach (var col in columnCaptions)
            {
                cell         = row.Cells[currentItemIndex + 1]; //Cells[0] is emtpy, the header for counter column
                column       = cell.Column;
                column.Width = availableColumnWidth;
                Paragraph p       = new Paragraph();
                var       caption = string.IsNullOrWhiteSpace(col.Caption) ? col.ColumnName : col.Caption;
                p.AddFormattedText(WideWordsAdjust(cell, caption, tmHeader), "Headers");
                cell.Add(p);
                currentItemIndex++;
            }


            //Fill with data(default style) the PDf table
            int rowCounter = 0;

            var             style = document.Styles["Normal"];
            TextMeasurement tm    = new TextMeasurement(document.Styles["Normal"].Font.Clone());

            //var doubleValue = 0.0;
            mycolor = new GroupStyleColorParse(opt.OddColor);
            GroupStyleColorParse mycolorEven = new GroupStyleColorParse(opt.EvenColor);


            foreach (var record in result)
            {
                var rowIn = table.AddRow();
                rowCounter++;
                rowIn.Shading.Color = rowCounter % 2 != 0
                    ? Color.FromRgbColor(255, new Color((byte)mycolor.Red, (byte)mycolor.Green, (byte)mycolor.Blue))
                    : Color.FromRgbColor(255, new Color((byte)mycolorEven.Red, (byte)mycolorEven.Green, (byte)mycolorEven.Blue));

                cell = rowIn.Cells[0];
                cell.AddParagraph(rowCounter.ToString());
                AutoFitColumn(cell, rowCounter.ToString(), tm);

                int    colCounter     = 0;
                string validPDFFormat = "";
                foreach (var visibleColumn in record.Columns)
                {
                    if (!visibleColumns.Any(c => c.ColumnName == visibleColumn.ColumnName))
                    {
                        continue;
                    }
                    colCounter += 1;
                    //var recordColumn = record.Columns.FirstOrDefault(c => c.ColumnName == visibleColumn.ColumnName);
                    cell           = rowIn.Cells[colCounter];
                    column         = cell.Column;
                    column.Width   = availableColumnWidth;
                    validPDFFormat = ApplyValueFormat(visibleColumn);
                    cell.AddParagraph(WideWordsAdjust(cell, validPDFFormat?.ToString(), tm));

                    //colCounter++;
                }
            }

            if (opt.NonGroupCount)
            {
                var totalsRow = table.AddRow();
                totalsRow.Cells[0].MergeRight = visibleColumns.Count();
                totalsRow.Cells[0].AddParagraph($"{BaseViewPageBase<object>.GetResourceValue("GlobalResources", "RES_DATALIST_AGGREGATORS_GrandCount")}{totalRows}");
            }

            //Add the table to the section and the document is ready for render
            if (opt.IncludeGridLines)
            {
                table.Borders.Visible = true;
            }

            if (_pdfOvveride == null)
            {
                section.Add(table);
            }
            else
            {
                _pdfOvveride?.Invoke(document, table);
            }

            PdfDocumentRenderer pdfRenderer = new PdfDocumentRenderer(true)
            {
                Document = document
            };

            pdfRenderer.RenderDocument();

            return(CreateFileAndSendDownloadLink(opt.Filename, null, "pdf", pdfRenderer));
        }