Create Documents with Document Writers - Console C#

This tutorial shows how to create a C# Windows console application that creates a new PDF document and adds the first pages from each pdf in a directory to the newly created PDF.

Summary This tutorial covers how to create a new PDF document and add pages to it using SVG in a C# Windows Console application
Completion Time 30 minutes
Visual Studio Project Download tutorial project (3 KB)
Platform C# Windows Console Application
IDE Visual Studio 2017, 2019
Development License Download LEADTOOLS
Try it in another language

Required Knowledge

Get familiar with the basic steps of creating a project by reviewing the Add References and Set a License tutorial, before working on the Create Documents with Document Writers - Console C# tutorial.

Create the Project and Add the LEADTOOLS References

In Visual Studio, create a new C# Windows Console project, and add the following necessary LEADTOOLS references.

If using NuGet references, this tutorial requires the following NuGet package:

If local DLL references are used, the following DLLs are needed. The DLLs are located at <INSTALL_DIR>\LEADTOOLS21\Bin\Dotnet4\x64:

For a complete list of which Codec DLLs are required for specific formats, refer to File Format Support.

Set the License File

The License unlocks the features needed for the project. It must be set before any toolkit function is called. For details, including tutorials for different platforms, refer to Setting a Runtime License.

There are two types of runtime licenses:


Adding LEADTOOLS NuGet and local references and setting a license are covered in more detail in the Add References and Set a License tutorial.

Create a New PDF Document and Add Pages Code

With the project created, the references added, and the license set, coding can begin.

In the Solution Explorer, open Program.cs. Add a new method called CreatePdfDocument() and call it inside the Main() method under SetLicense();. Add the below code to create a new PDF file and add the first page of each PDF in a given directory to the new PDF.

// Add to using block 
using System; 
using System.IO; 
using Leadtools; 
using Leadtools.Codecs; 
using Leadtools.Document.Writer; 
static void CreatePdfDocument() 
    using (RasterCodecs codecs = new RasterCodecs()) 
        string dir = @"C:\LEADTOOLS21\Resources\Images"; 
        int pageNumber = 1; 
        string[] pdfFiles = Directory.GetFiles( dir, "*.pdf"); 
        DocumentFormat format = DocumentFormat.Pdf; 
        string outFile = Path.Combine(dir, "DocumentWriters." + DocumentWriter.GetFormatFileExtension(format)); 
        codecs.Options.RasterizeDocument.Load.Resolution = 300; 
        DocumentWriter docWriter = new DocumentWriter(); 
        PdfDocumentOptions pdfOptions = docWriter.GetOptions(format) as PdfDocumentOptions; 
        pdfOptions.DocumentType = PdfDocumentType.PdfA; 
        pdfOptions.ImageOverText = true; 
        docWriter.SetOptions(format, pdfOptions); 
        // Begin a new PDF document 
        docWriter.BeginDocument(outFile, format); 
        // Add the pages 
        foreach (string file in pdfFiles) 
            DocumentWriterSvgPage page = new DocumentWriterSvgPage(); 
            page.SvgDocument = codecs.LoadSvg(file, pageNumber, null); 
            if (pdfOptions.ImageOverText) 
                // If we are using image/text, then load the overlay raster image  
                page.Image = codecs.Load(file, pageNumber); 
            // Add the page to the created PDF document 
            Console.WriteLine($"Added page {pageNumber} from {Path.GetFileNameWithoutExtension(file)}\n"); 
            // Dispose of resources 
            if (page.SvgDocument != null) 
            if (page.Image != null) 
        // Finalized document to disk 
        Console.WriteLine("PDF document saved successfully!"); 

Run the Project

Run the project by pressing F5, or by selecting Debug -> Start Debugging.

If the steps were followed correctly, the application runs and creates a new PDF file and adds the first page of each PDF file in a given directory using SVG and Document Writers.


This tutorial showed how to create documents using the Document Writers. It also covered how to use the DocumentWriter, PdfDocumentOptions, DocumentWriterSvgPage, and RasterCodecs classes.

See Also

Help Version 21.0.2023.3.1
Products | Support | Contact Us | Intellectual Property Notices
© 1991-2021 LEAD Technologies, Inc. All Rights Reserved.

Products | Support | Contact Us | Intellectual Property Notices
© 1991-2021 LEAD Technologies, Inc. All Rights Reserved.