Esempio n. 1
0
        /// <summary>
        /// Checks the src attribute for all images in the HTML, and attempts to
        /// find a resource of the same name. If the resource exists, it is
        /// written to a temporary file and the image's src is changed to point
        /// to the temp file.
        /// </summary>
        /// <param name="html">String containing valid HTML.</param>
        /// <returns>The modified HTML.</returns>
        private static string ParseHtmlImages(string html)
        {
            var doc = new HtmlAgilityPack.HtmlDocument();

            doc.LoadHtml(html);
            // Find images via xpath.
            var images = doc.DocumentNode.SelectNodes(@"//img");

            if (images != null)
            {
                foreach (HtmlNode image in images)
                {
                    string src = image.GetAttributeValue("src", null);
                    if (!File.Exists(src) && !string.IsNullOrEmpty(src))
                    {
                        string tempFileName = HtmlToMigraDoc.GetImagePath(src, Path.GetTempPath());
                        if (!string.IsNullOrEmpty(tempFileName))
                        {
                            image.SetAttributeValue("src", tempFileName);
                        }
                    }
                }
            }
            return(doc.DocumentNode.OuterHtml);
        }
Esempio n. 2
0
        /// <summary>
        /// Populate the view given the specified text.
        /// </summary>
        /// <param name="contents"></param>
        /// <param name="editingEnabled"></param>
        /// <returns></returns>
        private bool PopulateView(string contents, bool editingEnabled)
        {
            if (contents.Contains(@"{\rtf"))
            {
                richTextBox1.Rtf = contents;
            }
            else
            {
                var doc     = new MigraDoc.DocumentObjectModel.Document();
                var section = doc.AddSection();
                if (contents.Contains("<html>"))
                {
                    HtmlToMigraDoc.Convert(contents, section, ImagePath);
                }
                else
                {
                    MarkdownDeep.Markdown markDown = new MarkdownDeep.Markdown();
                    markDown.ExtraMode = true;
                    string html = markDown.Transform(contents);
                    editingEnabled = true;
                    HtmlToMigraDoc.Convert(html, section, ImagePath);
                }
                RtfDocumentRenderer renderer = new RtfDocumentRenderer();
                richTextBox1.Rtf = renderer.RenderToString(doc, null);
            }

            return(editingEnabled);
        }
Esempio n. 3
0
    public void Execute(MainPresenter mainPresenter)
    {
        // The image is actually called SoltaniChickpeaPhenology.png.
        HtmlToMigraDoc.GetImagePath("SoltaniChickpeaPhenology.PNG", Path.GetTempPath());

        // Close the user interface.
        mainPresenter.Close(askToSave: false);
    }
Esempio n. 4
0
        /// <summary>Adds a formatted paragraph to section.</summary>
        /// <param name="section">The section.</param>
        /// <param name="paragraph">The paragraph.</param>
        private void AddFormattedParagraphToSection(Section section, AutoDocumentation.Paragraph paragraph)
        {
            string html = markDown.Transform(paragraph.text);

            HtmlToMigraDoc.Convert(html, section, WorkingDirectory);

            Paragraph para = section.LastParagraph;

            para.Format.LeftIndent += Unit.FromCentimeter(paragraph.indent);
            if (paragraph.bookmarkName != null)
            {
                para.AddBookmark(paragraph.bookmarkName);
            }
            if (paragraph.handingIndent)
            {
                para.Format.LeftIndent      = "1cm";
                para.Format.FirstLineIndent = "-1cm";
            }
        }
Esempio n. 5
0
        /// <summary>Adds a formatted paragraph to section.</summary>
        /// <param name="section">The section.</param>
        /// <param name="paragraph">The paragraph.</param>
        private void AddFormattedParagraphToSection(Section section, AutoDocumentation.Paragraph paragraph)
        {
            string html = Markdown.ToHtml(paragraph.text, new MarkdownPipelineBuilder().UsePipeTables().UseEmphasisExtras().Build());

            HtmlToMigraDoc.Convert(html, section, WorkingDirectory, RelativePath);

            Paragraph para = section.LastParagraph;

            para.Format.LeftIndent += Unit.FromCentimeter(paragraph.indent);
            if (paragraph.bookmarkName != null)
            {
                para.AddBookmark(paragraph.bookmarkName);
            }
            if (paragraph.handingIndent)
            {
                para.Format.LeftIndent      = "1cm";
                para.Format.FirstLineIndent = "-1cm";
            }
        }
Esempio n. 6
0
        /// <summary>Adds a formatted paragraph to section.</summary>
        /// <param name="section">The section.</param>
        /// <param name="paragraph">The paragraph.</param>
        private void AddFormattedParagraphToSection(Section section, AutoDocumentation.Paragraph paragraph)
        {
            MarkdownDeep.Markdown markDown = new MarkdownDeep.Markdown();
            markDown.ExtraMode = true;
            string html = markDown.Transform(paragraph.text);

            string imageDirectory = Path.GetDirectoryName(ExplorerPresenter.ApsimXFile.FileName);

            HtmlToMigraDoc.Convert(html, section, imageDirectory);

            Paragraph para = section.LastParagraph;

            para.Format.LeftIndent = Unit.FromCentimeter(paragraph.indent);
            if (paragraph.bookmarkName != null)
            {
                para.AddBookmark(paragraph.bookmarkName);
            }
            if (paragraph.handingIndent)
            {
                para.Format.LeftIndent      = "1cm";
                para.Format.FirstLineIndent = "-1cm";
            }
        }
Esempio n. 7
0
        /// <summary>Creates the table.</summary>
        /// <param name="section">The section.</param>
        /// <param name="tableObj">The table to convert to html.</param>
        private void CreateTable(Section section, AutoDocumentation.Table tableObj)
        {
            var table = section.AddTable();

            table.Style               = "Table";
            table.Borders.Color       = Colors.Blue;
            table.Borders.Width       = 0.25;
            table.Borders.Left.Width  = 0.5;
            table.Borders.Right.Width = 0.5;
            table.Rows.LeftIndent     = 0;

            var       gdiFont  = new XFont("Arial", 10);
            XGraphics graphics = XGraphics.CreateMeasureContext(new XSize(2000, 2000), XGraphicsUnit.Point, XPageDirection.Downwards);

            // Add the required columns to the table.
            foreach (DataColumn column in tableObj.data.Table.Columns)
            {
                var column1 = table.AddColumn();
                column1.Format.Alignment = ParagraphAlignment.Right;
            }

            // Add a heading row.
            var headingRow = table.AddRow();

            headingRow.HeadingFormat    = true;
            headingRow.Format.Font.Bold = true;
            headingRow.Shading.Color    = Colors.LightBlue;

            for (int columnIndex = 0; columnIndex < tableObj.data.Table.Columns.Count; columnIndex++)
            {
                // Get column heading
                string heading = tableObj.data.Table.Columns[columnIndex].ColumnName;
                headingRow.Cells[columnIndex].AddParagraph(heading);

                // Get the width of the column
                double maxSize = graphics.MeasureString(heading, gdiFont).Width;
                for (int rowIndex = 0; rowIndex < tableObj.data.Count; rowIndex++)
                {
                    // Add a row to our table if processing first column.
                    MigraDoc.DocumentObjectModel.Tables.Row row;
                    if (columnIndex == 0)
                    {
                        table.AddRow();
                    }

                    // Get the row to process.
                    row = table.Rows[rowIndex + 1];

                    // Convert potential HTML to the cell in our row.
                    HtmlToMigraDoc.Convert(tableObj.data[rowIndex][columnIndex].ToString(),
                                           row.Cells[columnIndex],
                                           WorkingDirectory);

                    // Update the maximum size of the column with the value from the current row.
                    foreach (var element in row.Cells[columnIndex].Elements)
                    {
                        if (element is Paragraph)
                        {
                            var paragraph = element as Paragraph;
                            var contents  = string.Empty;
                            foreach (var paragraphElement in paragraph.Elements)
                            {
                                if (paragraphElement is MigraDoc.DocumentObjectModel.Text)
                                {
                                    contents += (paragraphElement as MigraDoc.DocumentObjectModel.Text).Content;
                                }
                                else if (paragraphElement is MigraDoc.DocumentObjectModel.Hyperlink)
                                {
                                    contents += (paragraphElement as MigraDoc.DocumentObjectModel.Hyperlink).Name;
                                }
                            }

                            var size = graphics.MeasureString(contents, gdiFont);
                            maxSize = Math.Max(maxSize, size.Width);
                        }
                    }
                }

                // maxWidth is the maximum allowed width of the column. E.g. if tableObj.ColumnWidth
                // is 50, then maxWidth is the amount of space taken up by 50 characters.
                // maxSize, on the other hand, is the length of the longest string in the column.
                // The actual column width is whichever of these two values is smaller.
                // MigraDoc will automatically wrap text to ensure the column respects this width.
                double maxWidth = graphics.MeasureString(new string('m', tableObj.ColumnWidth), gdiFont).Width;
                table.Columns[columnIndex].Width = Unit.FromPoint(Math.Min(maxWidth, maxSize) + 10);
            }

            section.AddParagraph();
        }
Esempio n. 8
0
        /// <summary>Creates the table.</summary>
        /// <param name="section">The section.</param>
        /// <param name="tableObj">The table to convert to html.</param>
        private void CreateTable(Section section, AutoDocumentation.Table tableObj)
        {
            var table = section.AddTable();

            table.Style               = "Table";
            table.Borders.Color       = Colors.Blue;
            table.Borders.Width       = 0.25;
            table.Borders.Left.Width  = 0.5;
            table.Borders.Right.Width = 0.5;
            table.Rows.LeftIndent     = 0;

            var       gdiFont  = new XFont("Arial", 10);
            XGraphics graphics = XGraphics.CreateMeasureContext(new XSize(2000, 2000), XGraphicsUnit.Point, XPageDirection.Downwards);

            // Add the required columns to the table.
            foreach (DataColumn column in tableObj.data.Table.Columns)
            {
                var column1 = table.AddColumn();
                column1.Format.Alignment = ParagraphAlignment.Right;
            }

            // Add a heading row.
            var headingRow = table.AddRow();

            headingRow.HeadingFormat    = true;
            headingRow.Format.Font.Bold = true;
            headingRow.Shading.Color    = Colors.LightBlue;

            for (int columnIndex = 0; columnIndex < tableObj.data.Table.Columns.Count; columnIndex++)
            {
                // Get column heading
                string heading = tableObj.data.Table.Columns[columnIndex].ColumnName;
                headingRow.Cells[columnIndex].AddParagraph(heading);

                // Get the width of the column
                double maxSize = graphics.MeasureString(heading, gdiFont).Width;
                for (int rowIndex = 0; rowIndex < tableObj.data.Count; rowIndex++)
                {
                    // Add a row to our table if processing first column.
                    MigraDoc.DocumentObjectModel.Tables.Row row;
                    if (columnIndex == 0)
                    {
                        table.AddRow();
                    }

                    // Get the row to process.
                    row = table.Rows[rowIndex + 1];

                    // Convert potential HTML to the cell in our row.
                    HtmlToMigraDoc.Convert(tableObj.data[rowIndex][columnIndex].ToString(),
                                           row.Cells[columnIndex],
                                           WorkingDirectory);

                    // Update the maximum size of the column with the value from the current row.
                    foreach (var element in row.Cells[columnIndex].Elements)
                    {
                        if (element is Paragraph)
                        {
                            var paragraph = element as Paragraph;
                            var contents  = string.Empty;
                            foreach (var paragraphElement in paragraph.Elements)
                            {
                                if (paragraphElement is MigraDoc.DocumentObjectModel.Text)
                                {
                                    contents += (paragraphElement as MigraDoc.DocumentObjectModel.Text).Content;
                                }
                                else if (paragraphElement is MigraDoc.DocumentObjectModel.Hyperlink)
                                {
                                    contents += (paragraphElement as MigraDoc.DocumentObjectModel.Hyperlink).Name;
                                }
                            }

                            var size = graphics.MeasureString(contents, gdiFont);
                            maxSize = Math.Max(maxSize, size.Width);
                        }
                    }
                }

                table.Columns[columnIndex].Width = Unit.FromPoint(maxSize + 10);
            }
            //for (int rowIndex = 0; rowIndex < tableObj.data.Count; rowIndex++)
            //{
            //    row = table.AddRow();
            //    for (int columnIndex = 0; columnIndex < tableObj.data.Table.Columns.Count; columnIndex++)
            //    {
            //        string cellText = tableObj.data[rowIndex][columnIndex].ToString();
            //
            //        var match = hrefRegEx.Match(cellText);
            //        if (match.Success)
            //        {
            //            var paragraph = row.Cells[columnIndex].AddParagraph();
            //            var hyperlink = paragraph.AddHyperlink(match.Groups[1].ToString().TrimStart('#'), HyperlinkType.Bookmark);
            //            hyperlink.AddFormattedText(match.Groups[2].ToString(), TextFormat.Underline);
            //        }
            //        else
            //        {
            //            match = italicsRegEx.Match(cellText);
            //            if (match.Success)
            //            {
            //                var para = row.Cells[columnIndex].AddParagraph(match.Groups[1].ToString());
            //                para.AddLineBreak();
            //                para.AddFormattedText(match.Groups[2].ToString(), TextFormat.Italic);
            //            }
            //            else
            //                row.Cells[columnIndex].AddParagraph(cellText);
            //        }
            //    }
            //
            //}
            section.AddParagraph();
        }