Пример #1
0
        /// <summary>
        /// Adds an image to the page. Only PNG images are supported out of the box.
        /// </summary>
        /// <param name="section"></param>
        /// <param name="pageContent"></param>
        /// <param name="pageWidth"></param>
        /// <param name="style"></param>
        private static void AddImage(Section section, PageContentElement pageContent, Unit pageWidth, Style style)
        {
            Unit originalImageWidthInches;

            // work around to get image dimensions
            using (System.Drawing.Image userImage = System.Drawing.Image.FromFile(pageContent.ImagePath))
            {
                // get image width in inches
                var imageInches = userImage.Width / userImage.VerticalResolution;
                originalImageWidthInches = new Unit(imageInches, UnitType.Inch);
            }

            // add image
            Image image = section.AddImage(pageContent.ImagePath);

            // Calculate Image size:
            // if actual image size is larger than PageWidth - margins, set image width as page width - margins
            Unit actualPageContentWidth = new Unit((pageWidth.Inch - section.PageSetup.LeftMargin.Inch - section.PageSetup.RightMargin.Inch), UnitType.Inch);

            if (originalImageWidthInches > actualPageContentWidth)
            {
                image.Width = actualPageContentWidth;
            }
            image.LockAspectRatio = true;
            image.Left            = pageContent.ImageAlignment.ConvertEnum <ShapePosition>();
        }
Пример #2
0
        private static void SetFont(Style style, PageContentElement textElement)
        {
            style.Font.Name = textElement.FontFamily;
            style.Font.Size = new Unit(textElement.FontSize, UnitType.Point);

            switch (textElement.FontStyle)
            {
            case FontStyleEnum.Bold:
                style.Font.Bold = true;
                break;

            case FontStyleEnum.BoldItalic:
                style.Font.Bold   = true;
                style.Font.Italic = true;
                break;

            case FontStyleEnum.Italic:
                style.Font.Italic = true;
                break;

            case FontStyleEnum.Underline:
                style.Font.Underline = Underline.Single;
                break;
            }
        }
Пример #3
0
 /// <summary>
 /// Set paragraph style
 /// </summary>
 /// <param name="style"></param>
 /// <param name="pageContent"></param>
 private static void SetParagraphStyle(Style style, PageContentElement pageContent)
 {
     style.ParagraphFormat.LineSpacing     = new Unit(pageContent.LineSpacingInPt, UnitType.Point);
     style.ParagraphFormat.LineSpacingRule = LineSpacingRule.Exactly;
     style.ParagraphFormat.Alignment       = pageContent.ParagraphAlignment.ConvertEnum <MigraDoc.DocumentObjectModel.ParagraphAlignment>();
     style.ParagraphFormat.SpaceBefore     = new Unit(pageContent.SpacingBeforeInPt, UnitType.Point);
     style.ParagraphFormat.SpaceAfter      = new Unit(pageContent.SpacingAfterInPt, UnitType.Point);
 }
Пример #4
0
        /// <summary>
        /// Helper method to set styles for header/footer graphics.
        /// </summary>
        /// <param name="pageContent"></param>
        /// <param name="row"></param>
        private static void FormatHeaderFooterLogo(PageContentElement pageContent, Row row)
        {
            if (string.IsNullOrWhiteSpace(pageContent.ImagePath) || !File.Exists(pageContent.ImagePath))
            {
                throw new FileNotFoundException($"Path to header graphics was empty or the file does not exist.");
            }

            var logo = row.Cells[0].AddImage(pageContent.ImagePath);

            logo.Height          = new Unit(pageContent.ImageHeightInCm, UnitType.Centimeter);
            logo.LockAspectRatio = true;
            logo.Top             = ShapePosition.Top;
            logo.Left            = ShapePosition.Left;
        }
Пример #5
0
        private static void AddTextContent(Section section, PageContentElement pageContent, Style style)
        {
            // skip if text content if empty
            if (string.IsNullOrWhiteSpace(pageContent.Text))
            {
                return;
            }

            var paragraph = section.AddParagraph();

            paragraph.Style             = style.Name;
            paragraph.Format.Font.Color = Colors.Black;

            //read text line by line
            string line;

            using (var reader = new StringReader(pageContent.Text))
            {
                while ((line = reader.ReadLine()) != null)
                {
                    // read text one char at a time, so that multiple whitespaces are added correctly
                    foreach (var c in line.ToCharArray())
                    {
                        if (Char.IsWhiteSpace(c))
                        {
                            paragraph.AddSpace(1);
                        }
                        else
                        {
                            paragraph.AddChar(c, 1);
                        }
                    }
                    // add newline
                    paragraph.AddLineBreak();
                }
            }
        }
Пример #6
0
        private static void AddTable(Section section, PageContentElement pageContent, Unit pageWidth)
        {
            TableDefinition tableData = JsonConvert.DeserializeObject <TableDefinition>(pageContent.Table);

            Table table;

            switch (tableData.TableType)
            {
            case TableTypeEnum.Header:
                table = section.Headers.Primary.AddTable();
                break;

            case TableTypeEnum.Footer:
                table = section.Footers.Primary.AddTable();
                break;

            default:
                table = section.AddTable();
                break;
            }

            Unit tableWidth             = new Unit(0, UnitType.Centimeter);
            Unit actualPageContentWidth = new Unit((pageWidth.Centimeter - section.PageSetup.LeftMargin.Centimeter - section.PageSetup.RightMargin.Centimeter), UnitType.Centimeter);

            foreach (var column in tableData.Columns)
            {
                Unit columnWidth = new Unit(column.WidthInCm, UnitType.Centimeter);
                tableWidth += columnWidth;
                if (tableWidth > actualPageContentWidth)
                {
                    throw new Exception($"Page allows table to be {actualPageContentWidth.Centimeter} cm wide. Provided table's width is larger than that, {tableWidth.Centimeter} cm.");
                }

                table.AddColumn(columnWidth);
            }


            if (tableData.HasHeaderRow)
            {
                var columnHeaders           = tableData.Columns.Select(column => column.Name).ToList();
                var headerColumnDefinitions = new List <TableColumnDefinition>();
                for (int i = 0; i < columnHeaders.Count; i++)
                {
                    headerColumnDefinitions.Add(new TableColumnDefinition {
                        Type = TableColumnType.Text
                    });
                }
                ProcessRow(table, headerColumnDefinitions, columnHeaders, tableData.StyleSettings);
            }

            foreach (var dataRow in tableData.RowData)
            {
                var data = dataRow.Select(row => row.Value).ToList();
                ProcessRow(table, tableData.Columns, data, tableData.StyleSettings);
            }

            if (tableData.StyleSettings.BorderWidthInPt > 0)
            {
                switch (tableData.StyleSettings.BorderStyle)
                {
                case TableBorderStyle.Top:
                    table.Borders.Top.Width = new Unit(tableData.StyleSettings.BorderWidthInPt, UnitType.Point);
                    break;

                case TableBorderStyle.Bottom:
                    table.Borders.Bottom.Width = new Unit(tableData.StyleSettings.BorderWidthInPt, UnitType.Point);
                    break;

                case TableBorderStyle.All:
                    table.Borders.Width = new Unit(tableData.StyleSettings.BorderWidthInPt, UnitType.Point);
                    break;

                case TableBorderStyle.None:
                    break;

                default:
                    break;
                }
            }
        }
Пример #7
0
        /// <summary>
        /// Adds a header or a footer to the document. Header/footer size is optimized for A4 size, portrait oriented page.
        /// Header/footer can have a logo field, text field, and page numbers.
        /// </summary>
        /// <param name="section"></param>
        /// <param name="pageContent"></param>
        /// <param name="style"></param>
        /// <param name="isHeader"></param>
        private static void AddHeaderFooterContent(Section section, PageContentElement pageContent, Style style, bool isHeader)
        {
            // skip if text content if empty
            if (string.IsNullOrWhiteSpace(pageContent.Text))
            {
                return;
            }

            Table table;

            if (isHeader)
            {
                table = section.Headers.Primary.AddTable();
            }
            else
            {
                table = section.Footers.Primary.AddTable();
            }

            Row row;

            Paragraph textField;
            Paragraph pagenumField;

            switch (pageContent.HeaderFooterStyle)
            {
            case HeaderFooterStyleEnum.Text:
                table.AddColumn("16cm");

                row = table.AddRow();
                row.VerticalAlignment = VerticalAlignment.Center;

                textField = row.Cells[0].AddParagraph();
                break;

            case HeaderFooterStyleEnum.TextPagenum:
                table.AddColumn("12cm");
                table.AddColumn("4cm");

                row = table.AddRow();
                row.VerticalAlignment = VerticalAlignment.Center;

                textField = row.Cells[0].AddParagraph();

                pagenumField = row.Cells[1].AddParagraph();
                FormatPagenumField(style, pagenumField);
                break;

            case HeaderFooterStyleEnum.LogoText:
                table.AddColumn("5cm");
                table.AddColumn("11cm");

                row = table.AddRow();
                row.VerticalAlignment = VerticalAlignment.Center;

                FormatHeaderFooterLogo(pageContent, row);

                textField = row.Cells[1].AddParagraph();
                break;

            case HeaderFooterStyleEnum.LogoTextPagenum:
                table.AddColumn("5cm");
                table.AddColumn("7cm");
                table.AddColumn("4cm");

                row = table.AddRow();
                row.VerticalAlignment = VerticalAlignment.Center;

                FormatHeaderFooterLogo(pageContent, row);

                textField = row.Cells[1].AddParagraph();

                pagenumField = row.Cells[2].AddParagraph();
                FormatPagenumField(style, pagenumField);
                break;

            default:
                throw new Exception($"Cannot insert header without proper style choice.");
            }

            textField.Style             = style.Name;
            textField.Format.Font.Color = Colors.Black;
            textField.AddText(pageContent.Text);

            if (pageContent.BorderWidthInPt > 0 && isHeader)
            {
                table.Borders.Bottom.Width = new Unit(pageContent.BorderWidthInPt, UnitType.Point);
            }
            else if (pageContent.BorderWidthInPt > 0 && !isHeader)
            {
                table.Borders.Top.Width = new Unit(pageContent.BorderWidthInPt, UnitType.Point);
            }
        }