Multiple Columns Support

This example demonstrates how to draw multi columns text.

When desinging a page document we often come across grid system with colums of different width

If you divide a page in rows and columns then a page will have rows of height specified and columns with width specified

But mostly there is a need where column are of different width in order to arrange all content in same row

After clicking a generate button will launch a pdf which shows three columns in one row. Second row contains two columns in a row

using System;
using System.Collections.Generic;
using System.Drawing;
using System.IO;
using System.Text;
using OfficeComponent.Pdf;
using OfficeComponent.Pdf.Fields;
using OfficeComponent.Pdf.Graphics;

namespace OfficeComponent.Samples
{
    class MultipleColumnsSupportExample : ExampleBase
    {
        public MultipleColumnsSupportExample(string commonDataPath, string outputDir)
            : base(commonDataPath, outputDir)
        {
        }

        public MultipleColumnsSupportExample(string commonDataPath, string outputDir, string xmlFile) : base(commonDataPath, outputDir, xmlFile)
        {
        }

        public override string Execute()
        {
            // Create a new instance of PdfDocument class.
            PdfDocument doc = new PdfDocument();
            doc.PageSettings.Size = new SizeF(870, 732);

            // Add a page
            PdfPage page = doc.Pages.Add();

            PdfSolidBrush brush = new PdfSolidBrush(Color.Black);

            // Create font
            PdfStandardFont font = new PdfStandardFont(PdfFontFamily.Helvetica, 2f);

            // Adding Header
            AddDocumentHeader(doc, "Multiple Columns Demo");

            // Adding Footer
            AddDocumentFooter(doc, "Copyright � " + DateTime.Now.Year);

            #region About OfficeComponentPdf HTML text

            string htmlText =
                "OfficeComponent PDF for .NET is a 100%-managed PDF document component that helps you to add PDF capabilities in your .NET applications. With a few lines of code, " +
                "you can create a complex PDF document from scratch or load an existing PDF file.

In addition to the ease-of-use and flexibility, the OfficeComponent PDF component " + "also offers many features including: drawing text, image, tables and other shapes, compression, hyperlinks, security and custom fonts. PDF files created " + "using the OfficeComponent PDF component are compatible with all versions of Adobe Acrobat or the free version and Acrobat Viewer from Adobe only."; // Rendering HtmlText PdfHtmlElement richTextElement = new PdfHtmlElement(htmlText, font, brush); richTextElement.TextAlign = TextAlign.Justify; // Formatting Layout PdfMetafileLayoutSettings format = new PdfMetafileLayoutSettings(); format.Layout = PdfLayoutType.OnePage; // Drawing htmlString richTextElement.Draw(page, new RectangleF(0, 15, 250, page.GetClientSize().Height), format); // Drawing PDF Image PdfBitmap image = new PdfBitmap(System.Drawing.Image.FromFile(CommonDataPath + "\\OfficeComponentPdf.gif")); page.Graphics.DrawImage(image, new PointF(50, 265)); htmlText = "Fully-managed .NET components written in C#.
" + "Comprehensive integrated product documentation and 4 Sample projects written in C#, VB.NET and ASP.NET.
" + "OfficeComponentPdf can run under .NET Framework 2.x and above (3.x, ...).
" + "Seamless integration with popular development environments including Microsoft Visual Studio .NET and CodeGear products.
" + "Supports Visual Studio 2005, 2008, 2010, Delphi, C# Builder and other compliant development environments."; // Rendering HtmlText richTextElement = new PdfHtmlElement(htmlText, font, brush); richTextElement.TextAlign = TextAlign.Justify; // Formatting Layout format = new PdfMetafileLayoutSettings(); format.Layout = PdfLayoutType.OnePage; // Drawing htmlString richTextElement.Draw(page, new RectangleF(0, 410, 240, page.GetClientSize().Height), format); #endregion #region About PDF htmlText = "PDF stands for \"Portable Document Format\"." + " The key word is portable, intended to combine the qualities of authenticity," + " reliability and ease of use together into a single packaged concept.

" + "Adobe Systems invented PDF technology in the early 1990s to smooth the " + "process of moving text and graphics from publishers to printing-presses." + " PDF turned out to be the very essence of paper, brought to life in a computer." + " In creating PDF, Adobe had almost unwittingly invented nothing less than a " + "bridge between the paper and computer worlds.

To be truly portable, an authentic electronic " + "document would have to appear exactly the same way on any computer at any time," + " at no cost to the user. It will deliver the exact same results in print or on-screen " + "with near-total reliability."; richTextElement = new PdfHtmlElement(htmlText, font, brush); richTextElement.TextAlign = TextAlign.Justify; richTextElement.Draw(page, new RectangleF(195, 15, 295, page.GetClientSize().Height), format); image = new PdfBitmap(System.Drawing.Image.FromFile(CommonDataPath + "\\PdfLogo.png")); page.Graphics.DrawImage(image, new PointF(50 + 210, 265)); htmlText = "PDF is used for representing two-dimensional documents in " + "a manner independent of the application software, hardware, and operating system.[1]" + "

Each PDF file encapsulates a complete description of a fixed-layout 2-D document " + "(and, with Acrobat 3-D, embedded 3-D documents) that includes the text, fonts, images, " + "and 2-D vector graphics which comprise the documents." + "

PDF is an open standard that was officially published on July 1, 2008 by the ISO as" + "ISO 32000-1:2008.[1]

" + "The PDF combines the technologies such as A sub-set of the PostScript page description programming " + "language, a font-embedding/replacement system to allow fonts to travel with the documents and a " + "structured storage system to bundle these elements and any associated content into a single file," + "with data compression where appropriate. 1-http://www.adobe.com/devnet/acrobat/pdfs/pdf_reference_1-7.pdf"; richTextElement = new PdfHtmlElement(htmlText, font, brush); richTextElement.TextAlign = TextAlign.Justify; richTextElement.Draw(page, new RectangleF(195, 395, 295, page.GetClientSize().Height + 10), format); #endregion #region About OfficeComponent // Drawing Image image = new PdfBitmap(System.Drawing.Image.FromFile(CommonDataPath + "\\Logo.gif")); page.Graphics.DrawImage(image, new PointF(435, 15)); htmlText = "OfficeComponent, a division of OfficeComponent, located in Walnut, California, is a leading provider of File Transfer, Mail, Compression, and SAML" + "- Single Sign On components. We specialize in developing network tools and libraries for Windows developers and providing custom software " + "development and consulting services. Building on its expertise in networking development and Microsoft technologies, " + "OfficeComponent helps customers to build competitive applications on their markets." + "

Every OfficeComponent license includes a one-year subscription for unlimited technical support and new releases." + "OfficeComponent licenses each product on a simple per-developer basis and charges no royalties," + "runtimes, or server deployment fees. A licensee can install his/her " + "license on multiple personal machines at no extra charge.

" + "At OfficeComponent we are very excited about the Microsoft .NET platform.

" + "We believe that one of the key benefits of .NET is improved programmer productivity. " + "Solutions that used to take a very long time with traditional tools can now be " + "implemented in a much shorter time period with the .NET platform."; richTextElement = new PdfHtmlElement(htmlText, font, brush); richTextElement.TextAlign = TextAlign.Justify; richTextElement.Draw(page, new RectangleF(425, 55, 290, page.GetClientSize().Height), format); #endregion // Save and close the document. var outputPath = Path.Combine(OutputDir, this.GetType().Name + "_" + Guid.NewGuid().ToString() + ".pdf"); doc.Save(outputPath); doc.Close(true); return outputPath; } private void AddDocumentHeader(PdfDocument doc, string title) { RectangleF rect = new RectangleF(0, 0, doc.Pages[0].GetClientSize().Width, 50); // Create page template PdfPageTemplateElement header = new PdfPageTemplateElement(rect); Color activeColor = System.Drawing.Color.FromArgb(44, 71, 120); SizeF imageSize = new SizeF(252f, 34f); // Locating the logo on the right corner of the Drawing Surface PointF imageLocation = new PointF(0, 9); PdfImage img = new PdfBitmap(CommonDataPath + "\\logo.gif"); // Draw the image in the Header. header.Graphics.DrawImage(img, imageLocation, imageSize); PdfSolidBrush brush = new PdfSolidBrush(activeColor); PdfFont font = new PdfStandardFont(PdfFontFamily.Helvetica, 16, PdfFontStyle.Bold); // Set formattings for the text PdfStringFormat format = new PdfStringFormat(); format.Alignment = PdfTextAlignment.Right; format.LineAlignment = PdfVerticalAlignment.Middle; // Draw title header.Graphics.DrawString(title, font, brush, new RectangleF(0, 0, header.Width, header.Height), format); format = new PdfStringFormat(); format.Alignment = PdfTextAlignment.Left; format.LineAlignment = PdfVerticalAlignment.Bottom; // Add header template at the top. doc.Template.Top = header; } private static void AddDocumentFooter(PdfDocument doc, string footerText) { RectangleF rect = new RectangleF(0, 0, doc.Pages[0].GetClientSize().Width, 50); //Create a page template PdfPageTemplateElement footer = new PdfPageTemplateElement(rect); PdfSolidBrush brush = new PdfSolidBrush(Color.Black); PdfFont font = new PdfStandardFont(PdfFontFamily.Helvetica, 10, PdfFontStyle.Bold); PdfStringFormat format = new PdfStringFormat(); format.Alignment = PdfTextAlignment.Left; format.LineAlignment = PdfVerticalAlignment.Middle; footer.Graphics.DrawString(footerText, font, brush, new RectangleF(0, 18, footer.Width, footer.Height), format); format = new PdfStringFormat(); format.Alignment = PdfTextAlignment.Right; format.LineAlignment = PdfVerticalAlignment.Bottom; //Create page number field PdfPageNumberField pageNumber = new PdfPageNumberField(font, brush); //Create page count field PdfPageCountField count = new PdfPageCountField(font, brush); PdfCompositeField compositeField = new PdfCompositeField(font, brush, "Page {0} of {1}", pageNumber, count); compositeField.Bounds = footer.Bounds; compositeField.Draw(footer.Graphics, new PointF(570, 40)); //Add the footer template at the bottom doc.Template.Bottom = footer; } } }
Imports System.IO
Imports System.Text
Imports OfficeComponent.Pdf
Imports OfficeComponent.Pdf.Fields
Imports OfficeComponent.Pdf.Graphics

Namespace OfficeComponent.Samples
	Friend Class MultipleColumnsSupportExample
		Inherits ExampleBase
		Public Sub New(ByVal commonDataPath As String, ByVal outputDir As String)
			MyBase.New(commonDataPath, outputDir)
		End Sub

		Public Sub New(ByVal commonDataPath As String, ByVal outputDir As String, ByVal xmlFile As String)
			MyBase.New(commonDataPath, outputDir, xmlFile)
		End Sub

		Public Overrides Function Execute() As String
			' Create a new instance of PdfDocument class.
			Dim doc As New PdfDocument()
			doc.PageSettings.Size = New SizeF(870, 732)

			' Add a page
			Dim page As PdfPage = doc.Pages.Add()

			Dim brush As New PdfSolidBrush(Color.Black)

			' Create font
			Dim font As New PdfStandardFont(PdfFontFamily.Helvetica, 2F)

			' Adding Header
			AddDocumentHeader(doc, "Multiple Columns Demo")

			' Adding Footer
			AddDocumentFooter(doc, "Copyright � " & Date.Now.Year)

'			#Region "About OfficeComponentPdf HTML text"

			Dim htmlText As String = "OfficeComponent PDF for .NET is a 100%-managed PDF document component that helps you to add PDF capabilities in your .NET applications. With a few lines of code, " & "you can create a complex PDF document from scratch or load an existing PDF file.

In addition to the ease-of-use and flexibility, the OfficeComponent PDF component " & "also offers many features including: drawing text, image, tables and other shapes, compression, hyperlinks, security and custom fonts. PDF files created " & "using the OfficeComponent PDF component are compatible with all versions of Adobe Acrobat or the free version and Acrobat Viewer from Adobe only." ' Rendering HtmlText Dim richTextElement As New PdfHtmlElement(htmlText, font, brush) richTextElement.TextAlign = TextAlign.Justify ' Formatting Layout Dim format As New PdfMetafileLayoutSettings() format.Layout = PdfLayoutType.OnePage ' Drawing htmlString richTextElement.Draw(page, New RectangleF(0, 15, 250, page.GetClientSize().Height), format) ' Drawing PDF Image Dim image As New PdfBitmap(System.Drawing.Image.FromFile(CommonDataPath & "\OfficeComponentPdf.gif")) page.Graphics.DrawImage(image, New PointF(50, 265)) htmlText = "Fully-managed .NET components written in C#.
" & "Comprehensive integrated product documentation and 4 Sample projects written in C#, VB.NET and ASP.NET.
" & "OfficeComponentPdf can run under .NET Framework 2.x and above (3.x, ...).
" & "Seamless integration with popular development environments including Microsoft Visual Studio .NET and CodeGear products.
" & "Supports Visual Studio 2005, 2008, 2010, Delphi, C# Builder and other compliant development environments." ' Rendering HtmlText richTextElement = New PdfHtmlElement(htmlText, font, brush) richTextElement.TextAlign = TextAlign.Justify ' Formatting Layout format = New PdfMetafileLayoutSettings() format.Layout = PdfLayoutType.OnePage ' Drawing htmlString richTextElement.Draw(page, New RectangleF(0, 410, 240, page.GetClientSize().Height), format) ' #End Region ' #Region "About PDF" htmlText = "PDF stands for ""Portable Document Format""." & " The key word is portable, intended to combine the qualities of authenticity," & " reliability and ease of use together into a single packaged concept.

" & "Adobe Systems invented PDF technology in the early 1990s to smooth the " & "process of moving text and graphics from publishers to printing-presses." & " PDF turned out to be the very essence of paper, brought to life in a computer." & " In creating PDF, Adobe had almost unwittingly invented nothing less than a " & "bridge between the paper and computer worlds.

To be truly portable, an authentic electronic " & "document would have to appear exactly the same way on any computer at any time," & " at no cost to the user. It will deliver the exact same results in print or on-screen " & "with near-total reliability." richTextElement = New PdfHtmlElement(htmlText, font, brush) richTextElement.TextAlign = TextAlign.Justify richTextElement.Draw(page, New RectangleF(195, 15, 295, page.GetClientSize().Height), format) image = New PdfBitmap(System.Drawing.Image.FromFile(CommonDataPath & "\PdfLogo.png")) page.Graphics.DrawImage(image, New PointF(50 + 210, 265)) htmlText = "PDF is used for representing two-dimensional documents in " & "a manner independent of the application software, hardware, and operating system.[1]" & "

Each PDF file encapsulates a complete description of a fixed-layout 2-D document " & "(and, with Acrobat 3-D, embedded 3-D documents) that includes the text, fonts, images, " & "and 2-D vector graphics which comprise the documents." & "

PDF is an open standard that was officially published on July 1, 2008 by the ISO as" & "ISO 32000-1:2008.[1]

" & "The PDF combines the technologies such as A sub-set of the PostScript page description programming " & "language, a font-embedding/replacement system to allow fonts to travel with the documents and a " & "structured storage system to bundle these elements and any associated content into a single file," & "with data compression where appropriate. 1-http://www.adobe.com/devnet/acrobat/pdfs/pdf_reference_1-7.pdf" richTextElement = New PdfHtmlElement(htmlText, font, brush) richTextElement.TextAlign = TextAlign.Justify richTextElement.Draw(page, New RectangleF(195, 395, 295, page.GetClientSize().Height + 10), format) ' #End Region ' #Region "About OfficeComponent" ' Drawing Image image = New PdfBitmap(System.Drawing.Image.FromFile(CommonDataPath & "\Logo.gif")) page.Graphics.DrawImage(image, New PointF(435, 15)) htmlText = "OfficeComponent, a division of OfficeComponent, located in Walnut, California, is a leading provider of File Transfer, Mail, Compression, and SAML" & "- Single Sign On components. We specialize in developing network tools and libraries for Windows developers and providing custom software " & "development and consulting services. Building on its expertise in networking development and Microsoft technologies, " & "OfficeComponent helps customers to build competitive applications on their markets." & "

Every OfficeComponent license includes a one-year subscription for unlimited technical support and new releases." & "OfficeComponent licenses each product on a simple per-developer basis and charges no royalties," & "runtimes, or server deployment fees. A licensee can install his/her " & "license on multiple personal machines at no extra charge.

" & "At OfficeComponent we are very excited about the Microsoft .NET platform.

" & "We believe that one of the key benefits of .NET is improved programmer productivity. " & "Solutions that used to take a very long time with traditional tools can now be " & "implemented in a much shorter time period with the .NET platform." richTextElement = New PdfHtmlElement(htmlText, font, brush) richTextElement.TextAlign = TextAlign.Justify richTextElement.Draw(page, New RectangleF(425, 55, 290, page.GetClientSize().Height), format) ' #End Region ' Save and close the document. Dim outputPath = Path.Combine(OutputDir, Me.GetType().Name & "_" & Guid.NewGuid().ToString() & ".pdf") doc.Save(outputPath) doc.Close(True) Return outputPath End Function Private Sub AddDocumentHeader(ByVal doc As PdfDocument, ByVal title As String) Dim rect As New RectangleF(0, 0, doc.Pages(0).GetClientSize().Width, 50) ' Create page template Dim header As New PdfPageTemplateElement(rect) Dim activeColor As Color = System.Drawing.Color.FromArgb(44, 71, 120) Dim imageSize As New SizeF(252F, 34F) ' Locating the logo on the right corner of the Drawing Surface Dim imageLocation As New PointF(0, 9) Dim img As PdfImage = New PdfBitmap(CommonDataPath & "\logo.gif") ' Draw the image in the Header. header.Graphics.DrawImage(img, imageLocation, imageSize) Dim brush As New PdfSolidBrush(activeColor) Dim font As PdfFont = New PdfStandardFont(PdfFontFamily.Helvetica, 16, PdfFontStyle.Bold) ' Set formattings for the text Dim format As New PdfStringFormat() format.Alignment = PdfTextAlignment.Right format.LineAlignment = PdfVerticalAlignment.Middle ' Draw title header.Graphics.DrawString(title, font, brush, New RectangleF(0, 0, header.Width, header.Height), format) format = New PdfStringFormat() format.Alignment = PdfTextAlignment.Left format.LineAlignment = PdfVerticalAlignment.Bottom ' Add header template at the top. doc.Template.Top = header End Sub Private Shared Sub AddDocumentFooter(ByVal doc As PdfDocument, ByVal footerText As String) Dim rect As New RectangleF(0, 0, doc.Pages(0).GetClientSize().Width, 50) 'Create a page template Dim footer As New PdfPageTemplateElement(rect) Dim brush As New PdfSolidBrush(Color.Black) Dim font As PdfFont = New PdfStandardFont(PdfFontFamily.Helvetica, 10, PdfFontStyle.Bold) Dim format As New PdfStringFormat() format.Alignment = PdfTextAlignment.Left format.LineAlignment = PdfVerticalAlignment.Middle footer.Graphics.DrawString(footerText, font, brush, New RectangleF(0, 18, footer.Width, footer.Height), format) format = New PdfStringFormat() format.Alignment = PdfTextAlignment.Right format.LineAlignment = PdfVerticalAlignment.Bottom 'Create page number field Dim pageNumber As New PdfPageNumberField(font, brush) 'Create page count field Dim count As New PdfPageCountField(font, brush) Dim compositeField As New PdfCompositeField(font, brush, "Page {0} of {1}", pageNumber, count) compositeField.Bounds = footer.Bounds compositeField.Draw(footer.Graphics, New PointF(570, 40)) 'Add the footer template at the bottom doc.Template.Bottom = footer End Sub End Class End Namespace