Products
PDFKit.NET 4.0
Created
7/17/2015
Tags
Fonts Manipulate PDF

This sample shows how to use text alignment and how docking works.

Docking refers to the placement of shapes in a container; in this sample we will be placing several text fragments in a vertical manner using the docking style DockStyle.Top.

// create a new document
Document document = new Document();

// append a new page (portrait Letter)
bool landscape = false;
Page page = new Page( PageSize.Letter, landscape );
document.Pages.Add( page );

// add a shapes collection that will be the container for multiline text shapes
ShapeCollection shapes = new ShapeCollection();
TranslateTransform translate = new TranslateTransform();
shapes.Transform = translate;
shapes.Width = page.Width / 2;
shapes.Height = page.Height * 0.85;
translate.X = (page.Width - shapes.Width) / 2;
translate.Y = (page.Height - shapes.Height) / 2;
page.Overlay.Add( shapes );

// add a rectangle shape to visualize the container
// note that the rectangle is positioned relative to its container, not the page     
RectangleShape rectangle = new RectangleShape( 
    0, 0, 
    shapes.Width, shapes.Height, 
    null, new SolidBrush( System.Drawing.Color.Salmon ) );
shapes.Add( rectangle );

// dock text to the top of the shapes collection - align left
MultilineTextShape multilineText = new MultilineTextShape();
multilineText.Fragments.Add( new Fragment( text, Font.TimesRoman, 16 ) );
multilineText.Dock = DockStyle.Top;
multilineText.Margin.Bottom = 10;
shapes.Add( multilineText );

// dock text to the top of the shapes collection - align center
multilineText = new MultilineTextShape();
multilineText.HorizontalAlignment = HorizontalAlignment.Center;
multilineText.Fragments.Add( new Fragment( text, Font.TimesRoman, 16 ) );
multilineText.Dock = DockStyle.Top;
multilineText.Margin.Bottom = 10;
shapes.Add( multilineText );

// dock text to the top of the shapes collection - align right
multilineText = new MultilineTextShape();
multilineText.HorizontalAlignment = HorizontalAlignment.Right;
multilineText.Fragments.Add( new Fragment( text, Font.TimesRoman, 16 ) );
multilineText.Dock = DockStyle.Top;
multilineText.Margin.Bottom = 10;
shapes.Add( multilineText );

// dock text to the top of the shapes collection - justify
multilineText = new MultilineTextShape();
multilineText.Justified = true;
multilineText.Fragments.Add( new Fragment( text, Font.TimesRoman, 16 ) );
multilineText.Dock = DockStyle.Top;
multilineText.Margin.Bottom = 10;
shapes.Add( multilineText );

multilineText = new MultilineTextShape();
multilineText.Justified = true;
multilineText.JustifyLastLine = true;
multilineText.Fragments.Add( new Fragment(text, Font.TimesRoman, 16) );
multilineText.Dock = DockStyle.Top;
multilineText.Margin.Bottom = 10;
shapes.Add(multilineText);	  
	  
// dock text to the top of the shapes collection - line spacing
multilineText = new MultilineTextShape();
multilineText.LineSpacing = 5;
multilineText.Fragments.Add( new Fragment( text, Font.TimesRoman, 16 ) );
multilineText.Dock = DockStyle.Top;
multilineText.Margin.Bottom = 10;
shapes.Add( multilineText );

// dock text to the top of the shapes collection - first line indentation
multilineText = new MultilineTextShape();
multilineText.FirstLineIndentation = 20;
multilineText.Fragments.Add( new Fragment( text, Font.TimesRoman, 16 ) );
multilineText.Dock = DockStyle.Top;
multilineText.Margin.Bottom = 10;
shapes.Add( multilineText );

// dock text to the top of the shapes collection - hang indentation
multilineText = new MultilineTextShape();
multilineText.HangIndentation = 20;
multilineText.Fragments.Add( new Fragment( text, Font.TimesRoman, 16 ) );
multilineText.Dock = DockStyle.Top;
multilineText.Margin.Bottom = 10;
shapes.Add( multilineText );

using ( FileStream file = new FileStream( @"..\..\dockmultilinetext.pdf", FileMode.Create, FileAccess.Write ) )
{
    document.Write(  file );
}
Const text As String = "With PDFKit.NET you can Manipulate existing " + "PDF documents and forms using a highly intuitive object model. " + "Add pages, stamp content and fill forms."

Shared Sub Main()
	' create a new document
	Dim document As New Document()

	' append a new page (portrait Letter)
	Dim landscape As Boolean = False
	Dim page As New Page(PageSize.Letter, landscape)
	document.Pages.Add(page)

	' add a shapes collection that will be the container for multiline text shapes
	Dim shapes As New ShapeCollection()
	Dim translate As New TranslateTransform()
	shapes.Transform = translate
	shapes.Width = page.Width / 2
	shapes.Height = page.Height * 0.8
	translate.X = (page.Width - shapes.Width) / 2
	translate.Y = (page.Height - shapes.Height) / 2
	page.Overlay.Add(shapes)

	' add a rectangle shape to visualize the container
	' note that the rectangle is positioned relative to its container, not the page     
	Dim rectangle As New RectangleShape(0, 0, shapes.Width, shapes.Height, Nothing, New SolidBrush(System.Drawing.Color.Salmon))
	shapes.Add(rectangle)

	' dock text to the top of the shapes collection - align left
	Dim multilineText As New MultilineTextShape()
	multilineText.Fragments.Add(New Fragment(text, Font.TimesRoman, 16))
	multilineText.Dock = DockStyle.Top
	multilineText.Margin.Bottom = 10
	shapes.Add(multilineText)

	' dock text to the top of the shapes collection - align center
	multilineText = New MultilineTextShape()
	multilineText.HorizontalAlignment = HorizontalAlignment.Center
	multilineText.Fragments.Add(New Fragment(text, Font.TimesRoman, 16))
	multilineText.Dock = DockStyle.Top
	multilineText.Margin.Bottom = 10
	shapes.Add(multilineText)

	' dock text to the top of the shapes collection - align right
	multilineText = New MultilineTextShape()
	multilineText.HorizontalAlignment = HorizontalAlignment.Right
	multilineText.Fragments.Add(New Fragment(text, Font.TimesRoman, 16))
	multilineText.Dock = DockStyle.Top
	multilineText.Margin.Bottom = 10
	shapes.Add(multilineText)

	' dock text to the top of the shapes collection - justify
	multilineText = New MultilineTextShape()
	multilineText.Justified = True
	multilineText.Fragments.Add(New Fragment(text, Font.TimesRoman, 16))
	multilineText.Dock = DockStyle.Top
	multilineText.Margin.Bottom = 10
	shapes.Add(multilineText)

	' dock text to the top of the shapes collection - line spacing
	multilineText = New MultilineTextShape()
	multilineText.LineSpacing = 5
	multilineText.Fragments.Add(New Fragment(text, Font.TimesRoman, 16))
	multilineText.Dock = DockStyle.Top
	multilineText.Margin.Bottom = 10
	shapes.Add(multilineText)

	' dock text to the top of the shapes collection - first line indentation
	multilineText = New MultilineTextShape()
	multilineText.FirstLineIndentation = 20
	multilineText.Fragments.Add(New Fragment(text, Font.TimesRoman, 16))
	multilineText.Dock = DockStyle.Top
	multilineText.Margin.Bottom = 10
	shapes.Add(multilineText)

	' dock text to the top of the shapes collection - hang indentation
	multilineText = New MultilineTextShape()
	multilineText.HangIndentation = 20
	multilineText.Fragments.Add(New Fragment(text, Font.TimesRoman, 16))
	multilineText.Dock = DockStyle.Top
	multilineText.Margin.Bottom = 10
	shapes.Add(multilineText)

	Using file As New FileStream("..\..\dockmultilinetext.pdf", FileMode.Create, FileAccess.Write)
		document.Write(file)
	End Using
End Sub