Hello World

This example demonstrates how to write a simple text to a PDF document.

Our PDF library adds the ability to write text in a pdf document using font and style.

Click on the generate button to see the output pdf.

Output pdf file has "Hello World !" written in it with black color and font.

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

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

        public HelloWorldExample(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();

            PdfPage page = doc.Pages.Add();

            // Create a solid brush
            PdfBrush brush = new PdfSolidBrush(Color.Black);

            const float fontSize = 24.0f;

            // Set the font
            PdfFont font = new PdfStandardFont(PdfFontFamily.Helvetica, fontSize);

            // Draw the text
            page.Graphics.DrawString("Hello World!", font, brush, new PointF(30, 30));



            // 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;
        }
    }
}
Imports System.IO
Imports System.Text
Imports OfficeComponent.Pdf
Imports OfficeComponent.Pdf.Graphics

Namespace OfficeComponent.Samples
	Friend Class HelloWorldExample
		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()

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

			' Create a solid brush
			Dim brush As PdfBrush = New PdfSolidBrush(Color.Black)

			Const fontSize As Single = 24.0F

			' Set the font
			Dim font As PdfFont = New PdfStandardFont(PdfFontFamily.Helvetica, fontSize)

			' Draw the text
			page.Graphics.DrawString("Hello World!", font, brush, New PointF(30, 30))



			' 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
	End Class
End Namespace