Paragraphs Example for WPF

Nevron NOV Diagram for WPF ships with hundreds of examples with full source code, as part of the Nevron Open Vision Suite. All WPF Diagram examples are also available as a stand-alone Visual Studio solution in C# and VB.NET, which you can download from the links below.

Paragraphs Example for WPF Description

Demonstrates how to modify different aspects of the rich text paragraphs.


Paragraphs Example

C# Paragraphs Example Code for Nevron Diagram for WPF
Language: C#

using Nevron.Nov;
using Nevron.Nov.Diagram;
using Nevron.Nov.Diagram.Shapes;
using Nevron.Nov.Dom;
using Nevron.Nov.Graphics;
using Nevron.Nov.Layout;
using Nevron.Nov.Text;
using Nevron.Nov.UI;
using Nevron.Nov.Windows.Forms;
using System.Text;
using System.Windows.Forms;

namespace NovWinFormsProject
{
	public partial class Form1 : Form
	{
		#region Constructors

		public Form1()
		{
			InitializeComponent();

			// Set form title
			Text = "NOV WinForms - NParagraphsExample";

			// Create the NOV example and its controls
			NWidget exampleContent = CreateExampleContent();
			NWidget exampleControls = CreateExampleControls();
			if (exampleControls != null)
			{
				exampleControls.PreferredWidth = 300;
			}

			// Place the example and its controls in a pair box
			NPairBox pairBox = new NPairBox(exampleContent, exampleControls);
			pairBox.FillMode = ENStackFillMode.First;
			pairBox.FitMode = ENStackFitMode.First;
			pairBox.Margins = new NMargins(NDesign.HorizontalSpacing, NDesign.VerticalSpacing);

			// Host the example in the form
			NNovWidgetHost host = new NNovWidgetHost(pairBox);
			host.Dock = DockStyle.Fill;
			Controls.Add(host);
		}

		#endregion

		#region Example

		public NWidget CreateExampleContent()
		{
			// Create a simple drawing
			NDrawingViewWithRibbon drawingViewWithRibbon = new NDrawingViewWithRibbon();
			m_DrawingView = drawingViewWithRibbon.View;

			m_DrawingView.Document.HistoryService.Pause();
			try
			{
				InitDiagram(m_DrawingView.Document);
			}
			finally
			{
				m_DrawingView.Document.HistoryService.Resume();
			}

			return drawingViewWithRibbon;
		}
		public NWidget CreateExampleControls()
		{
			return null;
		}
		public string GetExampleDescription()
        {
            return @"

Demonstrates how to modify different aspects of the rich text paragraphs.

"; } /// /// Gets the relevant documentation links for the example /// /// public (string Name, string Url)[] GetDocumentationUrls() { return new[] { ("Text Block", "https://www.nevron.com/docs/openvision/diagram-text-block.html"), ("Rich Text Paragraphs", "https://www.nevron.com/docs/openvision/rich-text-paragraphs.html"), }; } private void InitDiagram(NDrawingDocument drawingDocument) { NDrawing drawing = drawingDocument.Content; // hide the grid drawing.ScreenVisibility.ShowGrid = false; NShape shape1 = new NBasicShapeFactory().CreateShape(ENBasicShape.Rectangle); shape1.SetBounds(10, 10, 600, 900); NTextBlock textBlock = new NTextBlock(); shape1.TextBlock = textBlock; textBlock.Padding = new NMargins(20); textBlock.Content.Blocks.Clear(); AddFormattedTextToContent(textBlock.Content); drawing.ActivePage.Items.Add(shape1); } #endregion #region Implementation /// /// Adds rich formatted text to the specified text block content /// /// private void AddFormattedTextToContent(NTextBlockContent content) { // paragraphs with different horizontal alignment NParagraph paragraph; for (int i = 0; i < 4; i++) { paragraph = new NParagraph(); switch (i) { case 0: paragraph.HorizontalAlignment = ENAlign.Left; paragraph.Inlines.Add(new NTextInline(GetAlignedParagraphText("left"))); break; case 1: paragraph.HorizontalAlignment = ENAlign.Center; paragraph.Inlines.Add(new NTextInline(GetAlignedParagraphText("center"))); break; case 2: paragraph.HorizontalAlignment = ENAlign.Right; paragraph.Inlines.Add(new NTextInline(GetAlignedParagraphText("right"))); break; case 3: paragraph.HorizontalAlignment = ENAlign.Justify; paragraph.Inlines.Add(new NTextInline(GetAlignedParagraphText("justify"))); break; } content.Blocks.Add(paragraph); } { // borders paragraph = new NParagraph(); paragraph.BorderThickness = new NMargins(2, 2, 2, 2); paragraph.Border = NBorder.CreateFilledBorder(NColor.Red); paragraph.PreferredWidth = NMultiLength.NewPercentage(50); paragraph.Margins = new NMargins(5, 5, 5, 5); paragraph.Padding = new NMargins(5, 5, 5, 5); paragraph.PreferredWidth = NMultiLength.NewFixed(300); paragraph.PreferredHeight = NMultiLength.NewFixed(100); NTextInline textInline1 = new NTextInline("Paragraphs can have border, margins and padding as well as preffered size"); paragraph.Inlines.Add(textInline1); content.Blocks.Add(paragraph); } // First line indent and hanging indent content.Blocks.Add(GetTitleParagraph("Paragraph with First Line Indent and Hanging Indent", 2)); NParagraph paragraphWithIndents = new NParagraph(GetRepeatingText("First line indent -10dip, hanging indent 15dip.", 5)); paragraphWithIndents.HorizontalAlignment = ENAlign.Left; paragraphWithIndents.FirstLineIndent = -10; paragraphWithIndents.HangingIndent = 15; content.Blocks.Add(paragraphWithIndents); // First line indent and hanging indent content.Blocks.Add(GetTitleParagraph("Line Spacing", 2)); NParagraph paragraphWithMultipleLineSpacing = new NParagraph(GetRepeatingText("Line space is two times bigger than normal", 10)); paragraphWithMultipleLineSpacing.HorizontalAlignment = ENAlign.Left; paragraphWithMultipleLineSpacing.LineHeightMode = ENLineHeightMode.Multiple; paragraphWithMultipleLineSpacing.LineHeightFactor = 2.0; content.Blocks.Add(paragraphWithMultipleLineSpacing); NParagraph paragraphWithAtLeastLineSpacing = new NParagraph(GetRepeatingText("Line space is at least 20 dips.", 10)); paragraphWithAtLeastLineSpacing.HorizontalAlignment = ENAlign.Left; paragraphWithAtLeastLineSpacing.LineHeightMode = ENLineHeightMode.AtLeast; paragraphWithAtLeastLineSpacing.LineHeight = 20.0; content.Blocks.Add(paragraphWithAtLeastLineSpacing); NParagraph paragraphWithExactLineSpacing = new NParagraph(GetRepeatingText("Line space is exactly 20 dips.", 10)); paragraphWithExactLineSpacing.HorizontalAlignment = ENAlign.Left; paragraphWithExactLineSpacing.LineHeightMode = ENLineHeightMode.Exactly; paragraphWithExactLineSpacing.LineHeight = 20.0; content.Blocks.Add(paragraphWithExactLineSpacing); // BIDI formatting content.Blocks.Add(GetTitleParagraph("Paragraphs with BIDI text", 2)); paragraph = new NParagraph(); paragraph.HorizontalAlignment = ENAlign.Left; NTextInline latinText1 = new NTextInline("This is some text in English. Followed by Arabic:"); NTextInline arabicText = new NTextInline("أساسًا، تتعامل الحواسيب فقط مع الأرقام، وتقوم بتخزين الأحرف والمحارف الأخرى بعد أن تُعطي رقما معينا لكل واحد منها. وقبل اختراع "); NTextInline latinText2 = new NTextInline("This is some text in English."); paragraph.Inlines.Add(latinText1); paragraph.Inlines.Add(arabicText); paragraph.Inlines.Add(latinText2); content.Blocks.Add(paragraph); } /// /// Gets dummy text for aligned paragraphs /// /// /// private static string GetAlignedParagraphText(string alignment) { string text = string.Empty; for (int i = 0; i < 10; i++) { if (text.Length > 0) { text += " "; } text += "This is " + alignment + " aligned paragraph."; } return text; } /// /// Gets the specified text repeated /// /// /// /// internal static string GetRepeatingText(string text, int count) { StringBuilder builder = new StringBuilder(); for (int i = 0; i < count; i++) { if (builder.Length > 0) { builder.Append(" "); } builder.Append(text); } return builder.ToString(); } /// /// /// /// /// /// internal static NParagraph GetTitleParagraphNoBorder(string text, int level) { double fontSize = 10; ENFontStyle fontStyle = ENFontStyle.Regular; switch (level) { case 1: fontSize = 16; fontStyle = ENFontStyle.Bold; break; case 2: fontSize = 10; fontStyle = ENFontStyle.Bold; break; } NParagraph paragraph = new NParagraph(); paragraph.HorizontalAlignment = ENAlign.Left; paragraph.FontSize = fontSize; paragraph.FontStyle = fontStyle; NTextInline textInline = new NTextInline(text); textInline.FontStyle = fontStyle; textInline.FontSize = fontSize; paragraph.Inlines.Add(textInline); return paragraph; } /// /// Gets a paragraph with title formatting /// /// /// internal static NParagraph GetTitleParagraph(string text, int level) { NColor color = NColor.Black; NParagraph paragraph = GetTitleParagraphNoBorder(text, level); paragraph.HorizontalAlignment = ENAlign.Left; paragraph.Border = CreateLeftTagBorder(color); paragraph.BorderThickness = new NMargins(5.0, 0.0, 0.0, 0.0); return paragraph; } /// /// Creates a left tag border with the specified border /// /// /// private static NBorder CreateLeftTagBorder(NColor color) { NBorder border = new NBorder(); border.LeftSide = new NBorderSide(); border.LeftSide.Fill = new NColorFill(color); return border; } #endregion #region Event Handlers #endregion #region Fields private NDrawingView m_DrawingView; #endregion #region Static Methods #endregion #region Constants #endregion #region Nested Types #endregion } }

Download a fully functional C# (.cs) Paragraphs Example for WPF Solution

VB.NET Paragraphs Example Code for Nevron Diagram for WPF
Language: VB.NET

Imports Nevron.Nov
Imports Nevron.Nov.Diagram
Imports Nevron.Nov.Diagram.Shapes
Imports Nevron.Nov.Dom
Imports Nevron.Nov.Graphics
Imports Nevron.Nov.Layout
Imports Nevron.Nov.Text
Imports Nevron.Nov.UI
Imports Nevron.Nov.Windows.Wpf
Imports System.Text

Class MainWindow

#Region "Constructors"

	Public Sub New()
		' This call is required by the designer.
		InitializeComponent()

		' Add any initialization after the InitializeComponent() call.

		' Set form title
		Title = "NOV WPF - NParagraphsExample"

		' Create the NOV example and its controls
		Dim exampleContent As NWidget = CreateExampleContent()
		Dim exampleControls As NWidget = CreateExampleControls()
		If Not exampleControls Is Nothing Then
			exampleControls.PreferredWidth = 300
		End If

		' Place the example and its controls in a pair box
		Dim pairBox As NPairBox = New NPairBox(exampleContent, exampleControls)
		pairBox.FillMode = ENStackFillMode.First
		pairBox.FitMode = ENStackFitMode.First
		pairBox.Margins = New NMargins(NDesign.HorizontalSpacing, NDesign.VerticalSpacing)

		' Host the example in the form
		Dim host As NNovWidgetHost(Of NWidget) = New NNovWidgetHost(Of NWidget)(pairBox)
		Content = host
	End Sub


#End Region

#Region "Example"

        Public Function CreateExampleContent() As NWidget
            ' Create a simple drawing
            Dim drawingViewWithRibbon As NDrawingViewWithRibbon = New NDrawingViewWithRibbon()
            m_DrawingView = drawingViewWithRibbon.View

            m_DrawingView.Document.HistoryService.Pause()
            Try
                InitDiagram(m_DrawingView.Document)
            Finally
                m_DrawingView.Document.HistoryService.Resume()
            End Try

            Return drawingViewWithRibbon
        End Function
        Public Function CreateExampleControls() As NWidget
            Return Nothing
        End Function
        Public Function GetExampleDescription() As String
            Return "

Demonstrates how to modify different aspects of the rich text paragraphs.

" End Function ''' ''' Gets the relevant documentation links for the example ''' ''' Public Function GetDocumentationUrls() As (String, String)() Return {("Text Block", "https://www.nevron.com/docs/openvision/diagram-text-block.html"), ("Rich Text Paragraphs", "https://www.nevron.com/docs/openvision/rich-text-paragraphs.html")} End Function Private Sub InitDiagram(drawingDocument As NDrawingDocument) Dim drawing = drawingDocument.Content ' hide the grid drawing.ScreenVisibility.ShowGrid = False Dim shape1 As NShape = New NBasicShapeFactory().CreateShape(ENBasicShape.Rectangle) shape1.SetBounds(10, 10, 600, 900) Dim textBlock As NTextBlock = New NTextBlock() shape1.TextBlock = textBlock textBlock.Padding = New NMargins(20) textBlock.Content.Blocks.Clear() AddFormattedTextToContent(textBlock.Content) drawing.ActivePage.Items.Add(shape1) End Sub #End Region #Region "Implementation" #End Region #Region "Event Handlers" #End Region #Region "Fields" Private m_DrawingView As NDrawingView #End Region #Region "Static Methods" #End Region #Region "Constants" #End Region #Region "Nested Types" #End Region End Class

Download a fully functional VB.NET (.vb) Paragraphs Example for WPF Solution

Related Documentation Links