Create Documents with Document Writers - C# .NET Core

This tutorial shows how to create a new blank PDF document and add pages to it from existing PDF files in a C# .NET Core application using the LEADTOOLS SDK.

Overview  
Summary This tutorial covers how to create a new PDF document and add pages to it using LEADTOOLS Document Writers in a C# .NET Core Console application.
Completion Time 30 minutes
Visual Studio Project Download tutorial project (1 KB)
Platform C# .NET Core 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 - C# .NET Core tutorial.

Create the Project and Add LEADTOOLS References

Start with a copy of the project created in the Add References and Set a License tutorial. If you do not have that project, follow the steps in that tutorial to create it.

The references needed depend upon the purpose of the project. References can be added via NuGet packages.

This tutorial requires the following NuGet package:

For a complete list of which DLL files are required for your application, refer to Files to be Included With Your Application.

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:

Note

Adding LEADTOOLS NuGet 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 the following statements to the using block at the top of Program.cs.

C#
using System; 
using System.IO; 
using Leadtools; 
using Leadtools.Codecs; 
using Leadtools.Document.Writer; 

Add a new method named CreatePdfDocument(). Call this new method inside the Main() method under the set license code. Add the below code to create a new PDF file and add to it the first page of each PDF in a given directory.

C#
static void CreatePdfDocument() 
{ 
    using (RasterCodecs codecs = new RasterCodecs()) 
    { 
        string dir = @"C:\LEADTOOLS22\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 
            docWriter.AddPage(page); 
            Console.WriteLine($"Added page {pageNumber} from {Path.GetFileNameWithoutExtension(file)}\n"); 
            // Dispose of resources 
            if (page.SvgDocument != null) 
                page.SvgDocument.Dispose(); 
            if (page.Image != null) 
                page.Image.Dispose(); 
        } 
        // Finalized document to disk 
        docWriter.EndDocument(); 
        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 console appears and the application creates a new PDF file and adds the first page of each PDF file in a given directory using SVG and Document Writers.

Wrap-up

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

See Also

Help Version 22.0.2021.10.18
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.