Beispiel #1
0
        /// <summary>
        /// Renders the specified PDF styling.
        /// </summary>
        /// <param name="pdfStyling">The PDF styling.</param>
        /// <param name="section">The section.</param>
        public void Render(IPdfStyling pdfStyling, Section section)
        {
            TextFrame frame = section.AddTextFrame();

            frame.Top    = new Unit(_top);
            frame.Left   = new Unit(_left);
            frame.Width  = pdfStyling.MaxWidthInPoints;
            frame.Height = new Unit(_height);
            //frame.RelativeVertical = RelativeVertical.Margin;
            frame.RelativeVertical = _relativeVertical;

            Paragraph paragraph = frame.AddParagraph();

            if (string.IsNullOrWhiteSpace(_title) == false)
            {
                paragraph.AddFormattedText(_title.Trim(), TextFormat.Bold);
                paragraph.AddLineBreak();
            }

            int      count = _addressLines.Count();
            TextLine line;

            for (int i = 0; i < _addressLines.Count(); i++)
            {
                line = _addressLines.ElementAt(i);

                paragraph.AddLineBreak();
                paragraph.AddFormattedText(
                    text: line.Text,
                    textFormat: line.IsBold ? TextFormat.Bold : TextFormat.NotBold);
            }
        }
        /// <summary>
        /// Defines the PDF.
        /// </summary>
        /// <param name="title"></param>
        /// <param name="author"></param>
        /// <param name="subject"></param>
        /// <param name="keywords"></param>
        /// <param name="absolutePathToPdfTemplate"></param>
        /// <param name="pdfStyling"></param>
        public override void Define(
            string title,
            string author,
            string subject,
            string keywords,
            string absolutePathToPdfTemplate = null,
            IPdfStyling pdfStyling           = null)
        {
            base.Define(title, author, subject, keywords, absolutePathToPdfTemplate, pdfStyling);

            base.Elements.Add(new PdfAddress(string.Empty, _companyAddress, 20, 0, 120, RelativeVertical.Page));

            base.Elements.Add(new PdfAddress(_billingAddressTitle, _billingAddress, 0, 300, 80));

            base.Elements.Add(new PdfHeading(_invoiceTitle, 1));

            if (_invoiceDetails != null)
            {
                base.Elements.Add(_invoiceDetails);

                base.Elements.Add(new PdfSpace(10, 10));
            }

            base.Elements.Add(_invoiceSummary);

            if (_details != null)
            {
                base.Elements.Add(new PdfSpace(10, 10));

                base.Elements.Add(new PdfParagraph(paragraphs: _details));
            }
        }
        private void Footer(IPdfStyling pdfStyling, Section section)
        {
            TextFrame footer = section.AddTextFrame();

            footer.RelativeVertical = RelativeVertical.Margin;

            footer.Height = Unit.FromCentimeter(3.5);
            footer.Width  = pdfStyling.MaxWidthInPoints;
            footer.Top    = pdfStyling.MaxHeightInPoints - Unit.FromCentimeter(1);

            Paragraph p = footer.AddParagraph();

            p.AddText("Alle Rezepte auf");
            p.Format.SpaceBefore = 0;
            p.Format.SpaceAfter  = 0;
            p.Format.Font.Color  = pdfStyling.ParagraphSmallColor;
            p.Format.Alignment   = ParagraphAlignment.Center;

            Paragraph website = footer.AddParagraph();

            website.AddText("www.rimuss.ch");
            website.Format.SpaceBefore = 0;
            website.Format.SpaceAfter  = 0;
            website.Format.Font.Color  = Color.FromCmyk(38, 46, 82, 36);
            website.Format.Alignment   = ParagraphAlignment.Center;
        }
Beispiel #4
0
        private void Table(IPdfStyling pdfStyling, MigraDoc.DocumentObjectModel.Tables.Table table, IDictionary <string, string> rows)
        {
            Row       row;
            Font      font;
            Paragraph paragraph;

            foreach (KeyValuePair <string, string> item in rows)
            {
                row = table.AddRow();

                row.Cells[0].Format.LeftIndent = Unit.FromCentimeter(1);

                paragraph = row.Cells[0].AddParagraph();
                paragraph.Format.Font.Size  = 10;
                paragraph.Format.Font.Color = pdfStyling.ParagraphSmallColor;

                font       = paragraph.Format.Font.Clone();
                font.Color = pdfStyling.FontColor;

                paragraph.Format.AddTabStop("3.5cm", TabAlignment.Left, TabLeader.Dots);

                paragraph.AddFormattedText(item.Value, font);
                paragraph.AddTab();

                paragraph = row.Cells[1].AddParagraph();
                paragraph.AddFormattedText(item.Key, font);
            }
        }
Beispiel #5
0
        /*public PdfHeading(
         *      string text,
         *      string style)
         *      : this(text, 0, 10, style, RelativeVertical.Paragraph) { }*/

        /*public PdfHeading(
         *      string text,
         *      int top,
         *      int left,
         *      string style,
         *      RelativeVertical relativeVertical)
         * {
         *      _text = text;
         *      _top = top;
         *      _left = left;
         *      _style = style;
         *      _relativeVertical = relativeVertical;
         * }*/

        /// <summary>
        /// Renders the element.
        /// </summary>
        /// <param name="pdfStyling">The PDF styling.</param>
        /// <param name="section">The section.</param>
        public void Render(IPdfStyling pdfStyling, Section section)
        {
            /*TextFrame frame = section.AddTextFrame();
             *
             * frame.Width = new Unit(pdfStyling.MaxWidthInPoints);
             * frame.Height = new Unit(50); //TODO: Better way
             *
             * if (_relativeVertical == RelativeVertical.Paragraph)
             * {
             *      frame.MarginTop = new Unit(_top);
             *      //frame.MarginBottom = new Unit(_left);
             * }
             * else if (_relativeVertical == RelativeVertical.Page)
             * {
             *      frame.Top = new Unit(_top);
             *      frame.Left = new Unit(_left);
             * }
             *
             * frame.RelativeVertical = _relativeVertical;*/

            Paragraph paragraph = section.AddParagraph();

            paragraph.AddText(_text);
            if (string.IsNullOrWhiteSpace(_style) == false)
            {
                paragraph.SetStyle(_style);
            }
        }
        /// <summary>
        /// Renders the element.
        /// </summary>
        /// <param name="pdfStyling">The PDF styling.</param>
        /// <param name="section">The section.</param>
        public void Render(IPdfStyling pdfStyling, Section section)
        {
            Paragraph paragraph = section.AddParagraph();

            paragraph.Format.SpaceBefore = _spaceBefore;
            paragraph.Format.SpaceAfter  = _spaceAfter;
        }
Beispiel #7
0
        public void Render(IPdfStyling pdfStyling, Section section)
        {
            LeftSide(pdfStyling, section);

            RightSide(pdfStyling, section);

            Footer(pdfStyling, section);
        }
Beispiel #8
0
        /// <summary>
        /// Renders the element.
        /// </summary>
        /// <param name="pdfStyling">The PDF styling.</param>
        /// <param name="section">The section.</param>
        public void Render(IPdfStyling pdfStyling, Section section)
        {
            Paragraph paragraph = section.AddParagraph();

            Hyperlink hyperlink = paragraph.AddHyperlink(this._text, this._hyperlinkType);

            hyperlink.AddText(this._text);
        }
 /// <summary>
 /// Generates a PDF.
 /// </summary>
 /// <param name="title">The document title.</param>
 /// <param name="author">The document author.</param>
 /// <param name="subject">The document subject.</param>
 /// <param name="keywords">The document keywords.</param>
 /// <param name="pdfStyling">The PDF styling.</param>
 public PdfGenerator(
     string title,
     string author,
     string subject,
     string keywords,
     IPdfStyling pdfStyling)
     : this(title, author, subject, keywords, string.Empty, pdfStyling)
 {
 }
Beispiel #10
0
        private void LeftSide(IPdfStyling pdfStyling, Section section)
        {
            Image bottle = section.AddImage(@"D:\DEVELOPMENT\GIT\OPTEN Solutions\tests\Opten.Web.Infrastructure.Pdf.Test\Images\drink_winterbowle.jpg");

            bottle.LockAspectRatio  = true;
            bottle.Height           = pdfStyling.MaxHeightInPoints - (pdfStyling.Margin[0] * 2);
            bottle.Top              = pdfStyling.Margin[0] - Unit.FromCentimeter(0.3); //TODO: Why 0.3?
            bottle.Left             = pdfStyling.Margin[0] * 2;
            bottle.RelativeVertical = RelativeVertical.Margin;
        }
Beispiel #11
0
        /// <summary>
        /// Renders the element.
        /// </summary>
        /// <param name="pdfStyling">The PDF styling.</param>
        /// <param name="section">The section.</param>
        public void Render(IPdfStyling pdfStyling, Section section)
        {
            TextFrame hr = section.AddTextFrame();

            hr.Width            = new Unit(pdfStyling.MaxWidthInPoints);
            hr.Height           = new Unit(10);   //TODO: Custom height?
            hr.RelativeVertical = RelativeVertical.Line;

            Paragraph ruler = hr.AddParagraph();

            ruler.SetStyle(PdfStyleNames.Misc.HorizontalRule);
        }
Beispiel #12
0
        private void HorizontalRule(IPdfStyling pdfStyling, Cell cell)
        {
            TextFrame hr = cell.AddTextFrame();

            hr.Width  = _leftSideWidth;
            hr.Height = Unit.FromCentimeter(1);
            hr.Top    = Unit.FromCentimeter(1);

            Paragraph ruler = hr.AddParagraph();

            ruler.Format.Borders.Top.Color = pdfStyling.HorizontalRuleColor;
            ruler.Format.Borders.Top.Width = 0.5;
        }
        private void HorizontalRule(IPdfStyling pdfStyling, Section section)
        {
            TextFrame hr = section.AddTextFrame();

            hr.Width  = pdfStyling.MaxWidthInPoints;
            hr.Height = Unit.FromCentimeter(1);
            hr.Top    = Unit.FromCentimeter(1);

            Paragraph ruler = hr.AddParagraph();

            ruler.Format.Borders.Top.Color = pdfStyling.HorizontalRuleColor;
            ruler.Format.Borders.Top.Width = 0.5;
        }
        /// <summary>
        /// Renders the element.
        /// </summary>
        /// <param name="pdfStyling">The PDF styling.</param>
        /// <param name="section">The section.</param>
        public void Render(IPdfStyling pdfStyling, Section section)
        {
            TextFrame frame = section.Headers.Primary.AddTextFrame();

            frame.Width     = new Unit(pdfStyling.MaxWidthInPoints);
            frame.Height    = new Unit(pdfStyling.HeaderTitleFontSize * 2);          //TODO: Why?
            frame.MarginTop = new Unit(pdfStyling.HeaderTitleFontSize + 3);          //TODO: Why?

            Paragraph paragraph = frame.AddParagraph();

            paragraph.AddFormattedText(_title, _textFormat);
            paragraph.Format.Alignment = _alignment;
            paragraph.SetStyle(PdfStyleNames.Title.Header);
        }
Beispiel #15
0
        /// <summary>
        /// Renders the element.
        /// </summary>
        /// <param name="pdfStyling">The PDF styling.</param>
        /// <param name="section">The section.</param>
        public void Render(IPdfStyling pdfStyling, Section section)
        {
            Image image = new Image(this._fileName);             // We recommend to scale down image with ImageProcessor or ImageResizer!

            //double width = this._width > pdfStyling.MaxWidthInPoints ? pdfStyling.MaxWidthInPoints : this._width;
            image.Width = this._width;
            //image.Height = Unit.FromCentimeter(heightInCentimeter);
            image.LockAspectRatio = true;

            TextFrame frame = section.AddTextFrame();

            frame.Add(image);
            frame.Height = this._height;
            frame.Width  = this._width;
        }
        /// <summary>
        /// Defines the PDF.
        /// </summary>
        public virtual void Define(
            string title,
            string author,
            string subject,
            string keywords,
            string absolutePathToPdfTemplate = null,
            IPdfStyling pdfStyling           = null)
        {
            _title    = title;
            _author   = author;
            _subject  = subject;
            _keywords = keywords;
            _absolutePathToPdfTemplate = absolutePathToPdfTemplate;
            _pdfStyling = pdfStyling == null ? new PdfStyling() : pdfStyling;

            _isDefined = true;
        }
Beispiel #17
0
        private void AddTextWithSubtext(IPdfStyling pdfStyling, PdfTableCellWithSubtext pdfCell, Cell cell)
        {
            double space = (pdfStyling as PdfStyling)._paragraphSpaceAfter / 2;             //TODO: better way?

            if (pdfCell.SubtextAlignment == TextAlignment.Top)
            {
                cell.Add(pdfCell.Subtext.AsParagraphSmall());
            }

            Paragraph paragraph = pdfCell.Text.AsParagraph(pdfCell.IsBold);

            if (pdfCell.SubtextAlignment == TextAlignment.Top && space > 0)
            {
                // Half of paragraphs padding
                paragraph.Format.SpaceBefore = -space;
            }

            if (pdfCell.SubtextAlignment == TextAlignment.Left)
            {
                paragraph = new Paragraph();                 // Empty the paragraph
                paragraph.AddFormattedText(pdfCell.Subtext.Trim(), PdfStyleNames.Paragraph.Small);
                paragraph.AddText(" ");
                paragraph.AddText(pdfCell.Text.Trim());
            }
            else if (pdfCell.SubtextAlignment == TextAlignment.Right)
            {
                paragraph.AddText(" ");
                paragraph.AddFormattedText(pdfCell.Subtext.Trim(), PdfStyleNames.Paragraph.Small);
            }

            cell.Add(paragraph);

            if (pdfCell.SubtextAlignment == TextAlignment.Bottom)
            {
                paragraph = pdfCell.Subtext.AsParagraphSmall();

                if (space > 0)
                {
                    // Half of paragraphs padding
                    paragraph.Format.SpaceBefore = -space;
                }

                cell.Add(paragraph);
            }
        }
        /// <summary>
        /// Renders the pages.
        /// </summary>
        /// <param name="pdfStyling">The PDF styling.</param>
        /// <param name="document">The document.</param>
        public void Render(IPdfStyling pdfStyling, PdfDocument document)
        {
            try
            {
                PdfDocument pdf = PdfReader.Open(_filePath, PdfDocumentOpenMode.Import);

                for (int i = 0; i < pdf.PageCount; i++)
                {
                    PdfPage page = pdf.Pages[i];

                    document.AddPage(page);
                }
            }
            catch
            {
                //LOG?!
            }
        }
Beispiel #19
0
        /// <summary>
        /// Renders the element.
        /// </summary>
        /// <param name="pdfStyling">The PDF styling.</param>
        /// <param name="section">The section.</param>
        public void Render(IPdfStyling pdfStyling, Section section)
        {
            section.PageSetup.StartingNumber = 1;

            TextFrame frame = section.Footers.Primary.AddTextFrame();

            frame.Width     = new Unit(pdfStyling.MaxWidthInPoints);
            frame.Height    = new Unit(10);                   //TODO: Why?
            frame.MarginTop = pdfStyling.PageNumberMarginTop; //TODO: Why?
            //frame.MarginTop = new Unit(69.5);

            Paragraph paragraph = frame.AddParagraph();

            paragraph.Format.Alignment = pdfStyling.PageNumberAlignment;
            //paragraph.AddText("Page "); //TODO: Multilingual
            paragraph.AddPageField();
            paragraph.AddText(" / ");
            paragraph.AddNumPagesField();
            paragraph.SetStyle(PdfStyleNames.Paragraph.Small);
        }
Beispiel #20
0
        private void AddTextWithLines(IPdfStyling pdfStyling, IPdfTableCellWithTextLines pdfCell, Cell cell)
        {
            Paragraph paragraph;
            TextLine  textLine;
            double    space = (pdfStyling as PdfStyling)._paragraphSpaceAfter / 2;          //TODO: better way?

            for (int i = 0; i < pdfCell.TextLines.Count(); i++)
            {
                textLine  = pdfCell.TextLines.ElementAt(i);
                paragraph = textLine.Text.AsParagraph(textLine.IsBold);

                if (i > 0 && space > 0)
                {
                    // Half of paragraphs padding
                    paragraph.Format.SpaceBefore = -space;
                }

                cell.Add(paragraph);
            }
        }
Beispiel #21
0
        /// <summary>
        /// Defines the PDF.
        /// </summary>
        /// <param name="title"></param>
        /// <param name="author"></param>
        /// <param name="subject"></param>
        /// <param name="keywords"></param>
        /// <param name="absolutePathToPdfTemplate"></param>
        /// <param name="pdfStyling"></param>
        public override void Define(
            string title,
            string author,
            string subject,
            string keywords,
            string absolutePathToPdfTemplate = null,
            IPdfStyling pdfStyling           = null)
        {
            if (pdfStyling == null)
            {
                pdfStyling = new PdfStyling(
                    orientation: Orientation.Portrait,
                    margin: new Unit[4] {
                    0, 0, 0, 0
                },
                    showPageNumber: false);
            }

            base.Define(title, author, subject, keywords, absolutePathToPdfTemplate, pdfStyling);
        }
Beispiel #22
0
 private void AddText(IPdfStyling pdfStyling, IPdfTableCell pdfCell, Cell cell)
 {
     if (pdfCell is PdfTableCellWithSubtext)
     {
         AddTextWithSubtext(
             pdfStyling: pdfStyling,
             pdfCell: pdfCell as PdfTableCellWithSubtext,
             cell: cell);
     }
     else if (pdfCell is IPdfTableCellWithTextLines)
     {
         AddTextWithLines(
             pdfStyling: pdfStyling,
             pdfCell: pdfCell as IPdfTableCellWithTextLines,
             cell: cell);
     }
     else
     {
         cell.Add(pdfCell.Text.AsParagraph(pdfCell.IsBold));
     }
 }
Beispiel #23
0
        /// <summary>
        /// Defines the PDF.
        /// </summary>
        /// <param name="title"></param>
        /// <param name="author"></param>
        /// <param name="subject"></param>
        /// <param name="keywords"></param>
        /// <param name="absolutePathToPdfTemplate"></param>
        /// <param name="pdfStyling"></param>
        public override void Define(
            string title,
            string author,
            string subject,
            string keywords,
            string absolutePathToPdfTemplate = null,
            IPdfStyling pdfStyling           = null)
        {
            base.Define(title, author, subject, keywords, absolutePathToPdfTemplate, pdfStyling);

            if (string.IsNullOrWhiteSpace(title) == false)
            {
                base.Elements.Add(new PdfHeaderTitle(title, ParagraphAlignment.Center));
            }

            if (title.Equals(subject) == false && string.IsNullOrWhiteSpace(subject) == false)
            {
                base.Elements.Add(new PdfHeading(subject, 6));

                base.Elements.Add(new PdfHorizontalRule());
            }
        }
        /// <summary>
        /// Generates a PDF.
        /// </summary>
        /// <param name="title">The document title.</param>
        /// <param name="author">The document author.</param>
        /// <param name="subject">The document subject.</param>
        /// <param name="keywords">The document keywords.</param>
        /// <param name="absolutePathToPdfTemplate">The absolute path to PDF template.</param>
        /// <param name="pdfStyling">The PDF styling.</param>
        public PdfGenerator(
            string title,
            string author,
            string subject,
            string keywords,
            string absolutePathToPdfTemplate,
            IPdfStyling pdfStyling)
        {
            if (string.IsNullOrWhiteSpace(absolutePathToPdfTemplate) == false)
            {
                _background = new PdfBackground(absoluteUrl: absolutePathToPdfTemplate);
            }

            this.Elements = new List <IPdfElement>();
            this.Pages    = new List <IPdfPage>();

            _title    = title;
            _author   = author;
            _subject  = subject;
            _keywords = keywords;

            _pdfStyling = pdfStyling;
        }
        /// <summary>
        /// Renders the element.
        /// </summary>
        /// <param name="pdfStyling">The PDF styling.</param>
        /// <param name="section">The section.</param>
        public void Render(IPdfStyling pdfStyling, Section section)
        {
            Paragraph paragraph = section.AddParagraph();

            TextLine line;

            for (int i = 0; i < _paragraphs.Count(); i++)
            {
                line = _paragraphs.ElementAt(i);

                if (i > 0)
                {
                    paragraph.AddLineBreak();
                }

                //TODO: Extension > AsFormattedText?
                paragraph.AddFormattedText(
                    text: line.Text,
                    textFormat: line.IsBold ? TextFormat.Bold : TextFormat.NotBold);

                //paragraph.AddLineBreak();
            }
        }
Beispiel #26
0
        private void Footer(IPdfStyling pdfStyling, Section section)
        {
            TextFrame footer = section.AddTextFrame();

            footer.RelativeVertical = RelativeVertical.Margin;

            footer.Height = Unit.FromCentimeter(3.5);
            footer.Width  = _leftSideWidth + Unit.FromCentimeter(1);
            footer.Top    = pdfStyling.MaxHeightInPoints - Unit.FromCentimeter(1);
            footer.Left   = GetTableIndent(pdfStyling);

            Paragraph p = footer.AddParagraph();

            p.AddText("Alle Rezepte auf");
            p.Format.SpaceBefore = 0;
            p.Format.SpaceAfter  = 0;
            p.Format.Font.Color  = pdfStyling.ParagraphSmallColor;
            p.Format.Alignment   = ParagraphAlignment.Center;
            p.Format.LeftIndent  = Unit.FromCentimeter(-1);            // Because we have +1cm for the page

            Paragraph website = footer.AddParagraph();

            website.AddText("www.rimuss.ch");
            website.Format.SpaceBefore = 0;
            website.Format.SpaceAfter  = 0;
            website.Format.Font.Color  = Color.FromCmyk(38, 46, 82, 36);
            website.Format.Alignment   = ParagraphAlignment.Center;
            website.Format.LeftIndent  = Unit.FromCentimeter(-1);            // Because we have +1cm for the page

            Paragraph page = footer.AddParagraph();

            page.AddText("3");
            page.Format.Font.Size   = 9;
            page.Format.SpaceBefore = Unit.FromCentimeter(0.5);
            page.Format.SpaceAfter  = 0;
            page.Format.Alignment   = ParagraphAlignment.Right;
        }
        /// <summary>
        /// Renders the specified PDF styling.
        /// </summary>
        /// <param name="pdfStyling">The PDF styling.</param>
        /// <param name="section">The section.</param>
        public void Render(IPdfStyling pdfStyling, Section section)
        {
            PdfTable table = new PdfTable(
                style: TableStyle.NoSpacing,
                fitToDocument: true);

            // Title rows
            foreach (string title in _titles)
            {
                table.THead.Add(new PdfTableHeaderCell(title.Trim(), true, Alignment.Left));
            }

            // Get the max count

            int maxCount = _addresses.Max(o => o.Count());

            PdfTableRow row;

            string line;
            bool   isBold;

            for (int i = 0; i < maxCount; i++)
            {
                row = new PdfTableRow();
                foreach (IEnumerable <TextLine> addressLine in _addresses)
                {
                    line   = i >= addressLine.Count() ? string.Empty : addressLine.ElementAt(i).Text;
                    isBold = i >= addressLine.Count() ? false : addressLine.ElementAt(i).IsBold;

                    row.Add(new PdfTableCell(text: line, isBold: isBold));
                }

                table.TBody.Add(row);
            }

            table.Render(pdfStyling: pdfStyling, section: section);
        }
Beispiel #28
0
        /// <summary>
        /// Renders the element.
        /// </summary>
        /// <param name="pdfStyling">The PDF styling.</param>
        /// <param name="section">The section.</param>
        public void Render(IPdfStyling pdfStyling, Section section)
        {
            MigraDoc.DocumentObjectModel.Tables.Table table = AddTable(
                pdfStyling: pdfStyling,
                section: section);

            switch (_style)
            {
            case TableStyle.Bordered:
                pdfStyling.TableStyling.BorderedStyle(table);
                break;

            case TableStyle.Grid:
                pdfStyling.TableStyling.GridStyle(table);
                break;

            case TableStyle.OnlyRowsBordered:
                pdfStyling.TableStyling.OnlyRowsBorderedStyle(table);
                break;

            case TableStyle.NoBorders:
                pdfStyling.TableStyling.NoBordersStyle(table);
                break;

            case TableStyle.StripedRows:
                pdfStyling.TableStyling.StripedRowsStyle(table);
                break;

            case TableStyle.Zebra:
                pdfStyling.TableStyling.ZebraStyle(table);
                break;

            case TableStyle.NoSpacing:
                pdfStyling.TableStyling.NoSpacingStyle(table);
                break;
            }
        }
Beispiel #29
0
        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());
        }
Beispiel #30
0
        private void AddCell(
            IPdfStyling pdfStyling,
            MigraDoc.DocumentObjectModel.Tables.Table table,
            PdfTableRow pdfRow,
            bool isTFoot)
        {
            // Each item fills one row
            Row row = table.AddRow();

            row.TopPadding = Unit.FromPoint(5);
            //row.BottomPadding = Unit.FromPoint(2);

            IPdfTableCell pdfCell;
            int           pdfCellIndex, pdfCellIndexWithColspan;

            for (int j = 0; j < pdfRow.Cells.Count; j++)
            {
                pdfCellIndex            = j;
                pdfCellIndexWithColspan = pdfCellIndex;
                pdfCell = pdfRow.Cells[j];

                if (pdfCellIndex > 0)
                {
                    // If we have a colspan we have to update the index
                    // http://www.pdfsharp.net/wiki/invoice-sample.ashx
                    for (int y = 0; y < pdfCellIndex; y++)
                    {
                        if (pdfRow.Cells[y].Colspan > 1)
                        {
                            pdfCellIndexWithColspan += pdfRow.Cells[y].Colspan - 1;
                        }
                    }
                }

                AddText(
                    pdfStyling: pdfStyling,
                    pdfCell: pdfCell,
                    cell: row.Cells[pdfCellIndexWithColspan]);

                row.Cells[pdfCellIndexWithColspan].VerticalAlignment = VerticalAlignment.Top;

                if (pdfCell.Alignment != Alignment.Inherit)
                {
                    row.Cells[pdfCellIndexWithColspan].Format.Alignment = pdfCell.Alignment.GetAlignment();
                }

                if (pdfCell.Colspan > 1)
                {
                    row.Cells[pdfCellIndexWithColspan].MergeRight = pdfCell.Colspan - 1;
                }

                if (isTFoot)
                {
                    row.Cells[pdfCellIndexWithColspan].Style = PdfStyleNames.Table.TFoot;
                }
                else
                {
                    row.Cells[pdfCellIndexWithColspan].Style = PdfStyleNames.Table.TBody;
                }
            }
        }