Create and save PDF document

Docotic.Pdf Library Help > Samples > General operations > Create and save PDF document

This sample shows how to create and save PDF documents using Docotic.Pdf library.

The most important class in Docotic.Pdf library is PdfDocument. Each newly created PDF document already contains a page, you can access it using PdfDocument.Pages collection or PdfDocument.GetPage method. When document is completed, you can save it to a file or a stream using PdfDocument.Save(..) method.

When you need to open document immediately after saving, you can use System.Diagnostics.Process class and its Start method. Call to Process.Start("your_pdf.pdf") method will open PDF document in a default PDF viewer, if any.

CopyC#
using System.Diagnostics;

namespace BitMiracle.Docotic.Pdf.Samples
{
    public static class CreateAndSaveDocument
    {
        public static void Main()
        {
            // NOTE: 
            // When used in trial mode, the library imposes some restrictions.
            // Please visit http://bitmiracle.com/pdf-library/trial-restrictions.aspx
            // for more information.

            PdfDocument pdf = new PdfDocument();

            // let's draw "Hello, world" on the first page
            PdfPage firstPage = pdf.Pages[0];
            firstPage.Canvas.DrawString(10, 50, "Hello, world!");

            string pathToFile = "CreateAndSaveDocument.pdf";
            pdf.Save(pathToFile);
            pdf.Dispose();

            // opens saved PDF document in default PDF viewer
            Process.Start(pathToFile);
        }
    }
}
CopyVB.NET
Imports System.Diagnostics
Imports System.IO

Imports BitMiracle.Docotic.Pdf

Namespace BitMiracle.Docotic.Pdf.Samples
    Public NotInheritable Class CreateAndSaveDocument
        Public Shared Sub Main()
            ' NOTE: 
            ' When used in trial mode, the library imposes some restrictions.
            ' Please visit http://bitmiracle.com/pdf-library/trial-restrictions.aspx
            ' for more information.


            Dim pdf As New PdfDocument()

            ' let's draw "Hello, world" on the first page
            Dim firstPage As PdfPage = pdf.Pages(0)
            firstPage.Canvas.DrawString(10, 50, "Hello, world!")

            Dim pathToFile As String = "CreateAndSaveDocument.pdf"
            pdf.Save(pathToFile)
            pdf.Dispose()

            ' opens saved document in default PDF viewer
            Process.Start(pathToFile)
        End Sub
    End Class
End Namespace