Create Documents with Document Writers - C# .NET 6

This tutorial shows how to create a blank PDF document and add pages to it from existing PDF files in a C# .NET 6 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 6 Console application.
Completion Time 20 minutes
Visual Studio Project Download tutorial project (1 KB)
Platform C# .NET 6 Console Application
IDE Visual Studio 2022
Runtime Target .NET 6 or higher
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 this 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.

If you do not have that project, follow the steps in the relevant 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:

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; 
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:\LEADTOOLS23\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!"); 
    } 
} 

Handling Streams

To load the images from memory stream instead of file locations, first create the array of input memory streams after pdfFiles is created:

C#
MemoryStream[] pdfStreams = new MemoryStream[pdfFiles.Length]; 
for (int i = 0; i < pdfFiles.Length; i++) 
{ 
   byte[] pdfData = File.ReadAllBytes(pdfFiles[i]); 
   MemoryStream pdfStream = new MemoryStream(pdfData); 
   pdfStreams[i] = pdfStream; 
} 

Then use the code below to add pages from the streams.

C#
foreach (MemoryStream stream in pdfStreams) 
{ 
   DocumentWriterSvgPage page = new DocumentWriterSvgPage(); 
   page.SvgDocument = codecs.LoadSvg(stream, pageNumber, null); 
   if (pdfOptions.ImageOverText) 
   { 
      // If we are using image/text, then load the overlay raster image 
      page.Image = codecs.Load(stream, pageNumber); 
   } 
   // Add the page to the created PDF document 
   docWriter.AddPage(page); 
   Console.WriteLine($"Added page {pageNumber} from Stream {Array.IndexOf(pdfStreams, stream)} \n"); 
   // Dispose of resources 
   if (page.SvgDocument != null) 
      page.SvgDocument.Dispose(); 
   if (page.Image != null) 
      page.Image.Dispose(); 
} 

To create the document in a memory stream instead of a file location, use the code below:

C#
MemoryStream outStream = new MemoryStream(); 
docWriter.BeginDocument(outStream, format); 

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 23.0.2024.3.11
Products | Support | Contact Us | Intellectual Property Notices
© 1991-2024 LEAD Technologies, Inc. All Rights Reserved.


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